def _render(self, form, form_style, context, template_pack=TEMPLATE_PACK): attrs = {} data_target = self.data_target if data_target: if isinstance(self.data_target, FieldVal): data_target = self.data_target._render(form, form_style, context, template_pack=template_pack) attrs['data-target'] = data_target if self.url_encoder and 'src' not in self.attrs: attrs['src'] = self.url_encoder(form, form_style, context) flat_attrs = ' '.join(filter(lambda x: x, (flatatt(self.attrs), flatatt(attrs)))) value = self.value_template.render(context) if self.value_template else self.value if isinstance(value, Renderizable): value = value.render(form, form_style, context) context.update({ 'input': { 'name': self.name, 'id': self.id, 'input_type': self.input_type, 'field_classes': self.field_classes, }, 'value': value, 'flat_attrs': flat_attrs, }) return render_to_string(self.template, context)
def __init__(self, legend, *fields, **kwargs): self.fields = list(fields) self.legend = legend self.css_class = kwargs.pop('css_class', '') self.css_id = kwargs.pop('css_id', None) self.template = kwargs.pop('template', self.template) self.flat_attrs = flatatt(kwargs)
def render(self, name, value, attrs=None, renderer=None): location_ids = to_list(value) if value else [] locations = list( SQLLocation.active_objects.filter(domain=self.domain, location_id__in=location_ids)) initial_data = [{ 'id': loc.location_id, 'text': loc.get_path_display(), } for loc in locations] return get_template(self.template).render({ 'id': self.id, 'name': name, 'value': [loc.location_id for loc in locations], 'query_url': self.query_url, 'multiselect': self.multiselect, 'placeholder': self.placeholder, 'initial_data': initial_data, 'attrs': flatatt(self.build_attrs(self.attrs, attrs)), })
def __init__(self, *fields, legend=None, legend_size=None, legend_tag=None, **kwargs): self.fields = list(fields) self.context = {} if legend: self.context["legend"] = legend if legend_size: self.context["legend_size"] = Size.for_legend(legend_size) if legend_tag: self.context["legend_tag"] = legend_tag if hasattr(self, "css_class") and "css_class" in kwargs: self.css_class += " %s" % kwargs.pop("css_class") if not hasattr(self, "css_class"): self.css_class = kwargs.pop("css_class", None) self.css_id = kwargs.pop("css_id", None) self.template = kwargs.pop("template", self.template) self.flat_attrs = flatatt(kwargs)
def render(self, form, form_style, context, template_pack=None): context.update({ 'button_text': self.button_text, 'button_url': self.button_url, 'button_attrs': flatatt(self.attrs if isinstance(self.attrs, dict) else {}), }) return render_to_string(self.template, context)
def __init__(self, legend, *fields, **kwargs): self.fields = list(fields) self.legend = legend self.css_class = kwargs.pop("css_class", "") self.css_id = kwargs.pop("css_id", None) self.template = kwargs.pop("template", self.template) self.flat_attrs = flatatt(kwargs)
def render(self, form, form_style, context, template_pack=None): context.update({ 'button_text': self.button_text, 'button_url': self.button_url, 'button_attrs': flatatt(self.attrs if isinstance(self.attrs, dict) else {}), }) return render_to_string(self.template, context.flatten())
def render_static_field(self, field, form, form_style, context, **kwargs): template = self.get_template_name(kwargs.get('template_pack')) attrs = self.attrs if form and field in form.fields: value = form[field].value else: value = getattr(form.instance, field, None) text = getattr(form.instance, field, None) if callable(self.format): text = self.format(text) field = { 'auto_id': field, 'name': field, 'value': value, 'text': text, 'form': form } context.update({ 'wrapper_class': self.wrapper_class, 'field': field, 'flat_attrs': flatatt(attrs if isinstance(attrs, dict) else {}), }) if kwargs.get('extra_context') is not None: context.update(kwargs.get('extra_context')) return render_to_string(template, context.flatten())
def get_attributes(self, template_pack=TEMPLATE_PACK): """ Used by crispy_forms_tags to get helper attributes """ items = { 'form_method': self.form_method.strip(), 'form_tag': self.form_tag, 'form_style': self.form_style.strip(), 'form_show_errors': self.form_show_errors, 'help_text_inline': self.help_text_inline, 'error_text_inline': self.error_text_inline, 'html5_required': self.html5_required, 'form_show_labels': self.form_show_labels, 'disable_csrf': self.disable_csrf, 'label_class': self.label_class, 'field_class': self.field_class, 'include_media': self.include_media } bootstrap_size_match = re.findall('col-(lg|md|sm|xs)-(\d+)', self.label_class) if bootstrap_size_match: if template_pack == 'bootstrap4': offset_pattern = 'offset-%s-%s' else: offset_pattern = 'col-%s-offset-%s' items['bootstrap_checkbox_offsets'] = [offset_pattern % m for m in bootstrap_size_match] items['attrs'] = {} if self.attrs: items['attrs'] = self.attrs.copy() if self.form_action: items['attrs']['action'] = self.form_action.strip() if self.form_id: items['attrs']['id'] = self.form_id.strip() if self.form_class: # uni_form TEMPLATE PACK has a uniForm class by default if template_pack == 'uni_form': items['attrs']['class'] = "uniForm %s" % self.form_class.strip() else: items['attrs']['class'] = self.form_class.strip() else: if template_pack == 'uni_form': items['attrs']['class'] = self.attrs.get('class', '') + " uniForm" if self.form_group_wrapper_class: items['attrs']['form_group_wrapper_class'] = self.form_group_wrapper_class items['flat_attrs'] = flatatt(items['attrs']) if self.inputs: items['inputs'] = self.inputs if self.form_error_title: items['form_error_title'] = self.form_error_title.strip() if self.formset_error_title: items['formset_error_title'] = self.formset_error_title.strip() for attribute_name, value in self.__dict__.items(): if attribute_name not in items and attribute_name not in ['layout', 'inputs'] and not attribute_name.startswith('_'): items[attribute_name] = value return items
def get_attributes(self, template_pack=TEMPLATE_PACK): """ Used by crispy_forms_tags to get helper attributes """ items = { 'form_method': self.form_method.strip(), 'form_tag': self.form_tag, 'form_style': self.form_style.strip(), 'form_show_errors': self.form_show_errors, 'help_text_inline': self.help_text_inline, 'error_text_inline': self.error_text_inline, 'html5_required': self.html5_required, 'form_show_labels': self.form_show_labels, 'disable_csrf': self.disable_csrf, 'label_class': self.label_class, 'field_class': self.field_class, 'include_media': self.include_media } # col-[lg|md|sm|xs]-<number> label_size_match = re.search('(\d+)', self.label_class) device_type_match = re.search('(lg|md|sm|xs)', self.label_class) if label_size_match and device_type_match: try: items['label_size'] = int(label_size_match.groups()[0]) items['bootstrap_device_type'] = device_type_match.groups()[0] except: pass items['attrs'] = {} if self.attrs: items['attrs'] = self.attrs.copy() if self.form_action: items['attrs']['action'] = self.form_action.strip() if self.form_id: items['attrs']['id'] = self.form_id.strip() if self.form_class: # uni_form TEMPLATE PACK has a uniForm class by default if template_pack == 'uni_form': items['attrs']['class'] = "uniForm %s" % self.form_class.strip() else: items['attrs']['class'] = self.form_class.strip() else: if template_pack == 'uni_form': items['attrs']['class'] = self.attrs.get('class', '') + " uniForm" items['flat_attrs'] = flatatt(items['attrs']) if self.inputs: items['inputs'] = self.inputs if self.form_error_title: items['form_error_title'] = self.form_error_title.strip() if self.formset_error_title: items['formset_error_title'] = self.formset_error_title.strip() for attribute_name, value in self.__dict__.items(): if attribute_name not in items and attribute_name not in ['layout', 'inputs'] and not attribute_name.startswith('_'): items[attribute_name] = value return items
def get_buttons(self): """ Update the buttons to disable form validations since we use our own valdiation and text. """ buttons = super(AssignmentCreateUpdateMixin, self).get_buttons() for button in buttons: button.flat_attrs = flatatt({'formnovalidate': True}) + button.flat_attrs return buttons
def __init__(self, legend, *fields, **kwargs): self.fields = list(fields) self.legend = legend self.css_class = kwargs.pop('css_class', '') self.css_id = kwargs.pop('css_id', None) # Overrides class variable with an instance level variable self.template = kwargs.pop('template', self.template) self.flat_attrs = flatatt(kwargs)
def get_attributes(self, template_pack=TEMPLATE_PACK): """ Used by crispy_forms_tags to get helper attributes """ items = {} items["form_method"] = self.form_method.strip() items["form_tag"] = self.form_tag items["form_style"] = self.form_style.strip() items["form_show_errors"] = self.form_show_errors items["help_text_inline"] = self.help_text_inline items["error_text_inline"] = self.error_text_inline items["html5_required"] = self.html5_required items["form_show_labels"] = self.form_show_labels items["disable_csrf"] = self.disable_csrf items["label_class"] = self.label_class items["field_class"] = self.field_class label_size_match = re.match("col-lg-(\d+)", self.label_class) if label_size_match: try: items["label_size"] = int(label_size_match.groups()[0]) except: pass items["attrs"] = {} if self.attrs: items["attrs"] = self.attrs.copy() if self.form_action: items["attrs"]["action"] = self.form_action.strip() if self.form_id: items["attrs"]["id"] = self.form_id.strip() if self.form_class: # uni_form TEMPLATE PACK has a uniForm class by default if template_pack == "uni_form": items["attrs"]["class"] = "uniForm %s" % self.form_class.strip() else: items["attrs"]["class"] = self.form_class.strip() else: if template_pack == "uni_form": items["attrs"]["class"] = self.attrs.get("class", "") + " uniForm" items["flat_attrs"] = flatatt(items["attrs"]) if self.inputs: items["inputs"] = self.inputs if self.form_error_title: items["form_error_title"] = self.form_error_title.strip() if self.formset_error_title: items["formset_error_title"] = self.formset_error_title.strip() for attribute_name, value in self.__dict__.items(): if ( attribute_name not in items and attribute_name not in ["layout", "inputs"] and not attribute_name.startswith("_") ): items[attribute_name] = value return items
def get_attributes(self, template_pack=TEMPLATE_PACK): """ Used by crispy_forms_tags to get helper attributes """ items = {} items['form_method'] = self.form_method.strip() items['form_tag'] = self.form_tag items['form_style'] = self.form_style.strip() items['form_show_errors'] = self.form_show_errors items['help_text_inline'] = self.help_text_inline items['error_text_inline'] = self.error_text_inline items['html5_required'] = self.html5_required items['form_show_labels'] = self.form_show_labels items['disable_csrf'] = self.disable_csrf items['label_class'] = self.label_class items['field_class'] = self.field_class label_size_match = re.match('col-lg-(\d+)', self.label_class) if label_size_match: try: items['label_size'] = int(label_size_match.groups()[0]) except: pass items['attrs'] = {} if self.attrs: items['attrs'] = self.attrs.copy() if self.form_action: items['attrs']['action'] = self.form_action.strip() if self.form_id: items['attrs']['id'] = self.form_id.strip() if self.form_class: # uni_form TEMPLATE PACK has a uniForm class by default if template_pack == 'uni_form': items['attrs']['class'] = "uniForm %s" % self.form_class.strip( ) else: items['attrs']['class'] = self.form_class.strip() else: if template_pack == 'uni_form': items['attrs']['class'] = self.attrs.get('class', '') + " uniForm" items['flat_attrs'] = flatatt(items['attrs']) if self.inputs: items['inputs'] = self.inputs if self.form_error_title: items['form_error_title'] = self.form_error_title.strip() if self.formset_error_title: items['formset_error_title'] = self.formset_error_title.strip() for attribute_name, value in self.__dict__.items(): if attribute_name not in items and attribute_name not in [ 'layout', 'inputs' ] and not attribute_name.startswith('_'): items[attribute_name] = value return items
def get_buttons(self): """ Update the buttons to disable form validations since we use our own valdiation and text. """ buttons = super(AssignmentCreateUpdateMixin, self).get_buttons() for button in buttons: button.flat_attrs = flatatt({'formnovalidate': True }) + button.flat_attrs return buttons
def __init__(self, label, *fields, **kwargs): self.fields = list(fields) self.label_html = label self.label_class = kwargs.pop('label_class', u'blockLabel') self.css_class = kwargs.pop('css_class', u'ctrlHolder') self.css_id = kwargs.pop('css_id', None) self.template = kwargs.pop('template', self.template) self.field_template = kwargs.pop('field_template', self.field_template) self.flat_attrs = flatatt(kwargs)
def __init__(self, field_label, *fields, **kwargs): self.fields = list(fields) self.label_html = field_label self.css_class = kwargs.pop("css_class", "") self.css_id = kwargs.pop("css_id", "") self.field_class = kwargs.pop("field_class", None) self.label_class = kwargs.pop("label_class", None) self.help_bubble_text = kwargs.pop("help_bubble_text", "") self.flat_attrs = flatatt(kwargs)
def __init__(self, field_label, *fields, **kwargs): self.fields = list(fields) self.label_html = field_label self.css_class = kwargs.pop('css_class', '') self.css_id = kwargs.pop('css_id', '') self.field_class = kwargs.pop('field_class', None) self.label_class = kwargs.pop('label_class', None) self.help_bubble_text = kwargs.pop('help_bubble_text', '') self.flat_attrs = flatatt(kwargs)
def __init__(self, label, *fields, **kwargs): self.fields = list(fields) self.label_html = label self.label_class = kwargs.pop("label_class", "blockLabel") self.css_class = kwargs.pop("css_class", "ctrlHolder") self.css_id = kwargs.pop("css_id", None) self.help_text = kwargs.pop("help_text", None) self.template = kwargs.pop("template", self.template) self.field_template = kwargs.pop("field_template", self.field_template) self.flat_attrs = flatatt(kwargs)
def __init__(self, label, *fields, **kwargs): self.fields = list(fields) self.label_html = label self.label_class = kwargs.pop('label_class', '') self.css_class = kwargs.pop('css_class', '') self.css_id = kwargs.pop('css_id', None) self.help_text = kwargs.pop('help_text', None) self.template = kwargs.pop('template', self.template) self.field_template = kwargs.pop('field_template', self.field_template) self.flat_attrs = flatatt(kwargs)
def __init__(self, legend, *fields, **kwargs): self.fields = list(fields) self.legend = legend self.css_class = kwargs.pop("css_class", "") self.css_id = kwargs.pop("css_id", None) self.template = kwargs.pop("template", self.template) self.flat_attrs = flatatt(kwargs) self.collapsed = kwargs.pop('collapsed', False) self.div_id = kwargs.pop('div_id', None) or uuid.uuid4().hex[:6]
def __init__(self, *fields, **kwargs): self.fields = list(fields) if hasattr(self, "css_class") and "css_class" in kwargs: self.css_class += " %s" % kwargs.pop("css_class") if not hasattr(self, "css_class"): self.css_class = kwargs.pop("css_class", None) self.css_id = kwargs.pop("css_id", "") self.template = kwargs.pop("template", self.template) self.flat_attrs = flatatt(kwargs)
def __init__(self, name, value, **kwargs): self.name = name self.value = value self.id = kwargs.get('css_id', '') self.attrs = {} if 'css_class' in kwargs: self.field_classes += ' %s' % kwargs.pop('css_class') self.template = kwargs.pop('template', self.template) self.flat_attrs = flatatt(kwargs)
def __init__(self, *fields, **kwargs): self.fields = list(fields) if hasattr(self, 'css_class') and 'css_class' in kwargs: self.css_class += ' %s' % kwargs.pop('css_class') if not hasattr(self, 'css_class'): self.css_class = kwargs.pop('css_class', None) self.css_id = kwargs.pop('css_id', '') self.template = kwargs.pop('template', self.template) self.flat_attrs = flatatt(kwargs)
def get_attributes(self, template_pack=TEMPLATE_PACK): """ Used by crispy_forms_tags to get helper attributes """ items = {} items['form_method'] = self.form_method.strip() items['form_tag'] = self.form_tag items['form_style'] = self.form_style.strip() items['form_show_errors'] = self.form_show_errors items['help_text_inline'] = self.help_text_inline items['error_text_inline'] = self.error_text_inline items['html5_required'] = self.html5_required items['form_show_labels'] = self.form_show_labels items['disable_csrf'] = self.disable_csrf items['label_class'] = self.label_class items['field_class'] = self.field_class label_size_match = re.match('col-lg-(\d+)', self.label_class) if label_size_match: try: items['label_size'] = int(label_size_match.groups()[0]) except: pass items['attrs'] = {} if self.attrs: items['attrs'] = self.attrs.copy() if self.form_action: items['attrs']['action'] = self.form_action.strip() if self.form_id: items['attrs']['id'] = self.form_id.strip() if self.form_class: # uni_form TEMPLATE PACK has a uniForm class by default if template_pack == 'uni_form': items['attrs']['class'] = "uniForm %s" % self.form_class.strip() else: items['attrs']['class'] = self.form_class.strip() else: if template_pack == 'uni_form': items['attrs']['class'] = self.attrs.get('class', '') + " uniForm" items['flat_attrs'] = flatatt(items['attrs']) if self.inputs: items['inputs'] = self.inputs if self.form_error_title: items['form_error_title'] = self.form_error_title.strip() if self.formset_error_title: items['formset_error_title'] = self.formset_error_title.strip() for attribute_name, value in self.__dict__.items(): if attribute_name not in items and attribute_name not in ['layout', 'inputs'] and not attribute_name.startswith('_'): items[attribute_name] = value return items
def render(self, form, form_style, context, template_pack=None): template_pack = template_pack or get_template_pack() template = self.template.format(template_pack=template_pack) context.update( { "button_text": self.button_text, "button_url": self.button_url, "button_attrs": flatatt(self.attrs if isinstance(self.attrs, dict) else {}), } ) return render_to_string(template, context)
def __init__(self, name, value, **kwargs): self.name = name self.value = value self.id = kwargs.pop('css_id', '') self.attrs = {} if 'css_class' in kwargs: self.field_classes += ' %s' % kwargs.pop('css_class') self.template = kwargs.pop('template', self.template) self.flat_attrs = flatatt(kwargs)
def __init__(self, name, value, **kwargs): self.name = name self.value = value self.id = kwargs.pop("css_id", "") self.attrs = {} if "css_class" in kwargs: self.field_classes += " %s" % kwargs.pop("css_class") self.template = kwargs.pop("template", self.template) self.flat_attrs = flatatt(kwargs)
def _render(self, form, form_style, context, template_pack=TEMPLATE_PACK): attrs = self.get_attrs(form, form_style, context, template_pack=template_pack) if 'href' not in attrs: attrs['href'] = self.url_encoder and self.url_encoder(form) or 'javascript:;' context.update({ 'text': self.text, 'field_classes': self.field_classes, 'flat_attrs': flatatt(attrs), }) return render_to_string(self.template, context)
def __init__(self, value, icon=None, position=None, **kwargs): self.value = value self.icon = icon self.position = position self.id = kwargs.pop('css_id', '') self.attrs = {} self.field_classes = '' if 'css_class' in kwargs: self.field_classes += kwargs.pop('css_class') self.template = kwargs.pop('template', self.template) self.flat_attrs = flatatt(kwargs)
def __init__(self, primary_field, secondary_field, **kwargs): self.field1 = primary_field self.field2 = secondary_field self.label_html = kwargs.pop('label', u'unset') self.label_class = kwargs.pop('label_class', u'control-label') self.css_class = kwargs.pop('css_class', u'') self.css_id = kwargs.pop('css_id', None) self.template = kwargs.pop('template', self.template) self.field_template1 = kwargs.pop('template_field1', self.field_template1) self.field_template2 = kwargs.pop('template_field2', self.field_template2) self.flat_attrs = flatatt(kwargs)
def __init__(self, name, value, contents, **kwargs): self.name = name self.value = value self.contents = contents self.id = kwargs.pop('css_id', '') self.input_type = kwargs.pop('input_type', self.input_type) self.attrs = {} if kwargs.has_key('css_class'): self.field_classes += ' %s' % kwargs.pop('css_class') self.template = kwargs.pop('template', self.template) self.flat_attrs = flatatt(kwargs)
def __init__(self, formset, allow_blank=False, **kwargs): self.css_class = kwargs.pop('css_class', '') self.css_id = "%s-group" % formset.prefix self.template = formset.style.template self.inline_style = formset.style.name if allow_blank and len(formset) == 0: self.template = 'website/edit_inline/blank.tpl' self.inline_style = 'blank' self.formset = formset self.model = formset.model self.opts = formset.model._meta self.flat_attrs = flatatt(kwargs) self.extra_attrs = formset.style.get_attrs()
def __init__(self, button_text, **kwargs): if hasattr(self, 'css_class') and 'css_class' in kwargs: self.css_class += ' %s' % kwargs.pop('css_class') if not hasattr(self, 'css_class'): self.css_class = kwargs.pop('css_class', None) self.css_id = kwargs.pop('css_id', '') self.template = kwargs.pop('template', self.template) self.modal_id = kwargs.pop('modal_id', '') self.content_source = kwargs.pop('content_source', '') self.flat_attrs = flatatt(kwargs) self.button_text = button_text
def __init__(self, content, **kwargs): self.content = content self.template = kwargs.pop('template', self.template) kwargs.setdefault('type', 'button') # We turn css_id and css_class into id and class if 'css_id' in kwargs: kwargs['id'] = kwargs.pop('css_id') kwargs['class'] = self.field_classes if 'css_class' in kwargs: kwargs['class'] += " %s" % kwargs.pop('css_class') self.flat_attrs = flatatt(kwargs)
def _render(self, form, form_style, context, template_pack=TEMPLATE_PACK): attrs = self.get_attrs(form, form_style, context, template_pack=template_pack) attrs[self.element_attr] = self.url_encoder(form, form_style, context, *self.included_params, **self.url_encoder_kwargs) context.update({ 'form': form, 'field_classes': self.field_classes, 'value': self.value, 'icon_class': self.icon_class, 'icon_first': self.icon_first, 'flat_attrs': flatatt(attrs), 'tag': getattr(self, 'tag', None), }) return render_to_string(self.template, context)
def __init__(self, content, **kwargs): super(YouthmapStrictButton, self).__init__(content, **kwargs) self.content = content self.template = kwargs.pop('template', self.template) kwargs.setdefault('type', 'add') kwargs.setdefault('name', self.content) # We turn css_id and css_class into id and class if 'css_id' in kwargs: kwargs['id'] = kwargs.pop('css_id') kwargs['class'] = self.field_classes if 'css_class' in kwargs: kwargs['class'] += " %s" % kwargs.pop('css_class') self.flat_attrs = flatatt(kwargs)
def _render(self, form, form_style, context, template_pack=TEMPLATE_PACK): attrs = self.get_attrs(form, form_style, context, template_pack=template_pack) fields = [] bound_field = None if self.field: bound_field = get_bound_field(context, form, self.field) for field in self.fields: if bound_field is None and isinstance(field, six.string_types): bound_field = get_bound_field(context, form, field) fields.append(render_field(field, form, form_style, context, template=self.field_template, template_pack=template_pack)) return render_to_string(self.template, { 'input_size': self.input_size, 'active': getattr(self, 'active', False), 'fields': fields, 'field': bound_field, 'flat_attrs': flatatt(attrs), }, context)
def _render(self, form, form_style, context, template_pack=TEMPLATE_PACK): attrs = self.get_attrs(form, form_style, context, template_pack=template_pack) field = self.fields[0] if not hasattr(field, 'render'): bound_field = get_bound_field(context, form, field) if bound_field: field = RawField(field) else: field = HTML(field) fields = render_field(field, form, form_style, context, template_pack=template_pack) css_class = attrs.pop('class', self.css_class) context.update({ 'tag': self.tag, 'form': form, 'div': self, 'fields': fields, 'css_class': css_class, 'flat_attrs': flatatt(attrs) }) return render_to_string(self.template, context)
def _render(self, form, form_style, context, template_pack=TEMPLATE_PACK): attrs = self.get_attrs(form, form_style, context, template_pack=template_pack) fields_dict = OrderedDict() url_encoder_kwargs = self.url_encoder_kwargs all_params = self.params_retriever(context, **self.params_retriever_kwargs) for _key, params in all_params.items(): fields_dict.setdefault(_key, {}) fields_dict[_key].setdefault('_display', params['_display']) fields_dict[_key].setdefault('item_list', []) for label, included_params, url_encoder_kwargs in params['item_list']: _url_encoder_kwargs = self.url_encoder_kwargs.copy() _url_encoder_kwargs.update(url_encoder_kwargs) fields_dict[_key]['item_list'].append(render_field( LoadLazyFormSetFormAnchor( self.field_name, label, included_params=included_params, url_encoder_kwargs=_url_encoder_kwargs, icon_class=self.icon_class, ), form, form_style, context, template_pack=template_pack )) css_class = attrs.pop('class', self.css_class) return render_to_string(self.template, { 'form': form, 'css_id': self.css_id, 'css_class': css_class, 'flat_attrs': flatatt(attrs), 'fields': fields_dict.items(), })
def _render(self, form, form_style, context, template_pack=TEMPLATE_PACK): attrs = self.get_attrs(form, form_style, context, template_pack=template_pack) fields = [] for loader in self.loaders: fields.append( render_field( loader, form, form_style, context, template_pack=template_pack ) ) css_class = attrs.pop('class', self.css_class) return render_to_string(self.template, { 'form': form, 'css_id': self.css_id, 'css_class': css_class, 'flat_attrs': flatatt(attrs), 'fields': fields, })
def __init__(self, field_label, *fields, **kwargs): self.fields = list(fields) self.label_html = field_label self.flat_attrs = flatatt(kwargs)
def __init__(self, *fields, **kwargs): self.fields = flatten_list(fields) self.initial = kwargs.pop('data', None) is None self.attrs = kwargs self.flat_attrs = flatatt(self.attrs)
def flat_attrs(self): return flatatt(self.attrs)
def get_attributes(self, template_pack=TEMPLATE_PACK): # noqa: C901 """ Used by crispy_forms_tags to get helper attributes """ items = { "disable_csrf": self.disable_csrf, "error_text_inline": self.error_text_inline, "field_class": self.field_class, "field_template": self.field_template or "%s/field.html" % template_pack, "form_method": self.form_method.strip(), "form_show_errors": self.form_show_errors, "form_show_labels": self.form_show_labels, "form_style": self.form_style.strip(), "form_tag": self.form_tag, "help_text_inline": self.help_text_inline, "html5_required": self.html5_required, "include_media": self.include_media, "label_class": self.label_class, "use_custom_control": self.use_custom_control, } if template_pack == "bootstrap4": if "form-horizontal" in self.form_class.split(): bootstrap_size_match = re.findall(r"col(-(xl|lg|md|sm))?-(\d+)", self.label_class) if bootstrap_size_match: offset_pattern = "offset%s-%s" items["bootstrap_checkbox_offsets"] = [ offset_pattern % (m[0], m[-1]) for m in bootstrap_size_match ] else: bootstrap_size_match = re.findall(r"col-(lg|md|sm|xs)-(\d+)", self.label_class) if bootstrap_size_match: offset_pattern = "col-%s-offset-%s" items["bootstrap_checkbox_offsets"] = [offset_pattern % m for m in bootstrap_size_match] items["attrs"] = {} if self.attrs: items["attrs"] = self.attrs.copy() if self.form_action: items["attrs"]["action"] = self.form_action.strip() if self.form_id: items["attrs"]["id"] = self.form_id.strip() if self.form_class: # uni_form TEMPLATE PACK has a uniForm class by default if template_pack == "uni_form": items["attrs"]["class"] = "uniForm %s" % self.form_class.strip() else: items["attrs"]["class"] = self.form_class.strip() else: if template_pack == "uni_form": items["attrs"]["class"] = self.attrs.get("class", "") + " uniForm" if self.form_group_wrapper_class: items["attrs"]["form_group_wrapper_class"] = self.form_group_wrapper_class items["flat_attrs"] = flatatt(items["attrs"]) if self.inputs: items["inputs"] = self.inputs if self.form_error_title: items["form_error_title"] = self.form_error_title.strip() if self.formset_error_title: items["formset_error_title"] = self.formset_error_title.strip() for attribute_name, value in self.__dict__.items(): if ( attribute_name not in items and attribute_name not in ["layout", "inputs"] and not attribute_name.startswith("_") ): items[attribute_name] = value return items
def __init__(self, field, **kwargs): self.field = field self.attrs = {} self.template = kwargs.pop("template", self.template) self.flat_attrs = flatatt(kwargs)
def get_attributes(self, template_pack=TEMPLATE_PACK): """ Used by crispy_forms_tags to get helper attributes """ items = { 'form_method': self.form_method.strip(), 'form_tag': self.form_tag, 'form_style': self.form_style.strip(), 'form_show_errors': self.form_show_errors, 'help_text_inline': self.help_text_inline, 'error_text_inline': self.error_text_inline, 'html5_required': self.html5_required, 'form_show_labels': self.form_show_labels, 'disable_csrf': self.disable_csrf, 'label_class': self.label_class, 'field_class': self.field_class, 'include_media': self.include_media } # col-[lg|md|sm|xs]-<number> label_size_match = re.search('(\d+)', self.label_class) device_type_match = re.search('(lg|md|sm|xs)', self.label_class) if label_size_match and device_type_match: try: items['label_size'] = int(label_size_match.groups()[0]) items['bootstrap_device_type'] = device_type_match.groups()[0] except: pass items['attrs'] = {} if self.attrs: items['attrs'] = self.attrs.copy() if self.form_action: items['attrs']['action'] = self.form_action.strip() if self.form_id: items['attrs']['id'] = self.form_id.strip() if self.form_class: # uni_form TEMPLATE PACK has a uniForm class by default if template_pack == 'uni_form': items['attrs']['class'] = "uniForm %s" % self.form_class.strip( ) else: items['attrs']['class'] = self.form_class.strip() else: if template_pack == 'uni_form': items['attrs']['class'] = self.attrs.get('class', '') + " uniForm" items['flat_attrs'] = flatatt(items['attrs']) if self.inputs: items['inputs'] = self.inputs if self.form_error_title: items['form_error_title'] = self.form_error_title.strip() if self.formset_error_title: items['formset_error_title'] = self.formset_error_title.strip() for attribute_name, value in self.__dict__.items(): if attribute_name not in items and attribute_name not in [ 'layout', 'inputs' ] and not attribute_name.startswith('_'): items[attribute_name] = value return items
def __init__(self, name, value, icon_cssclass=None, **kwargs): self.extra_button_attributes = flatatt(self.get_extra_button_attributes()) self.icon_cssclass = icon_cssclass super(CradminSubmitButton, self).__init__(name, value, **kwargs) self.field_classes = self.button_css_classes
def flatatt_filter(attrs): return mark_safe(flatatt(attrs))
def get_attributes(self, template_pack=TEMPLATE_PACK): """ Used by crispy_forms_tags to get helper attributes """ items = { 'form_method': self.form_method.strip(), 'form_tag': self.form_tag, 'form_style': self.form_style.strip(), 'form_show_errors': self.form_show_errors, 'help_text_inline': self.help_text_inline, 'error_text_inline': self.error_text_inline, 'html5_required': self.html5_required, 'form_show_labels': self.form_show_labels, 'disable_csrf': self.disable_csrf, 'label_class': self.label_class, 'field_class': self.field_class, 'include_media': self.include_media } if template_pack == 'bootstrap4': bootstrap_size_match = re.findall('col-(xl|lg|md|sm)-(\d+)', self.label_class) if bootstrap_size_match: if template_pack == 'bootstrap4': offset_pattern = 'offset-%s-%s' else: offset_pattern = 'col-%s-offset-%s' items['bootstrap_checkbox_offsets'] = [offset_pattern % m for m in bootstrap_size_match] else: bootstrap_size_match = re.findall('col-(lg|md|sm|xs)-(\d+)', self.label_class) if bootstrap_size_match: if template_pack == 'bootstrap4': offset_pattern = 'offset-%s-%s' else: offset_pattern = 'col-%s-offset-%s' items['bootstrap_checkbox_offsets'] = [offset_pattern % m for m in bootstrap_size_match] items['attrs'] = {} if self.attrs: items['attrs'] = self.attrs.copy() if self.form_action: items['attrs']['action'] = self.form_action.strip() if self.form_id: items['attrs']['id'] = self.form_id.strip() if self.form_class: # uni_form TEMPLATE PACK has a uniForm class by default if template_pack == 'uni_form': items['attrs']['class'] = "uniForm %s" % self.form_class.strip() else: items['attrs']['class'] = self.form_class.strip() else: if template_pack == 'uni_form': items['attrs']['class'] = self.attrs.get('class', '') + " uniForm" if self.form_group_wrapper_class: items['attrs']['form_group_wrapper_class'] = self.form_group_wrapper_class items['flat_attrs'] = flatatt(items['attrs']) if self.inputs: items['inputs'] = self.inputs if self.form_error_title: items['form_error_title'] = self.form_error_title.strip() if self.formset_error_title: items['formset_error_title'] = self.formset_error_title.strip() for attribute_name, value in self.__dict__.items(): if attribute_name not in items and attribute_name not in ['layout', 'inputs'] and not attribute_name.startswith('_'): items[attribute_name] = value return items