Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/workflow ruff #22

Merged
merged 8 commits into from
May 4, 2024
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
29 changes: 29 additions & 0 deletions .github/workflows/codestyle_ruff.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: CI

on: pull_request

jobs:
ruff:
runs-on: ubuntu-latest
name: ruff
steps:
- name: Установка Python
uses: actions/setup-python@v4
with:
python-version: 3.12

- name: Установка Poetry
uses: snok/install-poetry@v1
with:
poetry-version: 1.7.1

- name: Извлечение репозитория
uses: actions/checkout@v4

- name: Установка зависимостей
run: |
poetry install

- name: ruff
run: |
poetry run ruff check
36 changes: 18 additions & 18 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
description = ""
authors = ["Anton Braun <[email protected]>"]
readme = "README.md"
package-mode = false

[tool.poetry.dependencies]
python = "^3.12"
Expand Down Expand Up @@ -49,6 +50,7 @@ exclude = [
"buck-out",
"build",
"dist",
"migrations",
"node_modules",
"site-packages",
"venv",
Expand Down Expand Up @@ -92,13 +94,13 @@ select = [
"TCH"

]
ignore = ["D100", "D103", "T201"]
ignore = ["D100", "D103", "T201", "D104"]

fixable = ["ALL"]
unfixable = []

[tool.ruff.format]
quote-style = "single"
quote-style = "double"
indent-style = "space"
skip-magic-trailing-comma = false
line-ending = "auto"
Expand Down
1 change: 1 addition & 0 deletions src/bot/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""The functionality of the telegram bot."""
3 changes: 0 additions & 3 deletions src/bot/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
from django.contrib import admin

# Register your models here.
5 changes: 3 additions & 2 deletions src/bot/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ class BotConfig(AppConfig):
name = "bot"

def stop_bot(self, **kwargs):
"""Stop application."""
self.bot.stop()

def ready(self) -> None:
"""Perform actions when the application is ready."""
import os

if os.environ.get('RUN_MAIN', None) != 'true':
if os.environ.get("RUN_MAIN", None) != "true":
from bot.bot_interface import Bot

self.bot = Bot()

asgi_shutdown.connect(self.stop_bot)

self.bot.start()
self.bot.start()
6 changes: 3 additions & 3 deletions src/bot/bot_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.conf import settings
from telegram.ext import ApplicationBuilder

from bot.handlers import start_handler, echo_handler
from bot.handlers import echo_handler, start_handler


class Bot:
Expand All @@ -17,7 +17,7 @@ def __init__(self):
self._stop_event = threading.Event()

def start(self):
"""Starts the bot in a separate thread and sets up signal handling."""
"""Start the bot in a separate thread and sets up signal handling."""
self._stop_event.clear()
bot_thread = threading.Thread(target=self._run)
bot_thread.start()
Expand All @@ -33,7 +33,7 @@ async def _build_app(self):
app.add_handler(start_handler)
app.add_handler(echo_handler)
return app

def _run(self):
"""Run the bot."""
asyncio.set_event_loop(asyncio.new_event_loop())
Expand Down
24 changes: 10 additions & 14 deletions src/bot/handlers.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
from telegram import Update
from telegram.ext import (
filters,
ContextTypes,
CommandHandler,
MessageHandler
)

from core.logging import log_errors
from telegram import Update
from telegram.ext import CommandHandler, ContextTypes, MessageHandler, filters


async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Handler for the /start command."""
"""/start command handler."""
await context.bot.send_message(
chat_id=update.effective_chat.id,
text=(
'Привет, я бот онлайн школы GoodStart.\n\n'
'Здесь ты найдёшь лучших преподавателей.')
"Привет, я бот онлайн школы GoodStart.\n\n"
"Здесь ты найдёшь лучших преподавателей."
),
)


