class Book(Node): """Class for Book node""" element_plural = "books" registry_plural = "books" _id = String(unique=True) book_id = String() authors = String() year = Integer() language = String() def __init__(self, **kwargs): self._props = kwargs def find_by_id(self): book = graph.books.query(book_id=self.book_id).first() return book def insert(self): """Inserting book node to graph""" graph.books.create(book_id=self.book_id, authors=self.authors, year=self.year, title=self.title, language=self.language) def linked_tags(self): """Return list of tags for specific book""" tags = [tag.outV().tag_name for tag in self.inE()] return tags def link_to_tag(self, tag_id): """Create relationship (Book)-[:TAGGED_TO]->(Tag)""" book = self.find_by_id() if book: tag = graph.tags.query(tag_id=tag_id).first() graph.tagged_to.create(book, tag) @staticmethod def books(): """Return list of all books""" books = graph.query(Book).all() return [book for book in books] @staticmethod def most_popular_books(): """ Return books which are read by most amount of users """ query = """ SELECT book_title, Count(tags) from ( MATCH {class: User, as: user}.in('reads') {as: book} RETURN book.title, user ) group by book_title """ return graph.query(query) # alright?
class OWorksAt(Relationship): label = 'oworksat' #aux_id=Integer(nullable=False,unique=True) from_postgresql_ouser_id=Integer(nullable=False,unique=True) to_postgresql_ocompany_id=Integer(nullable=False,unique=True)
class OFriends(Relationship): label = 'ofriends' #aux_id=Integer(nullable=False,unique=True) from_postgresql_ouser_id=Integer(nullable=False,unique=True) to_postgresql_ouser_id=Integer(nullable=False,unique=True)
class OCompany(Node): element_plural = 'ocompany' postgresql_id=Integer(nullable=False,unique=True)
class OUsers(Node): element_plural = 'ousers' postgresql_id=Integer(nullable=False,unique=True)
class X86CPU(CPU): element_plural = 'x86cpu' version = Integer(nullable=True)
class SmartPhone(Node): element_plural = "smartPhone" brand = String(unique=True) price = Integer()
class InferredSynapse(BioNode): element_type = 'InferredSynapse' element_plural = 'InferredSynapses' name = String(nullable=False, unique=False, indexed=True) N = Integer(nullable=True, unique=False, indexed=True)