def new_article(request): """new article""" article_class = get_article_class() new_article_form = get_new_article_form() content_type = ContentType.objects.get_for_model(article_class) perm = '{0}.add_{1}'.format(content_type.app_label, content_type.model) if not request.user.has_perm(perm): raise PermissionDenied if request.method == "POST": form = new_article_form(request.user, request.POST, request.FILES) if form.is_valid(): article = form.save() form.save_m2m() success_message(request, _('The article has been created properly')) return HttpResponseRedirect(article.get_edit_url()) else: form = new_article_form(request.user) return render( request, 'coop_cms/popup_new_article.html', locals() )
def new_link(request): """new link""" content_type = ContentType.objects.get_for_model(models.Link) perm = '{0}.add_{1}'.format(content_type.app_label, content_type.model) if not request.user.has_perm(perm): raise PermissionDenied if request.method == "POST": form = NewLinkForm(request.POST) if form.is_valid(): form.save() form.save_m2m() homepage_url = reverse('coop_cms_homepage') next_url = request.META.get('HTTP_REFERER', homepage_url) success_message(request, _('The link has been created properly')) return HttpResponseRedirect(next_url) else: form = NewLinkForm() context = { 'form': form, } return render(request, 'coop_cms/popup_new_link.html', context)
def new_link(request): """new link""" content_type = ContentType.objects.get_for_model(models.Link) perm = '{0}.add_{1}'.format(content_type.app_label, content_type.model) if not request.user.has_perm(perm): raise PermissionDenied if request.method == "POST": form = NewLinkForm(request.POST) if form.is_valid(): form.save() form.save_m2m() homepage_url = reverse('coop_cms_homepage') next_url = request.META.get('HTTP_REFERER', homepage_url) success_message(request, _('The link has been created properly')) return HttpResponseRedirect(next_url) else: form = NewLinkForm() context = { 'form': form, } return render( request, 'coop_cms/popup_new_link.html', context )
def post(self, request, *args, **kwargs): """handle http post -> edit""" if not self.edit_mode: raise Http404 if not self.can_edit_objects(): raise PermissionDenied self.formset = self.get_formset(request.POST, request.FILES) forms_args = html_editor_utils.extract_forms_args(request.POST) inline_html_forms = html_editor_utils.make_forms( forms_args, request.POST) # Handle case where formset post data has value which are not in the queryset formset_index_error = False try: formset_is_valid = self.formset.is_valid() except IndexError: formset_index_error = True formset_is_valid = False if formset_is_valid and all( [_form.is_valid() for _form in inline_html_forms]): for form in self.formset: if self._pre_save_object(form): obj = form.save() self._post_save_object(obj, form) if inline_html_forms: [_html_form.save() for _html_form in inline_html_forms] success_message(request, _('The objects have been saved properly')) url = self.get_success_url() return HttpResponseRedirect(url) else: if formset_index_error: logger.warning( _('Index error in formset: some objects may be missing')) error_message( request, _('An error occured: At least one object is missing. Please try again.' )) return HttpResponseRedirect(self.get_cancel_url()) else: for _form in self.formset: errors = _form.errors if errors: logger.warning(errors) return render(request, self.get_template(), self.get_context_data())
def post(self, request, *args, **kwargs): """handle http post -> edit""" if not self.edit_mode: raise ArticleNotAllowed self.object = self.get_object() if not self.can_edit_object(): # logger.warning("PermissionDenied") raise PermissionDenied self.form = self.get_form(request.POST, request.FILES, instance=self.object) forms_args = html_editor_utils.extract_forms_args(request.POST) inline_html_forms = html_editor_utils.make_forms( forms_args, request.POST) if self.form.is_valid() and all( [_form.is_valid() for _form in inline_html_forms]): if self.is_cache_enabled(): cache.delete(self.get_cache_key(self.object)) self.object = self.form.save() self.after_save(self.object) if inline_html_forms: [_the_form.save() for _the_form in inline_html_forms] success_message(request, _('The object has been saved properly')) return HttpResponseRedirect(self.object.get_absolute_url()) else: error_text = '<br />'.join([ '{0}'.format(_form.errors) for _form in [self.form] + inline_html_forms if _form.errors ]) error_message(request, _('An error occurred: {0}').format(error_text)) logger.debug("error: {0}".format(error_text)) return render(request, self.get_template(), self.get_context_data())