Skip to content

Commit

Permalink
www.employees_views: allow to show old approvals
Browse files Browse the repository at this point in the history
  • Loading branch information
xavfernandez committed Sep 13, 2024
1 parent 6b3d3d4 commit 3a0a1e7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
2 changes: 1 addition & 1 deletion itou/templates/approvals/includes/list_card.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ <h3>{{ approval.user.get_full_name }}</h3>
<div class="c-box--results__footer">

<div class="d-flex justify-content-md-end">
<a href="{% url 'employees:detail' public_id=approval.user.public_id %}?back_url={{ request.get_full_path|urlencode }}"
<a href="{% url 'employees:detail' public_id=approval.user.public_id %}?approval={{ approval.pk }}&back_url={{ request.get_full_path|urlencode }}"
class="btn btn-outline-primary btn-block w-100 w-md-auto"
{% matomo_event "salaries" "clic" "details-salarie" %}
aria-label="Voir les informations de {{ approval.user.get_full_name }}">Voir les informations</a>
Expand Down
31 changes: 20 additions & 11 deletions itou/www/employees_views/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import datetime
import logging
import urllib.parse
Expand All @@ -10,6 +11,7 @@
from django.views.generic import DetailView

from itou.approvals.models import (
Approval,
ProlongationRequest,
)
from itou.job_applications.enums import JobApplicationState
Expand Down Expand Up @@ -58,15 +60,19 @@ def get_queryset(self):
)
)

def get_job_application(self, employee):
def get_job_application(self, employee, approval):
if approval:
approval_filter = {"approval": approval}
else:
# To be consistent with previous ApprovalDetailView
# an approval is needed
approval_filter = {"approval__isnull": False}
return (
JobApplication.objects.filter(
job_seeker=employee,
state=JobApplicationState.ACCEPTED,
to_company=self.siae,
# To be consistent with previous ApprovalDetailView
# an approval is needed
approval__isnull=False,
**approval_filter,
)
.select_related(
"approval__user__jobseeker_profile",
Expand All @@ -92,13 +98,16 @@ def get_job_application(self, employee):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["siae"] = self.siae
job_application = self.get_job_application(self.object)
if job_application:
approval = job_application.approval
else:
# This shouldn't be possible except if the job application has been deleted
# in this case, use the last approval
approval = self.object.approvals.order_by("-end_at").first()
approval = None
if approval_pk := self.request.GET.get("approval"):
with contextlib.suppress(ValueError): # Ignore invalid approval parameter value
approval = Approval.objects.filter(user=self.object, pk=int(approval_pk)).first()
job_application = self.get_job_application(self.object, approval)
if approval is None:
if job_application:
approval = job_application.approval
else:
approval = self.object.approvals.order_by("-end_at").first()

context["can_view_personal_information"] = True # SIAE members have access to personal info
context["can_edit_personal_information"] = self.request.user.can_edit_personal_information(self.object)
Expand Down
2 changes: 1 addition & 1 deletion tests/www/approvals_views/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_list_view(self, client):
assert_previous_step(response, reverse("dashboard:index"))

employee_base_url = reverse("employees:detail", kwargs={"public_id": approval.user.public_id})
assertContains(response, f"{employee_base_url}?back_url={urlencode(url)}")
assertContains(response, f"{employee_base_url}?approval={approval.pk}&back_url={urlencode(url)}")
assertContains(response, self.TABS_CLASS)

def test_multiple_approvals_for_the_same_user(self, client):
Expand Down
16 changes: 15 additions & 1 deletion tests/www/employees_views/test_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,24 @@ def test_multiple_approvals(self, client):
new_approval = JobApplicationFactory(
job_seeker=expired_approval.user, to_company=company, with_approval=True
).approval
new_number = format_filters.format_approval_number(new_approval.number)
expired_number = format_filters.format_approval_number(expired_approval.number)
client.force_login(employer)
url = reverse("employees:detail", kwargs={"public_id": new_approval.user.public_id})
response = client.get(url)
assertContains(response, format_filters.format_approval_number(new_approval.number))
assertContains(response, new_number)
assertNotContains(response, expired_number)

# Allow to show previous approvals
response = client.get(f"{url}?approval={expired_approval.pk}")
assertNotContains(response, new_number)
assertContains(response, expired_number)

# Handle invalid values
for invalid_value in [new_approval.pk + expired_approval.pk, "not_a_number"]:
response = client.get(f"{url}?approval={invalid_value}")
assertContains(response, new_number)
assertNotContains(response, expired_number)

@pytest.mark.ignore_unknown_variable_template_error("with_matomo_event")
@freeze_time("2023-04-26")
Expand Down

0 comments on commit 3a0a1e7

Please sign in to comment.