This repository has been archived by the owner on Aug 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
mixins.py
66 lines (51 loc) · 2.2 KB
/
mixins.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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from django.core.exceptions import PermissionDenied
from django.http import Http404
from django.shortcuts import get_object_or_404
from oneanddone.tasks.models import Task, TaskAttempt
class APIOnlyCreatorMayDeleteMixin(object):
def pre_delete(self, obj):
if obj.creator != self.request.user:
raise PermissionDenied()
class APIRecordCreatorMixin(object):
def pre_save(self, obj):
obj.creator = self.request.user
class GetUserAttemptMixin(object):
"""
Retrieve a user attempt and add it to the view's self scope
for later use.
"""
def dispatch(self, request, *args, **kwargs):
self.attempt = get_object_or_404(TaskAttempt, pk=kwargs['pk'], user=request.user,
state__in=[TaskAttempt.FINISHED, TaskAttempt.ABANDONED])
return super(GetUserAttemptMixin, self).dispatch(request, *args, **kwargs)
class HideNonRepeatableTaskMixin(object):
"""
Do not allow access to a non-repeatable task that is not available to the user.
"""
def get_object(self, queryset=None):
task = super(HideNonRepeatableTaskMixin, self).get_object(queryset)
if not task.is_available_to_user(self.request.user):
raise Http404('Task unavailable.')
return task
class BaseURLMixin(object):
"""
Store the base url for the current site in self.
"""
def dispatch(self, request, *args, **kwargs):
link_prefix = 'http'
if self.request.is_secure():
link_prefix += 's'
self.base_url = link_prefix + '://%s' % self.request.get_host()
return super(BaseURLMixin, self).dispatch(request, *args, **kwargs)
class TaskMustBeAvailableMixin(object):
"""
Only allow published tasks to be listed, by filtering the
queryset.
"""
allow_expired_tasks = False
def get_queryset(self):
queryset = super(TaskMustBeAvailableMixin, self).get_queryset()
return queryset.filter(Task.is_available_filter(allow_expired=self.allow_expired_tasks))