8 Commits
9f27ab419f
...
da616498df
| Author | SHA1 | Message | Date |
|---|---|---|---|
|
|
da616498df |
Update dependencies
|
4 years ago |
|
|
20d343a73f |
Update dependencies
|
5 years ago |
|
|
3d1b53bcfd |
Add migrations
|
5 years ago |
|
|
716cbc3256 |
Build assigned titles message text from given title records
|
5 years ago |
|
|
1ee285f29e |
Reword docstring
|
5 years ago |
|
|
d682982852 |
Shorten imports
|
5 years ago |
|
|
af62d794ad
|
Upgrade pip inside venv and explicitly install wheel
See also https://github.com/pypa/pip/issues/8102 |
5 years ago |
|
|
a9276d3aa9
|
Lint Dockerfile with hadolint
|
5 years ago |
16 changed files with 368 additions and 232 deletions
-
5.ci/hadolint.yaml
-
15.woodpecker.yml
-
2Dockerfile
-
96alembic/versions/733a63a60643_build_titles_from_stats.py
-
303devpotato_bot/commands/daily_titles/assign_titles.py
-
6devpotato_bot/commands/daily_titles/daily_job.py
-
2devpotato_bot/commands/daily_titles/group_migrated.py
-
33devpotato_bot/commands/daily_titles/models/group_chat.py
-
2devpotato_bot/commands/daily_titles/refresh_participants.py
-
48devpotato_bot/commands/daily_titles/show.py
-
65devpotato_bot/commands/daily_titles/title_formatter.py
-
2devpotato_bot/commands/daily_titles/titles_pool/model_wrapper.py
-
2devpotato_bot/commands/daily_titles/user_leave_chat.py
-
3devpotato_bot/sample.py
-
10requirements.txt
-
6setup.py
@ -0,0 +1,5 @@ |
|||
--- |
|||
override: |
|||
info: |
|||
- DL3017 |
|||
- DL3018 |
|||
@ -0,0 +1,96 @@ |
|||
"""Build titles from stats |
|||
|
|||
Revision ID: 733a63a60643 |
|||
Revises: 981e95ceff06 |
|||
Create Date: 2021-04-24 19:18:54.642211 |
|||
|
|||
""" |
|||
from alembic import op |
|||
import sqlalchemy as sa |
|||
|
|||
|
|||
# revision identifiers, used by Alembic. |
|||
revision = "733a63a60643" |
|||
down_revision = "981e95ceff06" |
|||
branch_labels = None |
|||
depends_on = None |
|||
|
|||
|
|||
def upgrade(): |
|||
group_chats = sa.table( |
|||
"daily_titles_group_chats", |
|||
sa.column("chat_id", sa.BigInteger), |
|||
sa.column("last_triggered", sa.DateTime), |
|||
sa.column("last_titles", sa.Text), |
|||
sa.column("last_given_titles_count", sa.Integer), |
|||
) |
|||
participants = sa.table( |
|||
"daily_titles_participants", |
|||
sa.column("id", sa.BigInteger), |
|||
sa.column("chat_id", sa.BigInteger), |
|||
) |
|||
given_inevitable_titles = sa.table( |
|||
"daily_titles_given_inevitable_titles", |
|||
sa.column("id", sa.BigInteger), |
|||
sa.column("given_on", sa.DateTime), |
|||
sa.column("participant_id", sa.BigInteger), |
|||
) |
|||
given_shuffled_titles = sa.table( |
|||
"daily_titles_given_shuffled_titles", |
|||
sa.column("id", sa.BigInteger), |
|||
sa.column("given_on", sa.DateTime), |
|||
sa.column("participant_id", sa.BigInteger), |
|||
) |
|||
|
|||
inevitable_count = ( |
|||
sa.select(sa.func.count(given_inevitable_titles.c.id)) |
|||
.join_from( |
|||
given_inevitable_titles, |
|||
participants, |
|||
given_inevitable_titles.c.participant_id == participants.c.id, |
|||
) |
|||
.where( |
|||
given_inevitable_titles.c.given_on == group_chats.c.last_triggered, |
|||
participants.c.chat_id == group_chats.c.chat_id, |
|||
) |
|||
).scalar_subquery() |
|||
|
|||
shuffled_count = ( |
|||
sa.select(sa.func.count(given_shuffled_titles.c.id)) |
|||
.join_from( |
|||
given_shuffled_titles, |
|||
participants, |
|||
given_shuffled_titles.c.participant_id == participants.c.id, |
|||
) |
|||
.where( |
|||
given_shuffled_titles.c.given_on == group_chats.c.last_triggered, |
|||
participants.c.chat_id == group_chats.c.chat_id, |
|||
) |
|||
).scalar_subquery() |
|||
|
|||
select_counts = sa.case( |
|||
(group_chats.c.last_titles == None, None), |
|||
else_=(inevitable_count + shuffled_count), |
|||
) |
|||
|
|||
with op.batch_alter_table("daily_titles_group_chats", schema=None) as batch_op: |
|||
batch_op.add_column( |
|||
sa.Column("last_given_titles_count", sa.Integer(), nullable=True) |
|||
) |
|||
# we have to split the batch operation so last_given_titles_count will be present |
|||
# when we are trying to calculate its values |
|||
with op.batch_alter_table("daily_titles_group_chats", schema=None) as batch_op: |
|||
batch_op.execute( |
|||
group_chats.update().values(last_given_titles_count=select_counts) |
|||
) |
|||
batch_op.drop_column("last_titles_plain") |
|||
batch_op.drop_column("last_titles") |
|||
|
|||
|
|||
def downgrade(): |
|||
with op.batch_alter_table("daily_titles_group_chats", schema=None) as batch_op: |
|||
# Note that we don't generate contents for these columns from given_*_titles! |
|||
batch_op.add_column(sa.Column("last_titles", sa.TEXT(), nullable=True)) |
|||
batch_op.add_column(sa.Column("last_titles_plain", sa.TEXT(), nullable=True)) |
|||
|
|||
batch_op.drop_column("last_given_titles_count") |
|||
@ -0,0 +1,65 @@ |
|||
from __future__ import annotations |
|||
|
|||
from typing import TYPE_CHECKING |
|||
|
|||
from telegram.utils.helpers import escape_markdown, mention_markdown |
|||
|
|||
from ._strings import STREAK_MARK |
|||
|
|||
if TYPE_CHECKING: |
|||
from .models import GivenShuffledTitle |
|||
from .models import GivenInevitableTitle |
|||
from typing import List, Tuple |
|||
|
|||
|
|||
def format_given_shuffled_title(record: 'GivenShuffledTitle'): |
|||
participant = record.participant |
|||
user_name = escape_markdown(participant.full_name, version=2) |
|||
title_text = escape_markdown(record.title.text, version=2) |
|||
mention = mention_markdown(participant.user_id, participant.full_name, version=2) |
|||
plain = f" {user_name} \\- {title_text}" |
|||
with_mention = f" {mention} \\- {title_text}" |
|||
return plain, with_mention |
|||
|
|||
|
|||
def _format_streak_mark(streak_length: int): |
|||
if streak_length < 2: |
|||
return "" |
|||
return f"{streak_length}x{STREAK_MARK} " |
|||
|
|||
|
|||
def format_given_inevitable_title(record: 'GivenInevitableTitle'): |
|||
participant = record.participant |
|||
user_name = escape_markdown(participant.full_name, version=2) |
|||
title_text = escape_markdown(record.title.text, version=2) |
|||
streak_mark = _format_streak_mark(record.streak_length) |
|||
mention = mention_markdown(participant.user_id, participant.full_name, version=2) |
|||
plain = f" {streak_mark}{user_name} \\- {title_text}" |
|||
with_mention = f" {streak_mark}{mention} \\- {title_text}" |
|||
return plain, with_mention |
|||
|
|||
|
|||
def _format_lines(records, formatter, lines_plain, lines_mention): |
|||
for plain, mention in map(formatter, records): |
|||
lines_plain.append(plain) |
|||
lines_mention.append(mention) |
|||
|
|||
|
|||
def get_titles_text( |
|||
inevitable_titles: List['GivenInevitableTitle'], |
|||
shuffled_titles: List['GivenShuffledTitle'] |
|||
) -> Tuple[str, str]: |
|||
if not (inevitable_titles or shuffled_titles): |
|||
return '', '' |
|||
|
|||
lines_plain, lines_mention = [], [] # type: List[str], List[str] |
|||
|
|||
_format_lines(inevitable_titles, format_given_inevitable_title, lines_plain, lines_mention) |
|||
|
|||
if inevitable_titles and shuffled_titles: |
|||
lines_plain.append("") |
|||
lines_mention.append("") |
|||
|
|||
_format_lines(shuffled_titles, format_given_shuffled_title, lines_plain, lines_mention) |
|||
|
|||
return "\n".join(lines_plain), "\n".join(lines_mention) |
|||
@ -1,6 +1,6 @@ |
|||
python-telegram-bot==13.4.1 |
|||
ujson==4.0.2 |
|||
pytz>=2021.1 |
|||
cachetools==4.2.1 |
|||
SQLAlchemy==1.4.2 |
|||
alembic==1.5.7 |
|||
ujson==5.1.0 |
|||
pytz>=2021.3 |
|||
cachetools==5.0.0 |
|||
SQLAlchemy==1.4.11 |
|||
alembic==1.5.8 |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue