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: try: setting_value = force_unicode(setting.get_value()) except DjangoUnicodeDecodeError: setting_value = '' if setting.input_type == 'text': options = { 'label': setting.label, 'help_text': setting.description, 'initial': setting_value, 'required': False } 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 == 'select': 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 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: fields.update({"%s" % setting.name: forms.ChoiceField(**options)}) else: if user.is_superuser: fields.update({"%s" % setting.name: forms.ChoiceField(**options)}) elif setting.input_type == 'file': from tendenci.core.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')): file_display = '<img src="/files/%s/">' % tfile.pk 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)
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 == 'select': 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 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: fields.update( {"%s" % setting.name: forms.ChoiceField(**options)}) else: if user.is_superuser: fields.update( {"%s" % setting.name: forms.ChoiceField(**options)}) elif setting.input_type == 'file': from tendenci.core.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)
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 == "select": 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 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: fields.update({"%s" % setting.name: forms.ChoiceField(**options)}) else: if user.is_superuser: fields.update({"%s" % setting.name: forms.ChoiceField(**options)}) elif setting.input_type == "file": from tendenci.core.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)
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: if setting.input_type == 'text': options = { 'label': setting.label, 'help_text': setting.description, 'initial': setting.get_value(), 'required': False } 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 == 'select': 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 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.get_value(), 'choices': choices, 'required': required, } if setting.client_editable: fields.update( {"%s" % setting.name: forms.ChoiceField(**options)}) else: if user.is_superuser: fields.update( {"%s" % setting.name: forms.ChoiceField(**options)}) elif setting.input_type == 'file': from tendenci.core.files.models import File as TendenciFile file_display = '' try: try: val = int(setting.get_value()) except: val = 0 try: tfile = TendenciFile.objects.get(pk=val) except: tfile = None if tfile: if tfile.file.name.lower().endswith( ('.jpg', '.jpe', '.png', '.gif', '.svg')): file_display = '<img src="/files/%s/">' % tfile.pk 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)