예제 #1
0
    class Customer(User):
        role = StrField(default='customer')
        address = StrField()

        class Meta:
            collection = 'users'
            default_query = {User.role.s: 'customer'}
예제 #2
0
    class DraftDoc(Doc):
        name = StrField(allow_blank=True)
        draft = BoolField(default=True)

        class Meta:
            collection = 'docs'
            default_query = {'draft': False}
예제 #3
0
    class DocWithMeta(Document):
        _id = StrField(required=True)
        value = IntField(required=True)
        name = SynonymField(_id)

        class Meta:
            collection = 'docs'
            indexes = [IndexModel([('value', ASCENDING)], name='value_index')]
            default_query = {'value': 1}
            write_concern = WriteConcern(w=0)
예제 #4
0
class Search_query(Document):

    user_id = StrField()
    engine = StrField()
    query = StrField()
    result = StrField()
    date = DateTimeField()

    def to_json(self):
        return {
            'user_id': self.user_id,
            'engine': self.engine,
            'query': self.query,
            'result': self.result,
            'date': self.date
        }

    class Meta:
        collection = 'search_query'
예제 #5
0
    class User(Document):
        _id = StrField()
        role = StrField(default='user')

        name = SynonymField(_id)

        class Meta:
            collection = 'users'

        @classmethod
        def from_mongo(cls, data):
            return super(User, cls.get_class(data['role'])).from_mongo(data)

        @classmethod
        def get_class(cls, role):
            if role == 'user':
                return User
            if role == 'admin':
                return Admin
            if role == 'customer':
                return Customer
예제 #6
0
class User(Document):
    """User model."""

    _id = StrField(required=True, allow_blank=False, max_length=10)
    active = BoolField(required=True, default=True)
    posts = ListField(RefField('models.Post'),
                      required=True,
                      default=lambda: list())
    data = AnyField(required=False)

    # synonyms
    name = SynonymField(_id)
예제 #7
0
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')
        ]
예제 #8
0
class RefDoc(Document):
    str_field = StrField(required=False)
예제 #9
0
 class Doc(Document):
     set_choices = StrField(choices={'xxx', 'yyy'})
     dict_choices = StrField(choices={'xxx': 'AAA', 'yyy': 'BBB'})
예제 #10
0
FIELD_DEFAULT = [
    (AnyField, 'xxx'),
    (StrField, 'xxx'),
    (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
예제 #11
0
class WrongEmbDoc(EmbeddedDocument):
    wrong = StrField(required=True)
예제 #12
0
 class Child(Parent):
     name = StrField()
예제 #13
0
class Comment(EmbeddedDocument):
    """Comment model."""

    _id = ObjectIdField(required=True, default=lambda: ObjectId())
    body = StrField(required=True, allow_blank=False)
    author = RefField(User, required=True, mongo_name='user')
예제 #14
0
 class Parent(Document):
     name = StrField()
예제 #15
0
 class Doc(Document):
     _id = StrField(required=True, allow_blank=False)
예제 #16
0
 class Mixin:
     name = StrField()
예제 #17
0
 class Parent1(Root):
     value = FloatField()
     name = StrField()
예제 #18
0
 class Mixin(object):
     name = StrField()
예제 #19
0
 class Parent(Document):
     _id = StrField()
     name = SynonymField(_id)
예제 #20
0
    class Doc(Document):
        name = StrField(allow_blank=False)
        draft = BoolField(default=False)

        class Meta:
            collection = 'docs'
예제 #21
0
class DocWithSynonymStr(Document):
    _id = StrField(required=True, allow_blank=False)
    name = SynonymField('_id')
예제 #22
0
 class DocWithoutMeta(Document):
     _id = StrField(required=True)
     name = SynonymField(_id)
예제 #23
0
class Doc(Document):
    str_field = StrField(regex=r'[abc]+')
    int_field = IntField(gt=0, lt=10)
    emb_field = EmbDocField(EmbDoc)
    lst_field = ListField(RefField(RefDoc), min_length=1, max_length=1)
예제 #24
0
 class Child3(Parent1, Parent2):
     value = StrField()
예제 #25
0
 class DocWrongId(Document):
     _id = StrField(required=False)
예제 #26
0
    class Admin(User):
        role = StrField(default='admin')

        class Meta:
            collection = 'users'
            default_query = {User.role.s: 'admin'}
예제 #27
0
 class Parent2(Root):
     value = IntField()
     slug = StrField()
예제 #28
0
 class Child(Parent, EmbeddedDocument):
     name = StrField()