def __init__(self, *args, **kwargs): """ Builds the field for the setting's value based on the setting's properties. """ self.request = kwargs.pop('request', None) super(SettingForm, self).__init__(*args, **kwargs) setting = self.instance if setting: if setting.input_type == 'select': if setting.input_value == '<form_list>': choices = get_form_list(self.request.user) required = False elif setting.input_value == '<box_list>': choices = get_box_list(self.request.user) required = False else: choices = tuple([(s,s)for s in setting.input_value.split(',')]) required = True options = { 'label': setting.label, 'help_text': setting.description, 'initial': setting.value, 'choices': choices, 'required': required, } self.fields['value'] = forms.ChoiceField(**options) else: options = { 'label': setting.label, 'help_text': setting.description, 'initial': setting.value, 'required': False } self.fields['value'] = forms.CharField(**options)
def build_settings_form(user, settings): """ Create a set of fields and builds a form class returns SettingForm class """ fields = OrderedDict() for setting in settings: setting_label = mark_safe('{0} <a href="#id_{1}" title="Permalink to this setting"><i class="fa fa-link" aria-hidden="true"></i></a>'.format( setting.label, setting.name)) # Do not display standard regform settings if setting.scope_category == 'events' and setting.name.startswith('regform_'): continue try: setting_value = force_text(setting.get_value()) except DjangoUnicodeDecodeError: setting_value = '' if setting.input_type in ['text', 'textarea']: options = { 'label': setting_label, 'help_text': setting.description, 'initial': setting_value, 'required': False, 'label_suffix': "", } if setting.input_type == 'textarea': options['widget'] = forms.Textarea(attrs={'rows': 5, 'cols': 30}) if setting.client_editable: fields.update({"%s" % setting.name: forms.CharField(**options)}) else: if user.is_superuser: fields.update({"%s" % setting.name: forms.CharField(**options)}) elif setting.input_type in ['select', 'selectmultiple']: if setting.input_value == '<form_list>': choices = get_form_list(user) required = False elif setting.input_value == '<box_list>': choices = get_box_list(user) required = False elif setting.input_value == '<group_list>': choices, initial = get_group_list(user) required = True if not setting_value: setting_value = initial elif setting.input_value == '<timezone_list>': choices = get_timezone_choices() required = True if not setting_value: setting_value = django_settings.TIME_ZONE elif setting.input_value == '<language_list>': choices = get_languages_with_local_name() required = True elif setting.input_value == '<country_list>': choices = (('', '-----------'),) + tuple(COUNTRIES) required = False if setting.get_value() and setting.name == 'countrylistinitialchoices': setting_value = literal_eval(setting.get_value()) else: # Allow literal_eval in settings in order to pass a list from the setting # This is useful if you want different values and labels for the select options try: choices = tuple([(k, v)for k, v in literal_eval(setting.input_value)]) required = False # By adding #<box_list> on to the end of a setting, this will append the boxes # as select items in the list as well. if '<box_list>' in setting.input_value: box_choices = get_box_list(user)[1:] choices = (('Content', choices), ('Boxes', box_choices)) except: choices = tuple([(s.strip(), s.strip())for s in setting.input_value.split(',')]) required = True options = { 'label': setting_label, 'help_text': setting.description, 'initial': setting_value, 'choices': choices, 'required': required, 'label_suffix': "", } if setting.client_editable or user.is_superuser: if setting.input_type == 'selectmultiple': fields.update({"%s" % setting.name: forms.MultipleChoiceField(**options)}) else: fields.update({"%s" % setting.name: forms.ChoiceField(**options)}) elif setting.input_type == 'file': from tendenci.apps.files.models import File as TendenciFile file_display = '' try: try: val = int(setting_value) except ValueError: val = 0 try: tfile = TendenciFile.objects.get(pk=val) except Exception: tfile = None if tfile: if tfile.file.name.lower().endswith(('.jpg', '.jpe', '.png', '.gif', '.svg')): tfile_alt = tfile.file.name.lower()[:-4] file_display = '<img src="/files/%s/" alt="%s" title="%s">' % (tfile.pk, tfile_alt, tfile_alt) else: file_display = tfile.file.name except TendenciFile.DoesNotExist: file_display = "No file" options = { 'label': setting_label, 'help_text': "%s<br> Current File: %s" % (setting.description, file_display), #'initial': tfile and tfile.file, # Removed this so the file doesn't save over and over 'required': False, 'label_suffix': "", } if setting.client_editable: fields.update({"%s" % setting.name: forms.FileField(**options)}) else: if user.is_superuser: fields.update({"%s" % setting.name: forms.FileField(**options)}) attributes = { 'settings': settings, 'base_fields': fields, 'clean': clean_settings_form, 'save': save_settings_form, 'user': user, } return type('SettingForm', (forms.BaseForm,), attributes)
def build_settings_form(user, settings): """ Create a set of fields and builds a form class returns SettingForm class """ fields = OrderedDict() for setting in settings: # Do not display standard regform settings if setting.scope_category == 'events' and setting.name.startswith('regform_'): continue try: setting_value = force_unicode(setting.get_value()) except DjangoUnicodeDecodeError: setting_value = '' if setting.input_type in ['text', 'textarea']: options = { 'label': setting.label, 'help_text': setting.description, 'initial': setting_value, 'required': False } if setting.input_type == 'textarea': options['widget'] = forms.Textarea(attrs={'rows': 5, 'cols': 30}); if setting.client_editable: fields.update({"%s" % setting.name: forms.CharField(**options)}) else: if user.is_superuser: fields.update({"%s" % setting.name: forms.CharField(**options)}) elif setting.input_type in ['select', 'selectmultiple']: if setting.input_value == '<form_list>': choices = get_form_list(user) required = False elif setting.input_value == '<box_list>': choices = get_box_list(user) required = False elif setting.input_value == '<group_list>': choices, initial = get_group_list(user) required = True if not setting_value: setting_value = initial elif setting.input_value == '<timezone_list>': choices = zones.PRETTY_TIMEZONE_CHOICES required = True elif setting.input_value == '<language_list>': choices = get_languages_with_local_name() required = True elif setting.input_value == '<country_list>': choices = (('', '-----------'),) + tuple(COUNTRIES) required = False else: # Allow literal_eval in settings in order to pass a list from the setting # This is useful if you want different values and labels for the select options try: choices = tuple([(k, v)for k, v in literal_eval(setting.input_value)]) required = False # By adding #<box_list> on to the end of a setting, this will append the boxes # as select items in the list as well. if '<box_list>' in setting.input_value: box_choices = get_box_list(user)[1:] choices = (('Content', choices), ('Boxes', box_choices)) except: choices = tuple([(s.strip(), s.strip())for s in setting.input_value.split(',')]) required = True options = { 'label': setting.label, 'help_text': setting.description, 'initial': setting_value, 'choices': choices, 'required': required, } if setting.client_editable or user.is_superuser: if setting.input_type == 'selectmultiple': fields.update({"%s" % setting.name: forms.MultipleChoiceField(**options)}) else: fields.update({"%s" % setting.name: forms.ChoiceField(**options)}) elif setting.input_type == 'file': from tendenci.apps.files.models import File as TendenciFile file_display = '' try: try: val = int(setting_value) except ValueError: val = 0 try: tfile = TendenciFile.objects.get(pk=val) except Exception: tfile = None if tfile: if tfile.file.name.lower().endswith(('.jpg', '.jpe', '.png', '.gif', '.svg')): tfile_alt = tfile.file.name.lower()[:-4] file_display = '<img src="/files/%s/" alt="%s" title="%s">' % (tfile.pk, tfile_alt, tfile_alt) else: file_display = tfile.file.name except TendenciFile.DoesNotExist: file_display = "No file" options = { 'label': setting.label, 'help_text': "%s<br> Current File: %s" % (setting.description, file_display), #'initial': tfile and tfile.file, # Removed this so the file doesn't save over and over 'required': False } if setting.client_editable: fields.update({"%s" % setting.name: forms.FileField(**options)}) else: if user.is_superuser: fields.update({"%s" % setting.name: forms.FileField(**options)}) attributes = { 'settings': settings, 'base_fields': fields, 'clean': clean_settings_form, 'save': save_settings_form, 'user': user, } return type('SettingForm', (forms.BaseForm,), attributes)