From 807ab4c466dec26350b04b31f086f99158670daa Mon Sep 17 00:00:00 2001
From: Frank Poetzsch-Heffter <p-h@katharineum.de>
Date: Tue, 5 Nov 2019 20:13:56 +0100
Subject: [PATCH] check template

---
 biscuit/apps/fibu/filters.py                | 25 ++++++++++++
 biscuit/apps/fibu/models.py                 |  2 +-
 biscuit/apps/fibu/templates/fibu/check.html | 45 +++++++++++++++++++++
 biscuit/apps/fibu/templates/fibu/index.html |  1 -
 biscuit/apps/fibu/urls.py                   |  1 +
 biscuit/apps/fibu/views.py                  | 32 +++++++++++++++
 6 files changed, 104 insertions(+), 2 deletions(-)
 create mode 100644 biscuit/apps/fibu/filters.py
 create mode 100755 biscuit/apps/fibu/templates/fibu/check.html

diff --git a/biscuit/apps/fibu/filters.py b/biscuit/apps/fibu/filters.py
new file mode 100644
index 0000000..54b3417
--- /dev/null
+++ b/biscuit/apps/fibu/filters.py
@@ -0,0 +1,25 @@
+from django.contrib.auth.models import User
+import django_filters
+from .models import Booking
+from django.db.utils import ProgrammingError
+
+
+def get_fibu_users():
+    """ Find all users who sends an AUB """
+    try:
+        fibu_users = Booking.objects.values_list('contact')
+        users = list(User.objects.filter(id__in=fibu_users))
+        # user_ids = [(str(user.id),user.username) for user in users]
+        user_ids = [(str(user.id), user.last_name + ', ' + user.first_name) for user in users]
+        user_ids_sorted = sorted(user_ids, key=lambda user: user[1])
+        return user_ids_sorted
+    except ProgrammingError:
+        return []
+
+
+class BookingFilter(django_filters.FilterSet):
+    contact = django_filters.ChoiceFilter(label='Von', choices=get_fibu_users())
+
+    class Meta:
+        model = Booking
+        fields = ['contact', ]
diff --git a/biscuit/apps/fibu/models.py b/biscuit/apps/fibu/models.py
index 387232c..f58ddaf 100644
--- a/biscuit/apps/fibu/models.py
+++ b/biscuit/apps/fibu/models.py
@@ -50,7 +50,7 @@ class Booking(models.Model):
     description     = models.CharField(max_length=50)
 #    amount          = models.DecimalField(max_digits=10, decimal_places=2)
     planned_amount  = models.IntegerField()
-#    submission_date = models.DateField()
+    submission_date = models.DateField(default='2019-01-01')
 #    payout_number   = models.IntegerField()
 #    booking_date    = models.DateField()
 #    maturity        = models.DateField()
diff --git a/biscuit/apps/fibu/templates/fibu/check.html b/biscuit/apps/fibu/templates/fibu/check.html
new file mode 100755
index 0000000..4828168
--- /dev/null
+++ b/biscuit/apps/fibu/templates/fibu/check.html
@@ -0,0 +1,45 @@
+{% include 'partials/header.html' %}
+
+<main>
+    <h4>Anträge prüfen</h4>
+    <table class="highlight">
+    <thead>
+    <tr>
+        <th>Beschreibung</th>
+        <th class="flushright">Erwarteter Betrag</th>
+        <th>Status</th>
+    </tr>
+    </thead>
+    <tbody>
+        {% for booking in filter.qs %}
+            <tr>
+            <td>{{ booking.description }}</td>
+            <td class="flushright">{{ booking.planned_amount }} €</td>
+            <td><span class="badge new {{ booking.getStatus.style_class }}">{{ booking.getStatus.name }}</span></td>
+            <td>
+                <form action="" method="POST" class="right">
+                    {% csrf_token %}
+                    <input type="hidden" value="{{ booking.id }}" name="aub-id">
+                    {% if booking.status.id != 2 %}
+                        <button type="submit" name="allow"
+                            class="waves-effect waves-light btn-flat btn-flat-large" title="Annehmen">
+                            <i class="material-icons center green-text">check_circle</i>
+                        </button>
+                    {% endif %}
+                    {% if booking.status.id != 3 %}
+                        <button type="submit" name="deny"
+                             class="waves-effect waves-light btn-flat btn-flat-large" title="Ablehnen">
+                             <i class="material-icons center red-text">not_interested</i>
+                        </button>
+                    {% endif %}
+                </form>
+            </td>
+            </tr>
+        {% endfor %}
+    </tbody>
+    </table>
+
+
+ </main>
+
+{% include 'partials/footer.html' %}
diff --git a/biscuit/apps/fibu/templates/fibu/index.html b/biscuit/apps/fibu/templates/fibu/index.html
index 0fccc73..734dab3 100755
--- a/biscuit/apps/fibu/templates/fibu/index.html
+++ b/biscuit/apps/fibu/templates/fibu/index.html
@@ -36,7 +36,6 @@
         {% endfor %}
     </tbody>
     </table>
-    <ul class="collection">
     {% endblock %}
 </main>
 {% include 'partials/footer.html' %}
diff --git a/biscuit/apps/fibu/urls.py b/biscuit/apps/fibu/urls.py
index d3e5ee3..3e69546 100755
--- a/biscuit/apps/fibu/urls.py
+++ b/biscuit/apps/fibu/urls.py
@@ -4,6 +4,7 @@ from . import views
 
 urlpatterns = [
     path('', views.index, name='fibu_index'),
+    path('check', views.check, name='booking_check'),
     # 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 b73d8fd..fc33108 100644
--- a/biscuit/apps/fibu/views.py
+++ b/biscuit/apps/fibu/views.py
@@ -1,6 +1,7 @@
 from django.contrib.auth.decorators import login_required, permission_required
 from django.shortcuts import render, redirect, get_object_or_404
 from .models import Booking
+from .filters import BookingFilter
 from .forms import MakeBookingForm
 
 
@@ -38,3 +39,34 @@ def index(request):
         return redirect('fibu_index')
     context = {'bookings': items, 'form': form}
     return render(request, 'fibu/index.html', context)
+
+
+
+
+@login_required
+# @permission_required('fibu.check_booking')
+def check(request):
+    if request.method == 'POST':
+        if 'booking-id' in request.POST:
+            booking_id = request.POST['booking-id']
+            booking = Booking.objects.get(id=booking_id)
+            if 'allow' in request.POST:
+                Booking.objects.filter(id=booking_id).update(status=1)
+            elif 'deny' in request.POST:
+                Booking.objects.filter(id=booking_id).update(status=3)
+                # Notify user
+                # register_notification(title="Ihr Antrag auf Unterrichtsbefreiung wurde abgelehnt",
+                #                       description="Ihr Antrag auf Unterrichtsbefreiung vom {}, {} Uhr bis {}, {} Uhr wurde von der "
+                #                                   "Schulleitung abgelehnt. Für weitere Informationen kontaktieren Sie "
+                #                                   "bitte die Schulleitung."
+                #                       .format(formats.date_format(aub.from_date),
+                #                               formats.time_format(aub.from_time),
+                #                               formats.date_format(aub.to_date),
+                #                               formats.time_format(aub.to_time)),
+                #                       app=AubConfig.verbose_name, user=aub.created_by,
+                #                       link=request.build_absolute_uri(reverse('aub_details', args=[aub.id]))
+                #                       )
+
+    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})
-- 
GitLab