Uprava queries a dotazovani se pres base query builder, fix nahledu fotky pro editor, cache na fotky
This commit is contained in:
@@ -36,7 +36,6 @@ class AircraftQueries:
|
||||
public: Optional[bool] = False
|
||||
) -> Aircraft:
|
||||
filter_params = {}
|
||||
|
||||
if id:
|
||||
filter_params['object_id'] = id
|
||||
|
||||
@@ -45,7 +44,7 @@ class AircraftQueries:
|
||||
|
||||
return await AircraftQueryResolver().get_one(
|
||||
user_id=info.context.user_id,
|
||||
organization_ids=info.context.organization_ids,
|
||||
organization_ids=info.context.organization_ids if not public else None,
|
||||
only_public=public,
|
||||
**filter_params
|
||||
)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from typing import List
|
||||
import strawberry
|
||||
from sqlalchemy import delete
|
||||
from sqlalchemy import delete, select
|
||||
from sqlalchemy.dialects.mysql import insert
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from database import models
|
||||
@@ -8,6 +8,7 @@ from decorators.endpoints import authenticated_user_only
|
||||
from database.transaction import get_session
|
||||
from decorators.error_logging import error_logging
|
||||
from graphql_schema.entities.resolvers.base import BaseQueryResolver, BaseMutationResolver
|
||||
from graphql_schema.entities.resolvers.organization import OrganizationQueryResolver
|
||||
from graphql_schema.entities.types.mutation_input import CreateOrganizationInput, EditOrganizationInput
|
||||
from graphql_schema.entities.types.types import Organization
|
||||
|
||||
@@ -18,19 +19,13 @@ class OrganizationQueries:
|
||||
@error_logging
|
||||
@authenticated_user_only()
|
||||
async def organizations(root, info) -> List[Organization]:
|
||||
return await BaseQueryResolver(Organization, models.Organization).get_list(
|
||||
info.context.user_id,
|
||||
order_by=[models.Organization.name]
|
||||
)
|
||||
return await OrganizationQueryResolver().get_list()
|
||||
|
||||
@strawberry.field()
|
||||
@error_logging
|
||||
@authenticated_user_only()
|
||||
async def organization(root, info, id: int) -> Organization:
|
||||
return await BaseQueryResolver(Organization, models.Organization).get_one(
|
||||
object_id=id,
|
||||
user_id=info.context.user_id
|
||||
)
|
||||
return await OrganizationQueryResolver().get_one(object_id=id)
|
||||
|
||||
|
||||
@strawberry.type
|
||||
@@ -62,7 +57,7 @@ class OrganizationUserMutation:
|
||||
async def add_to_organization(root, info, organization_id: int) -> Organization:
|
||||
async with get_session() as db:
|
||||
organization = (await db.scalars(
|
||||
BaseQueryResolver(Organization, models.Organization).get_query(object_id=organization_id)
|
||||
OrganizationQueryResolver().get_query(object_id=organization_id)
|
||||
)).one()
|
||||
|
||||
try:
|
||||
@@ -83,7 +78,7 @@ class OrganizationUserMutation:
|
||||
async def remove_from_organization(root, info, organization_id: int) -> Organization:
|
||||
async with get_session() as db:
|
||||
organization = (await db.scalars(
|
||||
BaseQueryResolver(Organization, models.Organization).get_query(object_id=organization_id)
|
||||
OrganizationQueryResolver().get_query(object_id=organization_id)
|
||||
)).one()
|
||||
|
||||
await db.execute(
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
from operator import or_
|
||||
from typing import Set, Optional
|
||||
|
||||
from sqlalchemy import and_
|
||||
|
||||
from database import models
|
||||
from database.transaction import get_session
|
||||
from graphql_schema.entities.helpers.combobox import handle_combobox_save
|
||||
@@ -21,16 +24,20 @@ class AircraftQueryResolver(BaseQueryResolver):
|
||||
*args,
|
||||
**kwargs,
|
||||
):
|
||||
call_sign = kwargs.get("call_sign")
|
||||
filters = {}
|
||||
if object_id:
|
||||
filters['object_id'] = object_id
|
||||
if call_sign:
|
||||
filters['call_sign'] = call_sign
|
||||
if not organization_ids:
|
||||
filters['user_id'] = user_id
|
||||
if kwargs.get("call_sign"):
|
||||
filters['call_sign'] = kwargs["call_sign"]
|
||||
if kwargs.get("search"):
|
||||
filters['search'] = kwargs.pop("search", None)
|
||||
|
||||
query = super().get_query(
|
||||
order_by=[models.Aircraft.id.desc()],
|
||||
user_id=user_id if not organization_ids else None,
|
||||
only_my=False,
|
||||
only_public=kwargs.get("only_public", False),
|
||||
order_by=order_by,
|
||||
**filters
|
||||
)
|
||||
if organization_ids:
|
||||
@@ -38,7 +45,10 @@ class AircraftQueryResolver(BaseQueryResolver):
|
||||
query.filter(
|
||||
or_(
|
||||
models.Aircraft.created_by_id == user_id,
|
||||
models.Aircraft.organization_id.in_(organization_ids)
|
||||
and_(
|
||||
models.Aircraft.organization_id.in_(organization_ids),
|
||||
models.Aircraft.is_public.is_(True)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
@@ -35,38 +35,51 @@ class BaseQueryResolver(BaseResolver):
|
||||
object_id: Optional[int] = None,
|
||||
order_by: Optional[list] = None,
|
||||
only_public: Optional[bool] = False,
|
||||
only_my: Optional[bool] = False,
|
||||
url_slug: Optional[str] = None,
|
||||
**kwargs,
|
||||
):
|
||||
query = self.query_builder.get_simple_query(created_by_id=user_id, order_by=order_by, only_public=only_public)
|
||||
query = self.query_builder.get_simple_query(
|
||||
created_by_id=user_id,
|
||||
order_by=order_by,
|
||||
only_public=only_public,
|
||||
only_my=only_my,
|
||||
url_slug=url_slug
|
||||
)
|
||||
|
||||
if object_id:
|
||||
if not hasattr(self.model, "id"):
|
||||
raise AssertionError(f"Model {self.model} has no ID column! Cannot query by ID!")
|
||||
query = query.filter(self.model.id == object_id)
|
||||
|
||||
search = kwargs.pop("search", None)
|
||||
if search:
|
||||
search_clauses = []
|
||||
if hasattr(self.model, "name"):
|
||||
search_clauses.append(self.model.name.contains(search))
|
||||
|
||||
if hasattr(self.model, "description"):
|
||||
search_clauses.append(self.model.description.contains(search))
|
||||
|
||||
if search_clauses:
|
||||
query = query.filter(or_(*search_clauses))
|
||||
query = self._handle_search(query, kwargs.pop("search", None))
|
||||
|
||||
if kwargs:
|
||||
query = query.filter_by(**kwargs)
|
||||
|
||||
return query
|
||||
|
||||
def _handle_search(self, query, search):
|
||||
if not search:
|
||||
return query
|
||||
|
||||
search_clauses = []
|
||||
if hasattr(self.model, "name"):
|
||||
search_clauses.append(self.model.name.contains(search))
|
||||
|
||||
if hasattr(self.model, "description"):
|
||||
search_clauses.append(self.model.description.contains(search))
|
||||
|
||||
if search_clauses:
|
||||
query = query.filter(or_(*search_clauses))
|
||||
return query
|
||||
|
||||
async def get_list(self, user_id: Optional[int] = None, **kwargs) -> List[GQL_TYPE]:
|
||||
query = self.get_query(user_id, **kwargs)
|
||||
query = self.get_query(user_id=user_id, **kwargs)
|
||||
return await self._get_list(query)
|
||||
|
||||
async def get_one(self, user_id: Optional[int] = None, **kwargs) -> GQL_TYPE:
|
||||
query = self.get_query(user_id, **kwargs)
|
||||
query = self.get_query(user_id=user_id, **kwargs)
|
||||
return await self._get_one(query)
|
||||
|
||||
|
||||
|
||||
@@ -14,10 +14,16 @@ class CopilotQueryResolver(BaseQueryResolver):
|
||||
object_id: Optional[int] = None,
|
||||
order_by: Optional[list] = None,
|
||||
only_public: Optional[bool] = False,
|
||||
*args, **kwargs
|
||||
**kwargs
|
||||
):
|
||||
pilot_username = kwargs.pop("pilot_username", None)
|
||||
query = super().get_query(user_id, object_id, order_by, only_public, *args, **kwargs)
|
||||
|
||||
query = super().get_query(
|
||||
user_id=user_id, object_id=object_id, order_by=order_by,
|
||||
only_public=only_public,
|
||||
only_my=bool(user_id) and not only_public,
|
||||
**kwargs
|
||||
)
|
||||
|
||||
if pilot_username:
|
||||
query = (
|
||||
|
||||
@@ -21,7 +21,8 @@ class EventQueryResolver(BaseQueryResolver):
|
||||
user_id, object_id,
|
||||
order_by=[models.Event.date_from.desc()],
|
||||
url_slug=kwargs.get("url_slug"),
|
||||
only_public=not bool(user_id)
|
||||
only_public=only_public,
|
||||
only_my=True
|
||||
)
|
||||
|
||||
if kwargs.get('username'):
|
||||
|
||||
@@ -41,7 +41,8 @@ class FlightQueryResolver(BaseQueryResolver):
|
||||
user_id,
|
||||
**filters,
|
||||
order_by=[models.Flight.takeoff_datetime.desc()],
|
||||
only_public=only_public
|
||||
only_public=only_public,
|
||||
only_my=not only_public
|
||||
)
|
||||
|
||||
if kwargs.get("event_id"):
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
from typing import Optional
|
||||
from sqlalchemy import select
|
||||
from database import models
|
||||
from graphql_schema.entities.resolvers.base import BaseQueryResolver
|
||||
from graphql_schema.entities.types.types import Organization
|
||||
|
||||
|
||||
class OrganizationQueryResolver(BaseQueryResolver):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(Organization, models.Organization)
|
||||
|
||||
def get_query(
|
||||
self,
|
||||
object_id: Optional[int] = None,
|
||||
order_by: Optional[list] = None,
|
||||
**kwargs):
|
||||
|
||||
query = (
|
||||
select(models.Organization)
|
||||
.filter(models.Organization.deleted.is_(False))
|
||||
)
|
||||
|
||||
if not order_by:
|
||||
order_by = [models.Organization.name]
|
||||
|
||||
query = query.order_by(*order_by)
|
||||
|
||||
if object_id:
|
||||
query = query.where(models.Organization.id == object_id)
|
||||
|
||||
return query
|
||||
@@ -22,6 +22,7 @@ from graphql_schema.dataloaders.single_model import (
|
||||
airport_weather_info_loader, organizations_dataloader, flight_dataloader, photo_adjustment_dataloader,
|
||||
photo_dataloader, user_dataloader
|
||||
)
|
||||
from graphql_schema.permissions import IsAuthenticated
|
||||
from graphql_schema.sqlalchemy_to_strawberry_type import strawberry_sqlalchemy_type
|
||||
from paths import get_avatar_url, get_title_image_url, get_photo_thumbnail_url, get_photo_url, FLIGHT_GPX_TRACK_PATH
|
||||
|
||||
@@ -96,15 +97,6 @@ class Photo:
|
||||
)
|
||||
|
||||
|
||||
class IsAuthenticated(BasePermission):
|
||||
message = "User is not authenticated"
|
||||
error_class = GraphQLError
|
||||
error_extensions = {"code": "UNAUTHORIZED"}
|
||||
|
||||
def has_permission(self, source: Any, info: Info, **kwargs) -> bool:
|
||||
return bool(info.context.user_id)
|
||||
|
||||
|
||||
@strawberry_sqlalchemy_type(models.Flight)
|
||||
class Flight:
|
||||
def __init__(self, **kwargs):
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
from typing import Any
|
||||
from graphql import GraphQLError
|
||||
from strawberry import BasePermission
|
||||
from strawberry.types import Info
|
||||
|
||||
|
||||
class IsAuthenticated(BasePermission):
|
||||
message = "User is not authenticated"
|
||||
error_class = GraphQLError
|
||||
error_extensions = {"code": "UNAUTHORIZED"}
|
||||
|
||||
def has_permission(self, source: Any, info: Info, **kwargs) -> bool:
|
||||
return bool(info.context.user_id)
|
||||
Reference in New Issue
Block a user