def test_new_form_with_duplicate_page_name(self): data = { 'name': self.page.name, 'heading': 'Test Heading', 'menu_name': 'test_name', 'menu_location': 'main', 'layout': 'no-img', 'content': self.page.content } form = PageForm(data) self.assertFalse(form.is_valid()) self.assertIn( "Page with this Name already exists", form.errors['name'][0], )
def test_form_invalid_name(self): data = { 'name': 'name&', 'heading': 'Test Heading', 'menu_name': 'test_name', 'menu_location': 'main', 'layout': 'no-img', 'content': self.page.content } form = PageForm(data) self.assertFalse(form.is_valid()) self.assertIn( "This field must contain only letters, numbers, / or -", form.errors['name'][0], )
def post(self, request, *args, **kwargs): form = PageForm(request.POST) if form.is_valid(): page = form.save() picture_formset = PictureFormset(request.POST, request.FILES, instance=page) if form.is_valid() and picture_formset.is_valid(): form.save() picture_formset.save() messages.success( request, mark_safe("Page {} has been created".format(page.name))) ActivityLog.objects.create( log="Page {} (id {}) has been created by admin " "user {}".format(page.name, page.id, request.user.username)) else: if not picture_formset.is_valid(): for error in picture_formset.errors: for k, v in error.items(): messages.error( request, mark_safe("{}: {}".format(k.title(), v))) context = { 'form': form, 'picture_formset': picture_formset, 'sidenav_selection': 'page_list' } return TemplateResponse(request, self.template_name, context) else: messages.error(request, "Please correct the errors below") picture_formset = PictureFormset(request.POST, request.FILES) return TemplateResponse( request, self.template_name, { 'form': form, 'picture_formset': picture_formset, 'sidenav_selection': 'add_page' }) return HttpResponseRedirect(self.get_success_url())
def post(self, request, *args, **kwargs): form = PageForm(request.POST) if form.is_valid(): page = form.save() picture_formset = PictureFormset( request.POST, request.FILES, instance=page ) if form.is_valid() and picture_formset.is_valid(): form.save() picture_formset.save() messages.success(request, mark_safe( "Page {} has been created".format(page.name) ) ) ActivityLog.objects.create( log="Page {} (id {}) has been created by admin " "user {}".format( page.name, page.id, request.user.username ) ) else: if not picture_formset.is_valid(): for error in picture_formset.errors: for k, v in error.items(): messages.error( request, mark_safe("{}: {}".format(k.title(), v)) ) context = { 'form': form, 'picture_formset': picture_formset, 'sidenav_selection': 'page_list' } return TemplateResponse(request, self.template_name, context) else: messages.error(request, "Please correct the errors below") picture_formset = PictureFormset(request.POST, request.FILES) return TemplateResponse( request, self.template_name, {'form': form, 'picture_formset': picture_formset, 'sidenav_selection': 'add_page'} ) return HttpResponseRedirect(self.get_success_url())
def test_form_valid(self): form = PageForm(data=self.form_data(), instance=self.page) self.assertTrue(form.is_valid())
def post(self, request, *args, **kwargs): name = self.kwargs['name'] page = Page.objects.get(name=name) form = PageForm(request.POST, instance=page) picture_formset = PictureFormset(request.POST, request.FILES, instance=page) if form.is_valid() and picture_formset.is_valid(): if (form.has_changed() or picture_formset.has_changed()): change_messages = [] if form.has_changed(): change_messages.append("{} has been updated".format( ', '.join(form.changed_data))) page = form.save() for form in picture_formset.forms: if form.is_valid(): picture = form.save(commit=False) if 'DELETE' in form.changed_data: name = picture.image.name picture.delete() change_messages.append( 'Picture {} deleted from "{}" page'.format( name.split('/')[-1], page.name)) elif form.has_changed(): action = 'edited' if picture.id else 'added' picture.save() change_messages.append( 'Picture {} has been {}'.format( picture.image.name.split('/')[-1], action)) else: for error in form.errors: messages.error(request, mark_safe(error)) messages.success( request, mark_safe("<ul>{}</ul>".format(''.join([ '<li>{}</li>'.format(msg) for msg in change_messages ])))) ActivityLog.objects.create( log="Page {} (id {}) has been updated by admin user " "{}: {}".format(page.name, page.id, request.user.username, ', '.join(change_messages))) else: messages.info(request, "No changes made") else: messages.error(request, "Please correct the errors below") if not picture_formset.is_valid(): for error in picture_formset.errors: for k, v in error.items(): messages.error( request, mark_safe("{}: {}".format(k.title(), v))) context = { 'form': form, 'picture_formset': picture_formset, 'sidenav_selection': 'page_list' } return TemplateResponse(request, self.template_name, context) return HttpResponseRedirect(self.get_success_url(name=page.name))
def post(self, request, *args, **kwargs): name = self.kwargs['name'] page = Page.objects.get(name=name) form = PageForm(request.POST, instance=page) picture_formset = PictureFormset(request.POST, request.FILES, instance=page) if form.is_valid() and picture_formset.is_valid(): if (form.has_changed() or picture_formset.has_changed()): change_messages = [] if form.has_changed(): change_messages.append("{} has been updated".format( ', '.join(form.changed_data) ) ) page = form.save() for form in picture_formset.forms: if form.is_valid(): picture = form.save(commit=False) if 'DELETE' in form.changed_data: name = picture.image.name picture.delete() change_messages.append( 'Picture {} deleted from "{}" page'.format( name.split('/')[-1], page.name ) ) elif form.has_changed(): action = 'edited' if picture.id else 'added' picture.save() change_messages.append( 'Picture {} has been {}'.format( picture.image.name.split('/')[-1], action ) ) else: for error in form.errors: messages.error(request, mark_safe(error)) messages.success( request, mark_safe( "<ul>{}</ul>".format( ''.join(['<li>{}</li>'.format(msg) for msg in change_messages]) ) ) ) ActivityLog.objects.create( log="Page {} (id {}) has been updated by admin user " "{}: {}".format( page.name, page.id, request.user.username, ', '.join(change_messages) ) ) else: messages.info(request, "No changes made") else: messages.error(request, "Please correct the errors below") if not picture_formset.is_valid(): for error in picture_formset.errors: for k, v in error.items(): messages.error( request, mark_safe("{}: {}".format(k.title(), v)) ) context = { 'form': form, 'picture_formset': picture_formset, 'sidenav_selection': 'page_list' } return TemplateResponse(request, self.template_name, context) return HttpResponseRedirect(self.get_success_url(name=page.name))