Exemplo n.º 1
0
class Person(Collection):

    __collection__ = 'persons'
    _allow_extra_fields = False
    _index = [{'type': 'hash', 'unique': False, 'fields': ['name']}]
    _allow_extra_fields = False  # prevent extra properties from saving into DB

    _key = String(required=True)
    name = String(required=True, allow_none=False)
    age = Integer(missing=None)
    dob = Date()
    is_staff = Boolean(default=False)

    cars = relationship(__name__ + ".Car", '_key', target_field='owner_key')

    @property
    def is_adult(self):
        return self.age and self.age >= 18

    @lazy_property
    def lazy_is_adult(self):
        return self.age and self.age >= 18

    def __str__(self):
        return "<Person(" + self.name + ")>"
class Student(Collection):

    __collection__ = 'students'

    _key = String(required=True)  # registration number
    name = String(required=True, allow_none=False)
    age = Integer()
Exemplo n.º 3
0
class Person(Collection):

    __collection__ = "persons"
    _allow_extra_fields = False
    _index = [{"type": "hash", "unique": False, "fields": ["name"]}]
    _allow_extra_fields = False  # prevent extra properties from saving into DB

    _key = String()
    name = String(required=True, allow_none=False)
    age = Integer(allow_none=True, missing=None)
    dob = Date(allow_none=True, missing=None)
    is_staff = Boolean(default=False)

    cars = relationship(__name__ + ".Car", "_key", target_field="owner_key")

    @property
    def is_adult(self):
        return self.age and self.age >= 18

    @lazy_property
    def lazy_is_adult(self):
        return self.age and self.age >= 18

    def __str__(self):
        return "<Person(" + self.name + ")>"
Exemplo n.º 4
0
class Book(Collection):
    """Class for Book node"""

    __collection__ = "books"

    _key = String(required=True)  # book_id
    authors = String(required=True)
    year = Integer(required=True)
    title = String(required=True)
    language = String(required=True)

    def __init__(self, book_id=None, authors=None, year=None, title=None, language=None):
        super(Book, self).__init__()
        self._key = book_id
        self.authors = authors
        self.year = year
        self.title = title
        self.language = language

    def insert(self):
        """Inserting book node to graph"""
        db.add(self)

    def find_by_id(self):
        book = db.query(Book).by_key(self._key)
        return book

    def linked_tags(self):
        """Return list of tags for specific book"""
        book = db.query(Book).by_key(self._key)
        graph.expand(book, depth=1, direction='any')
        tags = [tag._object_from.tag_name for tag in book._relations["tagged_to"]]
        return tags

    def link_to_tag(self, tag_id):
        """Create relationship (Book)-[:TAGGED_TO]->(Tag) """
        if self.find_by_id():
            tag = db.query(Tag).by_key(tag_id)
            db.add(graph.relation(self, Relation("tagged_to"), tag))

    @staticmethod
    def books():
        """Return list of all books"""
        books = db.query(Book).all()
        return books


    @staticmethod
    def most_popular_books():
        """ Returns books which are read by most amount of users """
        query = """
                FOR user IN users
                    FOR book IN INBOUND user reads
                        COLLECT book_doc=book WITH COUNT INTO amount
                        SORT book_doc, amount ASC
                        RETURN {"book":book_doc, "count":amount}
                """
        return graph.aql(query)
Exemplo n.º 5
0
class Student(Collection):
    __collection__ = "students"

    _key = String(required=True)  # registration number
    name = String(required=True, allow_none=False)
    age = Integer(allow_none=True, missing=None)

    def __str__(self):
        return "<Student({})>".format(self.name)
Exemplo n.º 6
0
class Subject(Collection):
    __collection__ = "subjects"

    _key = String(required=True)  # subject code
    name = String(required=True)
    credit_hours = Integer(allow_none=True, missing=None)
    has_labs = Boolean(missing=True)

    def __str__(self):
        return "<Subject({})>".format(self.name)
