Skip to content

Commit

Permalink
⚡️ Improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidRomanovizc committed Aug 28, 2023
1 parent 771387a commit 41cbd92
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 24 deletions.
4 changes: 3 additions & 1 deletion data/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Miscellaneous:
client_id: str
redirect_url: URL
yoomoney_key: str
production: bool


@dataclass(frozen=True)
Expand Down Expand Up @@ -98,10 +99,11 @@ def load_config() -> Config:
client_id=env.str("CLIENT_ID"),
redirect_url=env.str("REDIRECT_URI"),
yoomoney_key=env.str("YOOMONEY_KEY"),
production=env.bool("PRODUCTION"),
),
)


# TODO: Переместить в dataclass
# TODO: Move to dataclass
BASE_DIR = Path(__file__).parent.parent
LOCALES_DIR = BASE_DIR / "locales"
2 changes: 2 additions & 0 deletions django_project/telegrambot/usersmanage/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class Meta:
"self", through="ViewedProfile", symmetrical=False
)
limit_of_views = models.PositiveIntegerField(default=10, null=True)
counter_of_report = models.PositiveIntegerField(default=0, null=True)
on_check_by_admin = models.BooleanField(default=False, null=True)

def __str__(self):
return f"№{self.id} ({self.telegram_id}) - {self.name}"
Expand Down
38 changes: 28 additions & 10 deletions functions/dating/reaction_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,21 +194,39 @@ async def execute(
self, call: CallbackQuery, state: FSMContext, callback_data: dict[str, str]
):
target_id = int(callback_data["target_id"])
target_user = await db_commands.select_user(telegram_id=target_id)

counter_of_report = target_user.counter_of_report
username = call.from_user.username
user_id = call.from_user.id
report_reason = await get_report_reason(call)

text = _(
"Жалоба от пользователя: <code>[@{username}</code> | <code>{tg_id}</code>]\n\n"
"На пользователя: <code>[{owner_id}]</code>\n"
"Причина жалобы: <code>{reason}</code>"
"Причина жалобы: <code>{reason}</code>\n"
"Количество жалоб на пользователя: <code>{counter_of_report}</code>"
).format(
username=call.from_user.username,
tg_id=call.from_user.id,
username=username,
tg_id=user_id,
owner_id=target_id,
reason=await get_report_reason(call),
reason=report_reason,
counter_of_report=counter_of_report,
)

await create_questionnaire(
form_owner=target_id,
chat_id=load_config().tg_bot.moderate_chat,
report_system=True,
add_text=text,
await db_commands.update_user_data(
telegram_id=target_id, counter_of_report=counter_of_report + 1
)
await asyncio.sleep(1)

moderate_chat = load_config().tg_bot.moderate_chat
if counter_of_report >= 5 and not target_user.on_check_by_admin:
await db_commands.update_user_data(
telegram_id=target_id, on_check_by_admin=True
)
await create_questionnaire(
form_owner=target_id,
chat_id=moderate_chat,
report_system=True,
add_text=text,
)
await asyncio.sleep(0.5)
31 changes: 18 additions & 13 deletions handlers/users/view_ques.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from aiogram.types import CallbackQuery
from django.db import IntegrityError

from data.config import load_config
from functions.dating import (
StartFindingSuccess,
StartFindingFailure,
Expand Down Expand Up @@ -50,8 +51,8 @@ async def handle_start_finding(call: CallbackQuery, state: FSMContext) -> None:

@dp.callback_query_handler(
action_report_keyboard.filter(
action=["adults_only", "drugs", "scam", "another", "cancel_report"]
)
action=["adults_only", "drugs", "scam", "another", "cancel_report"],
),
)
async def handle_report(
call: CallbackQuery, state: FSMContext, callback_data: dict[str, str]
Expand All @@ -65,16 +66,18 @@ async def handle_report(
"cancel_report": GoBackToViewing(),
}
strategy = strategy_mapping.get(action)
await strategy.execute(call, state, callback_data)

await call.message.answer(text=_("Жалоба успешно отправлена"))
strategy = GoBackToViewing()
await strategy.execute(call, state, callback_data)

if action != "cancel_report":
await call.message.answer(text=_("Жалоба успешно отправлена"))
strategy = GoBackToViewing()
await strategy.execute(call, state, callback_data)


@dp.callback_query_handler(
action_keyboard.filter(action=["like", "dislike", "stopped", "report"]),
state="finding",
state="*",
)
async def handle_action(
call: CallbackQuery, state: FSMContext, callback_data: dict[str, str]
Expand All @@ -83,13 +86,15 @@ async def handle_action(
profile_id = callback_data["target_id"]
user = await db_commands.select_user_object(telegram_id=call.from_user.id)
viewed_profile = await db_commands.select_user_object(telegram_id=profile_id)
try:
await db_commands.add_profile_to_viewed(
user=user, viewed_profile=viewed_profile
)
except IntegrityError:
logger.error("Дубликаты профилей")

if load_config().misc.production:
try:
await db_commands.add_profile_to_viewed(
user=user, viewed_profile=viewed_profile
)
except IntegrityError:
logger.error("Дубликаты профилей")
else:
pass
strategy_mapping = {
"like": LikeAction(),
"dislike": DislikeAction(),
Expand Down

0 comments on commit 41cbd92

Please sign in to comment.