-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Improve Reverse Delete Rules #2765
Open
combscCode
wants to merge
11
commits into
MongoEngine:master
Choose a base branch
from
combscCode:handle-uninitialized-delete-rules
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
174094b
add lazy delete rules
combscCode c2441c5
improve test case
combscCode fd5a5ae
run black
combscCode 7cc7f08
ok actually use pre-commit now
combscCode 4b25517
add myself to authors
combscCode b7cbf77
ugly test that I need to clean but its working
combscCode 0fa2ae7
add todo for how to fix test
combscCode 52dbc7c
fix test case to use documents that don't exist yet
combscCode 48dc7e9
Implement generic delete rules
combscCode 8f8eb3f
add warning to GenericReferenceField.document_type
combscCode 1514608
improve docs
combscCode 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
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 |
---|---|---|
|
@@ -30,7 +30,9 @@ | |
GeoJsonBaseField, | ||
LazyReference, | ||
ObjectIdField, | ||
_undefined_document_delete_rules, | ||
get_document, | ||
update_document_if_registered, | ||
) | ||
from mongoengine.base.utils import LazyRegexCompiler | ||
from mongoengine.common import _import_class | ||
|
@@ -1435,6 +1437,39 @@ def sync_all(self): | |
self.owner_document.objects(**filter_kwargs).update(**update_kwargs) | ||
|
||
|
||
class GenericReferenceDeleteHandler: | ||
"""Used to make delete rules work for GenericReferenceFields. | ||
|
||
Since delete rules are registered on single documents, we'll always need | ||
something like this to make a generic reference (AKA, a reference to | ||
multiple documents) with delete rules work. | ||
""" | ||
|
||
def __init__(self, documents): | ||
self.documents = documents | ||
|
||
def __getattr__(self, name): | ||
raise NotImplementedError( | ||
f"{self.__name__} is intended only to be used " | ||
"to enable generic reference delete rules. You " | ||
"are trying to access undefined attributes." | ||
) | ||
|
||
def register_delete_rule(self, document_cls, field_name, rule): | ||
for doc in self.documents: | ||
doc = update_document_if_registered(doc) | ||
if isinstance(doc, str): | ||
_undefined_document_delete_rules[doc].append( | ||
( | ||
document_cls, | ||
field_name, | ||
rule, | ||
) | ||
) | ||
else: | ||
doc.register_delete_rule(document_cls, field_name, rule) | ||
|
||
|
||
class GenericReferenceField(BaseField): | ||
"""A reference to *any* :class:`~mongoengine.document.Document` subclass | ||
that will be automatically dereferenced on access (lazily). | ||
|
@@ -1453,8 +1488,11 @@ class GenericReferenceField(BaseField): | |
* You can use the choices param to limit the acceptable Document types | ||
""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
def __init__(self, *args, reverse_delete_rule=DO_NOTHING, **kwargs): | ||
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. Would like to call this param out in the |
||
choices = kwargs.pop("choices", None) | ||
self.reverse_delete_rule = reverse_delete_rule | ||
if self.reverse_delete_rule is not DO_NOTHING and not choices: | ||
raise ValidationError("choices must be set to use reverse_delete_rules") | ||
super().__init__(*args, **kwargs) | ||
self.choices = [] | ||
# Keep the choices as a list of allowed Document class names | ||
|
@@ -1472,6 +1510,15 @@ def __init__(self, *args, **kwargs): | |
"Document subclasses and/or str" | ||
) | ||
|
||
@property | ||
def document_type(self): | ||
# This property is exposed purely for enabling reverse_delete_rule | ||
# on this class. Do not attempt to use it in any other way, if you | ||
# do a NotImplementedError will be raised. | ||
if not self.choices: | ||
return None | ||
return GenericReferenceDeleteHandler(self.choices) | ||
|
||
def _validate_choices(self, value): | ||
if isinstance(value, dict): | ||
# If the field has not been dereferenced, it is still a dict | ||
|
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
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.
In the ideal world I would cut out
document_type
here and just make fields responsible for implementingregister_delete_rule
.However, when I tried this I realized that it would break the interface for how to register delete rules and thus any users dynamically setting delete rules would be upset.