Exemplo n.º 7
0
class Car(Collection):
    __collection__ = "cars"
    _allow_extra_fields = True

    make = String(required=True)
    model = String(required=True)
    year = Integer(required=True)
    owner_key = String(allow_none=True, missing=None)

    owner = relationship(Person, "owner_key")

    def __str__(self):
        return "<Car({} - {} - {})>".format(self.make, self.model, self.year)
Exemplo n.º 8
0
class Car(Collection):

    __collection__ = 'cars'
    _allow_extra_fields = True

    make = String(required=True)
    model = String(required=True)
    year = Integer(required=True)
    owner_key = String()

    owner = relationship(Person, 'owner_key')

    def __str__(self):
        return "<Car({} - {} - {})>".format(self.make, self.model, self.year)
Exemplo n.º 9
0
        class MyModel(Collection):
            __collection__ = "MyCollection"

            _key = String()
            integer = Integer()
            datetime = DateTime()
            url = Url()
            bool_val = Boolean()
            str_list = List(String)
            floater = Float()
            date = Date()
            timedelta = TimeDelta()
            email = Email()
            number = Number()
            uuid = UUID()
            d = Dict()
Exemplo n.º 10
0
        class MyModel(Collection):
            __collection__ = "MyCollection"

            _key = String(allow_none=True)
            integer = Integer(allow_none=True)
            datetime = DateTime(allow_none=True)
            url = Url(allow_none=True)
            bool_val = Boolean(allow_none=True)
            str_list = List(String, allow_none=True)
            floater = Float(allow_none=True)
            date = Date(allow_none=True)
            timedelta = TimeDelta(allow_none=True)
            email = Email(allow_none=True)
            number = Number(allow_none=True)
            uuid = UUID(allow_none=True)
            d = Dict(allow_none=True)
Exemplo n.º 11
0
class Person(Collection):
    class Hobby(Collection):
        class Equipment(Collection):
            name = String(required=False, allow_none=True)
            price = Number(required=False, allow_none=True)

        name = String(required=False, allow_none=True)
        type = String(required=True, allow_none=False)
        equipment = List(Nested(Equipment.schema()),
                         required=False,
                         allow_none=True,
                         default=None)

    __collection__ = "persons"
    _allow_extra_fields = False
    _index = [{"type": "hash", "unique": False, "fields": ["name"]}]
    _allow_extra_fields = False  # prevent extra properties from saving into DB

    _key = String()
    name = String(required=True, allow_none=False)
    age = Integer(allow_none=True, missing=None)
    dob = Date(allow_none=True, missing=None)
    is_staff = Boolean(default=False)
    favorite_hobby = Nested(Hobby.schema(),
                            required=False,
                            allow_none=True,
                            default=None)
    hobby = List(Nested(Hobby.schema()),
                 required=False,
                 allow_none=True,
                 default=None)
    cars = relationship(__name__ + ".Car", "_key", target_field="owner_key")

    @property
    def is_adult(self):
        return self.age and self.age >= 18

    @lazy_property
    def lazy_is_adult(self):
        return self.age and self.age >= 18

    def __str__(self):
        return "<Person(" + self.name + ")>"
Exemplo n.º 12
0
class Follows(Relation):
    __collection__ = "follows"

    _key = Integer(required=True)
Exemplo n.º 13
0
class Reads(Relation):
    __collection__ = "reads"

    _key = Integer(required=True)
Exemplo n.º 14
0
class TaggedTo(Relation):
    __collection__ = "tagged_to"

    _key = Integer(required=True)
Exemplo n.º 15
0
 class _Schema(Schema):
     _key = String(required=True)  # subject code
     name = String(required=True)
     credit_hours = Integer()
     has_labs = Boolean(missing=True)
Exemplo n.º 16
0
 class _Schema(Schema):
     _key = String(required=True)  # registration number
     name = String(required=True, allow_none=False)
     age = Integer()
Exemplo n.º 17
0
        class Person(Collection):
            __collection__ = "persons"
            _key_field = "name"

            name = String(required=True, allow_none=False)
            age = Integer(allow_none=True, missing=None)
Exemplo n.º 18
0
class Likes(Relation):
    __collection__ = "likes"

    _key = Integer(required=True)