From 480e237438b19db95313d32c07373c194e5c42c4 Mon Sep 17 00:00:00 2001 From: Virginia Dooley Date: Wed, 29 May 2024 20:37:58 +0100 Subject: [PATCH] Complete process for choosing attributes to split --- ynr/apps/candidates/views/people.py | 23 ++++++++------ ynr/apps/people/forms/forms.py | 19 +++++------- .../templates/people/review_split_person.html | 31 +++++++++++++++++++ .../people/templates/people/split_person.html | 21 +++++++------ ynr/apps/people/urls.py | 5 +++ 5 files changed, 69 insertions(+), 30 deletions(-) create mode 100644 ynr/apps/people/templates/people/review_split_person.html diff --git a/ynr/apps/candidates/views/people.py b/ynr/apps/candidates/views/people.py index d5c9a800f..160c588ec 100644 --- a/ynr/apps/candidates/views/people.py +++ b/ynr/apps/candidates/views/people.py @@ -13,9 +13,9 @@ HttpResponsePermanentRedirect, HttpResponseRedirect, ) -from django.shortcuts import get_object_or_404 +from django.shortcuts import get_object_or_404, redirect from django.template.loader import render_to_string -from django.urls import reverse +from django.urls import reverse, reverse_lazy from django.utils.decorators import method_decorator from django.views.decorators.cache import cache_control from django.views.generic import FormView, TemplateView, UpdateView, View @@ -514,6 +514,7 @@ def get_context_data(self, **kwargs): def get_initial_data(self, form_class=None): person = self.get_person() + #: TO DO: Figure out how to return attribute names and values only for those that are not empty return [ {"attribute_name": "name", "attribute_value": person.name}, # {"attribute_name": "image", "attribute_value": person.image.image if person.image else None}, @@ -542,10 +543,8 @@ def get_initial_data(self, form_class=None): ] def get_form(self, form_class=None): - initial_data = self.get_initial_data() - print("Initial Data:", initial_data) return PersonSplitFormSet( - initial=self.get_initial_data(form_class), + initial=self.get_initial_data(), ) def get_person(self): @@ -555,7 +554,7 @@ def get_person(self): def form_valid(self, formset): choices = { "keep": [], - "add": [], + "move": [], "both": [], } for form in formset: @@ -577,14 +576,20 @@ def form_valid(self, formset): def post(self, request, *args, **kwargs): formset = PersonSplitFormSet(request.POST) - print("POST Data:", request.POST) # Print POST data to inspect if formset.is_valid(): return self.form_valid(formset) - - print("Formset Errors:", formset.errors) return self.form_invalid(formset) +class ReviewPersonSplitView(TemplateView): + template_name = "review_split_person.html" + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context["choices"] = self.request.session.get("choices", {}) + return context + + class NewPersonSelectElectionView(LoginRequiredMixin, FormView): """ For when we know new person's name, but not the election they are standing diff --git a/ynr/apps/people/forms/forms.py b/ynr/apps/people/forms/forms.py index 9f7851534..912300bc8 100644 --- a/ynr/apps/people/forms/forms.py +++ b/ynr/apps/people/forms/forms.py @@ -406,19 +406,14 @@ class UpdatePersonForm(BasePersonForm): class PersonSplitForm(forms.Form): + CHOICES = [ + ("keep", "Keep for original person"), + ("move", "Move to a new person"), + ("both", "Do both"), + ] attribute_name = forms.CharField(widget=forms.HiddenInput()) - attribute_value = forms.CharField( - widget=forms.TextInput(attrs={"readonly": "readonly"}) - ) - - choice = forms.ChoiceField( - choices=[ - ("keep", "Keep"), - ("add", "Add"), - ("both", "Both"), - ], - widget=forms.RadioSelect, - ) + attribute_value = forms.CharField(widget=forms.HiddenInput()) + choice = forms.ChoiceField(choices=CHOICES, widget=forms.RadioSelect) PersonSplitFormSet = formset_factory(PersonSplitForm, extra=0) diff --git a/ynr/apps/people/templates/people/review_split_person.html b/ynr/apps/people/templates/people/review_split_person.html new file mode 100644 index 000000000..4c6d33566 --- /dev/null +++ b/ynr/apps/people/templates/people/review_split_person.html @@ -0,0 +1,31 @@ +{% extends 'base.html' %} +{% load thumbnail %} +{% load static %} +{% load pipeline %} + +{% block content %} +

Review Person split choices

+

Keep

+ +

Move

+ +

Both

+ +
+ {% csrf_token %} + +
+ +{% endblock %} diff --git a/ynr/apps/people/templates/people/split_person.html b/ynr/apps/people/templates/people/split_person.html index 1e72fd63d..26e3c3095 100644 --- a/ynr/apps/people/templates/people/split_person.html +++ b/ynr/apps/people/templates/people/split_person.html @@ -29,15 +29,18 @@

Split {{ person.name }} ({{perso {{ form.attribute_name.value }} - {% if form.attribute_name.value == "image" and form.attribute_value.value %} - {% thumbnail form.attribute_value.value "100x100" as im %} - - {% endthumbnail %} - {% elif form.attribute_name.value == "image" and not form.attribute_value.value %} - No image available - {% else %} - {{ form.attribute_value.value }} - {% endif %} + + {% if form.attribute_name.value == "image" %} + {% thumbnail form.attribute_value.value "100x100" as im %} + + {% endthumbnail %} + {% else %} + {{ form.attribute_value.value }} + {% endif %} + + {{ form.attribute_name }} + {{ form.attribute_value }} + {{ form.choice.0 }} {{ form.choice.1 }} {{ form.choice.2 }} diff --git a/ynr/apps/people/urls.py b/ynr/apps/people/urls.py index de6321b1a..01a12124e 100644 --- a/ynr/apps/people/urls.py +++ b/ynr/apps/people/urls.py @@ -52,6 +52,11 @@ views.PersonSplitView.as_view(), name="person-split", ), + re_path( + r"^person/(?P\d+)/review/?$", + views.ReviewPersonSplitView.as_view(), + name="review_split_person", + ), re_path( r"^person/create/select_election$", views.NewPersonSelectElectionView.as_view(),