ぼざろクリーチャーシリーズ DB 兼 API(自分用)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

models.py 2.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # pylint: disable = missing-class-docstring
  2. # pylint: disable = missing-function-docstring
  3. """
  4. ぼざクリ DB の構成
  5. """
  6. from __future__ import annotations
  7. from datetime import date, datetime
  8. from eloquent import Model
  9. class Comment (Model):
  10. # pylint: disable = too-many-instance-attributes
  11. id: int
  12. video_id: int
  13. comment_no: int
  14. user_id: int
  15. content: str
  16. posted_at: datetime
  17. nico_count: int
  18. vpos_ms: int
  19. __timestamps__ = False
  20. @property
  21. def video (
  22. self,
  23. ) -> Video:
  24. return self.belongs_to (Video)
  25. @property
  26. def user (
  27. self,
  28. ) -> User:
  29. return self.belongs_to (User)
  30. def upsert (
  31. self,
  32. ) -> None:
  33. upsert (self, 'video_id', 'comment_no')
  34. class Tag (Model):
  35. id: int
  36. name: str
  37. __timestamps__ = False
  38. @property
  39. def video_tags (
  40. self,
  41. ) -> VideoTag:
  42. return self.has_many (VideoTag)
  43. class User (Model):
  44. id: int
  45. code: str
  46. __timestamps__ = False
  47. @property
  48. def comments (
  49. self,
  50. ) -> Comment:
  51. return self.has_many (Comment)
  52. class Video (Model):
  53. id: int
  54. code: str
  55. title: str
  56. description: str
  57. uploaded_at: datetime
  58. deleted_at: datetime | None
  59. __timestamps__ = False
  60. @property
  61. def video_histories (
  62. self,
  63. ) -> VideoHistory:
  64. return self.has_many (VideoHistory)
  65. @property
  66. def video_tags (
  67. self,
  68. ) -> VideoTag:
  69. return self.has_many (VideoTag)
  70. @property
  71. def comments (
  72. self,
  73. ) -> Comment:
  74. return self.has_many (Comment)
  75. def upsert (
  76. self,
  77. ) -> None:
  78. upsert (self, 'code')
  79. class VideoHistory (Model):
  80. id: int
  81. video_id: int
  82. fetched_at: date
  83. views_count: int
  84. __timestamps__ = False
  85. @property
  86. def video (
  87. self,
  88. ) -> Video:
  89. return self.belongs_to (Video)
  90. def upsert (
  91. self,
  92. ) -> None:
  93. upsert (self, 'video_id', 'fetched_at')
  94. class VideoTag (Model):
  95. id: int
  96. video_id: int
  97. tag_id: int
  98. tagged_at: date
  99. untagged_at: date | None
  100. __timestamps__ = False
  101. @property
  102. def video (
  103. self,
  104. ) -> Video:
  105. return self.belongs_to (Video)
  106. @property
  107. def tag (
  108. self,
  109. ) -> Tag:
  110. return self.belongs_to (Tag)
  111. def upsert (
  112. self,
  113. ) -> None:
  114. upsert (self, 'video_id', 'tag_id')
  115. def upsert (
  116. model: Model,
  117. *args: str,
  118. ) -> None:
  119. q = model.query ()
  120. for arg in args:
  121. q = q.where (arg, getattr (model, arg))
  122. row = q.first ()
  123. if row is not None:
  124. model.id = row.id
  125. model._Model__exists = True # pylint: disable = protected-access
  126. model.save ()