def __init__(self, attrs=None, types_to_set=[], supertype=None, show_production_type_field=False): self.id_widget = forms.HiddenInput() self.title_widget = forms.TextInput() self.byline_widget = BylineWidget() self.types_to_set = types_to_set self.supertype = supertype self.production_type_widget = ProductionTypeChoiceField( queryset=ProductionType.objects.all()).widget self.show_production_type_field = show_production_type_field super(ProductionWidget, self).__init__(attrs=attrs)
def __init__(self, attrs=None, types_to_set=[], supertype=None, show_production_type_field=False): self.id_widget = forms.HiddenInput() self.title_widget = forms.TextInput() self.byline_widget = BylineWidget() self.types_to_set = types_to_set self.supertype = supertype self.production_type_widget = ProductionTypeChoiceField(queryset=ProductionType.objects.all()).widget self.show_production_type_field = show_production_type_field super(ProductionWidget, self).__init__(attrs=attrs)
class ProductionWidget(forms.Widget): def __init__(self, attrs=None, types_to_set=[], supertype=None, show_production_type_field=False): self.id_widget = forms.HiddenInput() self.title_widget = forms.TextInput() self.byline_widget = BylineWidget() self.types_to_set = types_to_set self.supertype = supertype self.production_type_widget = ProductionTypeChoiceField( queryset=ProductionType.objects.all()).widget self.show_production_type_field = show_production_type_field super(ProductionWidget, self).__init__(attrs=attrs) def value_from_datadict(self, data, files, name): id = self.id_widget.value_from_datadict(data, files, name + '_id') title = self.title_widget.value_from_datadict(data, files, name + '_title') byline_lookup = self.byline_widget.value_from_datadict( data, files, name + '_byline') if self.show_production_type_field: type_to_set = self.production_type_widget.value_from_datadict( data, files, name + '_type') if type_to_set: types_to_set = [ProductionType.objects.get(id=type_to_set)] else: types_to_set = [] else: types_to_set = self.types_to_set if id or title: return ProductionSelection( id=id, title=title, byline_lookup=byline_lookup, types_to_set=types_to_set, ) else: return None def render(self, name, value, attrs=None): production_selection = ProductionSelection.from_value( value, types_to_set=self.types_to_set) production_id = production_selection.id if production_id: byline_text = production_selection.production.byline().__unicode__( ) if byline_text: static_view = [ # FIXME: HTMLencode "<b>%s</b> by %s" % (production_selection.production.title, byline_text) ] else: static_view = [ "<b>%s</b>" % production_selection.production.title ] else: static_view = [] title_attrs = self.build_attrs(attrs) title_attrs['class'] = 'title_field' if self.supertype: title_attrs['data-supertype'] = self.supertype byline_attrs = self.build_attrs(attrs) byline_attrs['id'] += '_byline' prodtype_attrs = self.build_attrs(attrs) prodtype_attrs['id'] += '_type' form_view = [ self.id_widget.render(name + '_id', production_id), self.title_widget.render(name + '_title', '', attrs=title_attrs), ' <label for="%s">by</label> ' % self.byline_widget.id_for_label('id_' + name + '_byline'), self.byline_widget.render(name + '_byline', '', attrs=byline_attrs), ] if self.show_production_type_field: form_view += [ '<label for="%s">Type:</label> ' % self.production_type_widget.id_for_label('id_' + name + '_type'), self.production_type_widget.render(name + '_type', '', attrs=prodtype_attrs) ] output = [ '<div class="production_field">', '<div class="static_view">', '<div class="static_view_text">', u''.join(static_view), '</div>', '</div>', '<div class="form_view">', u''.join(form_view), '</div>', '</div>', ] return mark_safe(u''.join(output)) def _has_changed(self, initial, data): initial = ProductionSelection.from_value(initial) data = ProductionSelection.from_value(data) return data != initial
class ProductionWidget(forms.Widget): def __init__(self, attrs=None, types_to_set=[], supertype=None, show_production_type_field=False): self.id_widget = forms.HiddenInput() self.title_widget = forms.TextInput() self.byline_widget = BylineWidget() self.types_to_set = types_to_set self.supertype = supertype self.production_type_widget = ProductionTypeChoiceField(queryset=ProductionType.objects.all()).widget self.show_production_type_field = show_production_type_field super(ProductionWidget, self).__init__(attrs=attrs) def value_from_datadict(self, data, files, name): id = self.id_widget.value_from_datadict(data, files, name + '_id') title = self.title_widget.value_from_datadict(data, files, name + '_title') byline_lookup = self.byline_widget.value_from_datadict(data, files, name + '_byline') if self.show_production_type_field: type_to_set = self.production_type_widget.value_from_datadict(data, files, name + '_type') if type_to_set: types_to_set = [ProductionType.objects.get(id=type_to_set)] else: types_to_set = [] else: types_to_set = self.types_to_set if id or title: return ProductionSelection( id=id, title=title, byline_lookup=byline_lookup, types_to_set=types_to_set, ) else: return None def render(self, name, value, attrs=None): production_selection = ProductionSelection.from_value(value, types_to_set=self.types_to_set) production_id = production_selection.id if production_id: byline_text = production_selection.production.byline().__unicode__() if byline_text: static_view = [ # FIXME: HTMLencode "<b>%s</b> by %s" % (production_selection.production.title, byline_text) ] else: static_view = [ "<b>%s</b>" % production_selection.production.title ] else: static_view = [] title_attrs = self.build_attrs(attrs) title_attrs['class'] = 'title_field' if self.supertype: title_attrs['data-supertype'] = self.supertype byline_attrs = self.build_attrs(attrs) byline_attrs['id'] += '_byline' prodtype_attrs = self.build_attrs(attrs) prodtype_attrs['id'] += '_type' form_view = [ self.id_widget.render(name + '_id', production_id), self.title_widget.render(name + '_title', '', attrs=title_attrs), ' <label for="%s">by</label> ' % self.byline_widget.id_for_label('id_' + name + '_byline'), self.byline_widget.render(name + '_byline', '', attrs=byline_attrs), ] if self.show_production_type_field: form_view += [ '<label for="%s">Type:</label> ' % self.production_type_widget.id_for_label('id_' + name + '_type'), self.production_type_widget.render(name + '_type', '', attrs=prodtype_attrs) ] output = [ '<div class="production_field">', '<div class="static_view">', '<div class="static_view_text">', u''.join(static_view), '</div>', '</div>', '<div class="form_view">', u''.join(form_view), '</div>', '</div>', ] return mark_safe(u''.join(output)) def _has_changed(self, initial, data): initial = ProductionSelection.from_value(initial) data = ProductionSelection.from_value(data) return data != initial