Skip to content

Commit

Permalink
Return ruff as Python code formatter with better settings
Browse files Browse the repository at this point in the history
  • Loading branch information
EXG1O committed Apr 26, 2024
1 parent fbebf36 commit b41b29e
Show file tree
Hide file tree
Showing 80 changed files with 850 additions and 516 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ jobs:
echo "POSTGRESQL_DATABASE_USER=github" >> .env
echo "POSTGRESQL_DATABASE_PASSWORD=password" >> .env
- name: Run ruff
run: ./env/bin/ruff check .

- name: Run mypy
run: ./env/bin/mypy .

Expand Down
3 changes: 1 addition & 2 deletions constructor_telegram_bots/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from constructor_telegram_bots.celery import celery_app


__all__ = ('celery_app',)
__all__ = ('celery_app',)
3 changes: 1 addition & 2 deletions constructor_telegram_bots/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import os


os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'constructor_telegram_bots.settings')

application = get_asgi_application()
application = get_asgi_application()
6 changes: 2 additions & 4 deletions constructor_telegram_bots/authentication.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from django.utils.translation import gettext as _

from rest_framework.authentication import TokenAuthentication
from rest_framework.request import Request
from rest_framework.authtoken.models import Token
from rest_framework.request import Request

from users.models import User

Expand All @@ -29,4 +27,4 @@ def authenticate(self, request: Request) -> tuple[User, str] | None:

return self.authenticate_credentials(token.key)
except Token.DoesNotExist:
return super().authenticate(request)
return super().authenticate(request)
4 changes: 2 additions & 2 deletions constructor_telegram_bots/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
from typing import Any
import os


os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'constructor_telegram_bots.settings')

celery_app = Celery('constructor_telegram_bots')
celery_app.config_from_object('django.conf:settings', namespace='CELERY')
celery_app.autodiscover_tasks(['user', 'telegram_bot'])


@signals.celeryd_after_setup.connect
def celery_after_setup(*args: Any, **kwargs: Any) -> None:
from telegram_bots.tasks import start_telegram_bots

start_telegram_bots.delay()
start_telegram_bots.delay()
3 changes: 1 addition & 2 deletions constructor_telegram_bots/gunicorn.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from constructor_telegram_bots.settings import BASE_DIR


workers = 9
max_requests = 1000
max_requests_jitter = 100

capture_output = True
accesslog = str(BASE_DIR / 'logs/gunicorn_info.log')
errorlog = str(BASE_DIR / 'logs/gunicorn_info.log')
errorlog = str(BASE_DIR / 'logs/gunicorn_info.log')
3 changes: 2 additions & 1 deletion constructor_telegram_bots/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from typing import Any


class LimitOffsetPagination(BaseLimitOffsetPagination):
def get_paginated_response(self, data: list[dict[str, Any]]) -> Response:
return Response({'count': self.count, 'results': data})
return Response({'count': self.count, 'results': data})
25 changes: 13 additions & 12 deletions constructor_telegram_bots/settings.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
from django.utils.translation import gettext_lazy as _

from utils.shortcuts import generate_random_string

import django_stubs_ext

from dotenv import load_dotenv

from utils.shortcuts import generate_random_string

from pathlib import Path
import os
import string
import sys
import os


django_stubs_ext.monkeypatch()
load_dotenv()


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

SECRET_KEY: str = os.getenv('SECRET_KEY', f'django-insecure-{generate_random_string(string.ascii_letters + string.digits, 50)}')
SECRET_KEY: str = os.getenv('SECRET_KEY', f'django-insecure-{generate_random_string(string.ascii_letters + string.digits, 50)}') # fmt: skip
DEBUG: bool = os.getenv('DEBUG', 'True') == 'True'

