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().__init__(attrs=attrs)
class CompetitionForm(forms.ModelForm): shown_date = FuzzyDateField(label="Date", required=False) production_type = ProductionTypeChoiceField(required=False, queryset=ProductionType.objects.all()) platform = forms.ModelChoiceField(required=False, queryset=Platform.objects.all()) def log_creation(self, user): Edit.objects.create(action_type='create_competiton', focus=self.instance, focus2=self.instance.party, description=(u"Added competition %s" % self.instance.name), user=user) @property def changed_data_description(self): descriptions = [] changed_fields = self.changed_data if 'name' in changed_fields: descriptions.append(u"name to %s" % self.cleaned_data['name']) if 'shown_date' in changed_fields: descriptions.append(u"date to %s" % self.cleaned_data['shown_date']) if 'platform' in changed_fields: descriptions.append(u"platform to %s" % self.cleaned_data['platform']) if 'production_type' in changed_fields: descriptions.append(u"production type to %s" % self.cleaned_data['production_type']) if descriptions: return u"Set %s" % (u", ".join(descriptions)) def log_edit(self, user): description = self.changed_data_description if description: Edit.objects.create(action_type='edit_competition', focus=self.instance, description=description, user=user) class Meta: model = Competition fields = ('name', 'shown_date', 'platform', 'production_type')
def __init__(self, *args, **kwargs): super(CreateGraphicsForm, self).__init__(*args, **kwargs) self.fields['type'] = ProductionTypeChoiceField( queryset=ProductionType.graphic_types(), initial=ProductionType.objects.get(internal_name='graphics')) self.fields['platform'] = forms.ModelChoiceField( required=False, queryset=Platform.objects.all(), empty_label='Any')
class MusicIndexFilterForm(forms.Form): platform = forms.ModelChoiceField(required=False, queryset=Platform.objects.all(), empty_label='All platforms') production_type = ProductionTypeChoiceField(required=False, queryset=ProductionType.objects.none(), empty_label='All types') def __init__(self, *args, **kwargs): super(MusicIndexFilterForm, self).__init__(*args, **kwargs) self.fields['production_type'].queryset = ProductionType.music_types()
class MusicIndexFilterForm(forms.Form): platform = forms.ModelChoiceField(required=False, queryset=Platform.objects.all(), empty_label='All platforms') production_type = ProductionTypeChoiceField( required=False, queryset=ProductionType.music_types(), empty_label='All types')
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, *args, **kwargs): super(GraphicsEditCoreDetailsForm, self).__init__(*args, **kwargs) self.has_multiple_types = False try: initial_type = self.instance.types.all()[0].id except IndexError: initial_type = None self.fields['type'] = ProductionTypeChoiceField( queryset=ProductionType.graphic_types(), initial=initial_type)
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().__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, renderer=None): production_selection = ProductionSelection.from_value(value, types_to_set=self.types_to_set) production_id = production_selection.id if production_id: byline_text = str(production_selection.production.byline()) 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, renderer=renderer), self.title_widget.render(name + '_title', '', attrs=title_attrs, renderer=renderer), ' <label for="%s">by</label> ' % self.byline_widget.id_for_label('id_' + name + '_byline'), self.byline_widget.render(name + '_byline', '', attrs=byline_attrs, renderer=renderer), ] 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, renderer=renderer) ] 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))
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