Skip to content

Commit

Permalink
more typing
Browse files Browse the repository at this point in the history
  • Loading branch information
alphatownsman committed May 28, 2024
1 parent bd69fd3 commit 7388360
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 11 deletions.
15 changes: 10 additions & 5 deletions journal/models/itemlist.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import typing
from functools import cached_property

import django.dispatch
Expand All @@ -18,18 +19,19 @@ class List(Piece):
List (abstract model)
"""

MEMBER_CLASS: "type[ListMember]"
members: "models.QuerySet[ListMember]"
owner = models.ForeignKey(APIdentity, on_delete=models.PROTECT)
visibility = models.PositiveSmallIntegerField(
default=0
) # 0: Public / 1: Follower only / 2: Self only
) # 0: Public / 1: Follower only / 2: Self only # type:ignore
created_time = models.DateTimeField(default=timezone.now)
edited_time = models.DateTimeField(auto_now=True)
metadata = models.JSONField(default=dict)

class Meta:
abstract = True

MEMBER_CLASS: Piece
# MEMBER_CLASS = None # subclass must override this
# subclass must add this:
# items = models.ManyToManyField(Item, through='ListMember')
Expand Down Expand Up @@ -76,9 +78,10 @@ def append_item(self, item, **params):
ml = self.ordered_members
p = {"parent": self}
p.update(params)
lm = ml.last()
member = self.MEMBER_CLASS.objects.create(
owner=self.owner,
position=ml.last().position + 1 if ml.count() else 1,
position=lm.position + 1 if lm else 1,
item=item,
**p,
)
Expand All @@ -96,7 +99,7 @@ def remove_item(self, item):
def update_member_order(self, ordered_member_ids):
for m in self.members.all():
try:
i = ordered_member_ids.index(m.id)
i = ordered_member_ids.index(m.pk)
if m.position != i + 1:
m.position = i + 1
m.save()
Expand Down Expand Up @@ -142,10 +145,12 @@ class ListMember(Piece):
parent = models.ForeignKey('List', related_name='members', on_delete=models.CASCADE)
"""

if typing.TYPE_CHECKING:
parent: models.ForeignKey["ListMember", "List"]
owner = models.ForeignKey(APIdentity, on_delete=models.PROTECT)
visibility = models.PositiveSmallIntegerField(
default=0
) # 0: Public / 1: Follower only / 2: Self only
) # 0: Public / 1: Follower only / 2: Self only # type:ignore[reportAssignmentType]
created_time = models.DateTimeField(default=timezone.now)
edited_time = models.DateTimeField(auto_now=True)
metadata = models.JSONField(default=dict)
Expand Down
2 changes: 1 addition & 1 deletion journal/models/like.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Like(Piece): # TODO remove
owner = models.ForeignKey(APIdentity, on_delete=models.PROTECT)
visibility = models.PositiveSmallIntegerField(
default=0
) # 0: Public / 1: Follower only / 2: Self only
) # 0: Public / 1: Follower only / 2: Self only # type: ignore
created_time = models.DateTimeField(default=timezone.now)
edited_time = models.DateTimeField(auto_now=True)
target = models.ForeignKey(Piece, on_delete=models.CASCADE, related_name="likes")
Expand Down
6 changes: 3 additions & 3 deletions journal/models/mark.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __init__(self, owner: APIdentity, item: Item):
self.item = item

@cached_property
def shelfmember(self) -> ShelfMember:
def shelfmember(self) -> ShelfMember | None:
return self.owner.shelf_manager.locate_item(self.item)

@property
Expand Down Expand Up @@ -198,7 +198,7 @@ def update(
last_visibility = self.visibility if last_shelf_type else None
if shelf_type is None: # TODO change this use case to DEFERRED status
# take item off shelf
if last_shelf_type:
if self.shelfmember:
Takahe.delete_posts(self.shelfmember.all_post_ids)
self.shelfmember.log_and_delete()
if self.comment:
Expand All @@ -207,7 +207,7 @@ def update(
self.rating.delete()
return
# create/update shelf member and shelf log if necessary
if last_shelf_type == shelf_type:
if self.shelfmember and last_shelf_type == shelf_type:
shelfmember_changed = False
log_entry = self.shelfmember.ensure_log_entry()
if metadata is not None and metadata != self.shelfmember.metadata:
Expand Down
3 changes: 3 additions & 0 deletions journal/models/shelf.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ class ShelfType(models.TextChoices):


class ShelfMember(ListMember):
if TYPE_CHECKING:
parent: models.ForeignKey["ShelfMember", "Shelf"]

parent = models.ForeignKey(
"Shelf", related_name="members", on_delete=models.CASCADE
)
Expand Down
3 changes: 3 additions & 0 deletions journal/models/tag.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import typing
from functools import cached_property

from django.core.validators import MaxValueValidator, MinValueValidator, RegexValidator
Expand All @@ -14,6 +15,8 @@


class TagMember(ListMember):
if typing.TYPE_CHECKING:
parent: models.ForeignKey["TagMember", "Tag"]
parent = models.ForeignKey("Tag", related_name="members", on_delete=models.CASCADE)

class Meta:
Expand Down
2 changes: 1 addition & 1 deletion journal/views/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def collection_update_item_note(request: AuthedHttpRequest, collection_uuid, ite
)
return collection_retrieve_items(request, collection_uuid, True)
else:
member = collection.get_member_for_item(item)
member: CollectionMember = collection.get_member_for_item(item) # type:ignore
return render(
request,
"collection_update_item_note.html",
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tool.pyright]
exclude = [ "media", ".venv", ".git", "playground", "catalog/*/tests.py", "neodb", "**/migrations", "**/sites/douban_*", "neodb-takahe" ]
exclude = [ "media", ".venv", ".git", "playground", "catalog/*/tests.py", "journal/tests.py", "neodb", "**/migrations", "**/sites/douban_*", "neodb-takahe" ]
reportIncompatibleVariableOverride = false
reportUnusedImport = false
reportUnknownVariableType = false
Expand Down

0 comments on commit 7388360

Please sign in to comment.