match sys.argv:
Expand Down Expand Up @@ -45,13 +46,13 @@
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'
CELERY_BEAT_SCHEDULE = {
'update_users_first_and_last_name_schedule' : {
'update_users_first_and_last_name_schedule': {
'task': 'users.tasks.update_users_first_and_last_name',
'schedule': 86400, # 24 ч.
'schedule': 86400, # 24 ч.
},
'check_confirm_code_generation_date_schedule': {
'task': 'users.tasks.check_confirm_code_generation_date',
'schedule': 3600, # 1 ч.
'schedule': 3600, # 1 ч.
},
}

Expand Down Expand Up @@ -84,7 +85,7 @@
'instruction',
'donation',
'privacy_policy',
]
] # fmt: skip

REST_FRAMEWORK = {
'EXCEPTION_HANDLER': 'drf_standardized_errors.handler.exception_handler',
Expand Down Expand Up @@ -213,15 +214,15 @@
'class': 'logging.StreamHandler',
'formatter': 'simple',
},
'django_info_file': {
'django_info_file': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'filename': BASE_DIR / 'logs/django_info.log',
'maxBytes': 10485760,
'backupCount': 10,
'formatter': 'verbose',
},
'django_error_file': {
'django_error_file': {
'level': 'WARNING',
'class': 'logging.handlers.RotatingFileHandler',
'filename': BASE_DIR / 'logs/django_error.log',
Expand All @@ -240,4 +241,4 @@
'propagate': True,
},
},
}
}
39 changes: 22 additions & 17 deletions constructor_telegram_bots/urls.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
from django.urls import path, re_path, include
from django.conf import settings
from django.contrib import admin
from django.views.i18n import JavaScriptCatalog
from django.urls import include, path, re_path
from django.views.generic import TemplateView
from django.conf import settings

from rest_framework.generics import GenericAPIView
from django.views.i18n import JavaScriptCatalog

import django_stubs_ext

from rest_framework.generics import GenericAPIView

django_stubs_ext.monkeypatch(extra_classes=(GenericAPIView,))


urlpatterns = [
path('admin/', admin.site.urls),
path('tinymce/', include('tinymce.urls')),

path('jsi18n/', JavaScriptCatalog.as_view(), name='javascript-catalog'),

path('api/', include(([
path('languages/', include('languages.urls')),
path('users/', include('users.urls')),
path('telegram-bots/', include('telegram_bots.urls')),
path('updates/', include('updates.urls')),
path('donations/', include('donation.urls')),
path('instruction/', include('instruction.urls')),
path('privacy-policy/', include('privacy_policy.urls')),
], 'api'))),
path(
'api/',
include(
(
[
path('languages/', include('languages.urls')),
path('users/', include('users.urls')),
path('telegram-bots/', include('telegram_bots.urls')),
path('updates/', include('updates.urls')),
path('donations/', include('donation.urls')),
path('instruction/', include('instruction.urls')),
path('privacy-policy/', include('privacy_policy.urls')),
],
'api',
)
),
),
]

if settings.DEBUG:
Expand All @@ -35,4 +40,4 @@
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns += [re_path(r'^.*', TemplateView.as_view(template_name='frontend/index.html'))]
urlpatterns += [re_path(r'^.*', TemplateView.as_view(template_name='frontend/index.html'))]
3 changes: 1 addition & 2 deletions constructor_telegram_bots/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import os


os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'constructor_telegram_bots.settings')

application = get_wsgi_application()
application = get_wsgi_application()
10 changes: 6 additions & 4 deletions donation/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from utils.html import format_html_link

from .models import Donation, Section, Button
from .models import Button, Donation, Section


@admin.register(Donation)
Expand All @@ -24,17 +24,19 @@ def sum_display(self, donation: Donation) -> str:
def contact_link_display(self, donation: Donation) -> str:
return format_html_link(donation.contact_link)


@admin.register(Section)
class SectionAdmin(TranslationAdmin): # FIXME: Need to add generics support
class SectionAdmin(TranslationAdmin): # FIXME: Need to add generics support
list_display = ('title', 'position')
fields = ('title', 'text', 'position')
formfield_overrides = {models.TextField: {'widget': TinyMCE}}


