# pylint: disable = missing-class-docstring # pylint: disable = missing-function-docstring """ ぼざクリ DB の構成 """ from __future__ import annotations from datetime import date, datetime from eloquent import Model class Comment (Model): # pylint: disable = too-many-instance-attributes id: int video_id: int comment_no: int user_id: int content: str posted_at: datetime nico_count: int vpos_ms: int __timestamps__ = False @property def video ( self, ) -> Video: return self.belongs_to (Video) @property def user ( self, ) -> User: return self.belongs_to (User) def upsert ( self, ) -> None: upsert (self, 'video_id', 'comment_no') class Tag (Model): id: int name: str __timestamps__ = False @property def video_tags ( self, ) -> VideoTag: return self.has_many (VideoTag) class User (Model): id: int code: str __timestamps__ = False @property def comments ( self, ) -> Comment: return self.has_many (Comment) class Video (Model): id: int code: str title: str description: str uploaded_at: datetime deleted_at: datetime | None __timestamps__ = False @property def video_histories ( self, ) -> VideoHistory: return self.has_many (VideoHistory) @property def video_tags ( self, ) -> VideoTag: return self.has_many (VideoTag) @property def comments ( self, ) -> Comment: return self.has_many (Comment) def upsert ( self, ) -> None: upsert (self, 'code') class VideoHistory (Model): id: int video_id: int fetched_at: date views_count: int __timestamps__ = False @property def video ( self, ) -> Video: return self.belongs_to (Video) def upsert ( self, ) -> None: upsert (self, 'video_id', 'fetched_at') class VideoTag (Model): id: int video_id: int tag_id: int tagged_at: date untagged_at: date | None __timestamps__ = False @property def video ( self, ) -> Video: return self.belongs_to (Video) @property def tag ( self, ) -> Tag: return self.belongs_to (Tag) def upsert ( self, ) -> None: upsert (self, 'video_id', 'tag_id') def upsert ( model: Model, *args: str, ) -> None: q = model.query () for arg in args: q = q.where (arg, getattr (model, arg)) row = q.first () if row is not None: model.id = row.id model._Model__exists = True # pylint: disable = protected-access model.save ()