Skip to content

Commit

Permalink
switch to using middleware for setting session details
Browse files Browse the repository at this point in the history
  • Loading branch information
struan committed May 7, 2024
1 parent b4b789c commit 40f5ef5
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 98 deletions.
1 change: 1 addition & 0 deletions ceuk-marking/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"crowdsourcer.middleware.AddStateMiddleware",
]

ROOT_URLCONF = "ceuk-marking.urls"
Expand Down
36 changes: 36 additions & 0 deletions crowdsourcer/middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from crowdsourcer.models import MarkingSession, ResponseType


class AddStateMiddleware:
def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
response = self.get_response(request)
return response

def process_view(self, request, view_func, view_args, view_kwargs):
session_name = view_kwargs.get("marking_session", None)
if session_name is not None:
current_session = MarkingSession.objects.filter(
label=session_name, active=True
).first()
else:
current_session = MarkingSession.objects.filter(active=True).first()

current_stage = current_session.stage
if current_stage is None:
current_stage = ResponseType.objects.filter(type="First Mark").first()

request.current_stage = current_stage
request.current_session = current_session

def process_template_response(self, request, response):
context = response.context_data

context["marking_session"] = request.current_session
context["sessions"] = MarkingSession.objects.filter(active=True)

response.context_data = context

return response
28 changes: 0 additions & 28 deletions crowdsourcer/mixins.py

This file was deleted.

28 changes: 14 additions & 14 deletions crowdsourcer/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from django.views.generic import ListView, TemplateView

from crowdsourcer.forms import ResponseFormset
from crowdsourcer.mixins import CurrentStateMixin
from crowdsourcer.models import (
Assigned,
PublicAuthority,
Expand All @@ -20,7 +19,7 @@
logger = logging.getLogger(__name__)


class BaseQuestionView(CurrentStateMixin, TemplateView):
class BaseQuestionView(TemplateView):
model = Response
formset = ResponseFormset
response_type = "First Mark"
Expand Down Expand Up @@ -49,15 +48,15 @@ def check_permissions(self):
self.request.user,
authority=self.kwargs["name"],
section=self.kwargs["section_title"],
marking_session=self.current_session,
marking_session=self.request.current_session,
current_stage=self.rt,
):
raise PermissionDenied

def get_initial_obj(self):
self.authority = PublicAuthority.objects.get(name=self.kwargs["name"])
self.questions = Question.objects.filter(
section__marking_session=self.current_session,
section__marking_session=self.request.current_session,
section__title=self.kwargs["section_title"],
questiongroup=self.authority.questiongroup,
how_marked__in=self.how_marked_in,
Expand Down Expand Up @@ -133,7 +132,7 @@ def get_context_data(self, **kwargs):
return context


class BaseSectionAuthorityList(CurrentStateMixin, ListView):
class BaseSectionAuthorityList(ListView):
template_name = "crowdsourcer/section_authority_list.html"
model = Section
context_object_name = "authorities"
Expand All @@ -155,7 +154,8 @@ def get_queryset(self):
return None

section = Section.objects.get(
title=self.kwargs["section_title"], marking_session=self.current_session
title=self.kwargs["section_title"],
marking_session=self.request.current_session,
)
questions = Question.objects.filter(section=section, how_marked__in=self.types)

Expand All @@ -175,7 +175,7 @@ def get_queryset(self):
question_list,
self.kwargs["section_title"],
self.request.user,
self.current_session,
self.request.current_session,
assigned=assigned,
question_types=self.types,
response_type=this_stage,
Expand All @@ -192,7 +192,7 @@ def get_context_data(self, **kwargs):
return context


class BaseAllSectionProgressView(CurrentStateMixin, UserPassesTestMixin, ListView):
class BaseAllSectionProgressView(UserPassesTestMixin, ListView):
template_name = "crowdsourcer/all_section_progress.html"
model = Section
context_object_name = "sections"
Expand All @@ -204,7 +204,7 @@ def test_func(self):
return self.request.user.is_superuser

def get_queryset(self):
return Section.objects.filter(marking_session=self.current_session)
return Section.objects.filter(marking_session=self.request.current_session)

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
Expand All @@ -221,7 +221,7 @@ def get_context_data(self, **kwargs):
question_list,
section.title,
self.request.user,
self.current_session,
self.request.current_session,
question_types=self.types,
response_type=rt,
).distinct()
Expand All @@ -246,7 +246,7 @@ def get_context_data(self, **kwargs):
}

