From 1a0c4a845c39f174b88c6ab2c724cf9e3c199eda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Krop=C3=A1=C4=8Dek?= Date: Tue, 19 Dec 2023 12:49:34 +0000 Subject: [PATCH] Crispy forms --- account/forms.py | 28 +++++++++++---- ...er_email_alter_user_first_name_and_more.py | 27 +++++++++++++++ account/models.py | 11 +++++- account/templates/account/login.html | 29 ++-------------- account/templates/account/me.html | 9 ++--- account/templates/account/register.html | 7 +++- account/views.py | 27 +++++++++++---- facturio/settings.py | 10 +++++- facturio/urls.py | 9 ++--- poetry.lock | 34 ++++++++++++++++++- pyproject.toml | 2 ++ subjects/__init__.py | 0 subjects/admin.py | 3 ++ subjects/apps.py | 6 ++++ subjects/migrations/__init__.py | 0 subjects/models.py | 3 ++ subjects/tests.py | 3 ++ subjects/views.py | 3 ++ templates/facturio/base.html | 23 ++++++------- templates/facturio/index.html | 2 ++ templates/facturio/invoice.html | 1 - 21 files changed, 172 insertions(+), 65 deletions(-) create mode 100644 account/migrations/0002_alter_user_email_alter_user_first_name_and_more.py create mode 100644 subjects/__init__.py create mode 100644 subjects/admin.py create mode 100644 subjects/apps.py create mode 100644 subjects/migrations/__init__.py create mode 100644 subjects/models.py create mode 100644 subjects/tests.py create mode 100644 subjects/views.py create mode 100644 templates/facturio/index.html diff --git a/account/forms.py b/account/forms.py index 3039979..e85403f 100644 --- a/account/forms.py +++ b/account/forms.py @@ -1,13 +1,29 @@ +from crispy_forms import helper, layout from django.contrib.auth.forms import AuthenticationForm, UserCreationForm +from .models import User + + # from .models import User class LoginForm(AuthenticationForm): - pass + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.helper = helper.FormHelper() + self.helper.form_action = "login" + self.helper.form_method = "post" + self.helper.add_input(layout.Submit('submit', 'Login')) -# -# class RegisterForm(UserCreationForm): -# class Meta(UserCreationForm.Meta): -# model = User -# fields = UserCreationForm.Meta.fields + +class RegisterForm(UserCreationForm): + class Meta: + model = User + fields = UserCreationForm.Meta.fields + ("first_name", "last_name", "email") + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.helper = helper.FormHelper() + self.helper.form_action = "register" + self.helper.form_method = "post" + self.helper.add_input(layout.Submit('submit', 'Register')) diff --git a/account/migrations/0002_alter_user_email_alter_user_first_name_and_more.py b/account/migrations/0002_alter_user_email_alter_user_first_name_and_more.py new file mode 100644 index 0000000..b8f4201 --- /dev/null +++ b/account/migrations/0002_alter_user_email_alter_user_first_name_and_more.py @@ -0,0 +1,27 @@ +# Generated by Django 5.0 on 2023-12-19 11:53 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("account", "0001_initial"), + ] + + operations = [ + migrations.AlterField( + model_name="user", + name="email", + field=models.EmailField(max_length=254, verbose_name="email address"), + ), + migrations.AlterField( + model_name="user", + name="first_name", + field=models.CharField(max_length=150, verbose_name="first name"), + ), + migrations.AlterField( + model_name="user", + name="last_name", + field=models.CharField(max_length=150, verbose_name="last name"), + ), + ] diff --git a/account/models.py b/account/models.py index 3d30525..dd9fec3 100644 --- a/account/models.py +++ b/account/models.py @@ -1,5 +1,14 @@ from django.contrib.auth.models import AbstractUser +from django.db import models +from django.utils.translation import gettext_lazy as _ class User(AbstractUser): - pass + REQUIRED_FIELDS = ["email", "first_name", "last_name"] + + first_name = models.CharField(_("first name"), max_length=150) + last_name = models.CharField(_("last name"), max_length=150) + email = models.EmailField(_("email address")) + + class Meta(AbstractUser.Meta): + pass diff --git a/account/templates/account/login.html b/account/templates/account/login.html index 03e1b73..d5c791a 100644 --- a/account/templates/account/login.html +++ b/account/templates/account/login.html @@ -1,32 +1,9 @@ {% extends "facturio/base.html" %} +{% load crispy_forms_tags %} + {% block title %}Login{% endblock %} {% block content %} -
-
- {% csrf_token %} -
- - -
-
- - -
- -
- {#
#} - {# {% csrf_token %}#} - {#
#} - {# #} - {# #} - {#
#} - {#
#} - {# #} - {# #} - {#
#} - {# #} - {#
#} -
+ {% crispy form form.helper %} {% endblock %} diff --git a/account/templates/account/me.html b/account/templates/account/me.html index 636e701..5887c40 100644 --- a/account/templates/account/me.html +++ b/account/templates/account/me.html @@ -6,10 +6,11 @@

About Me

-
-
Welcome, {{ request.user.username }}!
-

Email: {{ request.user.email }}

-
+
Welcome, {{ request.user.first_name }} {{ request.user.last_name }}!
+
    +
  • Username: {{ request.user.username }}
  • +
  • Email: {{ request.user.email }}
  • +
{% endblock %} diff --git a/account/templates/account/register.html b/account/templates/account/register.html index cd31cfa..932cd08 100644 --- a/account/templates/account/register.html +++ b/account/templates/account/register.html @@ -1,5 +1,10 @@ {% extends "facturio/base.html" %} + +{% load crispy_forms_tags %} + {% block title %}Register{% endblock %} + {% block content %} -

TODO: register

+ {# TODO: neukazovat všechny pičoviny hned najedou#} + {% crispy form form.helper %} {% endblock %} \ No newline at end of file diff --git a/account/views.py b/account/views.py index 43eb29e..5150483 100644 --- a/account/views.py +++ b/account/views.py @@ -1,15 +1,15 @@ from django.conf import settings from django.contrib.auth import login, logout from django.contrib.auth.decorators import login_required -from django.http import HttpRequest, HttpResponse, JsonResponse -from django.shortcuts import redirect, render +from django.http import HttpRequest, HttpResponse +from django.shortcuts import redirect, render, reverse from . import forms def auth_login(req: HttpRequest) -> HttpResponse: if req.method == "POST": - form = forms.LoginForm(req, req.POST) + form = forms.LoginForm(data=req.POST) if not form.is_valid(): return render(req, "account/login.html", dict(form=form)) @@ -21,8 +21,7 @@ def auth_login(req: HttpRequest) -> HttpResponse: return redirect(redirect_url) elif req.method == "GET": - - form = forms.LoginForm(req) + form = forms.LoginForm() return render(req, "account/login.html", dict(form=form)) return HttpResponse(status=405) @@ -35,7 +34,23 @@ def auth_logout(req: HttpRequest) -> HttpResponse: def auth_register(req: HttpRequest) -> HttpResponse: - return render(req, "account/register.html") + if req.user.is_authenticated: + return redirect(reverse("me")) + if req.method == "POST": + form = forms.RegisterForm(data=req.POST) + + if not form.is_valid(): + return render(req, "account/register.html", dict(form=form)) + + user = form.save() + login(req, user) + return redirect(settings.LOGIN_BASE_URL) + + elif req.method == "GET": + form = forms.RegisterForm() + return render(req, "account/register.html", dict(form=form)) + + return HttpResponse(status=405) @login_required diff --git a/facturio/settings.py b/facturio/settings.py index f5a24d8..fbc69af 100644 --- a/facturio/settings.py +++ b/facturio/settings.py @@ -35,7 +35,10 @@ INSTALLED_APPS = [ "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", - "account.apps.AccountConfig" + "crispy_forms", + "crispy_bootstrap5", + "account.apps.AccountConfig", + "subjects.apps.SubjectsConfig" ] MIDDLEWARE = [ @@ -124,3 +127,8 @@ LOGOUT_REDIRECT_URL = f"/" LOGIN_REDIRECT_URL = f"{LOGIN_BASE_URL}/me" AUTH_USER_MODEL = "account.User" + +# Crispy config +CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap5" + +CRISPY_TEMPLATE_PACK = "bootstrap5" diff --git a/facturio/urls.py b/facturio/urls.py index 5f34c30..179664c 100644 --- a/facturio/urls.py +++ b/facturio/urls.py @@ -15,20 +15,17 @@ Including another URLconf 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -# TODO: remove from django.http import HttpRequest, HttpResponse from django.shortcuts import render from django.urls import path, include -# TODO: remove -def demo(req: HttpRequest) -> HttpResponse: - return render(req, "facturio/invoice.html") +def landing_page(req: HttpRequest) -> HttpResponse: + return render(req, "facturio/index.html") urlpatterns = [ - path("", demo, name="main-page"), + path("", landing_page, name="main-page"), path("account/", include("account.urls")), path("admin/", admin.site.urls), ] - diff --git a/poetry.lock b/poetry.lock index 1b9573a..b0d5c30 100644 --- a/poetry.lock +++ b/poetry.lock @@ -14,6 +14,24 @@ files = [ [package.extras] tests = ["mypy (>=0.800)", "pytest", "pytest-asyncio"] +[[package]] +name = "crispy-bootstrap5" +version = "2023.10" +description = "Bootstrap5 template pack for django-crispy-forms" +optional = false +python-versions = ">=3.8" +files = [ + {file = "crispy-bootstrap5-2023.10.tar.gz", hash = "sha256:f16c44f1997310e5a89c0cf230402e7111cc1f942f64fb7e44603958b89b06a1"}, + {file = "crispy_bootstrap5-2023.10-py3-none-any.whl", hash = "sha256:9b5a6c9880f37cd32aa678c0d7576ae60b3e502c444c8712e582f8bd91659afb"}, +] + +[package.dependencies] +django = ">=4.2" +django-crispy-forms = ">=2" + +[package.extras] +test = ["pytest", "pytest-django"] + [[package]] name = "django" version = "5.0" @@ -34,6 +52,20 @@ tzdata = {version = "*", markers = "sys_platform == \"win32\""} argon2 = ["argon2-cffi (>=19.1.0)"] bcrypt = ["bcrypt"] +[[package]] +name = "django-crispy-forms" +version = "2.1" +description = "Best way to have Django DRY forms" +optional = false +python-versions = ">=3.8" +files = [ + {file = "django-crispy-forms-2.1.tar.gz", hash = "sha256:4d7ec431933ad4d4b5c5a6de4a584d24613c347db9ac168723c9aaf63af4bb96"}, + {file = "django_crispy_forms-2.1-py3-none-any.whl", hash = "sha256:d592044771412ae1bd539cc377203aa61d4eebe77fcbc07fbc8f12d3746d4f6b"}, +] + +[package.dependencies] +django = ">=4.2" + [[package]] name = "sqlparse" version = "0.4.4" @@ -64,4 +96,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = "^3.12" -content-hash = "1c6ab57fc89858a0923620d3307e9173662bad9b58d9dde4f567267506e03587" +content-hash = "a3eaa3850f93a5b33da70f1d4831d68ee7593e05e7a0e040de0cc09229be92fd" diff --git a/pyproject.toml b/pyproject.toml index 9c01d30..355cee7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,6 +9,8 @@ readme = "README.md" [tool.poetry.dependencies] python = "^3.12" django = "^5.0" +crispy-bootstrap5 = "^2023.10" +django-crispy-forms = "^2.1" [build-system] diff --git a/subjects/__init__.py b/subjects/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/subjects/admin.py b/subjects/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/subjects/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/subjects/apps.py b/subjects/apps.py new file mode 100644 index 0000000..cb02d70 --- /dev/null +++ b/subjects/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class SubjectsConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "subjects" diff --git a/subjects/migrations/__init__.py b/subjects/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/subjects/models.py b/subjects/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/subjects/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/subjects/tests.py b/subjects/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/subjects/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/subjects/views.py b/subjects/views.py new file mode 100644 index 0000000..727d556 --- /dev/null +++ b/subjects/views.py @@ -0,0 +1,3 @@ +# Create your views here. + +ARES_BASE_URL = "https://wwwinfo.mfcr.cz/cgi-bin/ares/darv_rzp.cgi?ico=27074358&xml=0&ver=1.0.4" diff --git a/templates/facturio/base.html b/templates/facturio/base.html index 9615bd7..e1d98ec 100644 --- a/templates/facturio/base.html +++ b/templates/facturio/base.html @@ -9,26 +9,25 @@