2015-06-02 12:59:49 +00:00
|
|
|
import os
|
|
|
|
|
2014-09-26 15:05:20 +00:00
|
|
|
from django.core import exceptions
|
2021-04-21 12:27:18 +00:00
|
|
|
from django.urls import reverse
|
2014-05-08 16:59:35 +00:00
|
|
|
from django.db import models
|
2015-06-02 12:59:49 +00:00
|
|
|
from django.db.models.fields.files import FileField, FieldFile
|
2014-05-08 16:59:35 +00:00
|
|
|
from django.utils.text import capfirst
|
|
|
|
|
|
|
|
from ..forms.fields import MultiSelectFormField
|
|
|
|
|
|
|
|
|
2016-04-30 11:23:13 +00:00
|
|
|
class MultiSelectField(models.CharField):
|
2014-05-08 16:59:35 +00:00
|
|
|
def formfield(self, **kwargs):
|
|
|
|
defaults = {
|
|
|
|
'required': not self.blank,
|
|
|
|
'label': capfirst(self.verbose_name),
|
|
|
|
'help_text': self.help_text,
|
|
|
|
'choices': self.choices
|
|
|
|
}
|
|
|
|
if self.has_default():
|
2015-04-03 13:03:08 +00:00
|
|
|
defaults['initial'] = self.get_default()
|
2014-05-08 16:59:35 +00:00
|
|
|
defaults.update(kwargs)
|
|
|
|
return MultiSelectFormField(**defaults)
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def get_db_prep_value(self, value, connection=None, prepared=False):
|
2015-04-02 16:14:55 +00:00
|
|
|
if isinstance(value, str):
|
2014-05-08 16:59:35 +00:00
|
|
|
return value
|
2015-04-08 14:41:09 +00:00
|
|
|
else:
|
2014-05-08 16:59:35 +00:00
|
|
|
return ','.join(value)
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def to_python(self, value):
|
2014-11-13 15:34:00 +00:00
|
|
|
if value:
|
2015-04-03 13:03:08 +00:00
|
|
|
if isinstance(value, str):
|
|
|
|
return value.split(',')
|
|
|
|
return value
|
2014-11-13 15:34:00 +00:00
|
|
|
return []
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2023-11-17 12:25:13 +00:00
|
|
|
def from_db_value(self, value, expression, connection):
|
2016-04-30 11:23:13 +00:00
|
|
|
if value:
|
|
|
|
if isinstance(value, str):
|
|
|
|
return value.split(',')
|
|
|
|
return value
|
|
|
|
return []
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def contribute_to_class(self, cls, name):
|
|
|
|
super(MultiSelectField, self).contribute_to_class(cls, name)
|
|
|
|
if self.choices:
|
|
|
|
def func(self, field=name, choices=dict(self.choices)):
|
2014-11-13 15:34:00 +00:00
|
|
|
return ','.join([choices.get(value, value) for value in getattr(self, field)])
|
2014-05-08 16:59:35 +00:00
|
|
|
setattr(cls, 'get_%s_display' % self.name, func)
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def validate(self, value, model_instance):
|
2015-05-14 13:28:54 +00:00
|
|
|
if self.choices:
|
2016-10-11 10:01:56 +00:00
|
|
|
arr_choices = self.get_choices_selected(self.get_choices())
|
2015-05-14 13:28:54 +00:00
|
|
|
for opt_select in value:
|
|
|
|
if (opt_select not in arr_choices):
|
|
|
|
msg = self.error_messages['invalid_choice'] % {'value': opt_select}
|
|
|
|
raise exceptions.ValidationError(msg)
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def get_choices_selected(self, arr_choices=''):
|
|
|
|
if not arr_choices:
|
|
|
|
return False
|
2015-04-04 17:44:07 +00:00
|
|
|
return [value for value, __ in arr_choices]
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
2014-07-10 15:19:06 +00:00
|
|
|
class NullableCharField(models.CharField):
|
|
|
|
def get_db_prep_value(self, value, connection=None, prepared=False):
|
|
|
|
return value or None
|
|
|
|
|
|
|
|
|
2015-06-02 12:59:49 +00:00
|
|
|
class PrivateFieldFile(FieldFile):
|
|
|
|
@property
|
|
|
|
def url(self):
|
|
|
|
self._require_file()
|
|
|
|
app_label = self.instance._meta.app_label
|
|
|
|
model_name = self.instance._meta.object_name.lower()
|
|
|
|
field_name = self.field.name
|
|
|
|
pk = self.instance.pk
|
|
|
|
filename = os.path.basename(self.path)
|
|
|
|
args = [app_label, model_name, field_name, pk, filename]
|
|
|
|
return reverse('private-media', args=args)
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2015-06-02 12:59:49 +00:00
|
|
|
@property
|
|
|
|
def condition(self):
|
|
|
|
return self.field.condition
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2015-06-02 12:59:49 +00:00
|
|
|
@property
|
|
|
|
def attachment(self):
|
|
|
|
return self.field.attachment
|
|
|
|
|
|
|
|
|
|
|
|
class PrivateFileField(FileField):
|
|
|
|
attr_class = PrivateFieldFile
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2015-06-02 12:59:49 +00:00
|
|
|
def __init__(self, verbose_name=None, name=None, upload_to='', storage=None, attachment=True,
|
|
|
|
condition=lambda request, instance: request.user.is_superuser, **kwargs):
|
|
|
|
super(PrivateFileField, self).__init__(verbose_name, name, upload_to, storage, **kwargs)
|
|
|
|
self.condition = condition
|
|
|
|
self.attachment = attachment
|