class User(ValidatingDocument): _id = ObjectIdField(required=True, default=lambda: ObjectId()) primary_email = StrField(allow_blank=False, required=True) password = StrField(allow_blank=False, required=True, before_set=encrypt_password) secondary_emails = ListField(StrField(allow_blank=False), required=False) given_names = ListField(StrField(allow_blank=False), required=False) family_name = StrField(required=False) nickname = StrField(required=False, allow_blank=False) created = DateTimeField(required=True) updated = DateTimeField(required=True) class Meta: collection = 'users' indexes = [ IndexModel([('primary_email', ASCENDING)], unique=True) ] def is_valid_password(self, password): return bcrypt.checkpw(password.encode(), self.password.encode()) async def pre_save(self): timestamp = datetime.utcnow() if not self.created: self.created = timestamp self.updated = timestamp
class Post(ValidatingDocument): _id = ObjectIdField(required=True, default=lambda: ObjectId()) user = RefField(User, required=True) title = StrField(allow_blank=False, required=True) body = StrField(allow_blank=False, required=True) created = DateTimeField(required=True) updated = DateTimeField(required=True) class Meta: collection = 'posts' async def pre_save(self): timestamp = datetime.utcnow() if not self.created: self.created = timestamp self.updated = timestamp
class Permission(ValidatingDocument): _id = ObjectIdField(required=True, default=lambda: ObjectId()) user = RefField(User, required=True) roles = ListField(StrField(allow_blank=False, required=True)) created = DateTimeField(required=True) updated = DateTimeField(required=True) class Meta: collection = 'permissions' indexes = [ IndexModel([('user', ASCENDING)], unique=True) ] async def pre_save(self): timestamp = datetime.utcnow() if not self.created: self.created = timestamp self.updated = timestamp
class Comment(ValidatingDocument): _id = ObjectIdField(required=True, default=lambda: ObjectId()) user = RefField(User, required=True) post = RefField(User, required=True) in_response_to = ObjectIdField() content = StrField(allow_blank=False, required=True) created = DateTimeField(required=True) updated = DateTimeField(required=True) class Meta: collection = 'comments' async def pre_save(self): timestamp = datetime.utcnow() if not self.created: self.created = timestamp self.updated = timestamp
class Post(Document): # _id field will be added automatically as # _id = ObjectIdField(defalut=lambda: ObjectId()) title = StrField(allow_blank=False, max_length=50) body = StrField() created = DateTimeField(default=lambda: datetime.utcnow()) views = IntField(default=0) rate = FloatField(default=0.0) author = RefField(User, mongo_name='user') comments = ListField(EmbDocField('models.Comment'), default=lambda: list()) class Meta: collection = 'posts' indexes = [IndexModel([('created', DESCENDING)])] default_sort = [('created', DESCENDING)]
class Post(Document): """Post model.""" title = StrField(required=True, allow_blank=False) views = IntField(required=True, default=0) created = DateTimeField(required=True, default=lambda: datetime.utcnow()) rate = FloatField(required=True, default=0.0) author = RefField(User, required=True) comments = ListField(EmbDocField('models.Comment'), required=True, default=lambda: list()) class Meta: """Post meta.""" collection = 'posts' indexes = [ IndexModel([('title', ASCENDING)], unique=True, name='title_index'), IndexModel([('author', ASCENDING), ('created', DESCENDING)], name='author_created_index') ]
(IntField, 13), (FloatField, 1.3), (BoolField, True), (DateTimeField, dt), (ObjectIdField, ObjectId('58ce6d537e592254b67a503d')), (EmailField, '*****@*****.**'), (DecimalField, Decimal('0.005')), ] @pytest.mark.parametrize('field, expected', [ (StrField(required=False), None), (IntField(required=False), None), (FloatField(required=False), None), (BoolField(required=False), None), (DateTimeField(required=False), None), (ObjectIdField(required=False), None), (EmbDocField(EmbDoc, required=False), None), (ListField(EmbDocField(EmbDoc), required=False), None), (RefField(RefDoc, required=False), None), (EmailField(required=False), None), ]) def test_field_not_exist_get_value(field, expected): class Doc(Document): value = field assert Doc().value is expected @pytest.mark.parametrize('field, default', FIELD_DEFAULT) def test_field_attributes(field, default):