From 93d76ac414b47364d7452a2954dced0b03c644a2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Benjamin=20H=C3=A4ttasch?=
 <benjamin.haettasch@cs.tu-darmstadt.de>
Date: Thu, 24 Oct 2019 02:03:15 +0200
Subject: [PATCH] Improve AK creation workflow

Introduce existing owner select field and dispatching views for POST submitted owner id
Adapt redirection flows
Adapt submission overview template
Introduce Owner edit view
Fix prepopulation issue in AKWish creation view (caused by inheritance)
Adapt template name to role as create and update template
Fix context issue for AK creation view (missing owner attribute)
---
 .../locale/de_DE/LC_MESSAGES/django.po        | 60 ++++++++++-----
 ...select.html => akowner_create_update.html} |  0
 .../AKSubmission/submission_overview.html     | 32 +++++++-
 .../AKSubmission/submit_new_wish.html         |  1 -
 AKSubmission/urls.py                          |  5 +-
 AKSubmission/views.py                         | 77 ++++++++++++++++---
 6 files changed, 140 insertions(+), 35 deletions(-)
 rename AKSubmission/templates/AKSubmission/{akowner_create_select.html => akowner_create_update.html} (100%)

diff --git a/AKSubmission/locale/de_DE/LC_MESSAGES/django.po b/AKSubmission/locale/de_DE/LC_MESSAGES/django.po
index 9c1e1d28..a01a0733 100644
--- a/AKSubmission/locale/de_DE/LC_MESSAGES/django.po
+++ b/AKSubmission/locale/de_DE/LC_MESSAGES/django.po
@@ -8,7 +8,7 @@ msgid ""
 msgstr ""
 "Project-Id-Version: PACKAGE VERSION\n"
 "Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2019-10-23 22:25+0000\n"
+"POT-Creation-Date: 2019-10-23 23:56+0000\n"
 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 "Language-Team: LANGUAGE <LL@li.org>\n"
@@ -22,7 +22,7 @@ msgstr ""
 #: templates/AKSubmission/ak_detail.html:18
 #: templates/AKSubmission/ak_list.html:8 templates/AKSubmission/ak_list.html:13
 #: templates/AKSubmission/ak_list.html:18
-#: templates/AKSubmission/akowner_create_select.html:13
+#: templates/AKSubmission/akowner_create_update.html:13
 #: templates/AKSubmission/submission_overview.html:6
 #: templates/AKSubmission/submission_overview.html:11
 #: templates/AKSubmission/submission_overview.html:15
@@ -92,61 +92,81 @@ msgstr "Diesen AK vorstellen"
 msgid "Reso"
 msgstr "Reso"
 
-#: templates/AKSubmission/akowner_create_select.html:7
-#: templates/AKSubmission/akowner_create_select.html:19
+#: templates/AKSubmission/akowner_create_update.html:7
+#: templates/AKSubmission/akowner_create_update.html:19
 msgid "AK Owner"
-msgstr ""
+msgstr "AK-Leitung"
 
-#: templates/AKSubmission/akowner_create_select.html:25
+#: templates/AKSubmission/akowner_create_update.html:25
 #: templates/AKSubmission/submit_new.html:27
 msgid "Reset"
 msgstr "Zurücksetzen"
 
-#: templates/AKSubmission/akowner_create_select.html:29
+#: templates/AKSubmission/akowner_create_update.html:29
 #: templates/AKSubmission/submit_new.html:31
 msgid "Cancel"
 msgstr "Abbrechen"
 
-#: templates/AKSubmission/akowner_create_select.html:32
+#: templates/AKSubmission/akowner_create_update.html:32
 #: templates/AKSubmission/submit_new.html:34
 msgid "Submit"
 msgstr "Eintragen"
 
-#: templates/AKSubmission/submission_overview.html:17
+#: templates/AKSubmission/submission_overview.html:19
 msgid ""
 "On this page you can see a list of current AKs, change them and add new ones."
 msgstr ""
 "Auf dieser Seite kannst du eine Liste von aktuellen AKs sehen, diese "
 "bearbeiten und neue hinzufügen."
 
