Skip to content

Commit 3363a62

Browse files
ivan-g-scntDamirkhon
authored andcommitted
M2-3148 Work with arbitrary server is taken into consideration
1 parent e149759 commit 3363a62

3 files changed

Lines changed: 45 additions & 14 deletions

File tree

src/apps/answers/crud/answers.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -601,21 +601,19 @@ async def removing_outdated_answers(
601601
raise AnswerRetentionType()
602602
border_datetime = datetime.datetime.utcnow() - retention_time
603603

604-
query = update(AnswerSchema)
604+
query: Query = delete(AnswerSchema)
605605
query = query.where(AnswerSchema.applet_id == applet_id)
606606
query = query.where(AnswerSchema.created_at < border_datetime)
607-
query = query.where(AnswerSchema.soft_exists())
608-
query = query.values(is_deleted=True)
609607
query = query.returning(column("id"))
610608
deleted_answer_ids: list[uuid.UUID] = [
611609
x[0] for x in await self._execute(query)
612610
]
613611

614-
query = update(AnswerItemSchema)
615-
query = query.where(AnswerItemSchema.answer_id.in_(deleted_answer_ids))
616-
query = query.where(AnswerSchema.soft_exists())
617-
query = query.values(is_deleted=True)
618-
await self._execute(query)
612+
item_query: Query = delete(AnswerItemSchema)
613+
item_query = item_query.where(
614+
AnswerItemSchema.answer_id.in_(deleted_answer_ids)
615+
)
616+
await self._execute(item_query)
619617

620618
async def update_encrypted_fields(
621619
self, user_public_key: str, data: list[AnswerItemDataEncrypted]

src/apps/answers/tasks.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,23 @@ async def removing_outdated_answers():
9595
for applet_data in applets_data:
9696
applet_id, retention_period, retention_type = applet_data
9797
retention_type = DataRetention(retention_type)
98-
await AnswersCRUD(session).removing_outdated_answers(
99-
applet_id, retention_period, retention_type
100-
)
98+
99+
arb_uri = await get_arbitrary_info(applet_id, session)
100+
if arb_uri:
101+
arb_session_maker = session_manager.get_session(arb_uri)
102+
try:
103+
async with arb_session_maker() as arb_session:
104+
await AnswersCRUD(
105+
arb_session
106+
).removing_outdated_answers(
107+
applet_id, retention_period, retention_type
108+
)
109+
finally:
110+
await arb_session_maker.remove()
111+
else:
112+
await AnswersCRUD(session).removing_outdated_answers(
113+
applet_id, retention_period, retention_type
114+
)
101115
await session.commit()
102116
except Exception as e:
103117
traceback.print_exception(e)

src/apps/applets/service/applet.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from apps.activity_flows.domain.flow_create import FlowCreate, FlowItemCreate
1313
from apps.activity_flows.service.flow import FlowService
1414
from apps.answers.crud.answers import AnswersCRUD
15+
from apps.answers.deps.preprocess_arbitrary import get_arbitrary_info
1516
from apps.applets.crud import (
1617
AppletHistoriesCRUD,
1718
AppletsCRUD,
@@ -58,6 +59,7 @@
5859
from apps.workspaces.errors import AppletEncryptionUpdateDenied
5960
from apps.workspaces.service.user_applet_access import UserAppletAccessService
6061
from config import settings
62+
from infrastructure.database import session_manager
6163

6264
__all__ = [
6365
"AppletService",
@@ -827,9 +829,26 @@ async def set_data_retention(
827829
applet_id, data_retention
828830
)
829831
if data_retention.retention != DataRetention.INDEFINITELY:
830-
await AnswersCRUD(self.session).removing_outdated_answers(
831-
applet_id, data_retention.period or 1, data_retention.retention
832-
)
832+
arb_uri = await get_arbitrary_info(applet_id, self.session)
833+
if arb_uri:
834+
arb_session_maker = session_manager.get_session(arb_uri)
835+
try:
836+
async with arb_session_maker() as arb_session:
837+
await AnswersCRUD(
838+
arb_session
839+
).removing_outdated_answers(
840+
applet_id,
841+
data_retention.period or 10**10,
842+
data_retention.retention,
843+
)
844+
finally:
845+
await arb_session_maker.remove()
846+
else:
847+
await AnswersCRUD(self.session).removing_outdated_answers(
848+
applet_id,
849+
data_retention.period or 10**10,
850+
data_retention.retention,
851+
)
833852

834853
async def get_full_applet(self, applet_id: uuid.UUID) -> AppletFull:
835854
schema = await AppletsCRUD(self.session).get_by_id(applet_id)

0 commit comments

Comments
 (0)