def post(self, request, *args, **kwargs): pages_forms = PagesFormset(request.POST) if pages_forms.has_changed(): deleted_page_names = [] deleted_page_ids = [] for form in pages_forms: if form.has_changed() and 'DELETE' in form.changed_data: page = Page.objects.get(id=form.instance.id) # delete associated pictures # loop to delete pics so we call delete() on each instance [pic.delete() for pic in Picture.objects.filter(page=page)] deleted_page_names.append(page.name) deleted_page_ids.append(page.id) # delete page page.delete() if len(deleted_page_names) == 1: msg = "Page '{}' has been deleted".format(deleted_page_names[0]) ActivityLog.objects.create( log="Page {} (id {}) has been deleted by admin " "user {}".format( deleted_page_names[0], deleted_page_ids[0], request.user.username ) ) elif len(deleted_page_names) > 1: msg = "Pages {} have been deleted".format( ', '.join(["'{}'".format(name) for name in deleted_page_names]), ) ActivityLog.objects.create( log= "Pages {} (ids {}) have been deleted by admin " "user {}".format( ', '.join( ["{}".format(name) for name in deleted_page_names] ), ', '.join( ['{}'.format(pageid for pageid in deleted_page_ids)] ), request.user.username ) ) else: msg = "No changes made" messages.success(request, msg) else: messages.info(request, "No changes made") return HttpResponseRedirect(self.get_success_url())
def post(self, request, *args, **kwargs): pages_forms = PagesFormset(request.POST) if pages_forms.has_changed(): deleted_page_names = [] deleted_page_ids = [] for form in pages_forms: if form.has_changed() and 'DELETE' in form.changed_data: page = Page.objects.get(id=form.instance.id) # delete associated pictures # loop to delete pics so we call delete() on each instance [pic.delete() for pic in Picture.objects.filter(page=page)] deleted_page_names.append(page.name) deleted_page_ids.append(page.id) # delete page page.delete() if len(deleted_page_names) == 1: msg = "Page '{}' has been deleted".format( deleted_page_names[0]) ActivityLog.objects.create( log="Page {} (id {}) has been deleted by admin " "user {}".format(deleted_page_names[0], deleted_page_ids[0], request.user.username)) elif len(deleted_page_names) > 1: msg = "Pages {} have been deleted".format( ', '.join([ "'{}'".format(name) for name in deleted_page_names ]), ) ActivityLog.objects.create( log="Pages {} (ids {}) have been deleted by admin " "user {}".format( ', '.join( ["{}".format(name) for name in deleted_page_names]), ', '.join([ '{}'.format(pageid for pageid in deleted_page_ids) ]), request.user.username)) else: msg = "No changes made" messages.success(request, msg) else: messages.info(request, "No changes made") return HttpResponseRedirect(self.get_success_url())
def test_formset_valid(self): formset = PagesFormset(data=self.formset_data()) self.assertTrue(formset.is_valid())
def get_context_data(self): context = super(PageListView, self).get_context_data() context['pages_formset'] = PagesFormset() context['sidenav_selection'] = 'page_list' return context