Skip to content
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

PYMODM-95 Validating EmbeddedMongoModel in EmbeddedDocument type fields #55

Merged
merged 1 commit into from Aug 24, 2018

Conversation

shreybatra
Copy link
Contributor

Checked and validated type of related_field in EmbeddedDocumentField and EmbeddedDocumentListField. Raised Value error the same was as RelatedModelFieldsBase.

@shreybatra
Copy link
Contributor Author

Also corrected the test case where Travis was failing. The testcase mentioned MongoModel in an EmbeddedDocumentListField. It has been corrected. @ShaneHarvey @prashantmital please review.

Copy link
Member

@ShaneHarvey ShaneHarvey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great start. I think it can be improved in two ways:

  1. Create a RelatedEmbeddedModelFieldsBase class (in pymodm/base/fields.py) that inherits from RelatedModelFieldsBase. The new class should perform the embedded model validation in __init__ as well as override the related_model property. This way the embedded model is validated even if it's a string. Also if we ever need to add a new field class that holds an embedded model we can just inherit from RelatedEmbeddedModelFieldsBase.

  2. Add tests in the relevant test/field_types/ files for validation of embedded model classes and strings.

@shreybatra shreybatra force-pushed the fix/embedded-model branch 2 times, most recently from 686c4b4 to a62a428 Compare August 21, 2018 05:40
@shreybatra
Copy link
Contributor Author

@ShaneHarvey done the changes. Please review. :)

Copy link
Member

@ShaneHarvey ShaneHarvey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! Just a few style comments.

pymodm/fields.py Outdated
from pymodm.errors import ValidationError, ConfigurationError
from pymodm.base.fields import MongoBaseField
from pymodm.files import File, GridFSStorage, FieldFile, ImageFieldFile
from pymodm.vendor import parse_datetime
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please undo these import rearrangements since it isn't relevant to this change. We like to keep them ordered the way they are: standard library, third party, and finally imports from the current library (pymodm).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ShaneHarvey done this.

@@ -267,6 +267,37 @@ def _model_to_document(self, value):
'%s is not a valid %s' % (value, self.related_model.__name__))


class RelatedEmbeddedModelFieldsBase(RelatedModelFieldsBase):
'''Base class for EmbeddedDocument and EmbeddedDocumentListField.'''
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change to double quotes ("""...""")?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ShaneHarvey done this.

@@ -194,3 +194,22 @@ def test_coerce_reference_type(self):
comment = Comment(body='this is a comment', post=post_id).save()
comment.refresh_from_db()
self.assertEqual('this is a post', comment.post.body)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

'''Base class for EmbeddedDocument and EmbeddedDocumentListField.'''

def __init__(self, model, verbose_name=None, mongo_name=None, **kwargs):
super(RelatedEmbeddedModelFieldsBase, self).__init__(model=model,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please wrap all lines at 80 characters.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ShaneHarvey done this.

@shreybatra
Copy link
Contributor Author

@ShaneHarvey done. Please review.

@ShaneHarvey ShaneHarvey merged commit 635bdfb into mongodb:master Aug 24, 2018
@ShaneHarvey
Copy link
Member

Thanks @shreybatra!

@shreybatra
Copy link
Contributor Author

@ShaneHarvey ur welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants