Skip to content

Commit

Permalink
WIP Confirm split person changes
Browse files Browse the repository at this point in the history
  • Loading branch information
VirginiaDooley committed May 30, 2024
1 parent 74bc6f5 commit 91cead4
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
63 changes: 63 additions & 0 deletions ynr/apps/candidates/views/people.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,69 @@ def get_context_data(self, **kwargs):
return context


class ConfirmPersonSplitView(TemplateView):
template_name = "people/confirm_split_person.html"

def get_success_url(self, person_id, new_person_id=None):
person_id = self.kwargs.get("person_id")
if new_person_id:
return reverse(
"confirm_split_person",
kwargs={"person_id": person_id, "new_person_id": new_person_id},
)
return reverse("confirm_split_person", kwargs={"person_id": person_id})

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
person_id = kwargs.get("person_id")
context["person"] = get_object_or_404(Person, pk=person_id)
return context

def post(self, request, *args, **kwargs):
person_id = self.request.session.get("person_id")
person = get_object_or_404(Person, pk=person_id)
choices = request.session.get("choices", {})
new_person_data = self.split(person, choices)
new_person_id = new_person_data.id if new_person_data else None
return redirect(
self.get_success_url(
person_id=person_id, new_person_id=new_person_id
)
)

def split(self, person, choices):
person_id = person.id
new_person = None
if not choices:
# TODO: ADD A MESSAGE TO SAY THAT NO CHOICES WERE MADE?
# TODO: Do we even need to do this?
return redirect("person-view", person_id=person_id)
if choices["both"] or choices["move"]:
new_person = Person.objects.create()
for choice in choices["keep"]:
for key, value in choice.items():
setattr(person, key, value)
person.save()
for choice in choices["move"]:
# create a new person with the chosen attribute values
new_person = Person.objects.create()
for key, value in choice.items():
setattr(new_person, key, value)
# remove this attribute from the original person
# TODO: however if this is a required person field, such as name, how can we handle it?
# perhaps we need to add a text input to the form in the previous step
# to allow the user to enter a new value
for key, value in choice.items():
setattr(person, key, None)
for choice in choices["both"]:
# create a new person with the chosen attribute values
for key, value in choice.items():
setattr(new_person, key, value)
person.save()
new_person.save()
return new_person


class NewPersonSelectElectionView(LoginRequiredMixin, FormView):
"""
For when we know new person's name, but not the election they are standing
Expand Down
12 changes: 12 additions & 0 deletions ynr/apps/people/templates/people/confirm_split_person.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends 'base.html' %}
{% load thumbnail %}
{% load static %}
{% load pipeline %}

{% block content %}
<h1>Confirm Split</h1>
<h2>Original Person: <a href="{% url 'person-view' new_person_id %}">{{ person_id }}</a></h2>

<h2>New Person: <a href="{% url 'person-view' new_person_id %}">{{ new_person_id}}</a></h2>

{% endblock %}
2 changes: 1 addition & 1 deletion ynr/apps/people/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
name="review_split_person",
),
re_path(
r"^person/(?P<person_id>\d+)/confirm_split/?$",
r"^person/(?P<person_id>\d+)/confirm_split_person/(?P<new_person_id>\d+)?/?$",
views.ConfirmPersonSplitView.as_view(),
name="confirm_split_person",
),
Expand Down

0 comments on commit 91cead4

Please sign in to comment.