Skip to content

Commit

Permalink
merge from develop
Browse files Browse the repository at this point in the history
  • Loading branch information
bobr2072 committed Sep 3, 2023
2 parents 5bdb198 + 736bc25 commit af306ba
Show file tree
Hide file tree
Showing 18 changed files with 324 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[flake8]
ignore = E501, E265, F811, PT001, DJ05, D100, D105, D104, W504, W292, D106, D107
ignore = E501, E265, F811, PT001, DJ05, D100, D105, D104, W504, W292, D106, D107, W503
max-line-length = 79
exclude =
*/migrations/
16 changes: 15 additions & 1 deletion .github/workflows/deploy-bot-on-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ jobs:
name: prod_deploy
if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps:
- name: copy service file
uses: appleboy/scp-action@v0.1.4
with:
host: ${{ secrets.VM_HOST }}
username: ${{ secrets.VM_USER }}
password: ${{ secrets.VM_PASSWORD }}
source: "infra/prod/"
target: /home/deploy/spread_wings_bot/infra/prod/
- name: ssh pull and start
uses: appleboy/ssh-action@master
with:
Expand All @@ -21,7 +29,6 @@ jobs:
password: ${{ secrets.VM_PASSWORD }}
script: |
cd /home/deploy/spread_wings_bot/infra/prod/
git pull
rm .env
touch .env
Expand Down Expand Up @@ -61,6 +68,13 @@ jobs:
# Cleaning unused containers, images, networks
docker system prune --force
# Installing defend service for app
# Шаг с копированием в строках 16-23 можно заменить командой ниже - нужно тестировать
# scp infra/prod/spread_wings_bot.service ${{ secrets.VM_USER }}@${{ secrets.VM_HOST }}:/spread_wings_bot/infra/prod/
sudo cp -f /home/deploy/spread_wings_bot/infra/prod/spread_wings_bot.service /etc/systemd/system/spread_wings_bot.service
sudo systemctl daemon-reload
sudo systemctl restart spread_wings_bot.service
# Installing the app
docker-compose -f docker-compose.stage.yaml stop
docker-compose -f docker-compose.stage.yaml pull
Expand Down
23 changes: 22 additions & 1 deletion .github/workflows/deploy-bot-on-stage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ jobs:
name: stage_deploy
if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps:
- name: Run a multi-line script
# uses: actions/checkout@v2
run: |
mkdir ../spread_wings_bot/infra/dev/
cp -TR . ../spread_wings_bot/infra/dev/
tar -cvf deploy.tar ../spread_wings_bot/infra/dev/
- name: copy service file
uses: appleboy/scp-action@v0.1.4
with:
host: ${{ secrets.VM_HOST }}
username: ${{ secrets.VM_USER }}
password: ${{ secrets.VM_PASSWORD }}
source: "deploy.tar"
target: /home/deploy/spread_wings_bot/infra/dev/
- name: ssh pull and start
uses: appleboy/ssh-action@master
with:
Expand All @@ -21,7 +36,6 @@ jobs:
password: ${{ secrets.VM_PASSWORD }}
script: |
cd /home/deploy/spread_wings_bot/infra/dev/
git pull
rm .env
touch .env
Expand Down Expand Up @@ -61,6 +75,13 @@ jobs:
# Cleaning unused containers, images, networks
docker system prune --force
# Installing defend service for app
# Шаг с копированием в строках 16-23 можно заменить командой ниже - нужно тестировать
# scp infra/dev/spread_wings_bot.service ${{ secrets.VM_USER }}@${{ secrets.VM_HOST }}:/spread_wings_bot/infra/dev/
sudo cp -f /home/deploy/spread_wings_bot/infra/dev/spread_wings_bot.service /etc/systemd/system/spread_wings_bot.service
sudo systemctl daemon-reload
sudo systemctl restart spread_wings_bot.service
# Installing the app
docker-compose -f docker-compose.stage.yaml stop
docker-compose -f docker-compose.stage.yaml pull
Expand Down
29 changes: 29 additions & 0 deletions infra/dev/spread_wings_bot.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[Unit]

Description=spread_wings_bot
Requires=docker.service
After=docker.service

[Service]

Restart=always
RestartSec=5
TimeOutStartSec=1200
User=root

WorkingDirectory=/home/deploy/spread_wings_bot/infra/dev/

ExecStartPre=docker-compose -f docker-compose.stage.yaml --env-file /home/deploy/spread_wings_bot/infra/dev/.env pull bot
ExecStartPre=docker-compose -f docker-compose.stage.yaml --env-file /home/deploy/spread_wings_bot/infra/dev/.env pull db
ExecStartPre=docker-compose -f docker-compose.stage.yaml --env-file /home/deploy/spread_wings_bot/infra/dev/.env pull redis
ExecStartPre=docker-compose -f docker-compose.stage.yaml --env-file /home/deploy/spread_wings_bot/infra/dev/.env pull nginx
ExecStartPre=docker-compose -f docker-compose.stage.yaml --env-file /home/deploy/spread_wings_bot/infra/dev/.env down

