-
Notifications
You must be signed in to change notification settings - Fork 7
/
viewmixins.py
116 lines (92 loc) · 4.73 KB
/
viewmixins.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# -*- coding: utf-8 -*-
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.utils.functional import cached_property
from rules.contrib.views import PermissionRequiredMixin
from erudit.models import Organisation
from core.subscription.models import JournalAccessSubscription
from .shortcuts import (
get_managed_organisations,
get_last_valid_subscription,
get_last_year_of_subscription
)
class OrganisationScopeMixin:
"""
The OrganisationScopeMixin provides a way to associate a view with a specific Organisation
instance. The Organisation instance must have the current user in its members. If not a
PermissionDenied error will be returned.
"""
force_scope_switch_to_pattern_name = None
scope_session_key = 'userspace:library-management:current-organisation-id'
def dispatch(self, request, *args, **kwargs):
self.request = request
response = self.init_scope()
return response if response \
else super(OrganisationScopeMixin, self).dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super(OrganisationScopeMixin, self).get_context_data(**kwargs)
context['scope_current_organisation'] = self.current_organisation
context['has_active_subscription'] = JournalAccessSubscription.valid_objects.filter(
organisation=self.current_organisation
).exists()
try:
context['has_legacy_profile'] = True if \
self.current_organisation.legacyorganisationprofile else False
except: # noqa
context['has_legacy_profile'] = False
context['last_valid_subscription'] = get_last_valid_subscription(self.current_organisation)
context['last_year_of_subscription'] = get_last_year_of_subscription(
self.current_organisation
)
context['scope_user_organisations'] = self.user_organisations
context['force_scope_switch_to_pattern_name'] = self.force_scope_switch_to_pattern_name
return context
def get_user_organisations(self):
""" Returns the organisations that can be accessed by the current user. """
return get_managed_organisations(self.request.user)
def init_current_organisation(self, organisation):
""" Associates the current organisation to the view and saves its ID into the session. """
self.current_organisation = organisation
self.request.session[self.scope_session_key] = organisation.id
def init_scope(self):
""" Initializes the Organisation scope. """
scoped_url = self.kwargs.get('organisation_pk') is not None
# We try to determine the current Organisation instance by looking
# first in the URL. If the organisation ID cannot be retrieved from there
# we try to fetch it from the session.
current_organisation_id = self.kwargs.get('organisation_pk', None) \
or self.request.session.get(self.scope_session_key, None)
organisation = None
if current_organisation_id is not None:
organisation = get_object_or_404(Organisation, id=current_organisation_id)
else:
user_organisations_qs = self.user_organisations
user_organisations_count = user_organisations_qs.count()
if user_organisations_count:
organisation = user_organisations_qs.first()
# Returns a 403 error if the user is not a member of the organisation
if organisation is None or not self.user_organisations.filter(id=organisation.id).exists():
raise PermissionDenied
if not scoped_url:
# Redirects to the scoped URL
resolver_match = self.request.resolver_match
args = resolver_match.args
kwargs = resolver_match.kwargs.copy()
kwargs.update({'organisation_pk': organisation.pk})
return HttpResponseRedirect(
reverse(':'.join([resolver_match.namespace, resolver_match.url_name]),
args=args, kwargs=kwargs))
self.init_current_organisation(organisation)
@cached_property
def user_organisations(self):
return self.get_user_organisations()
class OrganisationScopePermissionRequiredMixin(OrganisationScopeMixin, PermissionRequiredMixin):
raise_exception = True
def get_context_data(self, **kwargs):
context = super(OrganisationScopePermissionRequiredMixin, self).get_context_data(**kwargs)
context['library_permission_required'] = self.permission_required
return context
def get_permission_object(self):
return self.current_organisation