-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reverse relations input in mutations update #14
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
894ea92
Added global settings. Fixed relation without related_name ObjectType…
justinask7 a65106e
Flake updates
justinask7 09b0ef6
Added entry to README
justinask7 c97b9a2
PR updates
justinask7 608f8e8
Readme update
justinask7 b49aa03
Merge pull request #1 from CornerCaseTechnologies/ft/global-reverse-r…
justinask7 9032ea0
Updated imports in tests
justinask7 124fa02
PR updates: removing six, test assertion update
justinask7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ | |
check_perms, | ||
check_authenticated, | ||
) | ||
from .settings import graphene_django_plus_settings | ||
from .utils import ( | ||
get_node, | ||
get_nodes, | ||
|
@@ -94,7 +95,7 @@ def _get_fields(model, only_fields, exclude_fields, required_fields): | |
# can be set to null, otherwise updates won't work. | ||
fields.extend( | ||
[ | ||
(field.related_name, field) | ||
(field.related_name or field.name + "_set", field) | ||
for field in sorted( | ||
list(model._meta.related_objects), | ||
key=lambda field: field.name, | ||
|
@@ -133,6 +134,11 @@ def _get_fields(model, only_fields, exclude_fields, required_fields): | |
description=field.help_text, | ||
) | ||
elif isinstance(field, (ManyToOneRel, ManyToManyRel)): | ||
reverse_rel_include = graphene_django_plus_settings.MUTATIONS_INCLUDE_REVERSE_RELATIONS | ||
# Checking whether it was globally configured to not include reverse relations | ||
if isinstance(field, ManyToOneRel) and not reverse_rel_include and not only_fields: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @justinask7 Adding 'unless the field is in only_fields' to the comment certainly makes the reading easier. |
||
continue | ||
|
||
ret[name] = graphene.List( | ||
graphene.ID, | ||
description='Set list of {0}'.format( | ||
|
@@ -573,17 +579,17 @@ def perform_mutation(cls, root, info, **data): | |
cls.clean_instance(instance, cleaned_input) | ||
cls.save(info, instance, cleaned_input) | ||
|
||
# save m2m data | ||
# save m2m and related object's data | ||
for f in itertools.chain( | ||
instance._meta.many_to_many, | ||
instance._meta.related_objects, | ||
instance._meta.private_fields, | ||
): | ||
if isinstance(f, (ManyToOneRel, ManyToManyRel)): | ||
# Handle reverse side relationships. | ||
d = cleaned_input.get(f.related_name, None) | ||
d = cleaned_input.get(f.related_name or f.name + "_set", None) | ||
if d is not None: | ||
target_field = getattr(instance, f.related_name) | ||
target_field = getattr(instance, f.related_name or f.name + "_set") | ||
target_field.set(d) | ||
elif hasattr(f, 'save_form_data'): | ||
d = cleaned_input.get(f.name, None) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
""" | ||
Settings for graphene-django-plus are all namespaced in the GRAPHENE_DJANGO_PLUS setting. | ||
For example your project's `settings.py` file might look like this: | ||
GRAPHENE_DJANGO_PLUS = { | ||
'MUTATIONS_INCLUDE_REVERSE_RELATIONS': False | ||
} | ||
This module provides the `graphene_django_plus_settings` object, that is used to access | ||
graphene-django-plus settings, checking for user settings first, then falling | ||
back to the defaults. | ||
""" | ||
from __future__ import unicode_literals | ||
justinask7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import six | ||
justinask7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
from django.conf import settings | ||
from django.test.signals import setting_changed | ||
|
||
import importlib | ||
|
||
|
||
# Copied shamelessly from Django REST Framework and graphene-django | ||
|
||
DEFAULTS = { | ||
"MUTATIONS_INCLUDE_REVERSE_RELATIONS": True, | ||
} | ||
|
||
# List of settings that may be in string import notation. | ||
IMPORT_STRINGS = [] | ||
|
||
|
||
def perform_import(val, setting_name): | ||
""" | ||
If the given setting is a string import notation, | ||
then perform the necessary import or imports. | ||
""" | ||
if val is None: | ||
return None | ||
elif isinstance(val, six.string_types): | ||
justinask7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return import_from_string(val, setting_name) | ||
elif isinstance(val, (list, tuple)): | ||
return [import_from_string(item, setting_name) for item in val] | ||
return val | ||
|
||
|
||
def import_from_string(val, setting_name): | ||
""" | ||
Attempt to import a class from a string representation. | ||
""" | ||
try: | ||
# Nod to tastypie's use of importlib. | ||
parts = val.split(".") | ||
module_path, class_name = ".".join(parts[:-1]), parts[-1] | ||
module = importlib.import_module(module_path) | ||
return getattr(module, class_name) | ||
except (ImportError, AttributeError) as e: | ||
msg = "Could not import '%s' for graphene-django-plus setting '%s'. %s: %s." % ( | ||
val, | ||
setting_name, | ||
e.__class__.__name__, | ||
e, | ||
) | ||
raise ImportError(msg) | ||
|
||
|
||
class GrapheneDjangoPlusSettings(object): | ||
""" | ||
A settings object, that allows API settings to be accessed as properties. | ||
For example: | ||
from graphene_django_plus.settings import settings | ||
print(settings.MUTATIONS_INCLUDE_REVERSE_RELATIONS) | ||
Any setting with string import paths will be automatically resolved | ||
and return the class, rather than the string literal. | ||
""" | ||
|
||
def __init__(self, user_settings=None, defaults=None, import_strings=None): | ||
if user_settings: | ||
self._user_settings = user_settings | ||
self.defaults = defaults or DEFAULTS | ||
self.import_strings = import_strings or IMPORT_STRINGS | ||
self._cached_attrs = set() | ||
|
||
@property | ||
def user_settings(self): | ||
if not hasattr(self, "_user_settings"): | ||
self._user_settings = getattr(settings, "GRAPHENE_DJANGO_PLUS", {}) | ||
return self._user_settings | ||
|
||
def reload(self): | ||
for attr in self._cached_attrs: | ||
delattr(self, attr) | ||
self._cached_attrs.clear() | ||
if hasattr(self, '_user_settings'): | ||
delattr(self, '_user_settings') | ||
|
||
def __getattr__(self, attr): | ||
if attr not in self.defaults: | ||
raise AttributeError("Invalid graphene-django-plus setting: '%s'" % attr) | ||
|
||
try: | ||
# Check if present in user settings | ||
val = self.user_settings[attr] | ||
except KeyError: | ||
# Fall back to defaults | ||
val = self.defaults[attr] | ||
|
||
# Coerce import strings into classes | ||
if attr in self.import_strings: | ||
val = perform_import(val, attr) | ||
|
||
# Cache the result | ||
self._cached_attrs.add(attr) | ||
setattr(self, attr, val) | ||
return val | ||
|
||
|
||
graphene_django_plus_settings = GrapheneDjangoPlusSettings(None, DEFAULTS, IMPORT_STRINGS) | ||
|
||
|
||
def reload_graphene_django_plus_settings(*args, **kwargs): | ||
setting = kwargs["setting"] | ||
if setting == "GRAPHENE_DJANGO_PLUS": | ||
graphene_django_plus_settings.reload() | ||
|
||
|
||
setting_changed.connect(reload_graphene_django_plus_settings) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This condition is used 3 times, so a utility function could be used.