-#: templates/AKSubmission/submission_overview.html:20
+#: templates/AKSubmission/submission_overview.html:25
+#: templates/AKSubmission/submit_new_wish.html:5
+#: templates/AKSubmission/submit_new_wish.html:11
+#: templates/AKSubmission/submit_new_wish.html:15
+msgid "New AK Wish"
+msgstr "Neuer AK-Wunsch"
+
+#: templates/AKSubmission/submission_overview.html:29
+msgid "Who"
+msgstr "Wer"
+
+#: templates/AKSubmission/submission_overview.html:32
+msgid "I do not own AKs yet"
+msgstr "Ich leite bisher keine AKs"
+
+#: templates/AKSubmission/submission_overview.html:40
 #: templates/AKSubmission/submit_new.html:7
 #: templates/AKSubmission/submit_new.html:15
 #: templates/AKSubmission/submit_new.html:21
 msgid "New AK"
 msgstr "Neuer AK"
 
-#: templates/AKSubmission/submission_overview.html:21
-#: templates/AKSubmission/submit_new_wish.html:5
-#: templates/AKSubmission/submit_new_wish.html:12
-#: templates/AKSubmission/submit_new_wish.html:16
-msgid "New AK Wish"
-msgstr "Neuer AK-Wunsch"
+#: templates/AKSubmission/submission_overview.html:46
+msgid "Edit Person Info"
+msgstr "Personen-Info bearbeiten"
 
-#: templates/AKSubmission/submission_overview.html:25
+#: templates/AKSubmission/submission_overview.html:53
 msgid "Current AKs"
 msgstr "Aktuelle AKs"
 
-#: views.py:43
+#: views.py:44
 msgid "Wishes"
 msgstr "Wünsche"
 
-#: views.py:43
+#: views.py:44
 msgid "AKs one would like to have"
 msgstr ""
 "AKs die sich gewünscht wurden, aber bei denen noch nicht klar ist, wer sie "
 "macht. Falls du dir das vorstellen kannst, trag dich einfach ein"
 
-#: views.py:106
+#: views.py:105
 msgid "AK successfully created"
 msgstr "AK erfolgreich angelegt"
+
+#: views.py:183
+msgid "Person Info successfully updated"
+msgstr "Personen-Info erfolgreich bearbeitet"
+
+#: views.py:195
+msgid "No user selected"
+msgstr "Keine Person ausgewählt"
diff --git a/AKSubmission/templates/AKSubmission/akowner_create_select.html b/AKSubmission/templates/AKSubmission/akowner_create_update.html
similarity index 100%
rename from AKSubmission/templates/AKSubmission/akowner_create_select.html
rename to AKSubmission/templates/AKSubmission/akowner_create_update.html
diff --git a/AKSubmission/templates/AKSubmission/submission_overview.html b/AKSubmission/templates/AKSubmission/submission_overview.html
index 60eaf2bd..3d502fc1 100644
--- a/AKSubmission/templates/AKSubmission/submission_overview.html
+++ b/AKSubmission/templates/AKSubmission/submission_overview.html
@@ -14,11 +14,39 @@
 {% block content %}
     <h1>{{ event.name }}: {% trans "AK Submission" %}</h1>
 
+    {% include "AKSubmission/messages.html" %}
+
     {% blocktrans %}On this page you can see a list of current AKs, change them and add new ones.{% endblocktrans %}
 
     <div class="jumbotron" style="margin-top:20px;">
-        <a href="{% url 'submit:akowner_select_create' event_slug=event.slug %}" class="btn btn-primary">{% trans "New AK" %}</a>
-        <a href="{% url 'submit:submit_ak_wish' event_slug=event.slug  %}" class="btn btn-info">{% trans "New AK Wish" %}</a>
+        <form method="POST" action="#" class="form-row">
+            {% csrf_token %}
+            <a href="{% url 'submit:submit_ak_wish' event_slug=event.slug  %}" class="btn btn-info">
+                {% trans "New AK Wish" %}
+            </a>
+            <span style="font-size: 1.5em">
+                &nbsp;&nbsp; | &nbsp;
+                <label for="selectOwnerId" class="align-middle d-inline">{% trans "Who" %}:</label>
+            </span>&nbsp;&nbsp;
+            <select name="owner_id" id="selectOwnerId" class="">
+                <option value="-1">{% trans "I do not own AKs yet" %}</option>
+                {% for owner in existingOwners %}
+                    <option value="{{ owner.pk }}">{{ owner }}</option>
+                {% endfor %}
+            </select> &nbsp;&nbsp;
+            <input
+                    type="submit"
+                    class="btn btn-primary"
+                    value="{% trans "New AK" %}"
+                    formaction="{% url 'submit:akowner_select' event_slug=event.slug %}"
+            />&nbsp;&nbsp;
+            <input
+                    type="submit"
+                    class="btn btn-success"
+                    value="{% trans 'Edit Person Info' %}"
+                    formaction="{% url 'submit:akowner_edit_dispatch' event_slug=event.slug %}"
+            />
+        </form>
     </div>
 
 
