Skip to content

Commit

Permalink
control user stage with marker and not current stage
Browse files Browse the repository at this point in the history
Allow users to be on different stages based on the stage value in marker
and not controlled by the value in the current stage table. Means you
can have users doing first mark and audit at the same time.
  • Loading branch information
struan committed Oct 30, 2023
1 parent f29371e commit a63ea8a
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 8 deletions.
51 changes: 50 additions & 1 deletion crowdsourcer/tests/test_audit_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.test import TestCase
from django.urls import reverse

from crowdsourcer.models import Assigned, Question, Response, ResponseType
from crowdsourcer.models import Assigned, Marker, Question, Response, ResponseType


class BaseTestCase(TestCase):
Expand Down Expand Up @@ -62,6 +62,55 @@ def test_basics(self):
self.assertEqual(second["total"], 1)
self.assertEqual(second["complete"], 0)

def test_non_audit_assigned_user(self):
rt = ResponseType.objects.get(type="First Mark")
u = User.objects.get(username="marker")
m, _ = Marker.objects.get_or_create(user=u)
m.response_type = rt
m.save()

self.client.force_login(u)
self.user = u

response = self.client.get("/")
self.assertEqual(response.status_code, 200)

context = response.context
assignments = context["progress"]

self.assertEqual(len(assignments), 2)
first = assignments[0]

self.assertEqual(context["section_link"], "section_authorities")
self.assertEqual(first["assignment"].section.title, "Planning & Land Use")

url = reverse(
"authority_question_edit", args=("Aberdeenshire Council", "Transport")
)
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
url = reverse("authority_audit", args=("Aberdeenshire Council", "Transport"))
response = self.client.get(url)
self.assertEqual(response.status_code, 403)

rt = ResponseType.objects.get(type="Audit")
m.response_type = rt
m.save()

response = self.client.get("/")
self.assertEqual(response.status_code, 200)

context = response.context
self.assertEqual(context["section_link"], "audit_section_authorities")

rt = ResponseType.objects.get(type="Right of Reply")
m.response_type = rt
m.save()

response = self.client.get("/")
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, "/authority_ror_authorities/")


class TestSaveView(BaseTestCase):
fixtures = [
Expand Down
32 changes: 31 additions & 1 deletion crowdsourcer/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
from django.test import TestCase
from django.urls import reverse

from crowdsourcer.models import Assigned, PublicAuthority, Response, Section
from crowdsourcer.models import (
Assigned,
Marker,
PublicAuthority,
Response,
ResponseType,
Section,
)


class TestHomePage(TestCase):
Expand Down Expand Up @@ -63,6 +70,29 @@ def test_basics(self):
self.assertEqual(second["total"], 2)
self.assertEqual(second["complete"], 0)

def test_non_first_mark_assigned_user(self):
rt = ResponseType.objects.get(type="Audit")
u = User.objects.get(username="marker")
m, _ = Marker.objects.get_or_create(user=u)
m.response_type = rt
m.save()

response = self.client.get("/")
self.assertEqual(response.status_code, 200)

context = response.context
self.assertFalse(context["show_users"])

self.assertEqual(context["section_link"], "audit_section_authorities")
progress = context["progress"]

self.assertEqual(len(progress), 2)

first = progress[0]
second = progress[1]
self.assertEqual(first["assignment"].section.title, "Buildings & Heating")
self.assertEqual(second["assignment"].section.title, "Transport")


class TestAssignmentCompletionStats(BaseTestCase):
def test_completion_stats(self):
Expand Down
38 changes: 32 additions & 6 deletions crowdsourcer/views/marking.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,39 @@ class OverviewView(CurrentStageMixin, ListView):
def dispatch(self, request, *args, **kwargs):
user = self.request.user
if hasattr(user, "marker"):
url = None
marker = user.marker
# if a user only handles one council then they will have a council assigned
# directly in the marker object directly show them the sections page as it
# removes a step
if (
marker.response_type.type == "Right of Reply"
and marker.authority is not None
):
url = reverse(
"authority_ror_sections", kwargs={"name": marker.authority.name}
)
return redirect(url)
# some councils have shared back office staff so one person might be doing
# the RoR for multiple councils in which case they need to see the assignment
# screen
elif marker.response_type.type == "Right of Reply":
if Assigned.objects.filter(user=user).exists():
url = reverse("authority_ror_authorities")
return redirect(url)
count = Assigned.objects.filter(user=user).count()
# however if they only have a single assignment still skip the
# assignments screen
if count == 1:
assignment = Assigned.objects.filter(user=user).first()
url = reverse(
"authority_ror_sections",
kwargs={"name": assignment.authority.name},
)
# if they have nothing assigned then it's fine to show then the blank
# no assignments screen so handle everything else
else:
url = reverse("authority_ror_authorities")

if url is not None:
return redirect(url)

return super().dispatch(request, *args, **kwargs)

Expand All @@ -76,7 +96,8 @@ def get_context_data(self, **kwargs):
context["show_login"] = True
return context

context["show_users"] = self.request.user.is_superuser
user = self.request.user
context["show_users"] = user.is_superuser

assignments = (
context["assignments"]
Expand Down Expand Up @@ -130,9 +151,14 @@ def get_context_data(self, **kwargs):

context["progress"] = progress

if self.current_stage.type == "First Mark":
user_stage = self.current_stage.type
if hasattr(user, "marker"):
if user.marker.response_type:
user_stage = user.marker.response_type.type

if user_stage == "First Mark":
section_link = "section_authorities"
elif self.current_stage.type == "Audit":
elif user_stage == "Audit":
section_link = "audit_section_authorities"

context["page_title"] = "Assignments"
Expand Down

0 comments on commit a63ea8a

Please sign in to comment.