Newer
Older
import copy
from django.contrib.auth.mixins import UserPassesTestMixin
from django.contrib.messages.views import SuccessMessageMixin
from django.http import JsonResponse
from django.utils import translation
from django.utils.translation import ugettext_lazy as _
from django.views.generic import ListView
from django.views.generic.base import ContextMixin
from django.views.generic.detail import BaseDetailView
from django.views.generic.edit import ModelFormMixin
from ..models import (AppSettings, DataFile, DataSet, Lab, Log, Profile,
ShareGroup)
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
class StaffViewMixin(UserPassesTestMixin):
def test_func(self):
return self.request.user.is_staff
class PageViewMixin(ContextMixin):
page_title = ''
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['page_title'] = self.page_title
return context
class ActivePageViewMixin(PageViewMixin):
active_page = ''
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['active_page'] = self.active_page
return context
class SubmitLabelMixin(ContextMixin):
submit_label = ''
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['submit_label'] = self.submit_label
return context
class SuccessUrlMixin(ContextMixin):
success_url = '#'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['success_url'] = self.success_url
return context
class CreateViewMixin(SubmitLabelMixin, SuccessMessageMixin, SuccessUrlMixin, ActivePageViewMixin, ContextMixin):
submit_label = _('Create')
template_name = 'portal/secure/admin/create_update.html'
success_message = _('Successfully Created "%(name)s"')
class UpdateViewMixin(SubmitLabelMixin, SuccessMessageMixin, SuccessUrlMixin, ActivePageViewMixin, ContextMixin):
submit_label = _('Update')
template_name = 'portal/secure/admin/create_update.html'
success_message = _('Successfully Updated "%(name)s"')
class DeleteViewMixin(SubmitLabelMixin, SuccessMessageMixin, SuccessUrlMixin, ActivePageViewMixin, ContextMixin):
submit_label = _('Delete')
template_name = 'portal/secure/admin/delete.html'
success_message = _('Successfully Deleted "%(name)s"')
class AppSettingsMixin(ContextMixin):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
app_settings = AppSettings.objects.first()
if app_settings and app_settings.home_institution:
context['home_institution'] = app_settings.home_institution.id
return context
class AjaxDatatableBackboneMixin(ContextMixin):
"""List view to display a Datatable with AJAX datasource"""
template_name = 'portal/secure/admin/list.html'
create_url = '#'
create_label = _('Create New')
api_url = ''
dt_struct = {}
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['create_url'] = self.create_url
context['create_label'] = self.create_label
context['dt_struct'] = copy.copy(self.dt_struct)
class JSONListView(ListView):
"""Children must implement get_rows()
Keep in mind: overriding get_queryset() to add 'select_related' statements might provide nice speed boost
"""
def get_rows(self):
pass
def render_to_response(self, context, **response_kwargs):
return JsonResponse({'data': self.get_rows()}, **response_kwargs)
class JSONView(BaseDetailView):
"""Children must implement get_rows()
Keep in mind: overriding get_queryset() to add 'select_related' statements might provide nice speed boost
"""
def get_data(self):
pass
def render_to_response(self, context, **response_kwargs):
return JsonResponse({'data': self.get_data()}, **response_kwargs)
class LogMixin(ModelFormMixin):
def form_valid(self, form):
before = self.get_object()
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
for d in self.qs_params:
old = getattr(before, d)
new = form.cleaned_data[d]
if old != new:
removed = set(old.all()) - set(new.all())
added = set(new.all()) - set(old.all())
for o in added.union(removed):
type = 'remove'
if o in added:
type = 'add'
with translation.override('en'):
label_en = str(form.fields[d].label)
with translation.override('fr'):
label_fr = str(form.fields[d].label)
log = Log(action_type=type, profile=self.request.user.profile, obj_dataset=before, label_fr=label_fr, label_en=label_en)
if isinstance(o, Profile):
log.action_type += '_share'
log.obj_profile = o
elif isinstance(o, Lab):
log.action_type += '_share'
log.obj_lab = o
elif isinstance(o, ShareGroup):
log.action_type += '_share'
log.obj_group = o
elif isinstance(o, DataFile):
log.obj_datafile = o
log.save()
for d in form.changed_data:
if d not in self.qs_params:
with translation.override('en'):
label_en = str(form.fields['name'].label)
with translation.override('fr'):
label_fr = str(form.fields['name'].label)
Log.objects.create(action_type='edit', profile=self.request.user.profile, obj_dataset=before, label_fr=label_fr, label_en=label_en)