Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Book(db.Model):
- title = db.StringProperty(required=True)
- author = db.StringProperty(required=True)
- created = db.DateTimeProperty(auto_now_add=True)
- # Get all the Chapters for a book
- def getChapters(self):
- key = self.chapterMemkey()
- chapters = memcache.get(key)
- if chapters is None:
- logging.info('DB access for key %s.', key)
- chapters = Chapter.all().ancestor(self).order("number").fetch(100)
- if not memcache.set(key, chapters, 300):
- logging.error('Memcache set failed for Chapters.')
- else:
- logging.info('Memcache for key %s.', key)
- return chapters
- class Chapter(db.Model):
- """ All chapters that a book have """
- title = db.StringProperty(required=True)
- number = db.IntegerProperty(default=1)
- created = db.DateTimeProperty(auto_now_add=True)
- book = db.ReferenceProperty(Book,
- required=True,
- collection_name='chapters')
- # Search by Book (parent)
- @classmethod
- def byBook(cls, book, limit=100):
- chapter = book.getChapters()
- return chapter
- # Search by id
- @classmethod
- def byId(cls, id, book):
- return Chapter.get_by_id(long(id), parent=book)
- class Vote(db.Model):
- """ All votes that a book-chapter have """
- chapterNumber = db.IntegerProperty(required=True)
- option = db.IntegerProperty(default=1)
- value = db.IntegerProperty(default=1)
- book = db.ReferenceProperty(Book,
- required=True,
- collection_name='book_votes')
- chapter = db.ReferenceProperty(Chapter,
- required=True,
- collection_name='chapter_votes')
- # --------------------------
- # ClassMethods for the class
- # --------------------------
- # Search by Book (parent)
- @classmethod
- def byBook(cls, book, limit=100):
- vote = book.getVotes()
- return vote
- # Search by id
- @classmethod
- def byId(cls, id, book):
- return Vote.get_by_id(long(id), parent=book)
- @classmethod
- def byChapter(cls, chapter, limit=100):
- vote = chapter.getVotes()
- return vote
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement