def test_unpublish_not_current(self): new_version = Version( content_type=self.page_content_type, object_id=self.page.id, template_name='glitter/sample.html', owner=self.editor) new_version.generate_version() new_version.save() self.page.current_version = new_version self.page.save() # Unsaved version publish saves a new version number, and redirects to the live URL response = self.publisher_client.post(self.unpublish_version_url, follow=True) page = Page.objects.get(id=self.page.id) # refetch self.assertEqual(page.current_version.id, new_version.id) self.assertRedirects(response, self.view_version_url) self.assertTemplateUsed(response, 'glitter/sample.html')
def setUp(self): super(TestDiscardVersion, self).setUp() # Page with one saved version, one unsaved version self.page_url = '/edit-version/' self.page = Page.objects.create(url=self.page_url, title='Test page') self.page_version_1 = Version( content_type=self.page_content_type, object_id=self.page.id, template_name='glitter/sample.html', owner=self.editor) self.page_version_1.generate_version() self.page_version_1.save() self.page.current_version = self.page_version_1 self.page.save() self.page_version_2 = Version.objects.create( content_type=self.page_content_type, object_id=self.page.id, template_name='glitter/sample.html', owner=self.editor) # Unsaved version has an HTML block html_block = HTML.objects.create(content='<p>HTML Block</p>') content_block = ContentBlock.objects.create( obj_version=self.page_version_2, column='main_content', position=1, content_type=ContentType.objects.get_for_model(html_block), object_id=html_block.id) html_block.content_block = content_block html_block.save() # Discard page URLs info = self.page._meta.app_label, self.page._meta.model_name self.discard_version_1_url = reverse('admin:%s_%s_discard' % info, kwargs={ 'version_id': self.page_version_1.id, }) self.discard_version_2_url = reverse('admin:%s_%s_discard' % info, kwargs={ 'version_id': self.page_version_2.id, })
def duplicate_page(self, request, obj_id): obj = get_object_or_404(Page, id=obj_id) if not self.has_add_permission(request): raise PermissionDenied if request.method == "POST": form = DuplicatePageForm(request.POST or None) if form.is_valid(): new_page = form.save() # Use current version if exists if not get the latest if obj.current_version: current_version = obj.current_version else: current_version = obj.get_latest_version() if current_version: # Create a new version new_version = Version(content_object=new_page) new_version.template_name = current_version.template_name new_version.version_number = 1 new_version.owner = request.user new_version.save() self.duplicate_content(current_version, new_version) return HttpResponseRedirect( reverse('admin:glitter_pages_page_change', args=(new_page.id,)) ) else: form = DuplicatePageForm(initial={ 'url': obj.url, 'title': obj.title, 'parent': obj.parent, }) adminForm = admin.helpers.AdminForm( form=form, fieldsets=[('Duplicate Page: {}'.format(obj), { 'fields': DuplicatePageForm.Meta.fields })], prepopulated_fields=self.get_prepopulated_fields(request, obj), readonly_fields=self.get_readonly_fields(request, obj), model_admin=self ) context = { 'adminform': adminForm, 'opts': obj._meta, 'change': False, 'is_popup': False, 'save_as': False, 'has_delete_permission': self.has_delete_permission(request, obj), 'has_add_permission': self.has_add_permission(request), 'has_change_permission': self.has_change_permission(request, obj), } return render( request, 'admin/pages/page/duplicate_page.html', context )
def setUp(self): super(TestUnpublishVersion, self).setUp() # Page with one published version self.page_url = '/edit-version/' self.page = Page.objects.create(url=self.page_url, title='Test page') self.page_version = Version( content_type=self.page_content_type, object_id=self.page.id, template_name='glitter/sample.html', owner=self.editor) self.page_version.generate_version() self.page_version.save() self.page.current_version = self.page_version self.page.save() # Edit page URLs info = self.page._meta.app_label, self.page._meta.model_name self.unpublish_version_url = reverse('admin:%s_%s_unpublish' % info, kwargs={ 'version_id': self.page_version.id, }) self.view_version_url = reverse('admin:%s_%s_version' % info, kwargs={ 'version_id': self.page_version.id, })
class TestDiscardVersion(BaseEditCase): def setUp(self): super(TestDiscardVersion, self).setUp() # Page with one saved version, one unsaved version self.page_url = '/edit-version/' self.page = Page.objects.create(url=self.page_url, title='Test page') self.page_version_1 = Version( content_type=self.page_content_type, object_id=self.page.id, template_name='glitter/sample.html', owner=self.editor) self.page_version_1.generate_version() self.page_version_1.save() self.page.current_version = self.page_version_1 self.page.save() self.page_version_2 = Version.objects.create( content_type=self.page_content_type, object_id=self.page.id, template_name='glitter/sample.html', owner=self.editor) # Unsaved version has an HTML block html_block = HTML.objects.create(content='<p>HTML Block</p>') content_block = ContentBlock.objects.create( obj_version=self.page_version_2, column='main_content', position=1, content_type=ContentType.objects.get_for_model(html_block), object_id=html_block.id) html_block.content_block = content_block html_block.save() # Discard page URLs info = self.page._meta.app_label, self.page._meta.model_name self.discard_version_1_url = reverse('admin:%s_%s_discard' % info, kwargs={ 'version_id': self.page_version_1.id, }) self.discard_version_2_url = reverse('admin:%s_%s_discard' % info, kwargs={ 'version_id': self.page_version_2.id, }) def test_anon_user(self): # No for anon users response = self.anon_client.post(self.discard_version_2_url, follow=True) self.assertTemplateUsed(response, 'admin/login.html') def test_staff(self): # No for staff response = self.staff_client.post(self.discard_version_2_url) self.assertEqual(response.status_code, 403) def test_saved_version(self): # Can't discard a saved version response = self.editor_client.post(self.discard_version_1_url) self.assertEqual(response.status_code, 403) def test_other_editor(self): # No for other editor response = self.editor2_client.post(self.discard_version_2_url) self.assertEqual(response.status_code, 403) def test_discard_get(self): # Discard popup loads a simple form response = self.editor_client.get(self.discard_version_2_url) self.assertTemplateUsed(response, 'admin/glitter/version_discard.html') def test_discard_post(self): # Version gets deleted response = self.editor_client.post(self.discard_version_2_url) self.assertRaises( Version.DoesNotExist, Version.objects.get, id=self.page_version_2.id) self.assertTemplateUsed(response, 'admin/glitter/version_discarded.html')
class TestUnpublishVersion(BaseEditCase): def setUp(self): super(TestUnpublishVersion, self).setUp() # Page with one published version self.page_url = '/edit-version/' self.page = Page.objects.create(url=self.page_url, title='Test page') self.page_version = Version( content_type=self.page_content_type, object_id=self.page.id, template_name='glitter/sample.html', owner=self.editor) self.page_version.generate_version() self.page_version.save() self.page.current_version = self.page_version self.page.save() # Edit page URLs info = self.page._meta.app_label, self.page._meta.model_name self.unpublish_version_url = reverse('admin:%s_%s_unpublish' % info, kwargs={ 'version_id': self.page_version.id, }) self.view_version_url = reverse('admin:%s_%s_version' % info, kwargs={ 'version_id': self.page_version.id, }) def test_anon_user(self): # No for anon users response = self.anon_client.post(self.unpublish_version_url, follow=True) self.assertTemplateUsed(response, 'admin/login.html') def test_publisher_post(self): # Have to POST to unpublish response = self.editor_client.get(self.unpublish_version_url) self.assertEqual(response.status_code, 405) def test_staff(self): # No for staff response = self.staff_client.post(self.unpublish_version_url) self.assertEqual(response.status_code, 403) def test_editor(self): # Editor without publish permission can't unpublish response = self.editor_client.post(self.unpublish_version_url) self.assertEqual(response.status_code, 403) def test_unpublish(self): # We'll be redirected to the view version which is just unpublished response = self.publisher_client.post(self.unpublish_version_url, follow=True) page = Page.objects.get(id=self.page.id) # refetch self.assertEqual(page.current_version, None) self.assertRedirects(response, self.view_version_url) self.assertTemplateUsed(response, 'glitter/sample.html') def test_unpublish_not_current(self): new_version = Version( content_type=self.page_content_type, object_id=self.page.id, template_name='glitter/sample.html', owner=self.editor) new_version.generate_version() new_version.save() self.page.current_version = new_version self.page.save() # Unsaved version publish saves a new version number, and redirects to the live URL response = self.publisher_client.post(self.unpublish_version_url, follow=True) page = Page.objects.get(id=self.page.id) # refetch self.assertEqual(page.current_version.id, new_version.id) self.assertRedirects(response, self.view_version_url) self.assertTemplateUsed(response, 'glitter/sample.html')