@admin.register(Button)
class ButtonAdmin(TranslationAdmin): # FIXME: Need to add generics support
class ButtonAdmin(TranslationAdmin): # FIXME: Need to add generics support
list_display = ('text', 'url_display', 'position')
fields = ('text', 'url', 'position')

@admin.display(description=_('Ссылка'), ordering='url')
def url_display(self, button: Button) -> str:
return format_html_link(button.url)
return format_html_link(button.url)
2 changes: 1 addition & 1 deletion donation/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
class DonationConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'donation'
verbose_name = _('Пожертвования')
verbose_name = _('Пожертвования')
10 changes: 7 additions & 3 deletions donation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ class Meta(TypedModelMeta):
def __str__(self) -> str:
return self.contact_link


def section_position_default() -> int:
section: Section | None = Section.objects.last()
return section.position + 1 if section else 1

class Section(models.Model): # type: ignore [django-manager-missing]

class Section(models.Model): # type: ignore [django-manager-missing]
title = models.CharField(_('Заголовок'), max_length=255)
text = models.TextField(_('Текст'))
position = models.PositiveSmallIntegerField(_('Позиция'), blank=True, default=section_position_default)
Expand All @@ -35,11 +37,13 @@ class Meta(TypedModelMeta):
def __str__(self) -> str:
return self.title


def button_position_default() -> int:
button: Button | None = Button.objects.last()
return button.position + 1 if button else 1

class Button(models.Model): # type: ignore [django-manager-missing]

class Button(models.Model): # type: ignore [django-manager-missing]
text = models.CharField(_('Текст'), max_length=255)
url = models.URLField(_('Ссылка'))
position = models.PositiveSmallIntegerField(_('Позиция'), blank=True, default=button_position_default)
Expand All @@ -50,4 +54,4 @@ class Meta(TypedModelMeta):
verbose_name_plural = _('Кнопки')

def __str__(self) -> str:
return self.text
return self.text
6 changes: 4 additions & 2 deletions donation/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from utils import filters

from .models import Donation, Section, Button
from .models import Button, Donation, Section

from typing import Any

Expand All @@ -18,12 +18,14 @@ def to_representation(self, instance: Donation) -> dict[str, Any]:

return representation


class SectionSerializer(serializers.ModelSerializer[Section]):
class Meta:
model = Section
fields = ('id', 'title', 'text')


class ButtonSerializer(serializers.ModelSerializer[Button]):
class Meta:
model = Button
fields = ('id', 'text', 'url')
fields = ('id', 'text', 'url')
7 changes: 5 additions & 2 deletions donation/tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.test import TestCase
from django.http import HttpResponse
from django.test import TestCase
from django.urls import reverse

from rest_framework.test import APIClient
Expand All @@ -9,23 +9,26 @@ class CustomTestCase(TestCase):
def setUp(self) -> None:
self.client: APIClient = APIClient()


class DonationsAPIViewTests(CustomTestCase):
url: str = reverse('api:donation:index')

def test_get_method(self) -> None:
response: HttpResponse = self.client.get(self.url)
self.assertEqual(response.status_code, 200)


class SectionsAPIViewTests(CustomTestCase):
url: str = reverse('api:donation:sections')

def test_get_method(self) -> None:
response: HttpResponse = self.client.get(self.url)
self.assertEqual(response.status_code, 200)


class ButtonsAPIViewTests(CustomTestCase):
url: str = reverse('api:donation:buttons')

def test_get_method(self) -> None:
response: HttpResponse = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.status_code, 200)
7 changes: 4 additions & 3 deletions donation/translation.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from modeltranslation.translator import register, TranslationOptions
from modeltranslation.translator import TranslationOptions, register

from .models import Section, Button
from .models import Button, Section


@register(Section)
class SectionTranslationOptions(TranslationOptions):
fields = ('title', 'text')


@register(Button)
class ButtonTranslationOptions(TranslationOptions):
fields = ('text',)
fields = ('text',)
Loading

0 comments on commit b41b29e

Please sign in to comment.