1

add flake8 for python

This commit is contained in:
Motiejus Jakštys 2024-01-15 09:09:39 +02:00
parent 94626bb28b
commit 7db77e5b24
8 changed files with 39 additions and 19 deletions

View File

@ -34,8 +34,8 @@ if not environ.get("E11SYNC_DEBUG_PRINTED"):
print("DATABASE_PATH={}".format(_DATABASE_PATH)) print("DATABASE_PATH={}".format(_DATABASE_PATH))
environ["E11SYNC_DEBUG_PRINTED"] = "1" environ["E11SYNC_DEBUG_PRINTED"] = "1"
######################################## #######################################
## No more side effects after this place # No more side effects after this place
SECRET_KEY = _SECRET_KEY SECRET_KEY = _SECRET_KEY

View File

@ -22,6 +22,7 @@ from django.conf import settings
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
] + [ ] + [
path("__debug__/", include("debug_toolbar.urls"))] if settings.DEBUG else [] + [ path("__debug__/", include("debug_toolbar.urls"))
] if settings.DEBUG else [] + [
path('', include('signup.urls')), path('', include('signup.urls')),
] ]

View File

@ -3,6 +3,7 @@
import os import os
import sys import sys
def main(): def main():
"""Run administrative tasks.""" """Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'e11sync.settings') os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'e11sync.settings')

View File

@ -2,9 +2,16 @@ from django.contrib import admin
from .models import Signup from .models import Signup
@admin.register(Signup) @admin.register(Signup)
class SignupAdmin(admin.ModelAdmin): class SignupAdmin(admin.ModelAdmin):
_all_fields = ["email", "created_at", "anonymized_ip", "user_agent", "geoip"] _all_fields = [
"email",
"created_at",
"anonymized_ip",
"user_agent",
"geoip"
]
date_hierarchy = "created_at" date_hierarchy = "created_at"
list_display = ["email", "created_at", "user_agent"] + ["place_name"] list_display = ["email", "created_at", "user_agent"] + ["place_name"]

View File

@ -1,15 +1,16 @@
from django.db import models from django.db import models
class Signup(models.Model): class Signup(models.Model):
email = models.EmailField() email = models.EmailField()
created_at = models.DateTimeField(auto_now_add = True) created_at = models.DateTimeField(auto_now_add=True)
anonymized_ip = models.GenericIPAddressField() anonymized_ip = models.GenericIPAddressField()
user_agent = models.CharField(max_length = 255) user_agent = models.CharField(max_length=255)
geoip = models.JSONField(null = True, blank = True) geoip = models.JSONField(null=True, blank=True)
class Meta: class Meta:
constraints = [ constraints = [
models.UniqueConstraint(name = "unique_email", fields = ["email"]), models.UniqueConstraint(name="unique_email", fields=["email"]),
] ]
def __str__(self): def __str__(self):

View File

@ -3,6 +3,7 @@ from django.urls import reverse
from .models import Signup from .models import Signup
class SignupViewTest(TestCase): class SignupViewTest(TestCase):
def setUp(self): def setUp(self):
self.client = Client() self.client = Client()
@ -14,9 +15,9 @@ class SignupViewTest(TestCase):
def test_ok_signup(self): def test_ok_signup(self):
resp = self.client.post(reverse("index"), {"email": "foo@example.com"}, resp = self.client.post(reverse("index"), {"email": "foo@example.com"},
follow = True, follow=True,
HTTP_USER_AGENT = "foo-agent", HTTP_USER_AGENT="foo-agent",
REMOTE_ADDR = "127.0.0.2") REMOTE_ADDR="127.0.0.2")
self.assertEqual(resp.redirect_chain, [('/?success=subscribed', 302)]) self.assertEqual(resp.redirect_chain, [('/?success=subscribed', 302)])
self.assertContains(resp, "You are now subscribed!") self.assertContains(resp, "You are now subscribed!")
obj = Signup.objects.get() obj = Signup.objects.get()

View File

@ -31,10 +31,10 @@ def index(request):
geoip = None geoip = None
s = Signup( s = Signup(
email = request.POST.get("email"), email=request.POST.get("email"),
anonymized_ip = anonymous_ip, anonymized_ip=anonymous_ip,
user_agent = request.META["HTTP_USER_AGENT"], user_agent=request.META["HTTP_USER_AGENT"],
geoip = geoip, geoip=geoip,
) )
try: try:
@ -42,7 +42,7 @@ def index(request):
except ValidationError as e: except ValidationError as e:
print(e) print(e)
return render(request, "signup/index.html", return render(request, "signup/index.html",
{"error_message": e.message}) {"error_message": e.message})
logging.info("registering email={}".format(request.POST.get("email"))) logging.info("registering email={}".format(request.POST.get("email")))
try: try:
@ -52,7 +52,7 @@ def index(request):
return redirect(reverse("index") + "?success=already_subscribed") return redirect(reverse("index") + "?success=already_subscribed")
except DatabaseError: except DatabaseError:
logger.exception("database error when registering an email") logger.exception("database error when registering an email")
err = "Sorry, database error. Please come back later." err = "Sorry, database error. Please come back later."
return render(request, "signup/index.html", return render(request, "signup/index.html",
{"error_message": err}) {"error_message": err})
return redirect(reverse("index") + "?success=subscribed") return redirect(reverse("index") + "?success=subscribed")

View File

@ -70,6 +70,12 @@
statix.enable = true; statix.enable = true;
}; };
}; };
format = pkgs.runCommand "check-format" {} ''
${pkgs.python3Packages.flake8}/bin/flake8 \
--max-line-length 99 \
--exclude ${./.}/app/signup/migrations/ ${./.}
mkdir -p $out
'';
}; };
apps.e11sync-gunicorn = { apps.e11sync-gunicorn = {
@ -83,12 +89,15 @@
devShells.default = pkgs.mkShellNoCC { devShells.default = pkgs.mkShellNoCC {
packages = with pkgs; [ packages = with pkgs; [
python3Packages.django-debug-toolbar
python3Packages.django_5 python3Packages.django_5
python3Packages.django-compressor python3Packages.django-compressor
python3Packages.geoip2 python3Packages.geoip2
libmaxminddb libmaxminddb
dart-sass dart-sass
python3Packages.django-debug-toolbar
python3Packages.flake8
python3Packages.autopep8
]; ];
GEOIP_PATH = "${geoip-archive}"; GEOIP_PATH = "${geoip-archive}";
E11SYNC_DEBUG = "1"; E11SYNC_DEBUG = "1";