Skip to content

Commit

Permalink
fixup! 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 57c1994 commit e2d51eb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 59 deletions.
54 changes: 30 additions & 24 deletions ynr/apps/candidates/views/people.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,18 +596,32 @@ def get_context_data(self, **kwargs):
class ConfirmPersonSplitView(TemplateView):
template_name = "people/confirm_split_person.html"

def post(self, request, *args, **kwargs):
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", {})

# Perform the splitting logic
new_person_data = self.split(person, choices)

# Store the new person data in the session
request.session["new_person_data"] = new_person_data

return redirect("confirm_split_person", person_id=person.id)
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
Expand All @@ -616,6 +630,8 @@ def split(self, person, 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)
Expand All @@ -626,28 +642,18 @@ def split(self, person, choices):
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)
new_person.save()
person.save()
for choice in choices["both"]:
# 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)
# TODO: Do we even need to do this?
for key, value in choice.items():
setattr(person, key, value)
new_person.save()
person.save()

if new_person:
return redirect(
"confirm_split_person",
person_id=person_id,
new_person_id=new_person.id,
)
return redirect("person-view", person_id=person_id)
person.save()
new_person.save()
return new_person


class NewPersonSelectElectionView(LoginRequiredMixin, FormView):
Expand Down
37 changes: 3 additions & 34 deletions ynr/apps/people/templates/people/confirm_split_person.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,9 @@
{% load pipeline %}

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

<h2>Original Person: {{ person.name }}</h2>
<h3>Attributes to Keep</h3>
{% comment %} <ul>
{% for attribute in choices.keep %}
<li>{{ attribute }}: {{ person|attr:attribute }}</li>
{% endfor %}
</ul>
<h2>New Person: <a href="{% url 'person-view' new_person_id %}">{{ new_person_id}}</a></h2>

<h3>Attributes to Move</h3>
<ul>
{% for attribute in choices.move %}
<li>{{ attribute }}: {{ person|attr:attribute }}</li>
{% endfor %}
</ul>

<h3>Attributes to Keep and Move</h3>
<ul>
{% for attribute in choices.both %}
<li>{{ attribute }}: {{ person|attr:attribute }}</li>
{% endfor %}
</ul>

<h2>New Person</h2>
<ul>
{% for key, value in new_person_data.items %}
<li>{{ key }}: {{ value }}</li>
{% endfor %}
</ul>

<form method="post">
{% csrf_token %}
<button type="submit">Confirm</button>
<a href="{% url 'split_person' person_id=person.id %}">Cancel</a>
</form> {% endcomment %}
{% 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 e2d51eb

Please sign in to comment.