mirror of
https://github.com/flibusta-apps/book_library_server.git
synced 2025-12-06 15:15:36 +01:00
Add linters configs
This commit is contained in:
@@ -1,15 +1,17 @@
|
||||
from app.models import Author
|
||||
|
||||
from app.services.common import TRGMSearchService, GetRandomService
|
||||
|
||||
|
||||
GET_OBJECT_IDS_QUERY = """
|
||||
SELECT ARRAY(
|
||||
WITH filtered_authors AS (
|
||||
SELECT
|
||||
WITH filtered_authors AS (
|
||||
SELECT
|
||||
id,
|
||||
GREATEST(
|
||||
similarity((last_name || ' ' || first_name || ' ' || middle_name), :query),
|
||||
similarity(
|
||||
(last_name || ' ' || first_name || ' ' || middle_name),
|
||||
:query
|
||||
),
|
||||
similarity((last_name || ' ' || first_name), :query),
|
||||
similarity((last_name), :query)
|
||||
) as sml,
|
||||
@@ -27,7 +29,7 @@ SELECT ARRAY(
|
||||
EXISTS (
|
||||
SELECT * FROM book_authors
|
||||
LEFT JOIN books ON (books.id = book AND books.is_deleted = 'f')
|
||||
WHERE author = authors.id
|
||||
WHERE author = authors.id
|
||||
)
|
||||
)
|
||||
SELECT fauthors.id FROM filtered_authors as fauthors
|
||||
|
||||
@@ -2,15 +2,15 @@ from typing import Union
|
||||
|
||||
from fastapi import HTTPException, status
|
||||
|
||||
from app.models import Book as BookDB, Author as AuthorDB
|
||||
|
||||
from app.services.common import TRGMSearchService, GetRandomService
|
||||
from app.models import Author as AuthorDB
|
||||
from app.models import Book as BookDB
|
||||
from app.serializers.book import CreateBook, CreateRemoteBook
|
||||
from app.services.common import TRGMSearchService, GetRandomService
|
||||
|
||||
|
||||
GET_OBJECT_IDS_QUERY = """
|
||||
SELECT ARRAY(
|
||||
WITH filtered_books AS (
|
||||
WITH filtered_books AS (
|
||||
SELECT id, similarity(title, :query) as sml FROM books
|
||||
WHERE books.title % :query AND books.is_deleted = 'f'
|
||||
)
|
||||
@@ -42,9 +42,7 @@ class BookCreator:
|
||||
if len(author_ids) != len(authors):
|
||||
cls._raise_bad_request()
|
||||
|
||||
book = await BookDB.objects.create(
|
||||
**data_dict
|
||||
)
|
||||
book = await BookDB.objects.create(**data_dict)
|
||||
|
||||
for author in authors:
|
||||
await book.authors.add(author)
|
||||
@@ -56,14 +54,14 @@ class BookCreator:
|
||||
data_dict = data.dict()
|
||||
|
||||
author_ids = data_dict.pop("remote_authors", [])
|
||||
authors = await AuthorDB.objects.filter(source__id=data.source, remote_id__in=author_ids).all()
|
||||
authors = await AuthorDB.objects.filter(
|
||||
source__id=data.source, remote_id__in=author_ids
|
||||
).all()
|
||||
|
||||
if len(author_ids) != len(authors):
|
||||
cls._raise_bad_request()
|
||||
|
||||
book = await BookDB.objects.create(
|
||||
**data_dict
|
||||
)
|
||||
book = await BookDB.objects.create(**data_dict)
|
||||
|
||||
for author in authors:
|
||||
await book.authors.add(author)
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
from typing import Optional, Generic, TypeVar, Union
|
||||
from databases import Database
|
||||
|
||||
import aioredis
|
||||
from databases import Database
|
||||
from fastapi_pagination.api import resolve_params
|
||||
from fastapi_pagination.bases import AbstractParams, RawParams
|
||||
from app.utils.pagination import Page, CustomPage
|
||||
import aioredis
|
||||
import orjson
|
||||
|
||||
from ormar import Model, QuerySet
|
||||
from sqlalchemy import Table
|
||||
|
||||
from app.utils.pagination import Page, CustomPage
|
||||
|
||||
T = TypeVar('T', bound=Model)
|
||||
|
||||
T = TypeVar("T", bound=Model)
|
||||
|
||||
|
||||
class TRGMSearchService(Generic[T]):
|
||||
@@ -48,7 +48,9 @@ class TRGMSearchService(Generic[T]):
|
||||
@classmethod
|
||||
@property
|
||||
def object_ids_query(cls) -> str:
|
||||
assert cls.GET_OBJECT_IDS_QUERY is not None, f"GET_OBJECT_IDS_QUERY in {cls.__name__} don't set!"
|
||||
assert (
|
||||
cls.GET_OBJECT_IDS_QUERY is not None
|
||||
), f"GET_OBJECT_IDS_QUERY in {cls.__name__} don't set!"
|
||||
return cls.GET_OBJECT_IDS_QUERY
|
||||
|
||||
@classmethod
|
||||
@@ -56,9 +58,9 @@ class TRGMSearchService(Generic[T]):
|
||||
row = await cls.database.fetch_one(cls.object_ids_query, {"query": query_data})
|
||||
|
||||
if row is None:
|
||||
raise ValueError('Something is wrong!')
|
||||
raise ValueError("Something is wrong!")
|
||||
|
||||
return row['array']
|
||||
return row["array"]
|
||||
|
||||
@classmethod
|
||||
def get_cache_key(cls, query_data: str) -> str:
|
||||
@@ -66,7 +68,9 @@ class TRGMSearchService(Generic[T]):
|
||||
return f"{model_class_name}_{query_data}"
|
||||
|
||||
@classmethod
|
||||
async def get_cached_ids(cls, query_data: str, redis: aioredis.Redis) -> Optional[list[int]]:
|
||||
async def get_cached_ids(
|
||||
cls, query_data: str, redis: aioredis.Redis
|
||||
) -> Optional[list[int]]:
|
||||
try:
|
||||
key = cls.get_cache_key(query_data)
|
||||
data = await redis.get(key)
|
||||
@@ -80,7 +84,9 @@ class TRGMSearchService(Generic[T]):
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
async def cache_object_ids(cls, query_data: str, object_ids: list[int], redis: aioredis.Redis):
|
||||
async def cache_object_ids(
|
||||
cls, query_data: str, object_ids: list[int], redis: aioredis.Redis
|
||||
):
|
||||
try:
|
||||
key = cls.get_cache_key(query_data)
|
||||
await redis.set(key, orjson.dumps(object_ids), ex=cls.CACHE_TTL)
|
||||
@@ -88,7 +94,9 @@ class TRGMSearchService(Generic[T]):
|
||||
print(e)
|
||||
|
||||
@classmethod
|
||||
async def get_objects(cls, query_data: str, redis: aioredis.Redis) -> tuple[int, list[T]]:
|
||||
async def get_objects(
|
||||
cls, query_data: str, redis: aioredis.Redis
|
||||
) -> tuple[int, list[T]]:
|
||||
params = cls.get_raw_params()
|
||||
|
||||
cached_object_ids = await cls.get_cached_ids(query_data, redis)
|
||||
@@ -99,7 +107,7 @@ class TRGMSearchService(Generic[T]):
|
||||
else:
|
||||
object_ids = cached_object_ids
|
||||
|
||||
limited_object_ids = object_ids[params.offset:params.offset + params.limit]
|
||||
limited_object_ids = object_ids[params.offset : params.offset + params.limit]
|
||||
|
||||
queryset: QuerySet[T] = cls.model.objects
|
||||
|
||||
@@ -117,11 +125,7 @@ class TRGMSearchService(Generic[T]):
|
||||
|
||||
total, objects = await cls.get_objects(query, redis)
|
||||
|
||||
return CustomPage.create(
|
||||
items=objects,
|
||||
total=total,
|
||||
params=params
|
||||
)
|
||||
return CustomPage.create(items=objects, total=total, params=params)
|
||||
|
||||
|
||||
GET_RANDOM_OBJECT_ID_QUERY = """
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
from app.models import Sequence
|
||||
|
||||
from app.services.common import TRGMSearchService, GetRandomService
|
||||
|
||||
|
||||
GET_OBJECT_IDS_QUERY = """
|
||||
SELECT ARRAY (
|
||||
WITH filtered_sequences AS (
|
||||
SELECT
|
||||
SELECT
|
||||
id,
|
||||
similarity(name, :query) as sml,
|
||||
(
|
||||
SELECT count(*) FROM book_sequences
|
||||
LEFT JOIN books ON (books.id = book AND books.is_deleted = 'f')
|
||||
WHERE sequence = sequences.id
|
||||
WHERE sequence = sequences.id
|
||||
) as books_count
|
||||
FROM sequences
|
||||
WHERE name % :query AND
|
||||
|
||||
@@ -2,10 +2,12 @@ from typing import Union
|
||||
|
||||
from fastapi import HTTPException, status
|
||||
|
||||
from app.models import Author as AuthorDB
|
||||
from app.models import Book as BookDB
|
||||
from app.models import Source as SourceDB
|
||||
from app.models import Translation as TranslationDB
|
||||
from app.serializers.translation import CreateTranslation, CreateRemoteTranslation
|
||||
|
||||
from app.models import Translation as TranslationDB, Source as SourceDB, Book as BookDB, Author as AuthorDB
|
||||
|
||||
|
||||
class TranslationCreator:
|
||||
@classmethod
|
||||
@@ -14,23 +16,27 @@ class TranslationCreator:
|
||||
|
||||
@classmethod
|
||||
async def _create_translation(cls, data: CreateTranslation) -> TranslationDB:
|
||||
return await TranslationDB.objects.create(
|
||||
**data.dict()
|
||||
)
|
||||
return await TranslationDB.objects.create(**data.dict())
|
||||
|
||||
@classmethod
|
||||
async def _create_remote_translation(cls, data: CreateRemoteTranslation) -> TranslationDB:
|
||||
async def _create_remote_translation(
|
||||
cls, data: CreateRemoteTranslation
|
||||
) -> TranslationDB:
|
||||
source = await SourceDB.objects.get_or_none(id=data.source)
|
||||
|
||||
if source is None:
|
||||
cls._raise_bad_request()
|
||||
|
||||
book = await BookDB.objects.get_or_none(source__id=source.id, remote_id=data.remote_book)
|
||||
book = await BookDB.objects.get_or_none(
|
||||
source__id=source.id, remote_id=data.remote_book
|
||||
)
|
||||
|
||||
if book is None:
|
||||
cls._raise_bad_request()
|
||||
|
||||
translator = await AuthorDB.objects.get_or_none(source__id=source.id, remote_id=data.remote_translator)
|
||||
translator = await AuthorDB.objects.get_or_none(
|
||||
source__id=source.id, remote_id=data.remote_translator
|
||||
)
|
||||
|
||||
if translator is None:
|
||||
cls._raise_bad_request()
|
||||
@@ -42,7 +48,9 @@ class TranslationCreator:
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def create(cls, data: Union[CreateTranslation, CreateRemoteTranslation]) -> TranslationDB:
|
||||
async def create(
|
||||
cls, data: Union[CreateTranslation, CreateRemoteTranslation]
|
||||
) -> TranslationDB:
|
||||
if isinstance(data, CreateTranslation):
|
||||
return await cls._create_translation(data)
|
||||
if isinstance(data, CreateRemoteTranslation):
|
||||
|
||||
Reference in New Issue
Block a user