Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. class Book(db.Model):
  2. title = db.StringProperty(required=True)
  3. author = db.StringProperty(required=True)
  4. created = db.DateTimeProperty(auto_now_add=True)
  5.  
  6. # Get all the Chapters for a book
  7. def getChapters(self):
  8. key = self.chapterMemkey()
  9. chapters = memcache.get(key)
  10. if chapters is None:
  11. logging.info('DB access for key %s.', key)
  12. chapters = Chapter.all().ancestor(self).order("number").fetch(100)
  13. if not memcache.set(key, chapters, 300):
  14. logging.error('Memcache set failed for Chapters.')
  15. else:
  16. logging.info('Memcache for key %s.', key)
  17. return chapters
  18.  
  19. class Chapter(db.Model):
  20. """ All chapters that a book have """
  21. title = db.StringProperty(required=True)
  22. number = db.IntegerProperty(default=1)
  23. created = db.DateTimeProperty(auto_now_add=True)
  24.  
  25. book = db.ReferenceProperty(Book,
  26. required=True,
  27. collection_name='chapters')
  28.  
  29. # Search by Book (parent)
  30. @classmethod
  31. def byBook(cls, book, limit=100):
  32. chapter = book.getChapters()
  33. return chapter
  34.  
  35. # Search by id
  36. @classmethod
  37. def byId(cls, id, book):
  38. return Chapter.get_by_id(long(id), parent=book)
  39.  
  40. class Vote(db.Model):
  41. """ All votes that a book-chapter have """
  42. chapterNumber = db.IntegerProperty(required=True)
  43. option = db.IntegerProperty(default=1)
  44. value = db.IntegerProperty(default=1)
  45.  
  46. book = db.ReferenceProperty(Book,
  47. required=True,
  48. collection_name='book_votes')
  49. chapter = db.ReferenceProperty(Chapter,
  50. required=True,
  51. collection_name='chapter_votes')
  52.  
  53. # --------------------------
  54. # ClassMethods for the class
  55. # --------------------------
  56.  
  57. # Search by Book (parent)
  58. @classmethod
  59. def byBook(cls, book, limit=100):
  60. vote = book.getVotes()
  61. return vote
  62.  
  63. # Search by id
  64. @classmethod
  65. def byId(cls, id, book):
  66. return Vote.get_by_id(long(id), parent=book)
  67.  
  68. @classmethod
  69. def byChapter(cls, chapter, limit=100):
  70. vote = chapter.getVotes()
  71. return vote
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement