Skip to content

Commit

Permalink
fix type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
Her Email authored and alphatownsman committed Dec 6, 2023
1 parent 7fba0a7 commit 682b46e
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 31 deletions.
2 changes: 1 addition & 1 deletion boofilsic/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from users.views import login

urlpatterns = [
path("api/", api.urls), # type: ignore
path("api/", api.urls),
path("login/", login),
path("markdownx/", include("markdownx.urls")),
path("account/", include("users.urls")),
Expand Down
6 changes: 3 additions & 3 deletions catalog/common/downloaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def validate_response(self, response):
else:
return RESPONSE_INVALID_CONTENT

def _download(self, url) -> Tuple[DownloaderResponse | MockResponse, int]:
def _download(self, url) -> Tuple[DownloaderResponse | MockResponse | None, int]:
try:
if not _mock_mode:
resp = requests.get(
Expand Down Expand Up @@ -179,7 +179,7 @@ def _download(self, url) -> Tuple[DownloaderResponse | MockResponse, int]:
self.logs.append(
{"response_type": RESPONSE_NETWORK_ERROR, "url": url, "exception": e}
)
return None, RESPONSE_NETWORK_ERROR # type: ignore
return None, RESPONSE_NETWORK_ERROR

def download(self):
resp, self.response_type = self._download(self.url)
Expand Down Expand Up @@ -237,7 +237,7 @@ def download(self):
while retries:
retries -= 1
resp, self.response_type = self._download(self.url)
if self.response_type == RESPONSE_OK:
if self.response_type == RESPONSE_OK and resp:
return resp
elif self.response_type != RESPONSE_NETWORK_ERROR and retries == 0:
raise DownloadError(self)
Expand Down
2 changes: 1 addition & 1 deletion journal/models/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def save(self, *args, **kwargs):
):
self.catalog_item.title = self.title
self.catalog_item.brief = self.brief
self.catalog_item.cover = self.cover # type: ignore
self.catalog_item.cover = self.cover
self.catalog_item.save()
super().save(*args, **kwargs)
Takahe.post_collection(self)
Expand Down
9 changes: 6 additions & 3 deletions journal/models/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from users.models import APIdentity, User

if TYPE_CHECKING:
from django.db.models import ForeignKey

from .common import Piece


Expand All @@ -15,8 +17,9 @@ class UserOwnedObjectMixin:
visibility = models.PositiveSmallIntegerField(default=0)
"""

owner: APIdentity
visibility: int
if TYPE_CHECKING:
owner: ForeignKey[APIdentity, Piece]
visibility: int

def is_visible_to(self: "Piece | Self", viewing_user: User) -> bool: # type: ignore
owner = self.owner
Expand All @@ -26,7 +29,7 @@ def is_visible_to(self: "Piece | Self", viewing_user: User) -> bool: # type: ig
return True
if not viewing_user.is_authenticated:
return self.visibility == 0
viewer = viewing_user.identity # type: ignore[assignment]
viewer = viewing_user.identity
if not viewer:
return False
if self.visibility == 2:
Expand Down
2 changes: 1 addition & 1 deletion journal/views/mark.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def mark(request: AuthedHttpRequest, item_uuid):
visibility = int(request.POST.get("visibility", default=0))
rating_grade = request.POST.get("rating_grade", default=0)
rating_grade = int(rating_grade) if rating_grade else None
status = ShelfType(request.POST.get("status"))
status = ShelfType(request.POST.get("status", "wishlist"))
text = request.POST.get("text")
tags = request.POST.get("tags")
tags = tags.split(",") if tags else []
Expand Down
2 changes: 1 addition & 1 deletion social/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ActivityTemplate(models.TextChoices):


class LocalActivity(models.Model, UserOwnedObjectMixin):
owner = models.ForeignKey(APIdentity, on_delete=models.CASCADE) # type: ignore
owner = models.ForeignKey(APIdentity, on_delete=models.CASCADE)
visibility = models.PositiveSmallIntegerField(default=0) # type: ignore
template = models.CharField(
blank=False, choices=ActivityTemplate.choices, max_length=50
Expand Down
18 changes: 3 additions & 15 deletions takahe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
from .uris import *

if TYPE_CHECKING:
from django.db.models.manager import RelatedManager

from django_stubs_ext.db.models.manager import RelatedManager

_migration_mode = False

Expand Down Expand Up @@ -201,7 +200,8 @@ def valid(self):


class User(AbstractBaseUser):
identities: "RelatedManager[Identity]"
if TYPE_CHECKING:
identities: RelatedManager["Identity"]

class Meta:
# managed = False
Expand Down Expand Up @@ -1493,18 +1493,6 @@ def usage_days(self, num: int = 7) -> dict[date, int]:
results[date(year, month, day)] = val
return dict(sorted(results.items(), reverse=True)[:num])

def to_mastodon_json(self, following: bool | None = None):
value = {
"name": self.hashtag,
"url": self.urls.view.full(), # type: ignore
"history": [],
}

if following is not None:
value["following"] = following

return value


class PostInteraction(models.Model):
"""
Expand Down
8 changes: 4 additions & 4 deletions users/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ def OAuth2_login(request):

user: User = authenticate(request, token=token, site=site) # type: ignore
if user: # existing user
user.mastodon_token = token # type: ignore
user.mastodon_refresh_token = refresh_token # type: ignore
user.mastodon_token = token
user.mastodon_refresh_token = refresh_token
user.save(update_fields=["mastodon_token", "mastodon_refresh_token"])
return login_existing_user(request, user)
else: # newly registered user
Expand Down Expand Up @@ -284,7 +284,7 @@ def clean_email(self):

def send_verification_link(user_id, action, email):
s = {"i": user_id, "e": email, "a": action}
v = TimestampSigner().sign_object(s) # type: ignore
v = TimestampSigner().sign_object(s)
if action == "verify":
subject = f'{settings.SITE_INFO["site_name"]} - {_("验证电子邮件地址")}'
url = settings.SITE_INFO["site_url"] + "/account/verify_email?c=" + v
Expand Down Expand Up @@ -321,7 +321,7 @@ def send_verification_link(user_id, action, email):
def verify_email(request):
error = ""
try:
s = TimestampSigner().unsign_object(request.GET.get("c"), max_age=60 * 15) # type: ignore
s = TimestampSigner().unsign_object(request.GET.get("c"), max_age=60 * 15)
except Exception as e:
logger.error(e)
error = _("链接无效或已过期")
Expand Down
4 changes: 2 additions & 2 deletions users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def reject_follow_request(request: AuthedHttpRequest, user_name):
@login_required
def set_layout(request: AuthedHttpRequest):
if request.method == "POST":
layout = json.loads(request.POST.get("layout", {})) # type: ignore
layout = json.loads(request.POST.get("layout", "{}"))
if request.POST.get("name") == "profile":
request.user.preference.profile_layout = layout
request.user.preference.save(update_fields=["profile_layout"])
Expand Down Expand Up @@ -229,7 +229,7 @@ def report(request: AuthedHttpRequest):
form.save()
dw = settings.DISCORD_WEBHOOKS.get("user-report")
if dw:
webhook = SyncWebhook.from_url(dw) # type: ignore
webhook = SyncWebhook.from_url(dw)
webhook.send(
f"New report from {request.user} about {form.instance.reported_user} : {form.instance.message}"
)
Expand Down

0 comments on commit 682b46e

Please sign in to comment.