From 4b95292b14c4b16b09c1c89fcf115ad650ff6e74 Mon Sep 17 00:00:00 2001 From: sinavir Date: Mon, 6 Mar 2023 11:58:53 +0100 Subject: [PATCH] multiple improvments: --- hackens_orga/agent/apps.py | 7 +- hackens_orga/agent/migrations/0001_initial.py | 25 +++++-- hackens_orga/agent/models.py | 2 +- hackens_orga/agent/signals.py | 11 +++ .../budget/migrations/0001_initial.py | 70 +++++++++++++++---- .../budget/migrations/0002_add_permissions.py | 47 +++++++++++++ hackens_orga/budget/models.py | 26 ++++--- hackens_orga/frontend/migrations/__init__.py | 0 hackens_orga/frontend/views.py | 8 +-- hackens_orga/manage.py | 4 +- 10 files changed, 160 insertions(+), 40 deletions(-) create mode 100644 hackens_orga/agent/signals.py create mode 100644 hackens_orga/budget/migrations/0002_add_permissions.py delete mode 100644 hackens_orga/frontend/migrations/__init__.py diff --git a/hackens_orga/agent/apps.py b/hackens_orga/agent/apps.py index f1442df..cfbd7ac 100644 --- a/hackens_orga/agent/apps.py +++ b/hackens_orga/agent/apps.py @@ -2,5 +2,8 @@ from django.apps import AppConfig class AgentConfig(AppConfig): - default_auto_field = 'django.db.models.BigAutoField' - name = 'agent' + default_auto_field = "django.db.models.BigAutoField" + name = "agent" + + def ready(self): + from . import signals diff --git a/hackens_orga/agent/migrations/0001_initial.py b/hackens_orga/agent/migrations/0001_initial.py index 584e0cd..3d0bcb0 100644 --- a/hackens_orga/agent/migrations/0001_initial.py +++ b/hackens_orga/agent/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 3.2.16 on 2023-02-08 00:51 +# Generated by Django 4.1.6 on 2023-02-24 15:18 from django.conf import settings from django.db import migrations, models @@ -15,11 +15,26 @@ class Migration(migrations.Migration): operations = [ migrations.CreateModel( - name='Agent', + name="Agent", fields=[ - ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('name', models.CharField(max_length=255)), - ('user', models.OneToOneField(null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("name", models.CharField(max_length=255)), + ( + "user", + models.OneToOneField( + null=True, + on_delete=django.db.models.deletion.SET_NULL, + to=settings.AUTH_USER_MODEL, + ), + ), ], ), ] diff --git a/hackens_orga/agent/models.py b/hackens_orga/agent/models.py index 0ef5b6e..f144e2f 100644 --- a/hackens_orga/agent/models.py +++ b/hackens_orga/agent/models.py @@ -4,7 +4,7 @@ from django.db import models class Agent(models.Model): name = models.CharField(max_length=255) - user = models.OneToOneField(User, on_delete=models.CASCADE, null=True) + user = models.OneToOneField(User, on_delete=models.SET_NULL, null=True) def __repr__(self): return f"Agent_{self.name}" diff --git a/hackens_orga/agent/signals.py b/hackens_orga/agent/signals.py new file mode 100644 index 0000000..7f75715 --- /dev/null +++ b/hackens_orga/agent/signals.py @@ -0,0 +1,11 @@ +from django.contrib.auth.models import User +from django.db.models.signals import post_save +from django.dispatch import receiver + +from .models import Agent + + +@receiver(post_save, sender=User) +def create_profile(sender, instance, created, **kwargs): + if created: + Agent.objects.create(name=instance.username, user=instance) diff --git a/hackens_orga/budget/migrations/0001_initial.py b/hackens_orga/budget/migrations/0001_initial.py index 1a189b6..66a6f96 100644 --- a/hackens_orga/budget/migrations/0001_initial.py +++ b/hackens_orga/budget/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 3.2.16 on 2023-02-08 00:51 +# Generated by Django 4.1.6 on 2023-03-06 10:01 import datetime from django.db import migrations, models @@ -10,29 +10,69 @@ class Migration(migrations.Migration): initial = True dependencies = [ - ('agent', '0001_initial'), + ("agent", "0001_initial"), ] operations = [ migrations.CreateModel( - name='BudgetGroup', + name="BudgetGroup", fields=[ - ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('name', models.CharField(max_length=255)), - ('description', models.TextField(blank=True)), + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("name", models.CharField(max_length=255, verbose_name="Intitulé")), + ( + "description", + models.TextField(blank=True, verbose_name="Description"), + ), ], ), migrations.CreateModel( - name='BudgetLine', + name="BudgetLine", fields=[ - ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('amount', models.DecimalField(decimal_places=2, max_digits=12)), - ('comment', models.TextField(blank=True)), - ('date', models.DateField(default=datetime.date.today)), - ('facture', models.FileField(blank=True, null=True, upload_to='factures/')), - ('title', models.CharField(max_length=255)), - ('author', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='agent.agent')), - ('group', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='budget.budgetgroup')), + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "amount", + models.DecimalField( + decimal_places=2, max_digits=12, verbose_name="Montant" + ), + ), + ("comment", models.TextField(blank=True, verbose_name="Commentaire")), + ( + "date", + models.DateField(default=datetime.date.today, verbose_name="Date"), + ), + ("title", models.CharField(max_length=255, verbose_name="Libellé")), + ( + "author", + models.ForeignKey( + on_delete=django.db.models.deletion.PROTECT, + to="agent.agent", + verbose_name="Auteur", + ), + ), + ( + "group", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to="budget.budgetgroup", + verbose_name="Budget", + ), + ), ], ), ] diff --git a/hackens_orga/budget/migrations/0002_add_permissions.py b/hackens_orga/budget/migrations/0002_add_permissions.py new file mode 100644 index 0000000..3097e50 --- /dev/null +++ b/hackens_orga/budget/migrations/0002_add_permissions.py @@ -0,0 +1,47 @@ +# Generated by Django 4.1.6 on 2023-02-24 15:03 + +from django.contrib.auth.management import create_permissions +from django.db import migrations, models + + +def apply_migration(apps, schema_editor): + # Create all permissions + for app_config in apps.get_app_configs(): + app_config.models_module = True + create_permissions(app_config, verbosity=0) + app_config.models_module = None + Group = apps.get_model("auth", "Group") + grp, _ = Group.objects.get_or_create(name="hackens_member") + grp.save() + + Permission = apps.get_model("auth", "Permission") + ContentType = apps.get_model("contenttypes", "ContentType") + + BudgetGroup = apps.get_model("budget", "BudgetGroup") + BudgetLine = apps.get_model("budget", "BudgetLine") + + content_type = ContentType.objects.get_for_model(BudgetGroup) + all_permissions = Permission.objects.filter(content_type=content_type) + grp.permissions.add(*all_permissions) + + content_type = ContentType.objects.get_for_model(BudgetLine) + all_permissions = Permission.objects.filter(content_type=content_type) + grp.permissions.add(*all_permissions) + + grp.save() + + +def revert_migration(apps, schema_editor): + Group = apps.get_model("auth", "Group") + Group.objects.filter( + name="hackens_member", + ).delete() + + +class Migration(migrations.Migration): + + dependencies = [ + ("budget", "0001_initial"), + ] + + operations = [migrations.RunPython(apply_migration, revert_migration)] diff --git a/hackens_orga/budget/models.py b/hackens_orga/budget/models.py index a30ef4a..7f6d9af 100644 --- a/hackens_orga/budget/models.py +++ b/hackens_orga/budget/models.py @@ -5,13 +5,16 @@ from django.db import models class BudgetLine(models.Model): - amount = models.DecimalField(max_digits=12, decimal_places=2) - author = models.ForeignKey(Agent, on_delete=models.PROTECT) - comment = models.TextField(blank=True) - date = models.DateField(default=date.today) - facture = models.FileField(upload_to="factures/", null=True, blank=True) - group = models.ForeignKey("BudgetGroup", on_delete=models.CASCADE) - title = models.CharField(max_length=255) + amount = models.DecimalField( + max_digits=12, decimal_places=2, verbose_name="Montant" + ) + author = models.ForeignKey(Agent, on_delete=models.PROTECT, verbose_name="Auteur") + comment = models.TextField(blank=True, verbose_name="Commentaire") + date = models.DateField(default=date.today, verbose_name="Date") + group = models.ForeignKey( + "BudgetGroup", on_delete=models.CASCADE, verbose_name="Budget" + ) + title = models.CharField(max_length=255, verbose_name="Libellé") def __str__(self): return f"{self.title}" @@ -21,11 +24,12 @@ class BudgetLine(models.Model): class BudgetGroup(models.Model): - name = models.CharField(max_length=255) - description = models.TextField(blank=True) + name = models.CharField(max_length=255, verbose_name="Intitulé") + description = models.TextField(blank=True, verbose_name="Description") - def get_total(self): - return sum(i.amount for i in self.budgetline_set.all()) + def get_all_lines(self): + lines = self.budgetline_set.all() + return lines, sum(i.amount for i in lines) def __str__(self): return f"{self.name}" diff --git a/hackens_orga/frontend/migrations/__init__.py b/hackens_orga/frontend/migrations/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/hackens_orga/frontend/views.py b/hackens_orga/frontend/views.py index de4b3b9..6fcbaff 100644 --- a/hackens_orga/frontend/views.py +++ b/hackens_orga/frontend/views.py @@ -2,7 +2,7 @@ Frontend views for the app """ -from agent.model import Agent +from agent.models import Agent from budget.models import BudgetGroup, BudgetLine from django.contrib.auth.mixins import PermissionRequiredMixin from django.contrib.messages.views import SuccessMessageMixin @@ -42,7 +42,7 @@ class BudgetGroupUpdateView(SuccessMessageMixin, PermissionRequiredMixin, Update class BudgetLineCreateView(SuccessMessageMixin, PermissionRequiredMixin, CreateView): model = BudgetLine - fields = ["title", "comment", "amount", "author", "date", "group", "facture"] + fields = ["title", "comment", "amount", "author", "date", "group"] template_name = "frontend/budgetline_create.html" success_message = 'La dépense/recette "%(title)s" a été créée avec succès' permission_required = "budget.add_budgetline" @@ -61,7 +61,7 @@ class BudgetLineCreateView(SuccessMessageMixin, PermissionRequiredMixin, CreateV else: grp = None agents = Agent.objects.filter(user=self.user) - return {"author": agent, "group": grp} + return {"author": agents[0] if len(agents) >= 1 else None, "group": grp} def get_success_url(self): return reverse("frontend:budget") @@ -69,7 +69,7 @@ class BudgetLineCreateView(SuccessMessageMixin, PermissionRequiredMixin, CreateV class BudgetLineUpdateView(SuccessMessageMixin, UpdateView, PermissionRequiredMixin): model = BudgetLine - fields = ["title", "comment", "amount", "author", "date", "group", "facture"] + fields = ["title", "comment", "amount", "author", "date", "group"] template_name = "frontend/budgetline_update.html" success_message = 'La dépense/recette "%(title)s" a été mise à jour avec succès' permission_required = "budget.change_budgetline" diff --git a/hackens_orga/manage.py b/hackens_orga/manage.py index f1290e6..8b01882 100755 --- a/hackens_orga/manage.py +++ b/hackens_orga/manage.py @@ -6,7 +6,7 @@ import sys def main(): """Run administrative tasks.""" - os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hackens_orga.settings') + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hackens_orga.settings") try: from django.core.management import execute_from_command_line except ImportError as exc: @@ -18,5 +18,5 @@ def main(): execute_from_command_line(sys.argv) -if __name__ == '__main__': +if __name__ == "__main__": main()