def test_date_ordering(self): """Test page date ordering feature.""" self.set_setting("PAGE_USE_SITE_ID", False) author = get_user_model().objects.all()[0] yesterday = get_now() - datetime.timedelta(days=1) now = get_now() p1 = Page(author=author, status=Page.PUBLISHED, publication_date=now) p1.save() p2 = Page(author=author, publication_date=now, status=Page.PUBLISHED) p2.save() p3 = Page(author=author, publication_date=yesterday, status=Page.PUBLISHED) p3.save() p2.move_to(p1, position='first-child') p3.move_to(p1, position='first-child') p1 = Page.objects.get(pk=p1.id) p2 = Page.objects.get(pk=p2.id) p3 = Page.objects.get(pk=p3.id) self.assertEqual([p.id for p in p1.get_children_for_frontend()], [p3.id, p2.id]) self.assertEqual( [p.id for p in p1.get_date_ordered_children_for_frontend()], [p2.id, p3.id])
def _get_calculated_status(self): """Get the calculated status of the page based on :attr:`Page.publication_date`, :attr:`Page.publication_end_date`, and :attr:`Page.status`.""" if settings.PAGE_SHOW_START_DATE and self.publication_date: if self.publication_date > get_now(): return self.DRAFT if settings.PAGE_SHOW_END_DATE and self.publication_end_date: if self.publication_end_date < get_now(): return self.EXPIRED return self.status
def filter_published(self, queryset): """Filter the given pages :class:`QuerySet` to obtain only published page.""" if settings.PAGE_USE_SITE_ID: queryset = queryset.filter(sites=global_settings.SITE_ID) queryset = queryset.filter(status=self.model.PUBLISHED) if settings.PAGE_SHOW_START_DATE: queryset = queryset.filter(publication_date__lte=get_now()) if settings.PAGE_SHOW_END_DATE: queryset = queryset.filter(Q(publication_end_date__gt=get_now()) | Q(publication_end_date__isnull=True)) return queryset
def test_page_caculated_status(self): """Test calculated status property.""" self.set_setting("PAGE_SHOW_START_DATE", True) yesterday = get_now() - datetime.timedelta(days=1) tomorrow = get_now() + datetime.timedelta(days=1) page = self.new_page() self.assertEqual(page.calculated_status, Page.PUBLISHED) page.publication_date = tomorrow self.assertEqual(page.calculated_status, Page.DRAFT) page.publication_date = yesterday self.assertEqual(page.calculated_status, Page.PUBLISHED) self.set_setting("PAGE_SHOW_END_DATE", True) page.publication_end_date = yesterday self.assertEqual(page.calculated_status, Page.EXPIRED)
def test_page_freeze_date(self): """Test page freezing feature.""" c = self.get_admin_client() page_data = self.get_new_page_data() page_data['title'] = 'before' page_data['slug'] = 'before' response = c.post('/admin/pages/page/add/', page_data) self.assertRedirects(response, '/admin/pages/page/') page = Page.objects.from_path('before', None) self.assertEqual(page.freeze_date, None) limit = get_now() page.freeze_date = limit page.save() page_data['title'] = 'after' page_data['slug'] = 'after' # this post erase the limit response = c.post('/admin/pages/page/%d/' % page.id, page_data) self.assertRedirects(response, '/admin/pages/page/') page = Page.objects.from_path('after', None) page.freeze_date = limit self.assertEqual(page.slug(), 'before') page.freeze_date = None page.save() self.assertEqual(page.slug(), 'after') page.freeze_date = limit page.save() self.assertEqual(page.slug(), 'before')
def test_page_freeze_date(self): """Test page freezing feature.""" c = self.get_admin_client() page_data = self.get_new_page_data() page_data['title'] = 'before' page_data['slug'] = 'before' response = c.post(add_url, page_data) self.assertRedirects(response, changelist_url) page = Page.objects.from_path('before', None) self.assertEqual(page.freeze_date, None) limit = get_now() page.freeze_date = limit page.save() page_data['title'] = 'after' page_data['slug'] = 'after' # this post erase the limit response = c.post(reverse("admin:pages_page_change", args=[page.id]), page_data) self.assertRedirects(response, changelist_url) page = Page.objects.from_path('after', None) page.freeze_date = limit self.assertEqual(page.slug(), 'before') page.freeze_date = None page.save() self.assertEqual(page.slug(), 'after') page.freeze_date = limit page.save() self.assertEqual(page.slug(), 'before')
def drafts(self): """Creates a :class:`QuerySet` of drafts using the page's :attr:`Page.publication_date`.""" pub = self.on_site().filter(status=self.model.DRAFT) if settings.PAGE_SHOW_START_DATE: pub = pub.filter(publication_date__gte=get_now()) return pub
def test_page_freeze_date(self): """Test page freezing feature.""" c = self.get_admin_client() page_data = self.get_new_page_data() page_data['title'] = 'before' page_data['slug'] = 'before' response = c.post('/admin/pages/page/add/', page_data) self.assertRedirects(response, '/admin/pages/page/') page = Page.objects.from_path('before', None) self.assertEqual(page.freeze_date, None) limit = get_now() page.freeze_date = limit page.save() page_data['title'] = 'after' page_data['slug'] = 'after' # this post erase the limit response = c.post(reverse("admin:pages_page_change", args=[page.id]), page_data) self.assertRedirects(response, '/admin/pages/page/') page = Page.objects.from_path('after', None) page.freeze_date = limit self.assertEqual(page.slug(), 'before') page.freeze_date = None page.save() self.assertEqual(page.slug(), 'after') page.freeze_date = limit page.save() self.assertEqual(page.slug(), 'before')
def filter_published(self, queryset): """Filter the given pages :class:`QuerySet` to obtain only published page.""" if settings.PAGE_USE_SITE_ID: queryset = queryset.filter(sites=global_settings.SITE_ID) queryset = queryset.filter(status=self.model.PUBLISHED) if settings.PAGE_SHOW_START_DATE: queryset = queryset.filter(publication_date__lte=get_now()) if settings.PAGE_SHOW_END_DATE: queryset = queryset.filter( Q(publication_end_date__gt=get_now()) | Q(publication_end_date__isnull=True)) return queryset
def save(self, *args, **kwargs): """Override the default ``save`` method.""" if not self.status: self.status = self.DRAFT # Published pages should always have a publication date if self.publication_date is None and self.status == self.PUBLISHED: self.publication_date = get_now() # Drafts should not, unless they have been set to the future if self.status == self.DRAFT: if settings.PAGE_SHOW_START_DATE: if self.publication_date and self.publication_date <= get_now(): self.publication_date = None else: self.publication_date = None self.last_modification_date = get_now() super(Page, self).save(*args, **kwargs) # fix sites many-to-many link when the're hidden from the form if settings.PAGE_HIDE_SITES and self.sites.count() == 0: self.sites.add(Site.objects.get(pk=global_settings.SITE_ID))
def save(self, *args, **kwargs): """Override the default ``save`` method.""" if not self.status: self.status = self.DRAFT # Published pages should always have a publication date if self.publication_date is None and self.status == self.PUBLISHED: self.publication_date = get_now() # Drafts should not, unless they have been set to the future if self.status == self.DRAFT: if settings.PAGE_SHOW_START_DATE: if (self.publication_date and self.publication_date <= get_now()): self.publication_date = None else: self.publication_date = None self.last_modification_date = get_now() super(Page, self).save(*args, **kwargs) # fix sites many-to-many link when the're hidden from the form if settings.PAGE_HIDE_SITES and self.sites.count() == 0: self.sites.add(Site.objects.get(pk=global_settings.SITE_ID))
def test_date_ordering(self): """Test page date ordering feature.""" self.set_setting("PAGE_USE_SITE_ID", False) author = User.objects.all()[0] yesterday = get_now() - datetime.timedelta(days=1) now = get_now() p1 = Page(author=author, status=Page.PUBLISHED, publication_date=now) p1.save() p2 = Page(author=author, publication_date=now, status=Page.PUBLISHED) p2.save() p3 = Page(author=author, publication_date=yesterday, status=Page.PUBLISHED) p3.save() p2.move_to(p1, position="first-child") p3.move_to(p1, position="first-child") p1 = Page.objects.get(pk=p1.id) p2 = Page.objects.get(pk=p2.id) p3 = Page.objects.get(pk=p3.id) self.assertEqual([p.id for p in p1.get_children_for_frontend()], [p3.id, p2.id]) self.assertEqual([p.id for p in p1.get_date_ordered_children_for_frontend()], [p2.id, p3.id])
def expired(self): """Creates a :class:`QuerySet` of expired using the page's :attr:`Page.publication_end_date`.""" return self.on_site().filter( publication_end_date__lte=get_now())