diff --git a/AKSubmission/templates/AKSubmission/submit_new_wish.html b/AKSubmission/templates/AKSubmission/submit_new_wish.html
index 9f01bade..b0315ab0 100644
--- a/AKSubmission/templates/AKSubmission/submit_new_wish.html
+++ b/AKSubmission/templates/AKSubmission/submit_new_wish.html
@@ -8,7 +8,6 @@
     <li class="breadcrumb-item"><a href="#">AKPlanning</a></li>
     <li class="breadcrumb-item"><a href="#">{{ event.slug }}</a></li>
     <li class="breadcrumb-item"><a href="#">{% trans "AK Submission" %}</a></li>
-    <li class="breadcrumb-item"><a href="#">{{ owner.slug }}</a></li>
     <li class="breadcrumb-item active">{% trans "New AK Wish" %}</li>
 {% endblock %}
 
diff --git a/AKSubmission/urls.py b/AKSubmission/urls.py
index 7f12ffe8..e743db7a 100644
--- a/AKSubmission/urls.py
+++ b/AKSubmission/urls.py
@@ -13,7 +13,10 @@ urlpatterns = [
             path('aks/', views.AKListView.as_view(), name='ak_list'),
             path('aks/category/<int:category_pk>', views.AKListByCategoryView.as_view(), name='ak_list_by_category'),
             path('aks/tag/<int:tag_pk>', views.AKListByTagView.as_view(), name='ak_list_by_tag'),
-            path('owner/', views.AKOwnerSelectCreateView.as_view(), name='akowner_select_create'),
+            path('owner/', views.AKOwnerCreateView.as_view(), name='akowner_create'),
+            path('new/', views.AKOwnerSelectDispatchView.as_view(), name='akowner_select'),
+            path('owner/edit/', views.AKOwnerEditDispatchView.as_view(), name='akowner_edit_dispatch'),
+            path('<slug:slug>/edit/', views.AKOwnerEditView.as_view(), name='akowner_edit'),
             path('<slug:owner_slug>/new/', views.AKSubmissionView.as_view(), name='submit_ak'),
             path('new_wish/', views.AKWishSubmissionView.as_view(), name='submit_ak_wish'),
         ])
diff --git a/AKSubmission/views.py b/AKSubmission/views.py
index ad83df3a..c1cc51ff 100644
--- a/AKSubmission/views.py
+++ b/AKSubmission/views.py
@@ -1,8 +1,10 @@
 from django.contrib import messages
-from django.http import Http404
+from django.http import Http404, HttpResponseRedirect
+from django.shortcuts import get_object_or_404
 from django.urls import reverse_lazy
 from django.utils.translation import gettext_lazy as _
-from django.views.generic import ListView, DetailView, CreateView
+from django.views import View
+from django.views.generic import ListView, DetailView, CreateView, UpdateView
 
 from AKModel.models import AK, AKCategory, AKTag, AKOwner
 from AKModel.models import Event
@@ -42,6 +44,9 @@ class SubmissionOverviewView(FilterByEventSlugMixin, ListView):
             categories.append(({"name":_("Wishes"), "pk": "wish", "description": _("AKs one would like to have")}, ak_wishes))
         context["categories"] = categories
 
+        # Get list of existing owners for event (for AK submission start)
+        context["existingOwners"] = AKOwner.objects.filter(event=self.event)
+
         return context
 
 
@@ -91,16 +96,11 @@ class AKListByTagView(AKListView):
         return super().get_queryset().filter(tags=self.tag)
 
 