# compose up
ExecStart=docker-compose -f docker-compose.stage.yaml --env-file /home/deploy/spread_wings_bot/infra/dev/.env up

# compose down
ExecStop=docker-compose -f docker-compose.stage.yaml --env-file /home/deploy/spread_wings_bot/infra/dev/.env down

[Install]
WantedBy=multi-user.target
29 changes: 29 additions & 0 deletions infra/prod/spread_wings_bot.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[Unit]

Description=spread_wings_bot
Requires=docker.service
After=docker.service

[Service]

Restart=always
RestartSec=5
TimeOutStartSec=1200
User=root

WorkingDirectory=/home/deploy/spread_wings_bot/infra/prod/

ExecStartPre=docker-compose -f docker-compose.prod.yaml --env-file /home/deploy/spread_wings_bot/infra/prod/.env pull bot
ExecStartPre=docker-compose -f docker-compose.prod.yaml --env-file /home/deploy/spread_wings_bot/infra/prod/.env pull db
ExecStartPre=docker-compose -f docker-compose.prod.yaml --env-file /home/deploy/spread_wings_bot/infra/prod/.env pull redis
ExecStartPre=docker-compose -f docker-compose.prod.yaml --env-file /home/deploy/spread_wings_bot/infra/prod/.env pull nginx
ExecStartPre=docker-compose -f docker-compose.prod.yaml --env-file /home/deploy/spread_wings_bot/infra/prod/.env down

# compose up
ExecStart=docker-compose -f docker-compose.prod.yaml --env-file /home/deploy/spread_wings_bot/infra/prod/.env up

# compose down
ExecStop=docker-compose -f docker-compose.prod.yaml --env-file /home/deploy/spread_wings_bot/infra/prod/.env down

