From 4e7ef221e8843c7518d575d42b86c0e2af235019 Mon Sep 17 00:00:00 2001 From: Frank Poetzsch-Heffter <p-h@katharineum.de> Date: Sun, 22 Dec 2019 07:43:39 +0100 Subject: [PATCH] account view, ModelChoiceField of costcenter, modify form layout --- biscuit/apps/fibu/admin.py | 4 +- biscuit/apps/fibu/forms.py | 53 ++++++--- biscuit/apps/fibu/models.py | 19 +++- .../fibu/templates/fibu/account/edit.html | 23 ++++ .../fibu/templates/fibu/account/index.html | 49 +++++++++ .../templates/fibu/{ => booking}/check.html | 0 .../templates/fibu/{ => booking}/edit.html | 0 .../fibu/templates/fibu/costcenter/edit.html | 23 ++++ .../index.html} | 6 +- biscuit/apps/fibu/templates/fibu/index.html | 4 +- biscuit/apps/fibu/urls.py | 5 +- biscuit/apps/fibu/views.py | 104 ++++++++++++++++-- 12 files changed, 249 insertions(+), 41 deletions(-) create mode 100644 biscuit/apps/fibu/templates/fibu/account/edit.html create mode 100755 biscuit/apps/fibu/templates/fibu/account/index.html rename biscuit/apps/fibu/templates/fibu/{ => booking}/check.html (100%) rename biscuit/apps/fibu/templates/fibu/{ => booking}/edit.html (100%) create mode 100644 biscuit/apps/fibu/templates/fibu/costcenter/edit.html rename biscuit/apps/fibu/templates/fibu/{costcenter.html => costcenter/index.html} (95%) diff --git a/biscuit/apps/fibu/admin.py b/biscuit/apps/fibu/admin.py index d67d153..fc3ab85 100644 --- a/biscuit/apps/fibu/admin.py +++ b/biscuit/apps/fibu/admin.py @@ -1,4 +1,6 @@ from django.contrib import admin -from .models import Booking +from .models import Costcenter, Account, Booking +admin.site.register(Costcenter) +admin.site.register(Account) admin.site.register(Booking) diff --git a/biscuit/apps/fibu/forms.py b/biscuit/apps/fibu/forms.py index 155845b..5d12a54 100644 --- a/biscuit/apps/fibu/forms.py +++ b/biscuit/apps/fibu/forms.py @@ -3,17 +3,47 @@ from django.core.exceptions import ValidationError from django.utils import timezone from datetime import datetime from material import Layout, Row, Fieldset -from .models import Booking, Costcenter, SCHOOLYEARLIST +from .models import YEARLIST, Booking, Costcenter, Account + + +class EditBookingForm(forms.ModelForm): + description = forms.CharField(label='Beschreibung - Was soll gekauft werden?') + planned_amount = forms.IntegerField(label='Erwarteter Betrag - Welcher Betrag ist erforderlich (in Euro ohne Komma)?') + justification = forms.CharField(label='Begründung - Begründe ggf. deinen Antrag.', required=False) + + layout = Layout(Row('description', 'planned_amount'), Row('justification')) + + class Meta: + model = Booking + fields = ('id', 'description', 'planned_amount', 'justification') + class EditCostcenterForm(forms.ModelForm): - costcenter = forms.CharField(max_length=30, label='Kostenstelle') - schoolyear = forms.ChoiceField(choices=SCHOOLYEARLIST) + name = forms.CharField(max_length=30, label='Kostenstelle') + year = forms.ChoiceField(choices=YEARLIST, label='Jahr') - layout = Layout(Row(Fieldset('Kostenstelle', 'costcenter'), Fieldset('Schuljahr', 'schoolyear'))) + layout = Layout(Row('name','year')) class Meta: model = Costcenter - fields = ('id', 'costcenter', 'schoolyear') + fields = ('id', 'name', 'year') + + +class EditAccountForm(forms.ModelForm): + costcenterlist = Costcenter.objects.filter() + costcenter_choices = [(x, val.name) for x, val in enumerate(costcenterlist)] +# print('choices:', costcenter_choices) + + name = forms.CharField(max_length=30, label='Buchungskonto') + costcenter = forms.ModelChoiceField(queryset=costcenterlist, label='Kostenstelle') + print('costcenter:', costcenter) + budget = forms.IntegerField(label='Budget') + + layout = Layout(Row('name', 'costcenter', 'budget')) + + class Meta: + model = Account + fields = ('id', 'name', 'costcenter', 'budget') # @@ -54,16 +84,3 @@ class EditCostcenterForm(forms.ModelForm): # fields = ('cost_center', 'description', 'planned_amount') -class EditBookingForm(forms.ModelForm): - description = forms.CharField(label='Was soll gekauft werden?') - planned_amount = forms.IntegerField(label='Welcher Betrag ist dafür erforderlich (Euro)?') - justification = forms.CharField(label='Begründe ggf. deinen Antrag.', required=False) - - layout = Layout(Row(Fieldset('Beschreibung', 'description'), - Fieldset('Erwarteter Betrag', 'planned_amount')), - Row(Fieldset('Begründung (optional)', 'justification'))) - - class Meta: - model = Booking - fields = ('id', 'description', 'planned_amount', 'justification') - diff --git a/biscuit/apps/fibu/models.py b/biscuit/apps/fibu/models.py index 17ebce9..e27c3a1 100644 --- a/biscuit/apps/fibu/models.py +++ b/biscuit/apps/fibu/models.py @@ -1,7 +1,11 @@ from django.db import models from django.contrib.auth.models import User -SCHOOLYEARLIST = ['2019/2020','2020/2021','2021/2022','2022/2023','2023/2024'] +YEARLIST = [(2019,'2019'), + (2020,'2020'), + (2021,'2021'), + (2022,'2022'), + (2023,'2023')] class Status: def __init__(self, name, style_class): @@ -27,7 +31,10 @@ status_choices = [(x, val.name) for x, val in enumerate(status_list)] class Costcenter(models.Model): # Kostenstellen z.B. Schoolträger-konsumtiv, Schulträger-investiv, Elternberein, ... name = models.CharField(max_length=20) - schoolyear = models.CharField(max_length=20) + year = models.IntegerField(default=2019, choices=YEARLIST, verbose_name="Jahr") + def __str__(self): + return self.name + class Meta: permissions = ( ('edit_costcenter', 'Can edit cost center'), @@ -35,16 +42,16 @@ class Costcenter(models.Model): class Account(models.Model): # Buchungskonten, z.B. Fachschaften, Sekretariat, Schulleiter, Kopieren, Tafelnutzung - name = models.CharField(max_length=20) - costcenter = models.ForeignKey(to=Costcenter, on_delete=models.CASCADE) - budget = models.DecimalField(max_digits=9, decimal_places=2) + name = models.CharField(max_length=20, default='') + costcenter = models.ForeignKey(to=Costcenter, on_delete=models.CASCADE, default='') + budget = models.DecimalField(max_digits=9, decimal_places=2, default=0.00) class Meta: permissions = ( ('edit_account', 'Can edit account'), ) class Booking(models.Model): - account = models.ForeignKey(to=Account, on_delete=models.SET_NULL, blank=True, null=True) +# account = models.ForeignKey(to=Account, on_delete=models.SET_NULL, blank=True, null=True) contact = models.ForeignKey(to=User, related_name='bookings', on_delete=models.SET_NULL , verbose_name="Erstellt von", blank=True, null=True) # invoice_date = models.DateField() diff --git a/biscuit/apps/fibu/templates/fibu/account/edit.html b/biscuit/apps/fibu/templates/fibu/account/edit.html new file mode 100644 index 0000000..1bcb6c7 --- /dev/null +++ b/biscuit/apps/fibu/templates/fibu/account/edit.html @@ -0,0 +1,23 @@ +{% include 'partials/header.html' %} +{% load material_form %} + +<main> + + <h4>Buchungskonto bearbeiten</h4> + + <form method="POST"> + {% csrf_token %} + {% form form=form %} + {% endform %} + <button type="submit" class="waves-effect waves-light btn green"> + <i class="material-icons left">send</i> Änderungen übernehmen + </button> + <a href="{% url 'account' %}"> + <button type="button" class="waves-effect waves-light btn grey"> + <i class="material-icons left">cancel</i> Abbrechen + </button> + </a> + </form> + +</main> +{% include 'partials/footer.html' %} diff --git a/biscuit/apps/fibu/templates/fibu/account/index.html b/biscuit/apps/fibu/templates/fibu/account/index.html new file mode 100755 index 0000000..550a6e2 --- /dev/null +++ b/biscuit/apps/fibu/templates/fibu/account/index.html @@ -0,0 +1,49 @@ +{% include 'partials/header.html' %} +{% load material_form %} + +<main> + +{# <a href="{% url 'fibu_make_booking' %}" class="waves-effect waves-light btn green">Neuen Antrag stellen</a>#} + + {% block content %} + + <h4>Buchungskonten</h4> + <form method="POST" style="border-width: 5px; background: #eee;"> + {% csrf_token %} + {% form form=form %} + {% endform %} + <button type="submit" class="waves-effect waves-light btn green right"> + <i class="material-icons left">send</i> Buchungskonto anlegen + </button> + </form> + + <h4>Bestehende Buchungskonten</h4> + <div class="collection"> + {% for account in accounts %} + <div class="collection-item row"> + <span class="col s12 m6">{{ account.name }}</span> + <span class="col s12 m2 right-align">{{ account.costcenter }}</span> + <span class="col s12 m2 right-align">{{ account.budget }}</span> + <span class="col s12 m2 right-align"> + <form action="{% url 'account_edit' account.id %}" class="left"> + {% csrf_token %} + <input type="hidden" value="{{ account.id }}" name="account-id"> + <button type="submit" name="edit" + class="waves-effect waves-light btn-flat btn-flat-medium" title="Bearbeiten"> + <i class="material-icons center green-text">create</i> + </button> + </form> + <form action="" method="POST" class="left"> + {% csrf_token %} + <input type="hidden" value="{{ account.id }}" name="account-id"> + <button type="submit" onclick="return confirm('Wollen Sie das Buchungskonto wirklich löschen?')" name="cancel" class="waves-effect waves-light btn-flat btn-flat-medium" title="Löschen"> + <i class="material-icons center red-text">cancel</i> + </button> + </form> + </span> + </div> + {% endfor %} + </div> + {% endblock %} +</main> +{% include 'partials/footer.html' %} diff --git a/biscuit/apps/fibu/templates/fibu/check.html b/biscuit/apps/fibu/templates/fibu/booking/check.html similarity index 100% rename from biscuit/apps/fibu/templates/fibu/check.html rename to biscuit/apps/fibu/templates/fibu/booking/check.html diff --git a/biscuit/apps/fibu/templates/fibu/edit.html b/biscuit/apps/fibu/templates/fibu/booking/edit.html similarity index 100% rename from biscuit/apps/fibu/templates/fibu/edit.html rename to biscuit/apps/fibu/templates/fibu/booking/edit.html diff --git a/biscuit/apps/fibu/templates/fibu/costcenter/edit.html b/biscuit/apps/fibu/templates/fibu/costcenter/edit.html new file mode 100644 index 0000000..7dc11ec --- /dev/null +++ b/biscuit/apps/fibu/templates/fibu/costcenter/edit.html @@ -0,0 +1,23 @@ +{% include 'partials/header.html' %} +{% load material_form %} + +<main> + + <h4>Kostenstelle bearbeiten</h4> + + <form method="POST"> + {% csrf_token %} + {% form form=form %} + {% endform %} + <button type="submit" class="waves-effect waves-light btn green"> + <i class="material-icons left">send</i> Änderungen übernehmen + </button> + <a href="{% url 'costcenter' %}"> + <button type="button" class="waves-effect waves-light btn grey"> + <i class="material-icons left">cancel</i> Abbrechen + </button> + </a> + </form> + +</main> +{% include 'partials/footer.html' %} diff --git a/biscuit/apps/fibu/templates/fibu/costcenter.html b/biscuit/apps/fibu/templates/fibu/costcenter/index.html similarity index 95% rename from biscuit/apps/fibu/templates/fibu/costcenter.html rename to biscuit/apps/fibu/templates/fibu/costcenter/index.html index 480e15c..ae8e40a 100755 --- a/biscuit/apps/fibu/templates/fibu/costcenter.html +++ b/biscuit/apps/fibu/templates/fibu/costcenter/index.html @@ -8,11 +8,11 @@ {% block content %} <h4>Kostenstellen</h4> - <form method="POST" style="border-width: 5px;"> + <form method="POST" style="border-width: 5px; background: #eee;"> {% csrf_token %} {% form form=form %} {% endform %} - <button type="submit" class="waves-effect waves-light btn green"> + <button type="submit" class="waves-effect waves-light btn green right"> <i class="material-icons left">send</i> Kostenstelle anlegen </button> </form> @@ -22,7 +22,7 @@ {% for costcenter in costcenterlist %} <div class="collection-item row"> <span class="col s12 m8">{{ costcenter.name }}</span> - <span class="col s12 m2 right-align">{{ costcenter.schoolyear }} €</span> + <span class="col s12 m2 right-align">{{ costcenter.year }}</span> <span class="col s12 m2 right-align"> <form action="{% url 'costcenter_edit' costcenter.id %}" class="left"> {% csrf_token %} diff --git a/biscuit/apps/fibu/templates/fibu/index.html b/biscuit/apps/fibu/templates/fibu/index.html index 56aed0b..9e72faa 100755 --- a/biscuit/apps/fibu/templates/fibu/index.html +++ b/biscuit/apps/fibu/templates/fibu/index.html @@ -8,11 +8,11 @@ {% block content %} <h4>Beantragungen von {{ user }}</h4> - <form method="POST" style="border-width: 5px;"> + <form method="POST" style="border-width: 5px; background: #eee"> {% csrf_token %} {% form form=form %} {% endform %} - <button type="submit" class="waves-effect waves-light btn green"> + <button type="submit" class="waves-effect waves-light btn green right"> <i class="material-icons left">send</i> Antrag stellen </button> </form> diff --git a/biscuit/apps/fibu/urls.py b/biscuit/apps/fibu/urls.py index faf15d8..767aa14 100755 --- a/biscuit/apps/fibu/urls.py +++ b/biscuit/apps/fibu/urls.py @@ -5,8 +5,11 @@ from . import views urlpatterns = [ path('', views.index, name='fibu_index'), path('check', views.check, name='booking_check'), - path('costcenter', views.costcenter, name='costcenter'), path('edit/<int:id>', views.edit, name='booking_edit'), + path('costcenter', views.costcenter, name='costcenter'), + path('costcenter/edit/<int:id>', views.costcenter_edit, name='costcenter_edit'), + path('account', views.account, name='account'), + path('account/edit/<int:id>', views.account_edit, name='account_edit'), # path('make_booking', views.make_booking, name='fibu_make_booking'), # path('edit/<int:id>', views.edit, name='booking_edit'), ] diff --git a/biscuit/apps/fibu/views.py b/biscuit/apps/fibu/views.py index a5a11fc..36bc2c2 100644 --- a/biscuit/apps/fibu/views.py +++ b/biscuit/apps/fibu/views.py @@ -1,9 +1,9 @@ from django.contrib.auth.decorators import login_required, permission_required from django.urls import reverse from django.shortcuts import render, redirect, get_object_or_404 -from .models import Booking, Costcenter +from .models import Booking, Costcenter, Account from .filters import BookingFilter -from .forms import EditBookingForm, EditCostcenterForm +from .forms import EditBookingForm, EditCostcenterForm, EditAccountForm @login_required @@ -35,11 +35,10 @@ def index(request): else: form = EditBookingForm() if form.is_valid(): - account = '' description = form.cleaned_data['description'] planned_amount = form.cleaned_data['planned_amount'] justification = form.cleaned_data['justification'] - booking = Booking(account=account, description=description, planned_amount=planned_amount, contact=request.user, justification=justification) + booking = Booking(description=description, planned_amount=planned_amount, contact=request.user, justification=justification) booking.save() # a = Activity(user=request.user, title="Antrag auf Unterrichtsbefreiung gestellt", @@ -102,7 +101,7 @@ def check(request): booking_list = Booking.objects.filter(status=0).order_by('submission_date') bookings = BookingFilter(request.GET, queryset=booking_list) - return render(request, 'fibu/check.html', {'filter': bookings}) + return render(request, 'fibu/booking/check.html', {'filter': bookings}) @login_required @@ -129,8 +128,8 @@ def costcenter(request): form = EditCostcenterForm() if form.is_valid(): name = form.cleaned_data['name'] - schoolyear = form.cleaned_data['schoolyear'] - costcenter = Costcenter(name=name, schoolyear=schoolyear) + year = form.cleaned_data['year'] + costcenter = Costcenter(name=name, year=year) costcenter.save() # a = Activity(user=request.user, title="Antrag auf Unterrichtsbefreiung gestellt", @@ -140,6 +139,91 @@ def costcenter(request): # a.save() # return redirect('fibu_make_booking') return redirect('costcenter') - costcenter = Costcenter.objects.filter() - context = {'costcenter': costcenter, 'form': form} - return render(request, 'fibu/costcenter.html', context) + costcenterlist = Costcenter.objects.filter() + context = {'costcenterlist': costcenterlist, 'form': form} + return render(request, 'fibu/costcenter/index.html', context) + + +@login_required +# @permission_required('aub.apply_for_aub') +def costcenter_edit(request, id): + costcenter = get_object_or_404(Costcenter, id=id) + form = EditCostcenterForm(instance=costcenter) + template = 'fibu/costcenter/edit.html' + if request.method == 'POST': + form = EditCostcenterForm(request.POST, instance=costcenter) + print('\n\n\nBLUBB', form) + if form.is_valid(): + form.save() + # a = Activity(user=request.user, title="Antrag auf Unterrichtsbefreiung verändert", + # description="Sie haben Ihren Antrag auf Unterrichtsbefreiung " + + # "für den Zeitraum von {} bis {} bearbeitet.".format( + # aub.from_date, aub.to_date), app=AubConfig.verbose_name) + # a.save() + + return redirect(reverse('costcenter')) + context = {'form': form} + return render(request, template, context) + +@login_required +#@permission_required('fibu.view_booking') +def account(request): + if request.method == 'POST': + if 'account-id' in request.POST: + account_id = request.POST['account-id'] + account = Account.objects.get(id=account_id) + if 'cancel' in request.POST: + account.delete() +# a = Activity(user=aub_user, title="Antrag auf Unterrichtsbefreiung gelöscht", +# description="Sie haben Ihren Antrag auf Unterrichtsbefreiung " + +# "für den Zeitraum von {} bis {} gelöscht.".format( +# aub.from_date, aub.to_date), app=AubConfig.verbose_name) +# a.save() + print('Eintrag gelöscht') + return redirect('account') + print('Edit-Form erstellt ############# form.is_valid:', form.is_valid()) + form = EditAccountForm(instance=account) + else: + form = EditAccountForm(request.POST or None) + else: + form = EditAccountForm() + if form.is_valid(): + name = form.cleaned_data['name'] + costcenter = form.cleaned_data['costcenter'] + budget = form.cleaned_data['budget'] + account = Account(name=name, costcenter=costcenter, budget=budget) + account.save() + + # a = Activity(user=request.user, title="Antrag auf Unterrichtsbefreiung gestellt", + # description="Sie haben einen Antrag auf Unterrichtsbefreiung " + + # "für den Zeitraum von {} bis {} gestellt.".format( + # aub.from_date, aub.to_date), app=AubConfig.verbose_name) + # a.save() + # return redirect('fibu_make_booking') + return redirect('account') + accounts = Account.objects.filter() + context = {'accounts': accounts, 'form': form} + return render(request, 'fibu/account/index.html', context) + + +@login_required +# @permission_required('aub.apply_for_aub') +def account_edit(request, id): + account = get_object_or_404(Account, id=id) + form = EditAccountForm(instance=account) + template = 'fibu/account/edit.html' + if request.method == 'POST': + form = EditAccountForm(request.POST, instance=account) + print('\n\n\nBLUBB', form) + if form.is_valid(): + form.save() + # a = Activity(user=request.user, title="Antrag auf Unterrichtsbefreiung verändert", + # description="Sie haben Ihren Antrag auf Unterrichtsbefreiung " + + # "für den Zeitraum von {} bis {} bearbeitet.".format( + # aub.from_date, aub.to_date), app=AubConfig.verbose_name) + # a.save() + + return redirect(reverse('account')) + context = {'form': form} + return render(request, template, context) + -- GitLab