From 76f8df70ee391ca402864eb9fac267e1fd486840 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benni=20B=C3=A4rmann?= Date: Tue, 6 Dec 2022 16:19:02 +0000 Subject: [PATCH] added a presave signal for travel model to update own field with foreign key project field --- input/admin.py | 2 +- input/forms.py | 1 + input/migrations/0084_travel_project_end.py | 18 +++++++++++++++ input/migrations/0085_auto_20221205_1820.py | 18 +++++++++++++++ input/models.py | 25 ++++++++++++++++++++- 5 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 input/migrations/0084_travel_project_end.py create mode 100644 input/migrations/0085_auto_20221205_1820.py diff --git a/input/admin.py b/input/admin.py index cce44e1..9c5f954 100755 --- a/input/admin.py +++ b/input/admin.py @@ -32,7 +32,7 @@ class ProjectAdmin(admin.ModelAdmin): search_fields = ('name', 'pid','finance_id', 'realname', 'start', 'end', 'participants_estimated', 'participants_real', 'cost', 'status') list_display = ('name', 'pid','finance_id', 'realname', 'start', 'end', 'participants_estimated', 'participants_real', 'cost', 'status') # action = ['export_as_csv'] - date_hierarchy = 'start' + date_hierarchy = 'end' @admin.register(BusinessCard) diff --git a/input/forms.py b/input/forms.py index 261da35..076b099 100755 --- a/input/forms.py +++ b/input/forms.py @@ -66,6 +66,7 @@ class TravelForm(FdbForm): self.fields['checkin'].required = True self.fields['checkout'].required = True self.fields['hotel'].required = True + self.fields['project_end'].required = False class Meta: model = Travel diff --git a/input/migrations/0084_travel_project_end.py b/input/migrations/0084_travel_project_end.py new file mode 100644 index 0000000..6098047 --- /dev/null +++ b/input/migrations/0084_travel_project_end.py @@ -0,0 +1,18 @@ +# Generated by Django 3.1.2 on 2022-12-05 18:15 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('input', '0083_auto_20221205_0946'), + ] + + operations = [ + migrations.AddField( + model_name='travel', + name='project_end', + field=models.DateField(null=True, verbose_name='Projektende'), + ), + ] diff --git a/input/migrations/0085_auto_20221205_1820.py b/input/migrations/0085_auto_20221205_1820.py new file mode 100644 index 0000000..2cc3038 --- /dev/null +++ b/input/migrations/0085_auto_20221205_1820.py @@ -0,0 +1,18 @@ +# Generated by Django 3.1.2 on 2022-12-05 18:20 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('input', '0084_travel_project_end'), + ] + + operations = [ + migrations.AlterField( + model_name='travel', + name='project_end', + field=models.DateField(blank=True, null=True, verbose_name='Projektende'), + ), + ] diff --git a/input/models.py b/input/models.py index 78d44c4..30d1c02 100755 --- a/input/models.py +++ b/input/models.py @@ -87,9 +87,10 @@ class Project(Volunteer): notes = models.TextField(max_length=1000,null=True,blank=True,verbose_name='Anmerkungen') intern_notes = models.TextField(max_length=1000, blank=True, verbose_name="interne Anmerkungen") + end_mail_send = models.BooleanField(null=True, verbose_name='Endmail versenden') + # the following Fields are not supposed to be edited by users pid = models.CharField(max_length=15, null=True, blank=True) - end_mail_send = models.BooleanField(null=True, verbose_name='Endmail versenden') status = models.CharField(max_length=3,choices=(('RUN', 'läuft'),('END','beendet'),('NOT','nicht stattgefunden')),default='RUN') persons = models.IntegerField(default=1) finance_id = models.CharField(max_length=15, null= True, blank=True) @@ -149,6 +150,8 @@ TRANSPORT_CHOICES = {'BAHN': 'Bahn', PAYEDBY_CHOICES = {'WMDE': 'WMDE', 'REQU': 'Antragstellender Mensch'} +from django.contrib.contenttypes.models import ContentType + class Travel(Volunteer): # project variable is now null true and blank true, which means it can be saved without project id to be later on filled out by admins project = models.ForeignKey(Project, on_delete=models.CASCADE, null=True, blank=True) @@ -164,6 +167,26 @@ class Travel(Volunteer): notes = models.TextField(max_length=1000, blank=True, verbose_name='Anmerkungen') request_url = models.URLField(max_length=2000, verbose_name='Antrag (URL)') intern_notes = models.TextField(max_length=1000, blank=True, verbose_name='interne Anmerkungen') + project_end = models.DateField(blank=True, null=True, verbose_name='Projektende') + # use content type model to get the end date for the project foreign key + + +from django.db.models.signals import pre_save +from django.dispatch import receiver + +@receiver(pre_save, sender=Travel, dispatch_uid="get_project_end") +def getProjectEnd(sender, instance, **kwargs): + #instance.project_end = instance.project.end + instance.project_end = instance.project.end + + +# def save(self,*args,**kwargs): +# '''we generate the autogenerated fields here''' +# # we don't call save with args/kwargs to avoid UNIQUE CONSTRAINT errors +# # but maybe there is a better solution? +# intern_notes +# project_end = self.checkout +# super(Travel, self).save(*args,**kwargs) #abstract base class for Library and IFG class Grant(Extern):