vkk.workhours.contributor.mixins

 1from django.contrib.auth.mixins import AccessMixin
 2from django.utils import timezone
 3from django.db.models import Q
 4from vkk.workhours.models import ProjectAssignment, Period
 5from vkk.workhours.forms import PeriodSelectForm
 6
 7class ContributorRequiredMixin(AccessMixin):
 8    """Verify that the current user is a contributor to the given project."""
 9    def dispatch(self, request, *args, **kwargs):
10        assignment_query = ProjectAssignment.objects.filter(
11            pk=kwargs.get('assignment_pk')
12        )
13        if not request.user.is_authenticated or not assignment_query.exists():
14            return self.handle_no_permission()
15        return super().dispatch(request, *args, **kwargs)
16
17class ContributorPeriodSelectorMixin():
18    """Adds a Form for selecting Periods."""
19    def get_context_data(self, **kwargs):
20        now = timezone.now()
21        query_set = Period.objects.filter(
22            Q(dead_line__gte=now) | 
23            Q(workhours__project_assignment__pk=self.kwargs['assignment_pk']) |
24            Q(periodclosure__project_assignment__pk=self.kwargs['assignment_pk'])
25        ).distinct()
26        context = super().get_context_data(**kwargs)
27        context['period_selector'] = PeriodSelectForm(
28            queryset=query_set.order_by('-start'),
29            initial={'period': self.kwargs['period_pk']}
30        )
31        return context
class ContributorRequiredMixin(django.contrib.auth.mixins.AccessMixin):
 8class ContributorRequiredMixin(AccessMixin):
 9    """Verify that the current user is a contributor to the given project."""
10    def dispatch(self, request, *args, **kwargs):
11        assignment_query = ProjectAssignment.objects.filter(
12            pk=kwargs.get('assignment_pk')
13        )
14        if not request.user.is_authenticated or not assignment_query.exists():
15            return self.handle_no_permission()
16        return super().dispatch(request, *args, **kwargs)

Verify that the current user is a contributor to the given project.

def dispatch(self, request, *args, **kwargs):
10    def dispatch(self, request, *args, **kwargs):
11        assignment_query = ProjectAssignment.objects.filter(
12            pk=kwargs.get('assignment_pk')
13        )
14        if not request.user.is_authenticated or not assignment_query.exists():
15            return self.handle_no_permission()
16        return super().dispatch(request, *args, **kwargs)
Inherited Members
django.contrib.auth.mixins.AccessMixin
get_login_url
get_permission_denied_message
get_redirect_field_name
handle_no_permission
class ContributorPeriodSelectorMixin:
18class ContributorPeriodSelectorMixin():
19    """Adds a Form for selecting Periods."""
20    def get_context_data(self, **kwargs):
21        now = timezone.now()
22        query_set = Period.objects.filter(
23            Q(dead_line__gte=now) | 
24            Q(workhours__project_assignment__pk=self.kwargs['assignment_pk']) |
25            Q(periodclosure__project_assignment__pk=self.kwargs['assignment_pk'])
26        ).distinct()
27        context = super().get_context_data(**kwargs)
28        context['period_selector'] = PeriodSelectForm(
29            queryset=query_set.order_by('-start'),
30            initial={'period': self.kwargs['period_pk']}
31        )
32        return context

Adds a Form for selecting Periods.

def get_context_data(self, **kwargs):
20    def get_context_data(self, **kwargs):
21        now = timezone.now()
22        query_set = Period.objects.filter(
23            Q(dead_line__gte=now) | 
24            Q(workhours__project_assignment__pk=self.kwargs['assignment_pk']) |
25            Q(periodclosure__project_assignment__pk=self.kwargs['assignment_pk'])
26        ).distinct()
27        context = super().get_context_data(**kwargs)
28        context['period_selector'] = PeriodSelectForm(
29            queryset=query_set.order_by('-start'),
30            initial={'period': self.kwargs['period_pk']}
31        )
32        return context