Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# (c) 2018-2020
2# MPIB <https://www.mpib-berlin.mpg.de/>,
3# MPI-CBS <https://www.cbs.mpg.de/>,
4# MPIP <http://www.psych.mpg.de/>
5#
6# This file is part of Castellum.
7#
8# Castellum is free software; you can redistribute it and/or modify it
9# under the terms of the GNU Affero General Public License as published
10# by the Free Software Foundation; either version 3 of the License, or
11# (at your option) any later version.
12#
13# Castellum is distributed in the hope that it will be useful, but
14# WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16# Affero General Public License for more details.
17#
18# You should have received a copy of the GNU Affero General Public
19# License along with Castellum. If not, see
20# <http://www.gnu.org/licenses/>.
22from django import forms
23from django.core.exceptions import ImproperlyConfigured
24from django.http import HttpResponseBadRequest
25from django.utils.http import url_has_allowed_host_and_scheme
27from .forms import ReadonlyFormMixin
28from .forms import ReadonlyModelForm
31class ReadonlyMixin:
32 def get_readonly(self):
33 raise NotImplementedError
35 def get_form_class(self):
36 if self.form_class:
37 if issubclass(self.form_class, ReadonlyFormMixin): 37 ↛ 40line 37 didn't jump to line 40, because the condition on line 37 was never false
38 return self.form_class
39 else:
40 raise ImproperlyConfigured('form_class is not a subclass of ReadonlyFormMixin')
41 return forms.modelform_factory(self.model, fields=self.fields, form=ReadonlyModelForm)
43 def get_form_kwargs(self):
44 kwargs = super().get_form_kwargs()
45 kwargs['readonly'] = self.get_readonly()
46 return kwargs
48 def get_context_data(self, **kwargs):
49 context = super().get_context_data(**kwargs)
50 context['readonly'] = self.get_readonly()
51 return context
53 def post(self, request, *args, **kwargs):
54 self.object = self.get_object()
55 if self.get_readonly(): 55 ↛ 57line 55 didn't jump to line 57, because the condition on line 55 was never false
56 return HttpResponseBadRequest()
57 return super().post(request, *args, **kwargs)
60def get_next_url(request, default='/'):
61 url = request.POST.get('next', request.GET.get('next'))
62 is_safe = url_has_allowed_host_and_scheme(
63 url, allowed_hosts=[request.get_host()], require_https=request.is_secure()
64 )
65 if url and is_safe:
66 return url
67 else:
68 return default