Skip to content

Commit

Permalink
Merge pull request #5 from yefeza/develop
Browse files Browse the repository at this point in the history
Add custom fields for model type and custom ordering field for list query
  • Loading branch information
yefeza authored May 8, 2023
2 parents f8320e7 + 81a7617 commit 10f0954
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 6 deletions.
3 changes: 2 additions & 1 deletion docs/source/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,5 @@ This example assumes that you have two models called `User` and `Favorite` with
Release Notes
----------------------------

* Version 1.0.0 to 1.1.5 was a package developed for a specific project, and the code was not published on GitHub. The code was refactored and published on GitHub on version 1.2.0.
* Version 1.0.0 to 1.1.5 was a package developed for a specific project, and the code was not published on GitHub. The code was refactored and published on GitHub on version 1.2.0.
* Version 1.2.3 add support to set custom attributes on the model Type and set custom ordering field for the queries.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = django_graphbox
version = 1.2.0
version = 1.2.3
description = Package for easy building GraphQL API with Django
long_description = file:docs/source/quickstart.rst
url = https://github.com/yefeza/django-graphbox
Expand Down
10 changes: 8 additions & 2 deletions src/django_graphbox/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self, session_manager=None):
self._models_by_op_name = {}
self._session_manager = session_manager

def add_model(self, model, exclude_fields=(), pagination_length=0, pagination_style='infinite', external_filters=[], internal_filters=[], filters_opeator=Q.AND, access_group=None, access_by_operation={}, validators_by_operation={}, internal_field_resolvers={}, exclude_fields_by_operation={}, save_as_password=[], callbacks_by_operation={}, **kwargs):
def add_model(self, model, exclude_fields=(), pagination_length=0, pagination_style='infinite', external_filters=[], internal_filters=[], filters_opeator=Q.AND, access_group=None, access_by_operation={}, validators_by_operation={}, internal_field_resolvers={}, exclude_fields_by_operation={}, save_as_password=[], callbacks_by_operation={}, custom_attrs_for_type=[], ordering_field='id', **kwargs):
"""Add model for build operations.
Args:
Expand All @@ -40,12 +40,17 @@ def add_model(self, model, exclude_fields=(), pagination_length=0, pagination_st
internal_field_resolvers (dict): Dictionary with the internal field value resolvers on create_field and update_field operations
save_as_password (list): List of fields to save as password with make_password function.
callbacks_by_operation (dict): Dictionary with the callbacks list to use for the access. {'operation': [callable(info, model_instance, **kwargs)], ...}
custom_attrs_for_type (list): List of custom attributes to add to the model type. [{'name': 'attr_name', 'value': 'attr_value'}, ...]
ordering_field (str): Field to use for ordering the list_field operation.
"""
#get the model name
model_name = model.__name__
#crreate the model type
model_metaclass = type(f"Meta", (), {'model': model, 'exclude_fields': exclude_fields})
model_type = type(f"{model_name}Type", (DjangoObjectType,), {'Meta': model_metaclass})
#add custom attributes to the model type
for attr in custom_attrs_for_type:
setattr(model_type, attr['name'], attr['value'])
#create paginated type
if pagination_length > 0 and pagination_style == 'paginated':
paginated_type = type(f"{model_name}PageType", (graphene.ObjectType,), {'items': graphene.List(model_type), 'page': graphene.Int(), 'has_next_page': graphene.Boolean(), 'has_previous_page': graphene.Boolean(), 'total_pages': graphene.Int(), 'total_items': graphene.Int()})
Expand All @@ -68,7 +73,8 @@ def add_model(self, model, exclude_fields=(), pagination_length=0, pagination_st
'internal_field_resolvers': internal_field_resolvers,
'exclude_fields_by_operation': exclude_fields_by_operation,
'save_as_password': save_as_password,
'callbacks_by_operation': callbacks_by_operation
'callbacks_by_operation': callbacks_by_operation,
'ordering_field': ordering_field
}
self._models_config[model_name]=config

Expand Down
2 changes: 1 addition & 1 deletion src/django_graphbox/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
MODEL_FIELD_TO_GRAPHENE_TYPE = {
'AutoField': graphene.ID,
'BigAutoField': graphene.ID,
'BigIntegerField': graphene.Int,
'BigIntegerField': graphene.BigInt if hasattr(graphene, 'BigInt') else graphene.Int,
'BooleanField': graphene.Boolean,
'CharField': graphene.String,
'DateField': graphene.Date,
Expand Down
3 changes: 2 additions & 1 deletion src/django_graphbox/helpers/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def list_resolver_function(parent, info, **kwargs):
external_filters=config.get('external_filters')
internal_filters=config.get('internal_filters')
filters_operator=config.get('filters_operator')
ordering_field=config.get('ordering_field')
query_object=None
for filter_config in external_filters:
param_value=kwargs.get(filter_config.get('param_name'))
Expand Down Expand Up @@ -90,7 +91,7 @@ def list_resolver_function(parent, info, **kwargs):
pagina=kwargs.get('page')
inicio=(pagina*pagination_length)-pagination_length
fin=inicio+pagination_length
items=model.objects.filter(query_object)[inicio:fin]
items=model.objects.filter(query_object).order_by(ordering_field)[inicio:fin]
callbacks=config.get('callbacks_by_operation').get('list_field')
if callbacks is not None:
for callback in callbacks:
Expand Down

0 comments on commit 10f0954

Please sign in to comment.