Skip to content

Commit

Permalink
Complete process for choosing attributes to split
Browse files Browse the repository at this point in the history
  • Loading branch information
VirginiaDooley committed May 30, 2024
1 parent 94a70d7 commit 480e237
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 30 deletions.
23 changes: 14 additions & 9 deletions ynr/apps/candidates/views/people.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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):
Expand All @@ -555,7 +554,7 @@ def get_person(self):
def form_valid(self, formset):
choices = {
"keep": [],
"add": [],
"move": [],
"both": [],
}
for form in formset:
Expand All @@ -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
Expand Down
19 changes: 7 additions & 12 deletions ynr/apps/people/forms/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 31 additions & 0 deletions ynr/apps/people/templates/people/review_split_person.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{% extends 'base.html' %}
{% load thumbnail %}
{% load static %}
{% load pipeline %}

{% block content %}
<h1>Review Person split choices</h1>
<h2>Keep</h2>
<ul>
{% for item in choices.keep %}
<li>{{ item }}</li>
{% endfor %}
</ul>
<h2>Move</h2>
<ul>
{% for item in choices.move %}
<li>{{ item }}</li>
{% endfor %}
</ul>
<h2>Both</h2>
<ul>
{% for item in choices.both %}
<li>{{ item }}</li>
{% endfor %}
</ul>
<form action="{% url 'submit_choices' %}" method="post">
{% csrf_token %}
<button type="submit">Submit Choices</button>
</form>

{% endblock %}
21 changes: 12 additions & 9 deletions ynr/apps/people/templates/people/split_person.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@ <h1>Split <a href="{% url 'person-view' person_id %}">{{ person.name }} ({{perso

<tr>
<td>{{ form.attribute_name.value }}</td>
{% if form.attribute_name.value == "image" and form.attribute_value.value %}
{% thumbnail form.attribute_value.value "100x100" as im %}
<td><img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}"></td>
{% endthumbnail %}
{% elif form.attribute_name.value == "image" and not form.attribute_value.value %}
<td>No image available</td>
{% else %}
<td>{{ form.attribute_value.value }}</td>
{% endif %}
<td>
{% if form.attribute_name.value == "image" %}
{% thumbnail form.attribute_value.value "100x100" as im %}
<img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
{% endthumbnail %}
{% else %}
{{ form.attribute_value.value }}
{% endif %}

{{ form.attribute_name }}
{{ form.attribute_value }}
</td>
<td>{{ form.choice.0 }}</td>
<td>{{ form.choice.1 }}</td>
<td>{{ form.choice.2 }}</td>
Expand Down
5 changes: 5 additions & 0 deletions ynr/apps/people/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
views.PersonSplitView.as_view(),
name="person-split",
),
re_path(
r"^person/(?P<person_id>\d+)/review/?$",
views.ReviewPersonSplitView.as_view(),
name="review_split_person",
),
re_path(
r"^person/create/select_election$",
views.NewPersonSelectElectionView.as_view(),
Expand Down

0 comments on commit 480e237

Please sign in to comment.