Browse Source

#6 の対応

main
みてるぞ 4 weeks ago
parent
commit
ee971997ad
4 changed files with 52 additions and 31 deletions
  1. +4
    -2
      eloquent.pyi
  2. +14
    -28
      models.py
  3. +32
    -0
      my_eloquent.py
  4. +2
    -1
      update_db.py

+ 4
- 2
eloquent.pyi View File

@@ -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]: ...

+ 14
- 28
models.py View File

@@ -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')

+ 32
- 0
my_eloquent.py View File

@@ -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

+ 2
- 1
update_db.py View File

@@ -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:


Loading…
Cancel
Save