From ee971997adc2153e9c752196cb62bf687315da55 Mon Sep 17 00:00:00 2001 From: miteruzo Date: Wed, 16 Oct 2024 22:08:07 +0900 Subject: [PATCH] =?UTF-8?q?#6=20=E3=81=AE=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- eloquent.pyi | 6 ++++-- models.py | 42 ++++++++++++++---------------------------- my_eloquent.py | 32 ++++++++++++++++++++++++++++++++ update_db.py | 3 ++- 4 files changed, 52 insertions(+), 31 deletions(-) create mode 100644 my_eloquent.py diff --git a/eloquent.pyi b/eloquent.pyi index 9d2ce86..1ee4937 100644 --- a/eloquent.pyi +++ b/eloquent.pyi @@ -65,7 +65,7 @@ class Model: self, related_model: Type[_ModelT], foreign_key: str | None = None, - ) -> _ModelT: ... + ) -> list[_ModelT]: ... def belongs_to ( self, @@ -77,7 +77,7 @@ class Model: self, related_model: Type[_ModelT], foreign_key: str | None = None, - ) -> _ModelT: ... + ) -> list[_ModelT]: ... def save (self) -> None: ... @@ -135,3 +135,5 @@ class QueryBuilder (Generic[_ModelT]): def where (self, field: str, value: Any) -> QueryBuilder[_ModelT]: ... def where_null (self, field: str) -> QueryBuilder[_ModelT]: ... + + def _load_relation (self, relation_name: str) -> QueryBuilder[_ModelT]: ... diff --git a/models.py b/models.py index ced158e..d16e273 100644 --- a/models.py +++ b/models.py @@ -9,7 +9,7 @@ from __future__ import annotations from datetime import date, datetime -from eloquent import Model +from my_eloquent import Model class Comment (Model): @@ -40,8 +40,9 @@ class Comment (Model): def upsert ( self, + *args: str, ) -> None: - upsert (self, 'video_id', 'comment_no') + super ().upsert ('video_id', 'comment_no') class Tag (Model): @@ -53,7 +54,7 @@ class Tag (Model): @property def video_tags ( self, - ) -> VideoTag: + ) -> list[VideoTag]: return self.has_many (VideoTag) @@ -66,7 +67,7 @@ class User (Model): @property def comments ( self, - ) -> Comment: + ) -> list[Comment]: return self.has_many (Comment) @@ -83,25 +84,26 @@ class Video (Model): @property def video_histories ( self, - ) -> VideoHistory: + ) -> list[VideoHistory]: return self.has_many (VideoHistory) @property def video_tags ( self, - ) -> VideoTag: + ) -> list[VideoTag]: return self.has_many (VideoTag) @property def comments ( self, - ) -> Comment: + ) -> list[Comment]: return self.has_many (Comment) def upsert ( self, + *args: str, ) -> None: - upsert (self, 'code') + super ().upsert ('code') class VideoHistory (Model): @@ -120,8 +122,9 @@ class VideoHistory (Model): def upsert ( self, + *args: str, ) -> None: - upsert (self, 'video_id', 'fetched_at') + super ().upsert ('video_id', 'fetched_at') class VideoTag (Model): @@ -147,23 +150,6 @@ class VideoTag (Model): def upsert ( self, + *args: str, ) -> 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 () + super ().upsert ('video_id', 'tag_id') diff --git a/my_eloquent.py b/my_eloquent.py new file mode 100644 index 0000000..9f53a64 --- /dev/null +++ b/my_eloquent.py @@ -0,0 +1,32 @@ +from typing import Self + +import eloquent + + +class DatabaseManager (eloquent.DatabaseManager): + pass + + +class Model (eloquent.Model): + def upsert ( + self, + *args: str, + ) -> None: + q = self.query () + for arg in args: + q = q.where (arg, getattr (self, arg)) + row = q.first () + if row is not None: + self.id = row.id + self._Model__exists = True # pylint: disable = protected-access + self.save () + + @classmethod + def with_ ( + cls, + *relations: str, + ) -> eloquent.QueryBuilder[Self]: + q = cls.query () + for relation in relations: + q = q._load_relation (relation) + return q diff --git a/update_db.py b/update_db.py index 5ff33ca..4e8f25b 100644 --- a/update_db.py +++ b/update_db.py @@ -60,7 +60,8 @@ def update_tables ( video_history.fetched_at = now video_history.views_count = datum['viewCounter'] video_history.save () - video_tags = video.video_tags.where_not_null ('untagged_at').get () + video_tags = [video_tag for video_tag in video.video_tags + if video_tag.untagged_at is not None] tag: Tag | None video_tag: VideoTag | None for video_tag in video_tags: