def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name,) output = ['<ul class="cols">'] # Normalize to strings str_values = set([force_text(v) for v in value]) for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: if option_label.status_unit in (2,): final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i),) if 'disabled' in final_attrs: del final_attrs['disabled'] else: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i),) final_attrs['disabled'] = 'disabled' label_for = format_html(' for="{0}"', final_attrs['id']) else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_text(option_value) rendered_cb = cb.render(name, option_value) option_label = force_text(option_label) output.append(format_html('<li><label{0}>{1} <span>{2}</span></label></li>', label_for, rendered_cb, option_label)) output.append('</ul>') return mark_safe('\n'.join(output))
def render(self,name,value,attrs=None,renderer=None,choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs) #, name=name) output = ['<ul>'] str_values = set([v for v in value]) for (i, (option_value, option_label)) in enumerate(chain(self.choices, choices)): if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = ' for="%s"' % final_attrs['id'] else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) rendered_cb = cb.render(name, option_value) option_label = conditional_escape(option_label) x = option_label.split('|') if len(x) > 1: icon = x[0] option_label = x[1] else: icon = None if icon: image = "<img src='%s' />" % icon else: image = '' output.append('<li><label%s>%s %s %s</label></li>' % (label_for, rendered_cb, image, option_label)) output.append('</ul>') return mark_safe('\n'.join(output))
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) output = [u'<ul style="display:none">'] # Normalize to strings str_values = set([force_unicode(v) for v in value]) for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = u' for="%s"' % final_attrs['id'] else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) rendered_cb = cb.render(name, option_value) option_label = conditional_escape(force_unicode(option_label)) output.append(u'<li><label%s>%s %s</label></li>' % (label_for, rendered_cb, option_label)) output.append(u'</ul>') return mark_safe(u'\n'.join(output))
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) items = [] # list of dictionaries, contains: # extra_class, rendered_cb, content str_values = set([force_unicode(v) for v in value]) # Normalize to strings. for i, (option_value, item) in enumerate(self.choices): option_value = force_unicode(option_value) check_test = lambda value: value in str_values # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) final_attrs.update({'onClick': 'change_cb(this)'}) cb = CheckboxInput(final_attrs, check_test) rendered_cb = cb.render(name, option_value) cb = {"extra_class": 'image_selected' if check_test(option_value) else ""} cb["rendered_cb"] = rendered_cb cb["content"] = create_content(item, self.item_attrs) items.append(cb) return render_to_string("assets/admin/hero_image.html", {"items": items})
def render(self, name, value, attrs=None, choices=()): #print name, value, self.attrs, choices if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) #print final_attrs output = [u'<table id="%s">' % self.table_id] # Normalize to strings str_values = set([django.utils.encoding.force_unicode(v) for v in value]) for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): #print option_value, option_label # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = u' for="%s"' % final_attrs['id'] extra_columns_rendered = "".join( [ "<td>%s</td>" % column.render(option_value) for column in self.extra_columns ] ) else: label_for = u'' extra_columns_rendered = u'' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = django.utils.encoding.force_unicode(option_value) rendered_cb = cb.render(name, option_value) option_label = conditional_escape(django.utils.encoding.force_unicode(option_label)) # render the extra columns output.append(u'<tr><td><label%s>%s</label></td><td>%s</td>%s</tr>' % (label_for, option_label, rendered_cb, extra_columns_rendered)) output.append(u'</table>') return mark_safe(u'\n'.join(output))
def render_checkbox(self, final_attrs, option_value, option_label, str_values, name, label_for, has_children=False): cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_text(option_value) rendered_cb = cb.render(name, option_value) option_label = force_text(option_label) return u'''<div class="onoffswitch-wrapper"> %s <div class="onoffswitch-wrap"> <div class="onoffswitch-text">%s</div> <div class="onoffswitch"> %s <label class="onoffswitch-label" %s> <div class="onoffswitch-inner"></div> <div class="onoffswitch-switch"></div> </label> </div> <div class="onoffswitch-text">%s</div> </div> </div> <br clear="all" /> ''' % ( option_label, _(u'unblocked'), rendered_cb, label_for, _(u'blocked') )
def checkbox_inputs(field): if isinstance(field.field.widget, CheckboxInput): return [mark_safe(u'<label>%s<span>%s</span></label>' % (field, field.label))] name = field.name value = field.value() attrs = field.field.widget.attrs choices = field.field.choices if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = attrs.copy() final_attrs.update({'name': name}) # Normalize to strings str_values = set([force_unicode(v) for v in value]) checkbox_inputs = [] disabled_list = [] if isinstance(final_attrs.get('disabled'), collections.Iterable): disabled_list = final_attrs.pop('disabled') for i, (option_value, option_label) in enumerate(choices): _final_attrs = final_attrs.copy() # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: _final_attrs = dict(_final_attrs, id='%s_%s' % (final_attrs['id'], i)) if option_value in disabled_list: _final_attrs.update({'disabled': True}) cb = CheckboxInput(_final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) rendered_cb = cb.render(name, option_value) checkbox_inputs.append(mark_safe( u'<label>%s<span>%s</span></label>' % (rendered_cb, option_label) )) return checkbox_inputs
def render(self, name, value, attrs=None, choices=()): mark_all='' if value is None: value = [] mark_all = "markall" has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) output = ['<ul class="checkboxes">'] # Normalize to strings str_values = set([force_text(v) for v in value]) for i, (option_value, option_label) in enumerate(itertools.chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = format_html(' for="{0}"', final_attrs['id']) else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = (option_value) rendered_cb = cb.render(name, option_value) option_label = force_text(option_label) output.append(format_html('<li><label{0}>{1} {2}</label></li>', label_for, rendered_cb, option_label)) output.append('</ul>') output.append(format_html('<a href="#" data-elemento="{0}" class="checkbutton btn {1}">Desmarcar Todos </a>', name, mark_all)) return mark_safe('\n'.join(output))
def render_checkbox(self, final_attrs, option_value, option_label, str_values, name, label_for): from .models import RewardCategory checkbox = CheckboxInput( final_attrs, check_test=lambda value: value in str_values) option_value = force_text(option_value) rendered_cb = checkbox.render(name, option_value) option_label = force_text(option_label) # could do this in one go possibly above reward_category = RewardCategory.objects.get(pk=option_value) if reward_category.reward_first_ad: reward_text = mark_safe( u"<span class='alert alert-info' style='padding:2px;'>Voor " u"deze categorie geldt een extra waardering *</span>") return format_html( u'<div class="element checkbox-container"><label{0}>' u'{1} {2} - {3}</label>' u'</div><div class="clearfix"></div>', label_for, rendered_cb, option_label, reward_text ) else: return format_html( u'<div class="element checkbox-container"><label{0}>' u'{1} {2}</label><span class="clearfix"></span>' u'</div>', label_for, rendered_cb, option_label )
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) output = [] # Normalize to strings str_values = set([force_unicode(v) for v in value]) for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = u' for="%s"' % final_attrs['id'] else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) # Obtenemos el HTML del widget rendered_cb = cb.render(name, option_value) if str_values.intersection([u'JE']) and option_value != 'JE': # Calculamos la subcadena antes del cierre del tag (' >') n = len(rendered_cb) - 3 # Añadimos disabled al tag rendered_cb = rendered_cb[:n] + " disabled" + rendered_cb[n:] # totales es una lista de enteros con el resumen de las faltas del día if len(self.totales) > i: cad = u'<td>%s(%d)</td>' % (rendered_cb, self.totales[i]) else: cad = u'<td>%s</td>' % rendered_cb output.append(cad) return mark_safe(u'\n'.join(output))
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) output = ['<ul class="clear_ul">'] # Normalize to strings str_values = set([force_text(v) for v in value]) for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = format_html(' for="{0}"', final_attrs['id']) else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_text(option_value) rendered_cb = cb.render(name, option_value) option_label = force_text(option_label) count = Recipient.objects.filter(group_id=option_value).count() output.append(format_html('<li class="checkbox-container"><label{0}>{1} {2}</label> <span class="badge group-info">{3}</span></li>', label_for, rendered_cb, option_label, count)) output.append('</ul>') return mark_safe('\n'.join(output))
def render(self, name, value, attrs=None, choices=()): # based entirely on CheckboxSelectMultiple.render, except doesn't use a # <ul> if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) output = [u'<span>'] # Normalize to strings str_values = set([force_unicode(v) for v in value]) for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = u' for="%s"' % final_attrs['id'] else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) rendered_cb = cb.render(name, option_value) option_label = conditional_escape(force_unicode(option_label)) output.append(u'<span><label%s>%s %s</label></span>' % (label_for, rendered_cb, option_label)) output.append(u'</span>') return mark_safe(u'\n'.join(output))
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and "id" in attrs final_attrs = self.build_attrs(attrs, name=name) output = [u"<ul>"] # Normalize to strings str_values = set([force_unicode(v) for v in value]) for i, (option_value, option_label, option_help_text) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id="%s_%s" % (attrs["id"], i)) label_for = u' for="%s"' % final_attrs["id"] else: label_for = "" cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) rendered_cb = cb.render(name, option_value) option_label = conditional_escape(force_unicode(option_label)) output.append( u"<li><label%s>%s %s</label><p class='help'>%s</p></li>" % (label_for, rendered_cb, option_label, option_help_text) ) output.append(u"</ul>") return mark_safe(u"\n".join(output))
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) output = [u'<ul>'] # Normalize to strings str_values = set([force_unicode(v) for v in value]) for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = u' for="%s"' % final_attrs['id'] else: label_for = '' town_id = option_value if town_id in self.sda_mapping['town_maps'].keys() and self.sda_mapping['town_maps'][town_id] != '': rendered_map_button = """<button class='sda_map_button' onclick='return emerald.show_sda_map(event, "%s")'>map</button>""" % town_id else: rendered_map_button = """<button class='sda_map_button_disabled' onclick='return false'>map</button>""" cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) rendered_cb = cb.render(name, option_value) option_label = conditional_escape(force_unicode(option_label)) output.append(u'<li><label%s>%s %s %s</label></li>' % (label_for, rendered_map_button, rendered_cb, option_label)) output.append(u'</ul>') return mark_safe(u'\n'.join(output))
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) # Normalize to strings str_values = set([force_unicode(v) for v in value]) output = [u'<div class="groupbox">'] for z, g in enumerate(self.choices): output.append(u'<div class="groupbox-header">%s <span class="group-count">(<span></span>)</span></div>' % g[0]) output.append(u'<ul class="groupbox-list">') for i, (option_value, option_label) in enumerate(g[1]): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s_%s' % (attrs['id'], z, i)) label_for = u' for="%s"' % final_attrs['id'] else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) rendered_cb = cb.render(name, option_value) option_label = conditional_escape(force_unicode(option_label)) output.append(u'<li><label%s>%s %s</label></li>' % (label_for, rendered_cb, option_label)) output.append(u'</ul>') output.append(u'</div>') return mark_safe(u'\n'.join(output))
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) output = [u'<table class="permission_checkbox_table formtable_noborder">'] output.append(u' <tr>') output.append(u' <th></th>') output.append(u' <th>Name</th>') output.append(u' <th>Target</th>') output.append(u' </tr>') # Normalize to strings str_values = set([force_unicode(v) for v in value]) # Original code: multiple permissions were being shown # for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): for i, (option_value, option_label) in enumerate(chain(set(chain(self.choices, choices)))): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) row_id = u' "row_%s"' % final_attrs['id'] else: row_id = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) rendered_cb = cb.render(name, option_value) option_label = conditional_escape(force_unicode(option_label)) output.append(u' <tr id=%s>' % row_id) output.append(u' <td>%s</td>' % rendered_cb) output.append(u' %s' % option_label) output.append(u' </tr>') output.append(u'</table>') return mark_safe(u'\n'.join(output))
class UserPermissionsForm(forms.ModelForm): """ Custom user form based on the User model """ # attributes checkbox_attr = {'class': 'form-check-input'} is_module_leader = forms.BooleanField( required=False, widget=CheckboxInput(attrs=checkbox_attr)) is_office_admin = forms.BooleanField( required=False, widget=CheckboxInput(attrs=checkbox_attr)) is_year_tutor = forms.BooleanField( required=False, widget=CheckboxInput(attrs=checkbox_attr)) is_module_reviewer = forms.BooleanField( required=False, widget=CheckboxInput(attrs=checkbox_attr)) is_admin = forms.BooleanField(required=False, widget=CheckboxInput(attrs=checkbox_attr)) class Meta: model = User fields = ('is_module_leader', 'is_office_admin', 'is_year_tutor', 'is_module_reviewer', 'is_admin')
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs inline = attrs and attrs.get('inline', False) final_attrs = self.build_attrs(attrs, name=name) output = [ ] str_values = set([force_text(v) for v in value]) for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): if has_id: final_attrs = dict(final_attrs, id="%s_%s" % (attrs[id], i)) label_for = format_html(' for="{0}"', final_attrs['id']) else: label_for = '' if inline: label_class = "checkbox inline" else: label_class = "checkbox" label_class = format_html(' class="{0}"', label_class) cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_text(option_value) rendered_cb = cb.render(name, option_value) option_label = force_text(option_label) output.append(format_html('<label{0}{1}>{2} {3}</label>', label_for, label_class, rendered_cb, option_label)) return mark_safe(u'\n'.join(output))
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) output = [u'<div class="tag-wrapper">'] # Normalize to strings str_values = set([force_unicode(v) for v in value]) for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = u' for="%s"' % final_attrs['id'] else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) rendered_cb = cb.render(name, option_value) option_label = conditional_escape(force_unicode(option_label)) output.append(u'<span class="ui-toggle-button ui-tag">%s<label%s>%s</label></span>' % (rendered_cb, label_for, option_label)) """ <input type="checkbox" id="check3" /><label for="check3">U</label> """ output.append(u'</div>') return mark_safe(u' '.join(output))
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) output = [u'<div class="row">'] # Normalize to strings str_values = set([force_unicode(v) for v in value]) for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = u' for="%s"' % final_attrs['id'] else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) rendered_cb = cb.render(name, option_value) option_label = conditional_escape(force_unicode(option_label)) output.append(u'<div class="span2"><label%s>%s %s</label></div>' % (label_for, rendered_cb, option_label)) ultimo_fila=(((i+1) % 6) == 0) if ultimo_fila: output.append(u'</div>') output.append(u'<div class="row">') # Normalize to strings output.append(u'</div>') return mark_safe(u'\n'.join(output))
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) output = [u'<div>'] # Normalize to strings str_values = set([force_unicode(v) for v in value]) key = '' for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): if option_value in self.all_fields['user']['fields']: key = 'user' elif option_value in self.all_fields['membership']['fields']: key = 'membership' elif option_value in self.all_fields['membership_type']['fields']: key = 'membership_type' elif option_value in self.all_fields['payment']['fields']: key = 'payment' elif option_value in self.all_fields['user_group']['fields']: key = 'user_group' elif option_value in self.all_fields['industry']['fields']: key = 'industry' elif option_value in self.all_fields['region']['fields']: key = 'region' elif option_value in self.all_fields['admin']['fields']: key = 'admin' if key: self.all_fields[key]['options'].append( (i, option_value, option_label)) for key in self.all_fields.keys(): output.append(u'<div style="clear: both;"></div>') output.append(u'<h3>') output.append(self.all_fields[key]['title']) output.append(u'</h3>') output.append(u'<div class="fields-section">') for i, option_value, option_label in \ self.all_fields[key]['options']: # If an ID attribute was given, add a numeric index as a # suffix, so that the checkboxes don't all have the same # ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = u' for="%s"' % final_attrs['id'] else: label_for = '' cb = CheckboxInput( final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) rendered_cb = cb.render(name, option_value) option_label = conditional_escape(force_unicode(option_label)) output.append( u'<div class="field-box select-field"><label%s>%s %s</label></div>' % (label_for, rendered_cb, option_label)) output.append(u'</div>') output.append(u'</div>') return mark_safe(u'\n'.join(output))
def render(self, name, value, attrs=None, choices=()): PERSON_COL = 3 tmpcol = 1 old_group = None if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) output = [u'<table width="100%">'] # Normalize to strings str_values = set([force_unicode(v) for v in value]) for i, (option_value, user) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = u' for="%s"' % final_attrs['id'] else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) rendered_cb = cb.render(name, force_unicode(option_value)) #do group if old_group != user.get_profile().company: if tmpcol > 1: for i in range(tmpcol, PERSON_COL + 1): output.append('<td></td>') output.append(u'</tr>') output.append( u'<tr><td colspan="%(col)s"><p style="margin: 15px 0 10px 0;"><a class="smallcaps_title blue_bg white" href="">%(name)s</a></p></td></tr>' % { 'col': PERSON_COL, 'name': user.get_profile().company.short_name }) tmpcol = 1 old_group = user.get_profile().company #do user u = self.myrender(user, label_for, rendered_cb) if tmpcol == 1: u = '<tr>' + u tmpcol += 1 elif tmpcol == PERSON_COL: u += '</tr>' tmpcol = 1 else: tmpcol += 1 output.append(u) if tmpcol > 1: for i in range(tmpcol, PERSON_COL + 1): output.append('<td></td>') output.append(u'</tr>') output.append(u'</table>') return mark_safe(u'\n'.join(output))
class Meta: model = Setting fields = ('hidden_note', 'wallpaper', 'theme') widgets = { 'wallpaper': FileInput(), 'hidden_note': CheckboxInput(attrs={'id': 'hidden_note_id'}), 'theme': CheckboxInput(attrs={'id': 'theme_id'}), }
def __init__(self, widget, empty_value=None): """ Remebers the widget we are wrapping and precreates a checkbox input. Allows overriding the empty value. """ self.widget = widget self.checkbox = CheckboxInput() self.empty_value = empty_value
def render_option(self, attrs, name, selected_choices, option_value, option_label): container_attrs = attrs['container_attrs'] data_attrs = attrs['data_attrs'] img_url = settings.STATIC_URL+'i/characters/%s.png' % option_value item_image = '<img src="%s" alt="%s" title="%s" />' % (img_url, option_label, option_label) cb = CheckboxInput(data_attrs, check_test=lambda x: x in selected_choices) rendered_cb = cb.render(name, option_value) return mark_safe(u'<span%s>%s%s</span>' % (flatatt(container_attrs), rendered_cb, item_image))
class Meta: model = ServiceProvider fields = [ 'uses_ldapauth', 'server_names', 'target_group', 'service_account', 'service_account_contact', 'can_access_all_ldap_groups', 'local_storage_users', 'local_storage_passwords', 'local_storage_passwords_info', 'local_storage_groups', 'admin_require_manual_configuration', 'production' ] widgets = { 'service_account_contact': Textarea(attrs={'rows': 2}), 'local_storage_passwords_info': Textarea(attrs={'rows': 5}), 'service_account': CheckboxInput(attrs={'class': 'hideCheck1'}), 'local_storage_passwords': CheckboxInput(attrs={'class': 'hideCheck2'}), } help_texts = { 'uses_ldapauth': _('Does this service use the LDAPAuth proxy in order to access ' 'user and group data for authentication and access control?'), 'server_names': _('Full server names (not IPs), separated by space. User for access ' 'control.'), 'target_group': _('What is the target group (users) of this service?'), 'service_account': _('Separate service account is used for LDAP queries (recommended way).' ), 'service_account_contact': _('Email and phone number for delivering service account ' 'credentials. Use a [email protected] ' 'address and the same person\'s mobile phone in ' 'non-international format, that is, start it with 0. ' 'Separete the email and phone number by space.'), 'can_access_all_ldap_groups': _('Service requires access to all LDAP groups.'), 'local_storage_users': _('Service stores all users and released attributes locally. ' 'If you only save user data when user logs in, do not check ' 'this.'), 'local_storage_passwords': _('Service stores user passwords locally.'), 'local_storage_passwords_info': _('Why is this service storing user passwords ' 'locally and how? This is not generally a good ' 'idea.'), 'local_storage_groups': _('Service stores requested groups and their member lists ' 'locally.'), 'admin_require_manual_configuration': _('This service provider requires manual configuration. ' 'Set by registry admins if necessary.'), 'production': _('Publish this service to LDAP production servers.'), }
def render_title(self, writer, name, attrs=None): if self.field.check_all and isinstance(writer, HtmlWriter): final_attrs = self.build_title_attrs(base_css=self.css, extra_css=(name.replace('_', '-'),)) tip = ' <i class="icon-question-sign"></i>' if 'data-tip' in final_attrs else '' cb = CheckboxInput({'class': 'datatables-checkall', 'data-checkall': self.field.form_field_name}) title = cb.render('_%s_check_all' % self.field.form_field_name, False) return format_html(u'<span{0} >{1}{2}</span>', flatatt(final_attrs), title, mark_safe(tip)) else: return super(CheckboxWidget, self).render_title(writer, name, attrs)
class Meta: model = Character fields = [ 'name', 'char_class', 'level', 'expansion', 'hardcore', 'ladder' ] widgets = { "ladder": CheckboxInput(attrs={'checked': False}), "expansion": CheckboxInput(attrs={'checked': True}), "hardcore": CheckboxInput(attrs={'checked': False}) }
def render_checkbox(self, final_attrs, option_value, option_label, str_values, name, label_for): checkbox = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_text(option_value) rendered_cb = checkbox.render(name, option_value) option_label = force_text(option_label) return format_html( '<li><div class="element checkbox-container"><label{0}>' '{1} {2}</label></div></li>', label_for, rendered_cb, option_label)
class UserCreationForm(forms.ModelForm): """ Form that is used to create new users. """ # Note: Originally this was a combination of the two forms. # It turns out there is an issue with multiple inheritance # with forms. To resolve it is fairly complicated, so for the time # I have created a form that just contains every attribute. checkbox_attr = {'class': 'form-check-input'} attrs = {'class': 'form-control'} username = forms.CharField(required=True, widget=TextInput(attrs=attrs)) first_name = forms.CharField(required=True, widget=TextInput(attrs=attrs)) last_name = forms.CharField(required=True, widget=TextInput(attrs=attrs)) email = forms.EmailField(required=True, widget=TextInput(attrs=attrs)) # permissions is_module_leader = forms.BooleanField( required=False, widget=CheckboxInput(attrs=checkbox_attr)) is_office_admin = forms.BooleanField( required=False, widget=CheckboxInput(attrs=checkbox_attr)) is_year_tutor = forms.BooleanField( required=False, widget=CheckboxInput(attrs=checkbox_attr)) is_module_reviewer = forms.BooleanField( required=False, widget=CheckboxInput(attrs=checkbox_attr)) is_admin = forms.BooleanField(required=False, widget=CheckboxInput(attrs=checkbox_attr)) class Meta: model = User fields = ('username', 'first_name', 'last_name', 'email', 'is_module_leader', 'is_office_admin', 'is_year_tutor', 'is_module_reviewer', 'is_admin') def save(self, commit=True): user = super(UserCreationForm, self).save(commit=False) password = User.objects.make_random_password() user.set_password(password) # prepare the email mail = UserPasswordEmail(user, password) if commit: # save user and send the email user.save() mail.send() return user
def render_checkbox(self, final_attrs, option_value, option_label, str_values, name, label_for): checkbox = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_text(option_value) rendered_cb = checkbox.render(name, option_value) option_label = force_text(option_label) return format_html( '<div class="form-group"><div class="checkbox">' '<label{0}>{1}<span> {2}</span></label></div></div>', label_for, rendered_cb, option_label)
def render(self, name, value, attrs=None, choices=()): PERSON_COL=3 tmpcol = 1 old_group = None if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) output = [u'<table width="100%">'] # Normalize to strings str_values = set([force_unicode(v) for v in value]) for i, (option_value, user) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = u' for="%s"' % final_attrs['id'] else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) rendered_cb = cb.render(name, force_unicode(option_value)) #do group if old_group != user.get_profile().company: if tmpcol > 1 : for i in range(tmpcol, PERSON_COL+1 ): output.append( '<td></td>' ) output.append(u'</tr>') output.append( u'<tr><td colspan="%(col)s"><p style="margin: 15px 0 10px 0;"><a class="smallcaps_title blue_bg white" href="">%(name)s</a></p></td></tr>'%{'col': PERSON_COL, 'name': user.get_profile().company.short_name} ) tmpcol = 1 old_group = user.get_profile().company #do user u = self.myrender(user, label_for, rendered_cb) if tmpcol == 1: u = '<tr>' + u tmpcol += 1 elif tmpcol == PERSON_COL: u += '</tr>' tmpcol = 1 else: tmpcol += 1 output.append( u ) if tmpcol > 1 : for i in range(tmpcol, PERSON_COL+1 ): output.append( '<td></td>' ) output.append(u'</tr>') output.append(u'</table>') return mark_safe(u'\n'.join(output))
class Meta: model = Article fields = [ 'title', 'sw_title', 'cover_photo', 'content', 'sw_content', 'category', 'belong_to', 'active', 'display_cover_photo_on_view_article' ] widgets = { 'title': TextInput(attrs={ 'class': 'form-control', 'placeholder': 'enter title' }), 'sw_title': TextInput( attrs={ 'class': 'form-control', 'placeholder': 'ingiza kichwa cha makala', 'required': 'true' }), 'cover_photo': FileInput(attrs={ 'class': 'form-control', 'placeholder': 'enter cover photo' }), 'content': Textarea( attrs={ 'class': 'form-control no-resize summernote', 'placeholder': 'Please type what you want...', 'rows': 40, 'style': 'display: none;' }), 'sw_content': Textarea( attrs={ 'class': 'form-control no-resize summernote', 'placeholder': 'Tafadhali andika unachotaka...', 'rows': 40, 'style': 'display: none;', 'required': 'true' }), 'belong_to': Select(attrs={'class': 'form-control show-tick'}), 'category': Select(attrs={'class': 'form-control show-tick'}), 'active': CheckboxInput(attrs={'id': 'checkbox'}), 'display_cover_photo_on_view_article': CheckboxInput(attrs={'id': 'checkbox1'}), }
class Meta: model = Diarrhea_Symptom exclude = ['symptom'] widgets = { 'vomit': CheckboxInput(attrs={'class': 'form-check-input'}), 'flux_stool': CheckboxInput(attrs={'class': 'form-check-input'}), 'fever': CheckboxInput(attrs={'class': 'form-check-input'}), 'diarrhea_amount': NumberInput(attrs={'class': 'form-control'}), 'diarrhea_detail': TextInput(attrs={'class': 'form-control'}), 'stomachache': TextInput(attrs={'class': 'form-control'}), 'bowel_sound': TextInput(attrs={'class': 'form-control'}), 'current_history': Textarea(attrs={'class': 'form-control','rows':"15", 'cols':"4"}) }
class RetailCuttingPatternOutputForm(forms.ModelForm): # Use of check_test in widget because that fields are integer in model is_for_cradle = forms.BooleanField(label="Cradle?", required=False, widget=CheckboxInput(check_test=lambda x: (x or 0) % 2 == 1)) is_primary = forms.BooleanField(label="Primary?", required=False, widget=CheckboxInput(check_test=lambda x: (x or 0) % 2 == 1)) class Meta: model = RetailCuttingPatternOutput class Media: css = { 'all': ('django_bootstrap_dynamic_formsets/extra.css',) } # jquery-ui is required for django-bootstrap-dynamic-formset js = ('bower_components/jquery-ui/jquery-ui.min.js',)
class Meta: model = Rash_Symptom exclude = ['symptom'] widgets = { 'rash_area': TextInput(attrs={'class': 'form-control'}), 'rash_date': NumberInput(attrs={'class': 'form-control'}), 'itch': CheckboxInput(attrs={'class': 'form-check-input'}), 'pain': CheckboxInput(attrs={'class': 'form-check-input'}), 'sting': CheckboxInput(attrs={'class': 'form-check-input'}), 'fever': CheckboxInput(attrs={'class': 'form-check-input'}), 'swell': CheckboxInput(attrs={'class': 'form-check-input'}), 'rash_detail': TextInput(attrs={'class': 'form-control'}), 'pe': Textarea(attrs={'class': 'form-control', 'rows':'6', 'cols': '4'}), }
class Meta: model = Wound_Symptom exclude = ['symptom'] widgets = { 'emergency': CheckboxInput(attrs={'class': 'form-check-input'}), 'insurance': CheckboxInput(attrs={'class': 'form-check-input'}), 'is_safety': CheckboxInput(attrs={'class': 'form-check-input'}), 'wound_area': TextInput(attrs={'class': 'form-control'}), 'wound_date': DateInput(attrs={'class': 'form-control','type':'date'}), 'wound_locale': TextInput(attrs={'class': 'form-control'}), 'is_treat_before': CheckboxInput(attrs={'class': 'form-check-input'}), 'treatment_before_detail': TextInput(attrs={'class': 'form-control'}), 'time': TimeInput(attrs={'class': 'form-control','type':'time'}), }
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) tab_id = 1 active = 'active' output = [u'<div class="tab-content">', ] initial_qs = self.choices.queryset nav = ['<ul class="nav nav-tabs">', ] for tab in 'general, NHL, CFL, MLS, MLB, WNBA, NBA, NFL, UEFA-English, college-div1'.split(', '): nav.append('<li class="%s">' % active) nav.append('<a href="#%s-tab%d" data-toggle="tab">%s</a>' % ( name, tab_id, tab.replace('college-div1', 'College Div1'))) nav.append('</li>') output = output + [u'<div class="tab-pane %s" id="%s-tab%d">' % (active, name, tab_id), u'<ul>'] tab_id += 1 if tab_id != 1: active = '' # Normalize to strings str_values = set([force_unicode(v) for v in value]) self.choices.queryset = initial_qs.filter(sub_category=tab) for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s_%s' % (attrs['id'], i, tab.lower())) label_for = u' for="%s"' % final_attrs['id'] else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) rendered_cb = cb.render(name, option_value) option_label = conditional_escape(force_unicode(option_label)) output.append(u'<li><label%s>%s %s</label></li>' % (label_for, rendered_cb, option_label)) output.append(u'</ul>') output.append(u'</div>') nav.append('</ul>') output.append(u'</div>') output = nav + output return mark_safe(u'\n'.join(output))
def render(self, report, writer, name, value, item, row_number, attrs=None, **kwargs): """ Render a select form field """ if not isinstance(writer, HtmlWriter): return value if value is None: value = '' final_attrs = self.get_form_element_attributes(item, report._meta.form_prefix, self.field.form_field_name, row_number, value) cb = CheckboxInput(final_attrs) #, check_test=lambda value: value in str_values) try: checked = str(value) in kwargs['form_field_value'] except KeyError: checked = False return cb.render(self.field.form_field_name, checked)
def render(self, name, value, attrs=None, choices=()): spis_available_rules_choice = [] spis_mandatory_rules_choice = [] available_rule_qs = Rules_for_restore_zakaz.objects.filter(service_type=self.attrs['service_type_init']) if available_rule_qs: for i in available_rule_qs[0].rules.all().order_by('name_rule'): spis_available_rules_choice.append(i.id) for i in available_rule_qs[0].mandatory_rules.all().order_by('name_rule'): spis_mandatory_rules_choice.append(i.id) if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name,) output = ['<ul class="cols" id="id_spis_rules">'] # Normalize to strings str_values = set([force_text(v) for v in value]) for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: if option_value in spis_available_rules_choice: rule_obj = Spis_rules_for_restore_zakaz.objects.get(id=option_value) final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i),) if 'checked' in final_attrs: del final_attrs['checked'] if 'onclick' in final_attrs: del final_attrs['onclick'] if 'disabled' in final_attrs: del final_attrs['disabled'] if option_value in spis_mandatory_rules_choice: final_attrs['checked'] = 'checked' final_attrs['onclick'] = 'window.event.returnValue=false' if rule_obj.action_on_change: final_attrs['onclick'] = 'check_rule(this, "%s")' % rule_obj.action_on_change else: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i),) final_attrs['disabled'] = 'disabled' label_for = format_html(' for="{0}"', final_attrs['id']) else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_text(option_value) rendered_cb = cb.render(name, option_value) option_label = force_text(option_label) output.append(format_html(u'<li><label{0}>{1} <span>{2}</span></label></li>', label_for, rendered_cb, option_label)) output.append('</ul>') return mark_safe('\n'.join(output))
def render(self, name, value, attrs=None): substitutions = { 'initial_text': self.initial_text, 'input_text': self.input_text, 'clear_template': '', 'clear_checkbox_label': '', } template = '%(input)s' substitutions['input'] = Input.render(self, name, value, attrs) clear_template = '<div class="remove-image"><div class="remove-image btn btn-primary">Remove image</div></div><div class="image-removed">Image removed. Please click Submit to save your changes.</div>' + self.template_with_clear if value and hasattr(value, "url"): template = self.template_with_initial substitutions['initial'] = ('<div class="logo-image-container"><img class="logo-image" src="%s" alt="%s"/></div>' % (escape(value.url), escape(force_unicode(value)))) if not self.is_required: checkbox_name = self.clear_checkbox_name(name) checkbox_id = self.clear_checkbox_id(checkbox_name) substitutions['clear_checkbox_name'] = conditional_escape(checkbox_name) substitutions['clear_checkbox_id'] = conditional_escape(checkbox_id) substitutions['clear'] = CheckboxInput().render(checkbox_name, False, attrs={'id': checkbox_id}) substitutions['clear_template'] = clear_template % substitutions return mark_safe(template % substitutions)
def render(self, name, value, attrs=None,): substitutions = { 'initial_text': self.initial_text, 'input_text': self.input_text, 'clear_template': '', 'clear_checkbox_label': self.clear_checkbox_label, } template = u'%(input)s' substitutions['input'] = super(ClearableFileInput, self).render(name, value, attrs) if value and hasattr(value, "url"): template = self.template_with_initial if self.preview: substitutions['initial'] = (u'<a href="{0}">{1}</a><br /><br />\ <a href="{0}" target="_blank"><img src="{0}" alt="" width="{2}" /></a><br /><br />'.format (escape(value.url),'...'+escape(force_unicode(value))[-self.url_length:], self.image_width)) else: substitutions['initial'] = (u'<a href="{0}">{1}</a>'.format (escape(value.url),'...'+escape(force_unicode(value))[-self.url_length:])) if not self.is_required: checkbox_name = self.clear_checkbox_name(name) checkbox_id = self.clear_checkbox_id(checkbox_name) substitutions['clear_checkbox_name'] = conditional_escape(checkbox_name) substitutions['clear_checkbox_id'] = conditional_escape(checkbox_id) substitutions['clear'] = CheckboxInput().render(checkbox_name, False, attrs={'id': checkbox_id}) substitutions['clear_template'] = self.template_with_clear % substitutions return mark_safe(template % substitutions)
def make_context(self, request, errorcontext=None): context = super(EditVolunteerView, self).make_context(request, errorcontext) context, initial_form_info = setup_event_management_form( self.item.e_conference, self.item, self.occurrence, context, open_to_public=False) context['association_form'] = EventAssociationForm( initial={ 'staff_area': self.area, 'parent_event': self.parent_id }) context['edit_title'] = self.title context['scheduling_form'].fields['approval'].widget = CheckboxInput() if validate_perms(request, ('Volunteer Coordinator', ), require=False): context.update( self.get_worker_allocation_forms(errorcontext=errorcontext)) if self.request.GET.get('changed_id', None): context['changed_id'] = int( self.request.GET.get('changed_id', None)) else: context['start_open'] = True return context
def render(self, name, value, attrs=None): substitutions = { #uncomment to get 'Currently' 'initial_text': "", # self.initial_text, 'input_text': self.input_text, 'clear_template': '', 'clear_checkbox_label': self.clear_checkbox_label, } template = '%(input)s' substitutions['input'] = Input.render(self, name, value, attrs) if value and hasattr(value, "url"): template = self.template_with_initial substitutions['initial'] = ( '<img src="%s" alt="%s"/>' % (escape(value.url), escape(force_unicode(value)))) if not self.is_required: checkbox_name = self.clear_checkbox_name(name) checkbox_id = self.clear_checkbox_id(checkbox_name) substitutions['clear_checkbox_name'] = conditional_escape( checkbox_name) substitutions['clear_checkbox_id'] = conditional_escape( checkbox_id) substitutions['clear'] = CheckboxInput().render( checkbox_name, False, attrs={'id': checkbox_id}) substitutions[ 'clear_template'] = self.template_with_clear % substitutions return mark_safe(template % substitutions)
def render(self, name, value, attrs=None): substitutions = { 'initial_text': self.initial_text, 'input_text': self.input_text, 'clear_template': '', 'clear_checkbox_label': self.clear_checkbox_label, } template = u'%(input)s' substitutions['input'] = super(ClearableFileInput, self).render(name, value, attrs) if value and hasattr(value, "url"): template = self.template_with_initial substitutions['initial'] = get_attachment_filename_from_url( force_unicode(value)) if not self.is_required: checkbox_name = self.clear_checkbox_name(name) checkbox_id = self.clear_checkbox_id(checkbox_name) substitutions['clear_checkbox_name'] = conditional_escape( checkbox_name) substitutions['clear_checkbox_id'] = conditional_escape( checkbox_id) substitutions['clear'] = CheckboxInput().render( checkbox_name, False, attrs={'id': checkbox_id}) substitutions[ 'clear_template'] = self.template_with_clear % substitutions return mark_safe(template % substitutions)
def render(self, name, value, attrs=None, renderer=None): substitutions = { 'initial_text': self.initial_text, 'input_text': self.input_text, 'clear_template': '', 'clear_checkbox_label': self.clear_checkbox_label, 'pretty_input_start': self.pretty_input_start, 'pretty_input_end': self.pretty_input_end, } template = u'%(pretty_input_start)s%(input)s%(pretty_input_end)s' substitutions['input'] = super(ClearableFileInput, self).render(name, value, attrs, renderer) if value and hasattr(value, "url"): template = self.template_with_initial substitutions['initial'] = (u'<audio src="%s" controls></audio>' % (escape(value.url),)) if not self.is_required: checkbox_name = self.clear_checkbox_name(name) checkbox_id = self.clear_checkbox_id(checkbox_name) substitutions['clear_checkbox_name'] = conditional_escape(checkbox_name) substitutions['clear_checkbox_id'] = conditional_escape(checkbox_id) substitutions['clear'] = CheckboxInput().render( checkbox_name, False, attrs={'id': checkbox_id}, renderer=renderer) substitutions['clear_template'] = self.template_with_clear % substitutions return mark_safe(template % substitutions)
def field_to_widget(field): if type(field) is CharField: if field.choices: return Select(attrs={"class": "form-control"}) return TextInput(attrs={"class": "form-control", "rows": 1}) if type(field) is TextField: return Textarea(attrs={"class": "form-control", "rows": 1}) if type(field) is AutoField: return HiddenInput(attrs={"class": "form-control", "rows": 1}) if type(field) is IntegerField or type(field) is FloatField: return NumberInput(attrs={"class": "form-control"}) if type(field) is EmailField: return EmailInput(attrs={"class": "form-control"}) if type(field) is ForeignKey: return Select(attrs={"class": "form-control"}) if type(field) is ManyToManyField: return CheckboxSelectMultiple(attrs={"class": ""}) if type(field) is BooleanField: return CheckboxInput(attrs={"class": "form-control"}) if type(field) is FileField: return FileInput(attrs={"class": "form-control"}) if type(field) is DateField: return DateInput(attrs={"class": "form-control date", "type": "date"}) if type(field) is DateTimeField: return DateTimeInput(attrs={"class": "form-control datetimepicker"}) return TextInput(attrs={"class": "form-control", "rows": 1})
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) output = ['<ul class="checkbox-group-root">'] # Normalize to strings str_values = set([force_text(v) for v in value]) for i, (group_name, items) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: group_attrs = dict(final_attrs, id='%s_group_%s' % (attrs['id'], i), **{'class': 'checkbox-group'}) label_for = format_html(' for="{0}"', group_attrs['id']) else: group_attrs = final_attrs label_for = '' cb = CheckboxInput(group_attrs, check_test=lambda value: value in str_values) if group_name is None: group_name = 'Нет группы' rendered_cb = cb.render('', group_name) group_name = force_text(group_name) item_count = len(items) output.append(format_html('<li class="checkbox-group-container"><div class="checkbox-group-div"><label{0} class="checkbox-group-label">{1} {2}</label> <span class="badge group-info">{3}</span><span class="group-toggle glyphicon glyphicon-chevron-down pull-right"></span></div>', label_for, rendered_cb, group_name, item_count)) output.append('<ul class="checkbox-group-items">') for j, (option_value, option_label) in enumerate(items): if has_id: item_attrs = dict(final_attrs, id='%s_%s_group_%s' % (attrs['id'], j, i), **{'class': 'checkbox-item'}) label_for = format_html(' for="{0}"', item_attrs['id']) else: item_attrs = final_attrs label_for = '' cb = CheckboxInput(item_attrs, check_test=lambda value: value in str_values) option_value = force_text(option_value) rendered_cb = cb.render(name, option_value) option_label = force_text(option_label) output.append(format_html('<li><label{0}>{1} <span class="label_text">{2}</span></label></li>', label_for, rendered_cb, option_label)) output.append('</ul>') output.append('</li>') output.append('</ul>') return mark_safe('\n'.join(output))
def render_to_list(self, name, value, attrs=None, choices=(), keep_desc=True): """ Return list of HTML strings for each checkbox Param: value -- either a list of checkboxes to check off or a comma-seperated string of list checkboxes to check off. """ #print "render_to_list(%s) value: %s\t\tattrs=%s\n\tchoices=%s" % (name, value, attrs, choices) if value is None: value = [] if not isinstance(value, list): if isinstance(value,int): value = [value] else: value = value.split(",") has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) #print "\tfinal_attrs = %s" % ( final_attrs, ) output = [] # Normalize to strings str_values = set([force_unicode(v) for v in value]) #print "\tstr_values = %s" % ( str_values, ) for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = u' for="%s"' % final_attrs['id'] else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) rendered_cb = cb.render(name, option_value) if keep_desc: option_label = conditional_escape(force_unicode(option_label)) else: option_label = "" #print "option_value", option_value #print "rendered_cb", rendered_cb #print "label_for", label_for output.append(u'<label%s>%s%s</label>' % (label_for, rendered_cb, option_label)) return output
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs attrs.update({u'name':name}) final_attrs = self.build_attrs(attrs) grouped_list = [] for order_key, elems in self.grouped.items(): fieldset = '<fieldset class="%s"><legend>%s</legend><ul>' \ % (self.css_class, order_key.title()) output = [fieldset] # Normalize to strings str_values = set([force_text(v) for v in value]) for i, obj in enumerate(elems): option_value, option_label = obj.pk, obj.__unicode__() # If an ID attribute was given, add a numeric index as a suffix # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict( final_attrs, id='%s_%s_%s' % (attrs['id'], i, order_key) ) label_for = format_html(' for="{0}"', final_attrs['id']) else: label_for = '' cb = CheckboxInput( final_attrs, check_test=lambda value: value in str_values) option_value = force_text(option_value) rendered_cb = cb.render(name, option_value) option_label = force_text(option_label) output.append( format_html('<li><label{0}>{1} {2}</label></li>', label_for, rendered_cb, option_label) ) output.append('</ul></fieldset>') grouped_list.append('\n'.join(output)) return mark_safe('\n'.join(grouped_list))
def _render_li( self, option_value, option_label, final_attrs, suffix, str_values, subchoices=(), superchecked=False ): # If an ID attribute was given, add the suffix, # so that the checkboxes don't all have the same ID attribute. has_id = "id" in final_attrs if has_id: sub_attrs = dict(final_attrs, id="%s_%s" % (final_attrs["id"], suffix)) label_for = u' for="%s"' % sub_attrs["id"] else: sub_attrs = final_attrs label_for = "" option_value = force_unicode(option_value) checked = lambda value: value in str_values li = [u"<li>"] li_classes = [] if checked(option_value): li_classes.append("checked") if superchecked: li_classes.append("superchecked") cb = CheckboxInput(sub_attrs, check_test=checked) rendered_cb = cb.render(sub_attrs["name"], option_value) option_label = conditional_escape(force_unicode(option_label)) li.append(u"<label%s>%s %s</label>" % (label_for, rendered_cb, option_label)) subchecked = False if subchoices: if checked(option_value): superchecked = True sublist, subchecked = self._render_ul(subchoices, sub_attrs, str_values, superchecked) li.append(sublist) if subchecked: li_classes.append("subchecked") li.append(u"</li>") if li_classes: li[0] = u'<li class="%s">' % u" ".join(li_classes) return (u"\n".join(li), checked(option_value), subchecked)
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) output = [u'<ul>'] # Normalize to strings str_values = set([force_unicode(v) for v in value]) for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) checkbox = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) rendered_checkbox = checkbox.render(name, option_value) option_label = conditional_escape(force_unicode(option_label)) output.append(u'<li id="filter_val_%s" title="%s">%s<p>%s</p></li>' % (option_value.lower(), option_label.title(), rendered_checkbox, option_label)) output.append(u'</ul>') return mark_safe(u'\n'.join(output))
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] final_attrs = self.build_attrs(attrs, name=name) output = [u'<ul>'] str_values = set([force_unicode(v) for v in value]) i = 0 for sistema in self.servicios: output.append(u"<h3>%s</h3>" % sistema["sistema"]) choices = [(servicio.id, servicio.nombre) for servicio in sistema["servicios"]] for (option_value, option_label) in choices: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = u' for="%s"' % final_attrs['id'] cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) rendered_cb = cb.render(name, option_value) option_label = conditional_escape(force_unicode(option_label)) output.append(u'<li><label%s>%s %s</label></li>' % (label_for, rendered_cb, option_label)) i += 1 output.append(u'</ul>') return mark_safe(u'\n'.join(output))
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] has_id = attrs and 'id' in attrs final_attrs = self.build_attrs(attrs, name=name) output = [u'<table>'] # Normalize to strings str_values = set([force_unicode(v) for v in value]) # build table all_choices = list(chain(self.choices, choices)) rows_count = math.ceil(len(all_choices) / float(self.cols_count)) for row in range(0, rows_count): start = row*self.cols_count output.append(u'<tr>') for i in range(start, start+self.cols_count): if i >= len(all_choices): output.append(u'<td> </td>') else: (option_value, option_label) = all_choices[i] output.append(u'<td>') # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = u' for="%s"' % final_attrs['id'] else: label_for = '' cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) rendered_cb = cb.render(name, option_value) option_label = conditional_escape(force_unicode(option_label)) output.append(u'<div><img src="%s"/></div>' % self.thumb_urls[i]) output.append(u'<div><label%s>%s %s</label></div>' % (label_for, rendered_cb, option_label)) output.append(u'</td>') output.append(u'</tr>') output.append(u'</table>') return mark_safe(u'\n'.join(output))
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] final_attrs = self.build_attrs(attrs, name=name) output = [u'<ul>'] str_values = set([force_unicode(v) for v in value]) for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = u' for="%s"' % final_attrs['id'] # Caso exista uma pergunta para abrir # adiciona um atripbuto no checkbox schema_to_open = Escolha.objects.get(pk=option_value).schema_to_open if schema_to_open: final_attrs['schema_to_open'] = schema_to_open.name cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) rendered_cb = cb.render(name, option_value) option_label = conditional_escape(force_unicode(option_label)) output.append(u'<li><label%s>%s %s</label></li>' % (label_for, rendered_cb, option_label)) output.append(u'</ul>') return mark_safe(u'\n'.join(output))
def render(self, name, value, attrs=None, choices=()): if value is None: value = [] final_attrs = self.build_attrs(attrs, name=name) output = [u''] # Normalize to strings str_values = set([force_unicode(v) for v in value]) for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): # If an ID attribute was given, add a numeric index as a suffix, # so that the checkboxes don't all have the same ID attribute. if attrs and 'id' in attrs: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) label_for = u' for="%s"' % final_attrs['id'] else: label_for = '' checkbox = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) rendered_cb = checkbox.render(name, option_value) option_label = conditional_escape(force_unicode(option_label)) output.append(u'<li>%s <label%s>%s</label></li>' % (rendered_cb, label_for, option_label)) output.append(u'') return mark_safe(u'\n'.join(output))
def render(self, name, value, attrs=None): if value is None: value = [] value = map(int, value) attrs = self.attrs label_attrs = attrs.pop('label_attrs', None) label_id_related_attr = attrs.pop('label_id_related_attr', False) output = [] if label_attrs is not None: label_class = '%s' % join(label_attrs, ' ') else: label_class = '' for (option_value, option_label) in self.choices: cb = CheckboxInput(attrs, check_test=lambda x: x in value) rendered_cb = cb.render(name, option_value) option_label = force_unicode(option_label) if label_id_related_attr: label_id_related_class = ' '+label_id_related_attr+'%s ' % option_value else: label_id_related_class = '' label_class_final = ' class="%s%s"' % (label_class, label_id_related_class) output.append(u'<label%s>%s %s</label>' % (label_class_final, rendered_cb, option_label)) return mark_safe(u'\n'.join(output))
def render(self, name, value, attrs=None): value = [] if value is None else value output = [] output.append(u'<table class="services_properties wide center">') for srv in self.services: output.append(u'<tr><th colspan="%s">%s</th></tr>' % (self.COLUMNS * 2, srv.get('name'))) js_cb_all = u'''<script type="text/javascript"> $("#%(id)s_all").click(function() { var checked_status = this.checked; $("input[name^=%(id)s]").each(function() { this.checked = checked_status; }); }); </script>''' % ({'id': srv['id']}) id_all = '%s_all' % srv['id'] attrs = {'id': id_all} label_for = u' for="%s"' % id_all cb = CheckboxInput(attrs) rendered_cb = cb.render(id_all, id_all in self.sel_props) output.append(u'<tr><td colspan="%s">%s <label%s>%s</label> %s</td></tr>' % (self.COLUMNS * 2, rendered_cb, label_for, _('select all'), js_cb_all)) props = srv['properties'] rendered_props = [] for p in props: id = '%s_%s' % (srv['id'], p) attrs = {'id': id} label_for = u' for="%s"' % id cb = CheckboxInput(attrs) rendered_cb = cb.render(id, id in self.sel_props) rendered_props.append((rendered_cb, u'<label%s>%s</label>' % (label_for, p))) output.append(elems_as_table(rendered_props, self.COLUMNS)) output.append(u'</table><br>') return mark_safe(u'\n'.join(output))