From 7d1388df30b2dd45a16165e516d437ab06a0b327 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 10 Aug 2023 17:15:00 -0400 Subject: [PATCH] fix some type hints --- boofilsic/settings.py | 15 +++++++------ boofilsic/urls.py | 2 +- catalog/api.py | 2 +- catalog/common/models.py | 4 ++-- catalog/common/sites.py | 14 ++++++------ catalog/management/commands/discover.py | 2 +- common/api.py | 2 +- journal/templatetags/user_actions.py | 2 +- management/models.py | 2 +- mastodon/management/commands/wrong_sites.py | 24 --------------------- pyproject.toml | 10 +++++++++ social/models.py | 5 +++-- 12 files changed, 36 insertions(+), 48 deletions(-) delete mode 100644 mastodon/management/commands/wrong_sites.py diff --git a/boofilsic/settings.py b/boofilsic/settings.py index 4b1cd920..08ddd03a 100644 --- a/boofilsic/settings.py +++ b/boofilsic/settings.py @@ -237,7 +237,7 @@ SITE_INFO = { "sentry_dsn": None, } -REDIRECT_URIS = SITE_INFO["site_url"] + "/users/OAuth2_login/" +REDIRECT_URIS = f'{SITE_INFO["site_url"]}/users/OAuth2_login/' # if you are creating new site, use # REDIRECT_URIS = SITE_INFO["site_url"] + "/account/login/oauth" @@ -386,11 +386,11 @@ SEARCH_INDEX_NEW_ONLY = False # MEILISEARCH_KEY = 'deadbeef' # SEARCH_BACKEND = "TYPESENSE" -# TYPESENSE_CONNECTION = { -# "api_key": "xyz", -# "nodes": [{"host": "localhost", "port": "8108", "protocol": "http"}], -# "connection_timeout_seconds": 2, -# } +TYPESENSE_CONNECTION = { + "api_key": "xyz", + "nodes": [{"host": "localhost", "port": "8108", "protocol": "http"}], + "connection_timeout_seconds": 2, +} SEARCH_BACKEND = None @@ -410,7 +410,8 @@ DISABLE_MODEL_SIGNAL = False # disable index and social feeds during importing/ # SILKY_MAX_RESPONSE_BODY_SIZE = 1024 # If response body>1024 bytes, ignore # SILKY_INTERCEPT_PERCENT = 10 -DISCORD_WEBHOOKS = {} +DISCORD_WEBHOOKS = {"user-report": None} + NINJA_PAGINATION_PER_PAGE = 20 OAUTH2_PROVIDER = { diff --git a/boofilsic/urls.py b/boofilsic/urls.py index ebecef4b..ad023bf1 100644 --- a/boofilsic/urls.py +++ b/boofilsic/urls.py @@ -22,7 +22,7 @@ from common.api import api from users.views import login urlpatterns = [ - # path("api/", api.urls), # type: ignore + path("api/", api.urls), # type: ignore path("login/", login), path("markdownx/", include("markdownx.urls")), path("account/", include("users.urls")), diff --git a/catalog/api.py b/catalog/api.py index 0cf231f2..eb307efb 100644 --- a/catalog/api.py +++ b/catalog/api.py @@ -205,7 +205,7 @@ def search_item_legacy( query = query.strip() if not query: return 400, {"message": "Invalid query"} - result = Indexer.search(query, page=1, category=category) + result = Indexer.search(query, page=1, categories=[category]) return 200, {"items": result.items} diff --git a/catalog/common/models.py b/catalog/common/models.py index 001fd52d..7186ec76 100644 --- a/catalog/common/models.py +++ b/catalog/common/models.py @@ -246,8 +246,8 @@ class Item(SoftDeleteMixin, PolymorphicModel): url_path = "item" # subclass must specify this type = None # subclass must specify this parent_class = None # subclass may specify this to allow create child item - category = None # subclass must specify this - demonstrative = None # subclass must specify this + category: ItemCategory | None = None # subclass must specify this + demonstrative: str | None = None # subclass must specify this uid = models.UUIDField(default=uuid.uuid4, editable=False, db_index=True) title = models.CharField(_("标题"), max_length=1000, default="") brief = models.TextField(_("简介"), blank=True, default="") diff --git a/catalog/common/sites.py b/catalog/common/sites.py index c4f696b3..ee4be7b1 100644 --- a/catalog/common/sites.py +++ b/catalog/common/sites.py @@ -10,11 +10,11 @@ import json import logging import re from dataclasses import dataclass, field -from typing import Callable +from typing import Callable, Type import django_rq -from .models import ExternalResource, IdealIdTypes, IdType, Item +from .models import ExternalResource, IdealIdTypes, IdType, Item, SiteName _logger = logging.getLogger(__name__) @@ -38,10 +38,10 @@ class AbstractSite: Abstract class to represent a site """ - SITE_NAME = None - ID_TYPE = None - WIKI_PROPERTY_ID = "P0undefined0" - DEFAULT_MODEL = None + SITE_NAME: SiteName | None = None + ID_TYPE: IdType | None = None + WIKI_PROPERTY_ID: str | None = "P0undefined0" + DEFAULT_MODEL: Type[Item] | None = None URL_PATTERNS = [r"\w+://undefined/(\d+)"] @classmethod @@ -57,7 +57,7 @@ class AbstractSite: return False @classmethod - def id_to_url(cls, id_value): + def id_to_url(cls, id_value: str): return "https://undefined/" + id_value @classmethod diff --git a/catalog/management/commands/discover.py b/catalog/management/commands/discover.py index 7332d737..32e316ba 100644 --- a/catalog/management/commands/discover.py +++ b/catalog/management/commands/discover.py @@ -7,7 +7,7 @@ from django.utils import timezone from loguru import logger from catalog.models import * -from journal.models import Comment, ItemCategory, ShelfMember, query_item_category +from journal.models import Comment, ShelfMember, query_item_category MAX_ITEMS_PER_PERIOD = 12 MIN_MARKS = 2 diff --git a/common/api.py b/common/api.py index 41db7e76..901e7912 100644 --- a/common/api.py +++ b/common/api.py @@ -69,7 +69,7 @@ class PageNumberPagination(NinjaPageNumberPagination): api = NinjaAPI( auth=OAuthAccessTokenAuth(), - title=settings.SITE_INFO["site_name"] + " API", + title=f'{settings.SITE_INFO["site_name"]} API', version="1.0.0", description=f"{settings.SITE_INFO['site_name']} API
Learn more", ) diff --git a/journal/templatetags/user_actions.py b/journal/templatetags/user_actions.py index 57e2af54..6f9eecb7 100644 --- a/journal/templatetags/user_actions.py +++ b/journal/templatetags/user_actions.py @@ -1,5 +1,5 @@ from django import template -from django.shortcuts import reverse +from django.urls import reverse from journal.models import Collection, Like diff --git a/management/models.py b/management/models.py index ead05886..52cb6e0c 100644 --- a/management/models.py +++ b/management/models.py @@ -1,7 +1,7 @@ import re from django.db import models -from django.shortcuts import reverse +from django.urls import reverse from django.utils.translation import gettext_lazy as _ from markdown import markdown from markdownx.models import MarkdownxField diff --git a/mastodon/management/commands/wrong_sites.py b/mastodon/management/commands/wrong_sites.py deleted file mode 100644 index 38c3b6a6..00000000 --- a/mastodon/management/commands/wrong_sites.py +++ /dev/null @@ -1,24 +0,0 @@ -from django.conf import settings -from django.core.management.base import BaseCommand - -from mastodon.api import get_instance_info -from mastodon.models import MastodonApplication -from users.models import User - - -class Command(BaseCommand): - help = "Find wrong sites" - - def handle(self, *args, **options): - for site in MastodonApplication.objects.all(): - d = site.domain_name - login_domain = ( - d.strip().lower().split("//")[-1].split("/")[0].split("@")[-1] - ) - domain, version = get_instance_info(login_domain) - if d != domain: - print(f"{d} should be {domain}") - for u in User.objects.filter(mastodon_site=d, is_active=True): - u.mastodon_site = domain - print(f"fixing {u}") - u.save() diff --git a/pyproject.toml b/pyproject.toml index e5a525a8..e440c80c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,3 +7,13 @@ indent=2 [tool.isort] profile = "black" + +[tool.mypy] +exclude = [ + '^neodb/', + '^legacy/', +] +plugins = ["mypy_django_plugin.main"] + +[tool.django-stubs] +django_settings_module = "boofilsic.settings" diff --git a/social/models.py b/social/models.py index fe2fffa2..04acb68d 100644 --- a/social/models.py +++ b/social/models.py @@ -8,6 +8,7 @@ ActivityManager generates chronological view for user and, in future, ActivitySt import logging from functools import cached_property +from typing import Type from django.conf import settings from django.db import models @@ -125,8 +126,8 @@ class DataSignalManager: class DefaultActivityProcessor: - model = None - template = None + model: Type[Piece] + template: ActivityTemplate def __init__(self, action_object): self.action_object = action_object