multiple improvments:

This commit is contained in:
sinavir 2023-03-06 11:58:53 +01:00
parent 28f65de0c5
commit 4b95292b14
10 changed files with 160 additions and 40 deletions

View file

@ -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

View file

@ -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,
),
),
],
),
]

View file

@ -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}"

View file

@ -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)

View file

@ -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",
),
),
],
),
]

View file

@ -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)]

View file

@ -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}"

View file

@ -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"

View file

@ -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()