You've already forked hotpocket
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
|
||||
from django.db import models
|
||||
|
||||
from hotpocket_backend.apps.saves.models import Save
|
||||
from hotpocket_common.constants import AssociationsSearchMode
|
||||
from hotpocket_soa.dto.associations import AssociationsQuery
|
||||
|
||||
|
||||
class SaveAdapter:
|
||||
def get_for_processing(self, *, pk: uuid.UUID) -> Save | None:
|
||||
return Save.active_objects.get(pk=pk)
|
||||
|
||||
|
||||
class AssociationAdapter:
|
||||
def get_search_term_filters(self, *, query: AssociationsQuery) -> list[models.Q]:
|
||||
# I suck at naming things, LOL.
|
||||
result = [
|
||||
(
|
||||
models.Q(target__url__icontains=query.search)
|
||||
|
|
||||
models.Q(target_title__icontains=query.search)
|
||||
|
|
||||
models.Q(target__title__icontains=query.search)
|
||||
|
|
||||
models.Q(target_description__icontains=query.search)
|
||||
|
|
||||
models.Q(target__description__icontains=query.search)
|
||||
),
|
||||
]
|
||||
|
||||
return result
|
||||
|
||||
def get_search_filters(self, *, query: AssociationsQuery) -> list[models.Q]:
|
||||
result = [
|
||||
models.Q(account_uuid=query.account_uuid),
|
||||
models.Q(target__deleted_at__isnull=True),
|
||||
]
|
||||
|
||||
if query.mode == AssociationsSearchMode.ARCHIVED:
|
||||
result.append(models.Q(archived_at__isnull=False))
|
||||
else:
|
||||
result.append(models.Q(archived_at__isnull=True))
|
||||
|
||||
match query.mode:
|
||||
case AssociationsSearchMode.STARRED:
|
||||
result.append(models.Q(starred_at__isnull=False))
|
||||
|
||||
if query.before is not None:
|
||||
result.append(models.Q(pk__lt=query.before))
|
||||
|
||||
if query.after is not None:
|
||||
result.append(models.Q(pk__gte=query.after))
|
||||
|
||||
if query.search is not None:
|
||||
result.extend(self.get_search_term_filters(query=query))
|
||||
|
||||
return result
|
||||
@@ -0,0 +1,12 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import annotations
|
||||
|
||||
from .base import AssociationAdapter, SaveAdapter
|
||||
|
||||
|
||||
class BasicSaveAdapter(SaveAdapter):
|
||||
pass
|
||||
|
||||
|
||||
class BasicAssociationAdapter(AssociationAdapter):
|
||||
pass
|
||||
@@ -0,0 +1,49 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import uuid
|
||||
|
||||
import django.db
|
||||
from django.db import models
|
||||
|
||||
from hotpocket_backend.apps.saves.models import Save
|
||||
from hotpocket_common.db import postgres # noqa: F401
|
||||
from hotpocket_soa.dto.associations import AssociationsQuery
|
||||
|
||||
from .base import AssociationAdapter, SaveAdapter
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class PostgresSaveAdapter(SaveAdapter):
|
||||
ROW_LOCKED_MESSAGE = 'could not obtain lock on row in relation "saves_save"'
|
||||
|
||||
def get_for_processing(self, *, pk: uuid.UUID) -> Save | None:
|
||||
try:
|
||||
return Save.active_objects.select_for_update(nowait=True).get(pk=pk)
|
||||
except django.db.utils.OperationalError as exception:
|
||||
if exception.args[0].startswith(self.ROW_LOCKED_MESSAGE) is True:
|
||||
LOGGER.info('Trying to process a locked save: pk=`%s`', pk)
|
||||
return None
|
||||
|
||||
raise exception
|
||||
|
||||
|
||||
class PostgresAssociationAdapter(AssociationAdapter):
|
||||
def get_search_term_filters(self, *, query: AssociationsQuery) -> list[models.Q]:
|
||||
result = [
|
||||
(
|
||||
models.Q(target__url__ilike=query.search)
|
||||
|
|
||||
models.Q(target_title__ilike=query.search)
|
||||
|
|
||||
models.Q(target__title__ilike=query.search)
|
||||
|
|
||||
models.Q(target_description__ilike=query.search)
|
||||
|
|
||||
models.Q(target__description__ilike=query.search)
|
||||
),
|
||||
]
|
||||
|
||||
return result
|
||||
Reference in New Issue
Block a user