[Install]
WantedBy=multi-user.target
4 changes: 2 additions & 2 deletions src/bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ async def build_app() -> Application:
states={
States.GET_ASSISTANCE: [
CallbackQueryHandler(get_assistance, pattern=GET_ASSISTANCE),
],
States.REGION: [
]
+ [
CallbackQueryHandler(
select_type_of_assistance,
pattern=PATTERN.format(state=key),
Expand Down
4 changes: 3 additions & 1 deletion src/bot/constants/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
state="".join(f"{h_type}|" for h_type in HelpTypes.names)
)
HELP_TYPE = rf"({POSSIBLE_TYPE_OF_ASSISTANCE})(?:{PAGE_SEP_SYMBOL}(\d+))?"
GET_ASSISTANCE = PATTERN.format(
state=rf"({States.GET_ASSISTANCE.value})(?:{PAGE_SEP_SYMBOL}(\d+))?"
)
SEND_EMAIL = PATTERN.format(state=States.SEND_EMAIL.value)
GET_USER_QUESTION = PATTERN.format(state=States.GET_USER_QUESTION.value)
GET_ASSISTANCE = PATTERN.format(state=States.GET_ASSISTANCE.value)
FUND_PROGRAMS = PATTERN.format(
state=rf"({States.FUND_PROGRAMS.value})(?:{PAGE_SEP_SYMBOL}(\d+))?"
)
Expand Down
24 changes: 16 additions & 8 deletions src/bot/handlers/assistance.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
SELECT_FUND_PROGRAM,
SELECT_QUESTION,
)
from bot.constants.patterns import FUND_PROGRAMS, HELP_TYPE
from bot.constants.patterns import FUND_PROGRAMS, GET_ASSISTANCE, HELP_TYPE
from bot.constants.states import States
from bot.handlers.debug_handlers import debug_logger
from bot.keyboards.assistance import (
Expand All @@ -25,21 +25,29 @@
DEFAULT_PAGE = 1


@debug_logger(state=States.REGION, run_functions_debug_loger="get_assistance")
@debug_logger(
state=States.GET_ASSISTANCE, run_functions_debug_loger="get_assistance"
)
async def get_assistance(
update: Update,
context: ContextTypes.DEFAULT_TYPE,
) -> States:
"""Select a region of assistance."""
await update.callback_query.answer()
keyboard = await build_region_keyboard()
query = update.callback_query
callback_data = query.data.replace("back_to_", "")
_, page_number = parse_callback_data(callback_data, GET_ASSISTANCE)
page_number = page_number or DEFAULT_PAGE
await query.answer()
keyboard = await build_region_keyboard(page_number)
assistance_message = await BotSettings.objects.aget(
key="assistance_message"
)
await update.callback_query.edit_message_text(
text=assistance_message.value, reply_markup=keyboard
)
return States.REGION
if query.message.reply_markup.to_json() != keyboard.markup:
await query.edit_message_text(
text=assistance_message.value,
reply_markup=keyboard.markup,
)
return States.GET_ASSISTANCE


@debug_logger(
Expand Down
4 changes: 2 additions & 2 deletions src/bot/handlers/service_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ async def answer_all_messages(


FUNCTIONS: dict[str, Callable[[Any, Any], Awaitable[States]]] = {
States.GET_ASSISTANCE.value: start,
States.START: start,
States.ASSISTANCE_TYPE: select_type_of_assistance,
States.CONTACT_US: contact_with_us,
States.FUND_PROGRAMS: fund_programs,
States.REGION: get_assistance,
States.REGION.value: get_assistance,
States.SHOW_CONTACT: show_contact,
States.GET_USERNAME: get_user_question,
States.USERNAME_AFTER_RETURNING: get_username_after_returning_back,
Expand Down
59 changes: 37 additions & 22 deletions src/bot/keyboards/assistance.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
from bot_settings.models import BotSettings
from core.models import Region

PROGRAMS_PER_PAGE = 6
QUESTIONS_PER_PAGE = 6


async def build_assistance_keyboard() -> InlineKeyboardMarkup:
"""
Expand All @@ -43,30 +40,42 @@ async def build_assistance_keyboard() -> InlineKeyboardMarkup:
)


async def build_region_keyboard() -> InlineKeyboardMarkup:
async def build_region_keyboard(
page: int,
) -> InlineKeyboardPaginator:
"""
Build telegram assistance type keyboard async.
After building cache it.
"""
keyboard = [
[
InlineKeyboardButton(
text=region.region_name,
callback_data=region.region_key,
)
]
async for region in Region.objects.all()
]
back_button = [
[
queryset = await sync_to_async(list)(
Region.objects.all().values("region_name", "region_key")
)
region_per_page = await BotSettings.objects.aget(
key="regions_pagination_setting"
)
data_paginator = Paginator(queryset, int(region_per_page.value))
telegram_paginator = InlineKeyboardPaginator(
data_paginator.num_pages,
current_page=page,
data_pattern="".join(
[States.GET_ASSISTANCE.value, PAGE_SEP_SYMBOL, "{page}"]
),
)
for region in data_paginator.page(page):
telegram_paginator.add_before(
InlineKeyboardButton(
text=BACK_BUTTON,
callback_data=f"back_to_{States.GET_ASSISTANCE.value}",
text=region.get("region_name"),
callback_data=region.get("region_key"),
)
]
]
return InlineKeyboardMarkup(keyboard + back_button)
)
telegram_paginator.add_after(
InlineKeyboardButton(
text=BACK_BUTTON,
callback_data=f"back_to_{States.START.value}",
),
)
return telegram_paginator


async def build_question_keyboard(
Expand All @@ -85,7 +94,10 @@ async def build_question_keyboard(
question_type=question_type,
).values("id", "short_description")
)
data_paginator = Paginator(queryset, QUESTIONS_PER_PAGE)
questions_per_page = await BotSettings.objects.aget(
key="questions_pagination_setting"
)
data_paginator = Paginator(queryset, int(questions_per_page.value))
telegram_paginator = InlineKeyboardPaginator(
data_paginator.num_pages,
current_page=page,
Expand Down Expand Up @@ -131,7 +143,10 @@ async def build_fund_program_keyboard(
regions__region_key=region,
).values("id", "short_description")
)
data_paginator = Paginator(queryset, PROGRAMS_PER_PAGE)
programs_per_page = await BotSettings.objects.aget(
key="programs_pagination_setting"
)
data_paginator = Paginator(queryset, int(programs_per_page.value))
telegram_paginator = InlineKeyboardPaginator(
data_paginator.num_pages,
current_page=page,
Expand Down
49 changes: 49 additions & 0 deletions src/bot/migrations/0008_alter_coordinator_phone_number_and_more.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Generated by Django 4.2.4 on 2023-09-03 17:19

import django.core.validators
from django.db import migrations, models


class Migration(migrations.Migration):
"""Migrations for bot."""

dependencies = [
("bot", "0007_alter_question_short_description"),
]

operations = [
migrations.AlterField(
model_name="coordinator",
name="phone_number",
field=models.CharField(
blank=True,
help_text="Введите номер телефона регионального координатора",
max_length=20,
null=True,
validators=[
django.core.validators.RegexValidator(
message="Введите номер телефона в формате: +7 (777) 777-77-77",
regex="^[\\+]?[7, 8][-\\s\\.]?[(]?[0-9]{3}[)]?[-\\s\\.]?[0-9]{3}[-\\s\\.]?[0-9]{2}[-\\s\\.]?[0-9]{2}$",
)
],
verbose_name="Номер телефона",
),
),
migrations.AlterField(
model_name="coordinator",
name="telegram_account",
field=models.CharField(
blank=True,
help_text="Введите телеграмм-аккаунт регионального координатора",
max_length=32,
null=True,
validators=[
django.core.validators.RegexValidator(
message="Введите название аккаунта telegram в формате: username",
regex="^[\\w\\_]{5,32}$",
)
],
verbose_name="Telegram",
),
),
]
Loading

0 comments on commit af306ba

Please sign in to comment.