From c301256751965bb28c00b9240a9e4b5fd4315edb Mon Sep 17 00:00:00 2001 From: Untone Date: Mon, 8 Apr 2024 10:38:58 +0300 Subject: [PATCH] precommit --- .pre-commit-config.yaml | 2 +- CHANGELOG.txt | 5 +++ orm/community.py | 2 +- orm/shout.py | 4 +-- pyproject.toml | 77 +++++++---------------------------------- resolvers/__init__.py | 68 ++++++++++-------------------------- resolvers/author.py | 14 +++----- resolvers/community.py | 2 +- resolvers/editor.py | 4 +-- resolvers/follower.py | 23 +++++------- resolvers/notifier.py | 19 ++++------ resolvers/rating.py | 4 +-- resolvers/reaction.py | 4 +-- resolvers/reader.py | 5 +-- resolvers/stat.py | 8 ++--- resolvers/topic.py | 2 +- server.py | 2 +- services/auth.py | 1 + services/cache.py | 9 +++-- services/db.py | 10 +++--- services/logger.py | 1 + services/rediscache.py | 4 +-- services/viewed.py | 8 ++--- services/webhook.py | 2 +- 24 files changed, 92 insertions(+), 188 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0093544e..3011d7a4 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -12,7 +12,7 @@ repos: - id: check-merge-conflict - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.2.2 + rev: v0.3.5 hooks: - id: ruff args: [--fix] diff --git a/CHANGELOG.txt b/CHANGELOG.txt index e77fb783..2ccea193 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,3 +1,8 @@ +[0.3.3] +- feat: sentry integration enabled +- fix: reindex on update shout +- packages upgrade, isort + [0.3.2] - redis cache for what author follows - redis cache for followers diff --git a/orm/community.py b/orm/community.py index 1d34b50c..9180e04c 100644 --- a/orm/community.py +++ b/orm/community.py @@ -3,8 +3,8 @@ import time from sqlalchemy import Column, ForeignKey, Integer, String from sqlalchemy.orm import relationship -from services.db import Base from orm.author import Author +from services.db import Base class CommunityAuthor(Base): diff --git a/orm/shout.py b/orm/shout.py index bba8acb8..4e3d3026 100644 --- a/orm/shout.py +++ b/orm/shout.py @@ -3,11 +3,11 @@ import time from sqlalchemy import JSON, Boolean, Column, ForeignKey, Integer, String from sqlalchemy.orm import relationship -from services.db import Base -from orm.community import Community from orm.author import Author +from orm.community import Community from orm.reaction import Reaction from orm.topic import Topic +from services.db import Base class ShoutTopic(Base): diff --git a/pyproject.toml b/pyproject.toml index da7d75d7..3c2fc354 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "core" -version = "0.3.2" +version = "0.3.3" description = "core module for discours.io" authors = ["discoursio devteam"] license = "MIT" @@ -8,79 +8,26 @@ readme = "README.md" [tool.poetry.dependencies] python = "^3.12" -SQLAlchemy = "^2.0.22" +SQLAlchemy = "^2.0.29" psycopg2-binary = "^2.9.9" redis = {extras = ["hiredis"], version = "^5.0.1"} -sentry-sdk = { version = "^1.4.1", extras = ["starlette", "ariadne", "sqlalchemy"] } -starlette = "^0.36.1" -gql = "^3.4.1" -ariadne = "^0.21" -pre-commit = "^3.6.0" -granian = "^1.0.1" -google-analytics-data = "^0.18.3" -opensearch-py = "^2.4.2" -httpx = "^0.26.0" +sentry-sdk = {version = "^1.44.1", extras = ["starlette", "ariadne", "sqlalchemy"]} +starlette = "^0.37.2" +gql = "^3.5.0" +ariadne = "^0.23.0" +pre-commit = "^3.7.0" +granian = "^1.2.1" +google-analytics-data = "^0.18.7" +opensearch-py = "^2.5.0" +httpx = "^0.27.0" dogpile-cache = "^1.3.1" colorlog = "^6.8.2" sqlalchemy-searchable = "^2.1.0" [tool.poetry.group.dev.dependencies] -ruff = "^0.2.1" +ruff = "^0.3.5" isort = "^5.13.2" -pyright = "^1.1.350" [build-system] requires = ["poetry-core>=1.0.0"] build-backend = "poetry.core.masonry.api" - -[tool.isort] -multi_line_output = 3 -include_trailing_comma = false -force_grid_wrap = 0 -use_parentheses = true -ensure_newline_before_comments = true -line_length = 120 - -[lint] -select = ["E4", "E7", "E9", "F"] -ignore = [] -line-length = 120 -target-version = "py312" - -[tool.ruff.format] -# Prefer single quotes over double quotes. -quote-style = "single" -skip-magic-trailing-comma = true - -[tool.pyright] -venvPath = "." -venv = ".venv" -include = ["."] -useLibraryCodeForTypes = false -disableLanguageServices = false -disableOrganizeImports = false -reportMissingImports = true -reportMissingModuleSource = "warning" -reportImportCycles = "warning" -maxMemoryForLargeFile = 4096 -pythonVersion = "3.12" -autoImportCompletions = true -useVirtualEnv = true -typeCheckingMode = "basic" -disableJediCompletion = true -disableCompletion = false -disableSnippetCompletion = false -disableGoToDefinition = false -disableRenaming = false -disableSignatureHelp = false -diagnostics = true -logLevel = "debug" -pluginSearchPaths = [] -typings = {} -mergeTypeStubPackages = false - -[tool.pytest.ini_options] -pythonpath = ["."] - -[tool.pytest] -python_files = "*_test.py" diff --git a/resolvers/__init__.py b/resolvers/__init__.py index d943ed5e..a071ea72 100644 --- a/resolvers/__init__.py +++ b/resolvers/__init__.py @@ -1,54 +1,24 @@ -from resolvers.author import ( - get_author, - get_author_follows, - get_author_follows_topics, - get_author_follows_authors, - get_author_followers, - get_author_id, - get_authors_all, - load_authors_by, - update_author, - search_authors, -) -from resolvers.rating import rate_author +from resolvers.author import (get_author, get_author_followers, + get_author_follows, get_author_follows_authors, + get_author_follows_topics, get_author_id, + get_authors_all, load_authors_by, search_authors, + update_author) from resolvers.community import get_communities_all, get_community from resolvers.editor import create_shout, delete_shout, update_shout -from resolvers.follower import ( - follow, - unfollow, - get_topic_followers, - get_shout_followers, -) -from resolvers.reaction import ( - create_reaction, - delete_reaction, - load_reactions_by, - load_shouts_followed, - update_reaction, -) -from resolvers.reader import ( - get_shout, - load_shouts_by, - load_shouts_feed, - load_shouts_random_top, - load_shouts_random_topic, - load_shouts_search, - load_shouts_unrated, -) -from resolvers.topic import ( - get_topic, - get_topics_all, - get_topics_by_author, - get_topics_by_community, -) - -from resolvers.notifier import ( - load_notifications, - notification_mark_seen, - notifications_seen_after, - notifications_seen_thread, -) - +from resolvers.follower import (follow, get_shout_followers, + get_topic_followers, unfollow) +from resolvers.notifier import (load_notifications, notification_mark_seen, + notifications_seen_after, + notifications_seen_thread) +from resolvers.rating import rate_author +from resolvers.reaction import (create_reaction, delete_reaction, + load_reactions_by, load_shouts_followed, + update_reaction) +from resolvers.reader import (get_shout, load_shouts_by, load_shouts_feed, + load_shouts_random_top, load_shouts_random_topic, + load_shouts_search, load_shouts_unrated) +from resolvers.topic import (get_topic, get_topics_all, get_topics_by_author, + get_topics_by_community) from services.cache import events_register events_register() diff --git a/resolvers/author.py b/resolvers/author.py index e1b8f525..6dc08383 100644 --- a/resolvers/author.py +++ b/resolvers/author.py @@ -2,27 +2,23 @@ import asyncio import json import time -from sqlalchemy import select, or_, and_, text, desc +from sqlalchemy import and_, desc, or_, select, text from sqlalchemy.orm import aliased from sqlalchemy_searchable import search from orm.author import Author, AuthorFollower from orm.shout import ShoutAuthor, ShoutTopic from orm.topic import Topic -from resolvers.stat import ( - get_authors_with_stat_cached, - author_follows_authors, - author_follows_topics, - get_with_stat, -) -from services.cache import set_author_cache, update_author_followers_cache +from resolvers.stat import (author_follows_authors, author_follows_topics, + get_authors_with_stat_cached, get_with_stat) from services.auth import login_required +from services.cache import set_author_cache, update_author_followers_cache from services.db import local_session from services.encoders import CustomJSONEncoder +from services.logger import root_logger as logger from services.memorycache import cache_region from services.rediscache import redis from services.schema import mutation, query -from services.logger import root_logger as logger @mutation.field('update_author') diff --git a/resolvers/community.py b/resolvers/community.py index e7f747ab..baa9dc7a 100644 --- a/resolvers/community.py +++ b/resolvers/community.py @@ -5,8 +5,8 @@ from orm.author import Author from orm.community import Community, CommunityAuthor from orm.shout import ShoutCommunity from services.db import local_session -from services.schema import query from services.logger import root_logger as logger +from services.schema import query def add_community_stat_columns(q): diff --git a/resolvers/editor.py b/resolvers/editor.py index ce338384..9c842c80 100644 --- a/resolvers/editor.py +++ b/resolvers/editor.py @@ -1,6 +1,6 @@ import time -from sqlalchemy import and_, select, desc +from sqlalchemy import and_, desc, select from sqlalchemy.orm import joinedload from sqlalchemy.sql.functions import coalesce @@ -13,10 +13,10 @@ from resolvers.follower import reactions_follow, reactions_unfollow from services.auth import login_required from services.db import local_session from services.diff import apply_diff, get_diff +from services.logger import root_logger as logger from services.notify import notify_shout from services.schema import mutation, query from services.search import search_service -from services.logger import root_logger as logger @query.field('get_my_shout') diff --git a/resolvers/follower.py b/resolvers/follower.py index 714ee675..6f9fcc76 100644 --- a/resolvers/follower.py +++ b/resolvers/follower.py @@ -3,33 +3,26 @@ import time from typing import List from psycopg2.errors import UniqueViolation -from sqlalchemy import select, or_ +from sqlalchemy import or_, select from sqlalchemy.sql import and_ from orm.author import Author, AuthorFollower from orm.community import Community - # from orm.community import Community from orm.reaction import Reaction from orm.shout import Shout, ShoutReactionsFollower from orm.topic import Topic, TopicFollower -from resolvers.stat import ( - get_authors_with_stat_cached, - author_follows_topics, - author_follows_authors, - get_topics_with_stat_cached, -) +from resolvers.stat import (author_follows_authors, author_follows_topics, + get_authors_with_stat_cached, + get_topics_with_stat_cached) from services.auth import login_required +from services.cache import (DEFAULT_FOLLOWS, update_followers_for_author, + update_follows_for_author) from services.db import local_session -from services.cache import ( - DEFAULT_FOLLOWS, - update_follows_for_author, - update_followers_for_author, -) -from services.notify import notify_follower -from services.schema import mutation, query from services.logger import root_logger as logger +from services.notify import notify_follower from services.rediscache import redis +from services.schema import mutation, query @mutation.field('follow') diff --git a/resolvers/notifier.py b/resolvers/notifier.py index 9f22ba63..3197daa1 100644 --- a/resolvers/notifier.py +++ b/resolvers/notifier.py @@ -2,24 +2,19 @@ import json import time from typing import List, Tuple -from sqlalchemy.exc import SQLAlchemyError - -from orm.author import Author -from orm.shout import Shout -from services.auth import login_required -from services.schema import mutation, query from sqlalchemy import and_, select +from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.orm import aliased from sqlalchemy.sql import not_ -from orm.notification import ( - Notification, - NotificationAction, - NotificationEntity, - NotificationSeen, -) +from orm.author import Author +from orm.notification import (Notification, NotificationAction, + NotificationEntity, NotificationSeen) +from orm.shout import Shout +from services.auth import login_required from services.db import local_session from services.logger import root_logger as logger +from services.schema import mutation, query def query_notifications( diff --git a/resolvers/rating.py b/resolvers/rating.py index 2ec7956e..84d513d3 100644 --- a/resolvers/rating.py +++ b/resolvers/rating.py @@ -1,7 +1,7 @@ -from sqlalchemy import and_, func, case, true, select +from sqlalchemy import and_, case, func, select, true from sqlalchemy.orm import aliased -from orm.author import AuthorRating, Author +from orm.author import Author, AuthorRating from orm.reaction import Reaction, ReactionKind from orm.shout import Shout from services.auth import login_required diff --git a/resolvers/reaction.py b/resolvers/reaction.py index ae53b7ae..958d8b6c 100644 --- a/resolvers/reaction.py +++ b/resolvers/reaction.py @@ -1,7 +1,7 @@ import time from typing import List -from sqlalchemy import and_, case, desc, func, select, text, asc +from sqlalchemy import and_, asc, case, desc, func, select, text from sqlalchemy.orm import aliased, joinedload from sqlalchemy.sql import union @@ -13,10 +13,10 @@ from resolvers.editor import handle_proposing from resolvers.follower import reactions_follow from services.auth import add_user_role, login_required from services.db import local_session +from services.logger import root_logger as logger from services.notify import notify_reaction from services.schema import mutation, query from services.viewed import ViewedStorage -from services.logger import root_logger as logger def add_reaction_stat_columns(q, aliased_reaction): diff --git a/resolvers/reader.py b/resolvers/reader.py index 55efd2cd..3b8d60b5 100644 --- a/resolvers/reader.py +++ b/resolvers/reader.py @@ -1,6 +1,7 @@ from sqlalchemy import bindparam, distinct, or_, text from sqlalchemy.orm import aliased, joinedload -from sqlalchemy.sql.expression import and_, asc, case, desc, func, nulls_last, select +from sqlalchemy.sql.expression import (and_, asc, case, desc, func, nulls_last, + select) from orm.author import Author, AuthorFollower from orm.reaction import Reaction, ReactionKind @@ -10,10 +11,10 @@ from resolvers.reaction import add_reaction_stat_columns from resolvers.topic import get_topics_random from services.auth import login_required from services.db import local_session +from services.logger import root_logger as logger from services.schema import query from services.search import search_text from services.viewed import ViewedStorage -from services.logger import root_logger as logger def query_shouts(): diff --git a/resolvers/stat.py b/resolvers/stat.py index f3dd53aa..95e73349 100644 --- a/resolvers/stat.py +++ b/resolvers/stat.py @@ -1,14 +1,14 @@ import json -from sqlalchemy import func, distinct, select, join, and_ +from sqlalchemy import and_, distinct, func, join, select from sqlalchemy.orm import aliased +from orm.author import Author, AuthorFollower from orm.reaction import Reaction, ReactionKind +from orm.shout import Shout, ShoutAuthor, ShoutTopic +from orm.topic import Topic, TopicFollower from resolvers.rating import add_author_rating_columns -from orm.topic import TopicFollower, Topic from services.db import local_session -from orm.author import AuthorFollower, Author -from orm.shout import ShoutTopic, ShoutAuthor, Shout from services.logger import root_logger as logger from services.rediscache import redis diff --git a/resolvers/topic.py b/resolvers/topic.py index d6a169a8..0174459f 100644 --- a/resolvers/topic.py +++ b/resolvers/topic.py @@ -6,8 +6,8 @@ from orm.topic import Topic from resolvers.stat import get_topics_with_stat_cached, get_with_stat from services.auth import login_required from services.db import local_session -from services.schema import mutation, query from services.memorycache import cache_region +from services.schema import mutation, query @query.field('get_topics_all') diff --git a/server.py b/server.py index 09df9401..52104e42 100644 --- a/server.py +++ b/server.py @@ -1,9 +1,9 @@ from granian.constants import Interfaces from granian.server import Granian + from services.logger import root_logger as logger from settings import PORT - if __name__ == '__main__': logger.info('started') diff --git a/services/auth.py b/services/auth.py index d0922232..ec22b851 100644 --- a/services/auth.py +++ b/services/auth.py @@ -1,4 +1,5 @@ from functools import wraps + import httpx from starlette.exceptions import HTTPException diff --git a/services/cache.py b/services/cache.py index 51b5e177..d91f787c 100644 --- a/services/cache.py +++ b/services/cache.py @@ -1,17 +1,16 @@ import asyncio - -from sqlalchemy import select, event import json +from sqlalchemy import event, select + from orm.author import Author, AuthorFollower from orm.reaction import Reaction -from orm.shout import ShoutAuthor, Shout +from orm.shout import Shout, ShoutAuthor from orm.topic import Topic, TopicFollower from resolvers.stat import get_with_stat from services.encoders import CustomJSONEncoder -from services.rediscache import redis from services.logger import root_logger as logger - +from services.rediscache import redis DEFAULT_FOLLOWS = { 'topics': [], diff --git a/services/db.py b/services/db.py index 0ee579d4..08b037df 100644 --- a/services/db.py +++ b/services/db.py @@ -1,10 +1,12 @@ import json import math import time - +import traceback +import warnings from typing import Any, Callable, Dict, TypeVar -from sqlalchemy import exc, event, Engine -from sqlalchemy import inspect, Column, Integer, create_engine, JSON + +from sqlalchemy import (JSON, Column, Engine, Integer, create_engine, event, + exc, inspect) from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import Session, configure_mappers from sqlalchemy.sql.schema import Table @@ -12,8 +14,6 @@ from sqlalchemy_searchable import make_searchable from services.logger import root_logger as logger from settings import DB_URL -import warnings -import traceback # Подключение к базе данных SQLAlchemy engine = create_engine(DB_URL, echo=False, pool_size=10, max_overflow=20) diff --git a/services/logger.py b/services/logger.py index d1b89b75..63918372 100644 --- a/services/logger.py +++ b/services/logger.py @@ -1,4 +1,5 @@ import logging + import colorlog # Define the color scheme diff --git a/services/rediscache.py b/services/rediscache.py index bcc14b80..5affa776 100644 --- a/services/rediscache.py +++ b/services/rediscache.py @@ -1,9 +1,9 @@ +import logging + import redis.asyncio as aredis -import logging from settings import REDIS_URL - # Set redis logging level to suppress DEBUG messages logger = logging.getLogger('redis') logger.setLevel(logging.WARNING) diff --git a/services/viewed.py b/services/viewed.py index 6542e2e0..7f698567 100644 --- a/services/viewed.py +++ b/services/viewed.py @@ -7,12 +7,8 @@ from typing import Dict # ga from google.analytics.data_v1beta import BetaAnalyticsDataClient -from google.analytics.data_v1beta.types import ( - DateRange, - Dimension, - Metric, - RunReportRequest, -) +from google.analytics.data_v1beta.types import (DateRange, Dimension, Metric, + RunReportRequest) from orm.author import Author from orm.shout import Shout, ShoutAuthor, ShoutTopic diff --git a/services/webhook.py b/services/webhook.py index 449ef27a..5b3da279 100644 --- a/services/webhook.py +++ b/services/webhook.py @@ -2,9 +2,9 @@ import os import re from starlette.endpoints import HTTPEndpoint +from starlette.exceptions import HTTPException from starlette.requests import Request from starlette.responses import JSONResponse -from starlette.exceptions import HTTPException from orm.author import Author from services.db import local_session