Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
run:
source ./venv/bin/activate && uvicorn --reload --log-config logging_test.conf social.routes.base:app

format: configure
source ./venv/bin/activate && autoflake -r --in-place --remove-all-unused-imports ./auth_backend
source ./venv/bin/activate && isort ./auth_backend
source ./venv/bin/activate && black ./auth_backend

configure: venv
source ./venv/bin/activate && pip install -r requirements.dev.txt -r requirements.txt

venv:
python3.11 -m venv venv

db:
docker run -d -p 5432:5432 -e POSTGRES_HOST_AUTH_METHOD=trust --name db-social postgres:15
sleep 3

migrate:
alembic upgrade head
source ./venv/bin/activate && alembic upgrade head

test:
source ./venv/bin/activate && python3 -m pytest --verbosity=2 --showlocals --log-level=DEBUG
2 changes: 1 addition & 1 deletion migrations/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def run_migrations_online():

"""
configuration = config.get_section(config.config_ini_section)
configuration['sqlalchemy.url'] = settings.DB_DSN
configuration['sqlalchemy.url'] = str(settings.DB_DSN)
connectable = engine_from_config(
configuration,
prefix="sqlalchemy.",
Expand Down
1 change: 1 addition & 0 deletions requirements.dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
autoflake
black
httpx
pytest
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ fastapi
fastapi-sqlalchemy
psycopg2-binary
pydantic[dotenv]
Comment thread
dyakovri marked this conversation as resolved.
pydantic-settings
uvicorn
alembic
SQLAlchemy
Expand Down
7 changes: 2 additions & 5 deletions social/routes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@
settings = get_settings()
app = FastAPI(
title='Сервис мониторинга активности',
description=(
'Серверная часть сервиса для выдачи печенек за активности'
),
description=('Серверная часть сервиса для выдачи печенек за активности'),
version=__version__,

# Настраиваем интернет документацию
root_path=settings.ROOT_PATH if __version__ != 'dev' else '/',
docs_url=None if __version__ != 'dev' else '/docs',
Expand All @@ -28,7 +25,7 @@

app.add_middleware(
DBSessionMiddleware,
db_url=settings.DB_DSN,
db_url=str(settings.DB_DSN),
engine_args={"pool_pre_ping": True, "isolation_level": "AUTOCOMMIT"},
)

Expand Down
13 changes: 5 additions & 8 deletions social/settings.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import os

from pydantic import BaseSettings, PostgresDsn
from pydantic import ConfigDict, PostgresDsn
from pydantic_settings import BaseSettings
from functools import lru_cache


class Settings(BaseSettings):
"""Application settings"""

model_config = ConfigDict(case_sensitive=True, env_file=".env", extra="allow")
Comment thread
dyakovri marked this conversation as resolved.

DB_DSN: PostgresDsn = 'postgresql://postgres@localhost:5432/postgres'
ROOT_PATH: str = '/' + os.getenv('APP_NAME', '')

Expand All @@ -15,13 +18,7 @@ class Settings(BaseSettings):
CORS_ALLOW_METHODS: list[str] = ['*']
CORS_ALLOW_HEADERS: list[str] = ['*']

TELEGRAM_BOT_TOKEN: str | None

class Config:
"""Pydantic BaseSettings config"""

case_sensitive = True
env_file = ".env"
TELEGRAM_BOT_TOKEN: str | None = None


@lru_cache
Expand Down
6 changes: 4 additions & 2 deletions social/telegram/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ async def send_help(update: Update, context: CustomContext):
await context.bot.send_message(
chat_id=update.effective_message.chat.id,
reply_to_message_id=update.effective_message.id,
text=dedent("""
text=dedent(
"""
Привет, я ответственный за печеньки!
Моя основная цель – помогать различным комьюнити расти
"""),
"""
),
parse_mode='markdown',
)
15 changes: 7 additions & 8 deletions social/telegram/handlers_viribus.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
MessageHandler,
CommandHandler,
)
from telegram.ext.filters import (
Chat
)
from telegram.ext.filters import Chat

from social.settings import get_settings
from social.telegram.utils import CustomContext
Expand All @@ -37,6 +35,7 @@
"""
]


def register_handlers(app: Application):
app.add_handler(CommandHandler(filters=Chat(CHAT_ID), callback=change_slug, command="slug"))
app.add_handler(MessageHandler(filters=Chat(CHAT_ID), callback=delete_system_message))
Expand All @@ -49,9 +48,7 @@ async def delete_system_message(update: Update, context: CustomContext):
await context.bot.send_message(
chat_id=CHAT_ID,
message_thread_id=MAIN_TOPIC_ID,
text=dedent(choice(GREETINGS)).format(
name=user.name, id=user.id
),
text=dedent(choice(GREETINGS)).format(name=user.name, id=user.id),
parse_mode='markdown',
)
logger.info(f"User {user.name} greeting sent")
Expand All @@ -69,11 +66,13 @@ async def change_slug(update: Update, context: CustomContext):
await context.bot.send_message(
chat_id=update.effective_message.chat.id,
reply_to_message_id=update.effective_message.id,
text=dedent("""
text=dedent(
"""
Эта команда меняет текст, который пишется справа от имени пользователя в этом чате
Текст толжен содержать только буквы, цифры, пробелы и некоторую пунктуацию, не более 16 символов
Напиши `/slug текст` для применения
"""),
"""
),
parse_mode='markdown',
)
return
Expand Down
1 change: 1 addition & 0 deletions social/telegram/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class WebhookUpdate:
user_id: int
payload: str


class CustomContext(CallbackContext[ExtBot, dict, dict, dict]):
"""
Custom CallbackContext class that makes `user_data` available for updates of type
Expand Down