diff --git a/accounts/admin.py b/accounts/admin.py index 6d53b53..4753f4b 100644 --- a/accounts/admin.py +++ b/accounts/admin.py @@ -1,6 +1,13 @@ from django.contrib import admin -from django.contrib.auth.admin import UserAdmin +from django.contrib.auth.admin import UserAdmin as BaseUserAdmin +from django.utils.translation import gettext_lazy as _ -from .models import User +from . import models -admin.site.register(User, UserAdmin) + +@admin.register(models.User) +class UserAdmin(BaseUserAdmin): + fieldsets = ( + *BaseUserAdmin.fieldsets, + (_("Subjects"), {"fields": ("subjects",)}) + ) diff --git a/accounts/migrations/0003_user_subjects.py b/accounts/migrations/0003_user_subjects.py new file mode 100644 index 0000000..cbca3b6 --- /dev/null +++ b/accounts/migrations/0003_user_subjects.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0.1 on 2024-01-29 21:06 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("accounts", "0002_alter_user_email_alter_user_first_name_and_more"), + ("subjects", "0003_alter_subject_options"), + ] + + operations = [ + migrations.AddField( + model_name="user", + name="subjects", + field=models.ManyToManyField(to="subjects.subject"), + ), + ] diff --git a/accounts/migrations/0004_alter_user_subjects.py b/accounts/migrations/0004_alter_user_subjects.py new file mode 100644 index 0000000..56a21b5 --- /dev/null +++ b/accounts/migrations/0004_alter_user_subjects.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0.1 on 2024-01-29 21:19 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("accounts", "0003_user_subjects"), + ("subjects", "0003_alter_subject_options"), + ] + + operations = [ + migrations.AlterField( + model_name="user", + name="subjects", + field=models.ManyToManyField(blank=True, to="subjects.subject"), + ), + ] diff --git a/accounts/models.py b/accounts/models.py index dd9fec3..1406cf0 100644 --- a/accounts/models.py +++ b/accounts/models.py @@ -2,6 +2,8 @@ from django.contrib.auth.models import AbstractUser from django.db import models from django.utils.translation import gettext_lazy as _ +from subjects.models import Subject + class User(AbstractUser): REQUIRED_FIELDS = ["email", "first_name", "last_name"] @@ -10,5 +12,7 @@ class User(AbstractUser): last_name = models.CharField(_("last name"), max_length=150) email = models.EmailField(_("email address")) + subjects = models.ManyToManyField(Subject, blank=True) + class Meta(AbstractUser.Meta): - pass + ... diff --git a/accounts/templates/account/me.html b/accounts/templates/account/me.html index 75218d1..db158ef 100644 --- a/accounts/templates/account/me.html +++ b/accounts/templates/account/me.html @@ -4,8 +4,8 @@ {% block title %}{% trans "About Me" %}{% endblock %} {% block content %} -

{% trans "About Me" %}

-
+

{% trans "About Me" %}