assigned = Section.objects.filter(
marking_session=self.current_session
marking_session=self.request.current_session
).annotate(
num_authorities=Subquery(
Assigned.objects.filter(section=OuterRef("pk"), response_type=rt)
Expand All @@ -266,7 +266,7 @@ def get_context_data(self, **kwargs):
return context


class BaseSectionProgressView(CurrentStateMixin, UserPassesTestMixin, ListView):
class BaseSectionProgressView(UserPassesTestMixin, ListView):
template_name = "crowdsourcer/section_progress.html"
model = Section
context_object_name = "sections"
Expand All @@ -291,7 +291,7 @@ def get_context_data(self, **kwargs):
question_list,
section.title,
self.request.user,
self.current_session,
self.request.current_session,
question_types=self.types,
response_type=rt,
)
Expand Down Expand Up @@ -332,7 +332,7 @@ def get_context_data(self, **kwargs):
return context


class BaseAuthorityAssignmentView(CurrentStateMixin, UserPassesTestMixin, ListView):
class BaseAuthorityAssignmentView(UserPassesTestMixin, ListView):
template_name = "crowdsourcer/authorities_assigned.html"
model = PublicAuthority
context_object_name = "authorities"
Expand Down
15 changes: 7 additions & 8 deletions crowdsourcer/views/marking.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from django.urls import reverse
from django.views.generic import ListView, TemplateView

from crowdsourcer.mixins import CurrentStateMixin
from crowdsourcer.models import (
Assigned,
MarkingSession,
Expand All @@ -25,7 +24,7 @@ class PrivacyPolicyView(TemplateView):
template_name = "crowdsourcer/privacy.html"


class OverviewView(CurrentStateMixin, ListView):
class OverviewView(ListView):
template_name = "crowdsourcer/assignments.html"
model = Assigned
context_object_name = "assignments"
Expand All @@ -43,7 +42,7 @@ def dispatch(self, request, *args, **kwargs):
sessions = marker.marking_session.filter(active=True).all()
if len(sessions) == 1:
session = marker.marking_session.first()
if self.current_session != session:
if self.request.current_session != session:
url = reverse(
"session_urls:home", kwargs={"marking_session": session.label}
)
Expand All @@ -63,13 +62,13 @@ def dispatch(self, request, *args, **kwargs):
elif marker.response_type.type == "Right of Reply":
if Assigned.objects.filter(user=user).exists():
count = Assigned.objects.filter(
user=user, marking_session=self.current_session
user=user, marking_session=self.request.current_session
).count()
# however if they only have a single assignment still skip the
# assignments screen
if count == 1:
assignment = Assigned.objects.filter(
user=user, marking_session=self.current_session
user=user, marking_session=self.request.current_session
).first()
url = reverse(
"authority_ror_sections",
Expand Down Expand Up @@ -108,7 +107,7 @@ def get_queryset(self):

qs = qs.filter(user__is_active=True)

qs = qs.filter(marking_session=self.current_session)
qs = qs.filter(marking_session=self.request.current_session)
return qs

def get_context_data(self, **kwargs):
Expand All @@ -127,7 +126,7 @@ def get_context_data(self, **kwargs):
)

types = Question.VOLUNTEER_TYPES
if self.current_stage.type == "Audit":
if self.request.current_stage.type == "Audit":
types = ["volunteer", "national_volunteer", "foi"]

first_mark = ResponseType.objects.get(type="First Mark")
Expand All @@ -154,7 +153,7 @@ def get_context_data(self, **kwargs):
question_list,
assignment.section.title,
assignment.user,
self.current_session,
self.request.current_session,
]
if assignment.authority_id is not None:
authorities = Assigned.objects.filter(
Expand Down
Loading

0 comments on commit 40f5ef5

Please sign in to comment.