From b6970cdf04ff416993556eb9a6baf6476c2460ae Mon Sep 17 00:00:00 2001 From: Benni Baermann Date: Thu, 14 Jan 2021 15:10:29 +0100 Subject: [PATCH] added ugly function to beautify data --- evapp/migrations/0003_auto_20210114_1357.py | 18 ++++++++++++++++ evapp/models.py | 6 +++++- evapp/views.py | 23 +++++++++++++++++---- 3 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 evapp/migrations/0003_auto_20210114_1357.py diff --git a/evapp/migrations/0003_auto_20210114_1357.py b/evapp/migrations/0003_auto_20210114_1357.py new file mode 100644 index 0000000..8f1fef2 --- /dev/null +++ b/evapp/migrations/0003_auto_20210114_1357.py @@ -0,0 +1,18 @@ +# Generated by Django 3.1.4 on 2021-01-14 13:57 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('evapp', '0002_auto_20201223_1223'), + ] + + operations = [ + migrations.AlterField( + model_name='employee', + name='transponder', + field=models.CharField(choices=[('NORM', 'allgemeiner Transponder'), ('SPECIAL', 'besondere Schließungen (bitte angeben)'), ('NOTRANS', 'Kein Transponder')], default='NORM', max_length=7), + ), + ] diff --git a/evapp/models.py b/evapp/models.py index 77bb5a5..487e221 100644 --- a/evapp/models.py +++ b/evapp/models.py @@ -1,6 +1,10 @@ from django.db import models from multiselectfield import MultiSelectField +# ATTENTION!!! +# No key should be used twice in any of these dicts because of the +# implementation in views.EvaFormView.beautify_data() +# DEPARTMENT_CHOICES = {'PROG': 'Programme', 'SOFT': 'Softwareentwicklung', 'CENT': 'Central', @@ -32,7 +36,7 @@ ACCOUNT_CHOICES = {'OTRSWMDE': 'OTRS (WMDE)', TRANSPONDER_CHOICES = {'NORM': 'allgemeiner Transponder', 'SPECIAL': 'besondere Schließungen (bitte angeben)', - 'NO': 'Kein Transponder',} + 'NOTRANS': 'Kein Transponder',} class Employee(models.Model): # personal data diff --git a/evapp/views.py b/evapp/views.py index 5f3db15..29864f1 100644 --- a/evapp/views.py +++ b/evapp/views.py @@ -1,4 +1,5 @@ from smtplib import SMTPException +import collections from django.views.generic.edit import CreateView from django.urls import reverse @@ -8,7 +9,8 @@ from django.template.loader import get_template from formtools.wizard.views import CookieWizardView from django.shortcuts import render -from .models import Employee +from .models import Employee, DEPARTMENT_CHOICES, LAPTOP_CHOICES, OS_CHOICES,\ + MOBILE_CHOICES, LANG_CHOICES, ACCOUNT_CHOICES, TRANSPONDER_CHOICES from .forms import PersonalForm, WorkingForm, ITForm, OfficeForm, DummyForm from .settings import MAILS, EVA_MAIL @@ -20,12 +22,11 @@ class EvaFormView(CookieWizardView): template_name = 'evapp/employee_form.html' form_list = [PersonalForm, WorkingForm, ITForm, OfficeForm, DummyForm] instance = None - # data = {} # we need this to display all the data in the last step def get_context_data(self, form, **kwargs): context = super().get_context_data(form=form, **kwargs) - context.update({'data': self.get_all_cleaned_data()}) + context.update({'data': self.beautify_data(self.get_all_cleaned_data())}) return context #this makes shure, that we use the same model instance for all steps @@ -56,7 +57,7 @@ class EvaFormView(CookieWizardView): # only the relevant data should be in the context data = self.get_all_cleaned_data() newdata = {k: v for k, v in data.items() if (k in MAILS[department]['DATA'])} - + context = {'data': newdata} try: @@ -73,3 +74,17 @@ class EvaFormView(CookieWizardView): except SMTPException: self.instance.delete() return HttpResponse('Error in sending mails (propably wrong adress?). Data not saved!') + + # use long form for contextdata instead of short form if available + # + # ATTENTION! + # This implementation works only for unique keys over all of these dicts from model.py + # + def beautify_data(self, data): + for k, v in data.items(): + # print(f"v: {v}") + if isinstance(v,collections.Hashable): + if v in TRANSPONDER_CHOICES.keys(): + print(f"found {v} in TRANSPONDER_CHOICES") + data.update({k : TRANSPONDER_CHOICES[v]}) + return data