@log_errors
async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Handler for echoing user messages."""
"""Echo messages handler."""
await context.bot.send_message(
chat_id=update.effective_chat.id,
text=update.message.text
text=update.message.text,
)

start_handler = CommandHandler('start', start)

start_handler = CommandHandler("start", start)
echo_handler = MessageHandler(filters.TEXT & (~filters.COMMAND), echo)
3 changes: 0 additions & 3 deletions src/bot/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
from django.db import models

# Create your models here.
3 changes: 0 additions & 3 deletions src/bot/tests.py

This file was deleted.

3 changes: 0 additions & 3 deletions src/bot/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
from django.shortcuts import render

# Create your views here.
5 changes: 4 additions & 1 deletion src/core/asgi_dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

from django.core.asgi import get_asgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.config.settings_dev") # настройки для разработки
os.environ.setdefault(
"DJANGO_SETTINGS_MODULE",
"core.config.settings_dev",
) # настройки для разработки

application = get_asgi_application()
5 changes: 4 additions & 1 deletion src/core/asgi_prod.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

from django.core.asgi import get_asgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.config.settings_prod") # настройки для сервера
os.environ.setdefault(
"DJANGO_SETTINGS_MODULE",
"core.config.settings_prod",
) # настройки для сервера

application = get_asgi_application()
5 changes: 2 additions & 3 deletions src/core/config/settings_base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
from pathlib import Path

import environ
Expand All @@ -13,7 +12,7 @@

SECRET_KEY = env.str("SECRET_KEY", default=DEFAULT)

ALLOWED_HOSTS = ['*']
ALLOWED_HOSTS = ["*"]

LOCAL_APPS = [
"django.contrib.admin",
Expand All @@ -22,7 +21,7 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
'bot.apps.BotConfig',
"bot.apps.BotConfig",
]

EXTERNAL_APPS = []
Expand Down
6 changes: 4 additions & 2 deletions src/core/config/settings_for_dev.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
"""Settings for development."""

from core.config.settings_base import * # noqa

DEBUG = True

DATABASES = {
"default": {
"ENGINE": env.str(
"POSTGRES_ENGINE", default="django.db.backends.postgresql"
"POSTGRES_ENGINE",
default="django.db.backends.postgresql",
),
"NAME": env.str("POSTGRES_NAME", default="postgres"),
"USER": env.str("POSTGRES_USER", default="postgres"),
"PASSWORD": env.str("POSTGRES_PASSWORD", default="postgres"),
"HOST": env.str("POSTGRES_HOST", default="localhost"),
"PORT": env.str("POSTGRES_PORT", default="5432"),
}
},
}
6 changes: 4 additions & 2 deletions src/core/config/settings_for_prod.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
"""Settings for production."""

from core.config.settings_base import * # noqa

DEBUG = False

DATABASES = {
"default": {
"ENGINE": env.str(
"POSTGRES_ENGINE", default="django.db.backends.postgresql"
"POSTGRES_ENGINE",
default="django.db.backends.postgresql",
),
"NAME": env.str("POSTGRES_NAME", default="postgres"),
"USER": env.str("POSTGRES_USER", default="postgres"),
"PASSWORD": env.str("POSTGRES_PASSWORD", default="postgres"),
"HOST": env.str("POSTGRES_HOST", default="localhost"),
"PORT": env.str("POSTGRES_PORT", default="5432"),
}
},
}
3 changes: 2 additions & 1 deletion src/core/config/settings_for_tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Settings for running tests."""

from core.config.settings_base import * # noqa

DEBUG = False
Expand All @@ -7,5 +8,5 @@
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
},
}
6 changes: 4 additions & 2 deletions src/core/logging/logger.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
def log_errors(f):
"""Decorator to log errors raised by the decorated function."""
"""Log errors decorator raised by the decorated function."""

def inner(*args, **kwargs):
try:
return f(*args, **kwargs)
except Exception as e:
error_message = f'Произошла ошибка: {e}'
error_message = f"Произошла ошибка: {e}"
print(error_message)
raise e

return inner
16 changes: 8 additions & 8 deletions src/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
import environ

env = environ.Env(
DEBUG=(bool, False)
DEBUG=(bool, False),
)

BASE_DIR = Path(__file__).resolve().parent.parent

environ.Env.read_env(os.path.join(BASE_DIR, '.env'))
environ.Env.read_env(os.path.join(BASE_DIR, ".env"))

SECRET_KEY = env('SECRET_KEY')
SECRET_KEY = env("SECRET_KEY")

DEBUG = env('DEBUG')
DEBUG = env("DEBUG")

ALLOWED_HOSTS = env('ALLOWED_HOSTS').split()
ALLOWED_HOSTS = env("ALLOWED_HOSTS").split()

INSTALLED_APPS = [
"django.contrib.admin",
Expand All @@ -24,7 +24,7 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
'bot.apps.BotConfig',
"bot.apps.BotConfig",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -61,7 +61,7 @@
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
},
}

AUTH_PASSWORD_VALIDATORS = [
Expand Down Expand Up @@ -91,4 +91,4 @@

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

TELEGRAM_TOKEN = env('TELEGRAM_TOKEN')
TELEGRAM_TOKEN = env("TELEGRAM_TOKEN")
Loading
Loading