-class AKSubmissionView(EventSlugMixin, CreateView):
+class AKAndAKWishSubmissionView(EventSlugMixin, CreateView):
     model = AK
     template_name = 'AKSubmission/submit_new.html'
     form_class = AKForm
 
-    def get_initial(self):
-        initials = super(AKSubmissionView, self).get_initial()
-        initials['owners'] = [AKOwner.get_by_slug(self.kwargs['owner_slug'])]
-        return initials
-
     def get_success_url(self):
         messages.add_message(self.request, messages.SUCCESS, _("AK successfully created"))
         return reverse_lazy('submit:ak_detail', kwargs={'event_slug': self.kwargs['event_slug'], 'pk': self.object.pk})
@@ -123,14 +123,26 @@ class AKSubmissionView(EventSlugMixin, CreateView):
         return super().form_valid(form)
 
 
-class AKWishSubmissionView(AKSubmissionView):
+class AKSubmissionView(AKAndAKWishSubmissionView):
+    def get_initial(self):
+        initials = super(AKAndAKWishSubmissionView, self).get_initial()
+        initials['owners'] = [AKOwner.get_by_slug(self.kwargs['owner_slug'])]
+        return initials
+
+    def get_context_data(self, *, object_list=None, **kwargs):
+        context = super().get_context_data(object_list=object_list, **kwargs)
+        context['owner'] = get_object_or_404(AKOwner, slug=self.kwargs['owner_slug'])
+        return context
+
+
+class AKWishSubmissionView(AKAndAKWishSubmissionView):
     template_name = 'AKSubmission/submit_new_wish.html'
     form_class = AKWishForm
 
 
-class AKOwnerSelectCreateView(EventSlugMixin, CreateView):
+class AKOwnerCreateView(EventSlugMixin, CreateView):
     model = AKOwner
-    template_name = 'AKSubmission/akowner_create_select.html'
+    template_name = 'AKSubmission/akowner_create_update.html'
     form_class = AKOwnerForm
 
     def get_success_url(self):
@@ -144,3 +156,46 @@ class AKOwnerSelectCreateView(EventSlugMixin, CreateView):
         instance.event = Event.get_by_slug(self.kwargs["event_slug"])
 
         return super().form_valid(form)
+
+
+class AKOwnerSelectDispatchView(EventSlugMixin, View):
+    """
+    This view only serves as redirect to prepopulate the owners field in submission create view
+    """
+    def post(self, request, *args, **kwargs):
+        owner_id = request.POST["owner_id"]
+
+        if owner_id == "-1":
+            return HttpResponseRedirect(
+                reverse_lazy('submit:akowner_create', kwargs={'event_slug': kwargs['event_slug']}))
+
+        owner = get_object_or_404(AKOwner, pk=request.POST["owner_id"])
+        return HttpResponseRedirect(
+            reverse_lazy('submit:submit_ak', kwargs={'event_slug': kwargs['event_slug'], 'owner_slug': owner.slug}))
+
+
+class AKOwnerEditView(EventSlugMixin, UpdateView):
+    model = AKOwner
+    template_name = "AKSubmission/akowner_create_update.html"
+    form_class = AKOwnerForm
+
+    def get_success_url(self):
+        messages.add_message(self.request, messages.SUCCESS, _("Person Info successfully updated"))
+        return reverse_lazy('submit:submission_overview', kwargs={'event_slug': self.kwargs['event_slug']})
+
+
+class AKOwnerEditDispatchView(EventSlugMixin, View):
+    """
+    This view only serves as redirect choose the correct edit view
+    """
+    def post(self, request, *args, **kwargs):
+        owner_id = request.POST["owner_id"]
+
+        if owner_id == "-1":
+            messages.add_message(self.request, messages.WARNING, _("No user selected"))
+            return HttpResponseRedirect(
+                reverse_lazy('submit:submission_overview', kwargs={'event_slug': kwargs['event_slug']}))
+
+        owner = get_object_or_404(AKOwner, pk=request.POST["owner_id"])
+        return HttpResponseRedirect(
+            reverse_lazy('submit:akowner_edit', kwargs={'event_slug': kwargs['event_slug'], 'slug': owner.slug}))
-- 
GitLab