+
{% blocktrans trimmed with firstname=request.user.first_name lastname=request.user.last_name %}Welcome, {{ firstname }} {{ lastname }}!{% endblocktrans %} @@ -19,4 +19,14 @@
+ {% if request.user.subjects.exists %} +
+
{% trans "Linked subjects" %}
+ +
+ {% endif %} {% endblock %} diff --git a/locale/cs/LC_MESSAGES/django.po b/locale/cs/LC_MESSAGES/django.po index dae3bcb..dfd218a 100644 --- a/locale/cs/LC_MESSAGES/django.po +++ b/locale/cs/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-01-29 20:46+0000\n" +"POT-Creation-Date: 2024-02-01 20:43+0000\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -18,25 +18,31 @@ msgstr "" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=4; plural=(n == 1 && n % 1 == 0) ? 0 : (n >= 2 && n " "<= 4 && n % 1 == 0) ? 1: (n % 1 != 0 ) ? 2 : 3;\n" + +#: accounts/admin.py:12 subjects/models.py:9 +#: subjects/templates/subjects/index.html:4 templates/facturio/base.html:20 +msgid "Subjects" +msgstr "Subjekty" + #: accounts/forms.py:17 accounts/templates/account/login.html:6 -#: templates/facturio/base.html:39 +#: templates/facturio/base.html:42 msgid "Login" msgstr "Přihlásit se" #: accounts/forms.py:30 accounts/templates/account/register.html:6 -#: templates/facturio/base.html:42 +#: templates/facturio/base.html:45 msgid "Register" msgstr "Registrovat se" -#: accounts/models.py:9 +#: accounts/models.py:11 msgid "first name" msgstr "křestní jméno" -#: accounts/models.py:10 +#: accounts/models.py:12 msgid "last name" msgstr "přijmení" -#: accounts/models.py:11 +#: accounts/models.py:13 msgid "email address" msgstr "emailová adresa" @@ -59,6 +65,10 @@ msgstr "Uživatelské jméno: %(username)s" msgid "Email: %(email)s" msgstr "Email: %(email)s" +#: accounts/templates/account/me.html:24 +msgid "Linked subjects" +msgstr "Propojené Subjekty" + #: facturio/settings.py:109 msgid "English" msgstr "Angličtina" @@ -92,11 +102,6 @@ msgstr "Vytvořit" msgid "Subject" msgstr "Subjekt" -#: subjects/models.py:9 subjects/templates/subjects/index.html:4 -#: templates/facturio/base.html:19 -msgid "Subjects" -msgstr "Subjekty" - #: subjects/models.py:18 subjects/templates/subjects/index.html:12 msgid "Name" msgstr "Jméno" @@ -129,14 +134,26 @@ msgstr "Vytvořit Subjekty" msgid "Add new" msgstr "Přidat nový" +#: subjects/templates/subjects/index.html:18 +msgid "Connect" +msgstr "Propojit" + +#: subjects/templates/subjects/index.html:35 +msgid "Link" +msgstr "Propojit" + +#: subjects/templates/subjects/index.html:38 +msgid "Unlink" +msgstr "Odpojit" + #: templates/facturio/base.html:8 templates/facturio/index.html:4 msgid "App" msgstr "Aplikace" -#: templates/facturio/base.html:31 +#: templates/facturio/base.html:34 msgid "Profile" msgstr "Profil" -#: templates/facturio/base.html:34 +#: templates/facturio/base.html:37 msgid "Logout" msgstr "Odhlásit se" diff --git a/subjects/migrations/0003_alter_subject_options.py b/subjects/migrations/0003_alter_subject_options.py new file mode 100644 index 0000000..2a18129 --- /dev/null +++ b/subjects/migrations/0003_alter_subject_options.py @@ -0,0 +1,16 @@ +# Generated by Django 5.0.1 on 2024-01-29 21:06 + +from django.db import migrations + + +class Migration(migrations.Migration): + dependencies = [ + ("subjects", "0002_alter_subject_city_alter_subject_city_part_and_more"), + ] + + operations = [ + migrations.AlterModelOptions( + name="subject", + options={"verbose_name": "Subject", "verbose_name_plural": "Subjects"}, + ), + ] diff --git a/subjects/models.py b/subjects/models.py index e9ea42b..1a82855 100644 --- a/subjects/models.py +++ b/subjects/models.py @@ -50,3 +50,6 @@ class Subject(models.Model): def __str__(self): return self.name + + def get_linked_users(self) -> list[int]: + return list(self.user_set.values_list("id", flat=True)) diff --git a/subjects/templates/subjects/index.html b/subjects/templates/subjects/index.html index eac8df0..5e673b0 100644 --- a/subjects/templates/subjects/index.html +++ b/subjects/templates/subjects/index.html @@ -15,19 +15,32 @@ {% trans "Zip Code" %} {% trans "City" %} {% trans "City part" %} + {% trans "Connect" %} {% for subject in subjects %} - - {{ subject.id }} - {{ subject.name }} - {{ subject.vat_id }} - {{ subject.street }} - {{ subject.zip_code }} - {{ subject.city }} - {{ subject.city_part }} - + {# FIXME: in the future, or when this create problems, find a better way#} + {% with linked_users=subject.get_linked_users %} + + {{ subject.id }} + {{ subject.name }} + {{ subject.vat_id }} + {{ subject.street }} + {{ subject.zip_code }} + {{ subject.city }} + {{ subject.city_part }} + + {% if request.user.id not in linked_users %} + {% trans "Link" %} + {% else %} + {% trans "Unlink" %} + {% endif %} + + + {% endwith %} {% endfor %} diff --git a/subjects/urls.py b/subjects/urls.py index 799d82a..0c086c3 100644 --- a/subjects/urls.py +++ b/subjects/urls.py @@ -6,5 +6,7 @@ app_name = "subjects" urlpatterns = [ path("", views.main_page, name="list"), - path("new/", views.create_subject, name="create") + path("new/", views.create_subject, name="create"), + path("link/", views.link_subject, name="link"), + path("unlink/", views.unlink_subject, name="unlink") ] diff --git a/subjects/views.py b/subjects/views.py index 0c8cab5..067e94e 100644 --- a/subjects/views.py +++ b/subjects/views.py @@ -1,4 +1,5 @@ from ares_util.ares import call_ares +from django.contrib.auth.decorators import login_required from django.http import HttpRequest, HttpResponse from django.shortcuts import render, redirect from django.urls import reverse @@ -10,11 +11,13 @@ def build_address(street: str, zip_code: int | str, city: str, city_part: str) - return f"{street}, {zip_code}, {city} - {city_part}" +@login_required def main_page(req: HttpRequest) -> HttpResponse: subjects = models.Subject.objects.all() return render(req, "subjects/index.html", dict(subjects=subjects)) +@login_required def create_subject(req: HttpRequest) -> HttpResponse: if req.method == "POST": form = forms.CreateSubjectForm(data=req.POST) @@ -45,3 +48,17 @@ def create_subject(req: HttpRequest) -> HttpResponse: return render(req, "subjects/create.html", dict(form=form)) return HttpResponse(status=405) + + +@login_required +def link_subject(request: HttpRequest, subject_id: int) -> HttpResponse: + subject = models.Subject.objects.get(pk=subject_id) + subject.user_set.add(request.user) + return redirect(reverse("subjects:list")) + + +@login_required +def unlink_subject(request: HttpRequest, subject_id: int) -> HttpResponse: + subject = models.Subject.objects.get(pk=subject_id) + subject.user_set.remove(request.user) + return redirect(reverse("subjects:list")) diff --git a/templates/facturio/base.html b/templates/facturio/base.html index 71039a1..929fbb6 100644 --- a/templates/facturio/base.html +++ b/templates/facturio/base.html @@ -13,16 +13,19 @@