django-utils

personal collection of django utilities
git clone https://git.ce9e.org/django-utils.git

commit
a7fa2bf106ac6dc7a685cb4db646bbc03067f8a4
parent
9f3a341cded8e09b1e029dabb206e24cb6cb3941
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2019-09-15 09:08
add ReadonlyMixin

Diffstat

A utils/readonly.py 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

1 files changed, 59 insertions, 0 deletions


diff --git a/utils/readonly.py b/utils/readonly.py

@@ -0,0 +1,59 @@
   -1     1 from django import forms
   -1     2 from django.core.exceptions import ImproperlyConfigured
   -1     3 from django.http import HttpResponseBadRequest
   -1     4 
   -1     5 
   -1     6 def supports_readonly(field):
   -1     7     input_type = getattr(field.widget, 'input_type', 'text')
   -1     8     return not (
   -1     9         hasattr(field, 'choices') or input_type in ['checkbox', 'radio', 'file']
   -1    10     )
   -1    11 
   -1    12 
   -1    13 class ReadonlyFormMixin:
   -1    14     def __init__(self, *args, **kwargs):
   -1    15         self.readonly = kwargs.pop('readonly', False)
   -1    16         super().__init__(*args, **kwargs)
   -1    17 
   -1    18         if self.readonly:
   -1    19             for field in self.fields.values():
   -1    20                 if supports_readonly(field):
   -1    21                     field.widget.attrs['readonly'] = True
   -1    22                 else:
   -1    23                     field.widget.attrs['disabled'] = True
   -1    24 
   -1    25 
   -1    26 class ReadonlyModelForm(ReadonlyFormMixin, forms.ModelForm):
   -1    27     pass
   -1    28 
   -1    29 
   -1    30 class ReadonlyMixin:
   -1    31     def get_readonly(self):
   -1    32         raise NotImplementedError
   -1    33 
   -1    34     def get_form_class(self):
   -1    35         if self.form_class:
   -1    36             if issubclass(self.form_class, ReadonlyFormMixin):
   -1    37                 return self.form_class
   -1    38             else:
   -1    39                 msg = 'form_class is not a subclass of ReadonlyFormMixin'
   -1    40                 raise ImproperlyConfigured(msg)
   -1    41         return forms.modelform_factory(
   -1    42             self.model, fields=self.fields, form=ReadonlyModelForm
   -1    43         )
   -1    44 
   -1    45     def get_form_kwargs(self):
   -1    46         kwargs = super().get_form_kwargs()
   -1    47         kwargs['readonly'] = self.get_readonly()
   -1    48         return kwargs
   -1    49 
   -1    50     def get_context_data(self, **kwargs):
   -1    51         context = super().get_context_data(**kwargs)
   -1    52         context['readonly'] = self.get_readonly()
   -1    53         return context
   -1    54 
   -1    55     def post(self, request, *args, **kwargs):
   -1    56         self.object = self.get_object()
   -1    57         if self.get_readonly():
   -1    58             return HttpResponseBadRequest()
   -1    59         return super().post(request, *args, **kwargs)