def forms_invalid(self, form, period_formset_with_days, resource_image_formset): messages.error(self.request, 'Tallennus epäonnistui. Tarkista lomakkeen virheet.') # Extra forms are not added upon post so they # need to be added manually below. This is because # the front-end uses the empty 'extra' forms for cloning. temp_image_formset = get_resource_image_formset() temp_period_formset = get_period_formset() temp_day_form = temp_period_formset.forms[0].days.forms[0] resource_image_formset.forms.append(temp_image_formset.forms[0]) period_formset_with_days.forms.append(temp_period_formset.forms[0]) # Add a nested empty day to each period as well. for period in period_formset_with_days: period.days.forms.append(temp_day_form) trans_fields = forms.get_translated_field_count(resource_image_formset) return self.render_to_response( self.get_context_data( form=form, period_formset_with_days=period_formset_with_days, resource_image_formset=resource_image_formset, trans_fields=trans_fields, page_headline=_('Edit resource'), ) )
def get(self, request, *args, **kwargs): if self.pk_url_kwarg in kwargs: self.object = self.get_object() page_headline = _('Edit resource') else: page_headline = _('Create new resource') self.object = None form = self.get_form() period_formset_with_days = get_period_formset( self.request, instance=self.object, ) resource_image_formset = get_resource_image_formset( self.request, instance=self.object, ) trans_fields = forms.get_translated_field_count(resource_image_formset) accessibility_data_link = self._get_accessibility_data_link(request) return self.render_to_response( self.get_context_data( accessibility_data_link=accessibility_data_link, form=form, period_formset_with_days=period_formset_with_days, resource_image_formset=resource_image_formset, trans_fields=trans_fields, page_headline=page_headline, ) )
def add_empty_forms(self, period_formset): # Extra forms are not added upon post so they # need to be added manually below. This is because # the front-end uses the empty 'extra' forms for cloning. temp_period_formset = get_period_formset() temp_day_form = temp_period_formset.forms[0].days.forms[0] period_formset.forms.append(temp_period_formset.forms[0]) # Add a nested empty day to each period as well. for period in period_formset: period.days.forms.append(temp_day_form) return period_formset
def post(self, request, *args, **kwargs): if self.pk_url_kwarg in kwargs: self.object = self.get_object() else: self.object = None form = self.get_form() period_formset_with_days = get_period_formset(request=request, instance=self.object) resource_image_formset = get_resource_image_formset(request=request, instance=self.object) if self._validate_forms(form, period_formset_with_days, resource_image_formset): return self.forms_valid(form, period_formset_with_days, resource_image_formset) else: return self.forms_invalid(form, period_formset_with_days, resource_image_formset)
def get_period_formset(self): return get_period_formset( self.request, instance=self.object, parent_class=self.model, )