def published(self, site=None): pub = self.on_site(site).filter(published=True) if get_cms_setting('SHOW_START_DATE'): pub = pub.filter( Q(publication_date__lt=timezone.now()) | Q(publication_date__isnull=True)) if get_cms_setting('SHOW_END_DATE'): pub = pub.filter( Q(publication_end_date__gte=timezone.now()) | Q(publication_end_date__isnull=True)) return pub
def save(self, no_signals=False, commit=True, **kwargs): """ Args: commit: True if model should be really saved """ # delete template cache if hasattr(self, '_template_cache'): delattr(self, '_template_cache') created = not bool(self.pk) # Published pages should always have a publication date # if the page is published we set the publish date if not set yet. if self.publication_date is None and self.published: self.publication_date = timezone.now() - timedelta(seconds=5) if self.reverse_id == "": self.reverse_id = None from cms.utils.permissions import _thread_locals user = getattr(_thread_locals, "user", None) if user: self.changed_by = user.username else: self.changed_by = "script" if created: self.created_by = self.changed_by if commit: if no_signals: # ugly hack because of mptt self.save_base(cls=self.__class__, **kwargs) else: super(Page, self).save(**kwargs)
def reset_timetravel_date(): """ A function to reset the timetravel date within the thread local. It sets the date to the current date and time, according to its appropriate timezone. """ set_timetravel_date(timezone.now())
def update_moderation_message(page, message): """This is bit special.. It updates last page state made from current user for given page. Its called after page is saved - page state is created when page gets saved (in signal), so this might have a concurrency issue, but probably will work in 99,999%. If any page state isn't found in last UPDATE_TOLERANCE seconds, a new state will be created instead of affecting old message. """ UPDATE_TOLERANCE = 30 # max in last 30 seconds from cms.utils.permissions import get_current_user user = get_current_user() created = timezone.now() - datetime.timedelta(seconds=UPDATE_TOLERANCE) try: state = page.pagemoderatorstate_set.filter( user=user, created__gt=created).order_by('-created')[0] except IndexError: state = None if not state or state.message: # If no state was found or it already has a message, create a new one state = PageModeratorState(user=user, page=page, action=PageModeratorState.ACTION_CHANGED) state.message = message state.save()
def published(self, site=None): pub = self.on_site(site).filter(published=True) if settings.CMS_SHOW_START_DATE: pub = pub.filter( Q(publication_date__lt=timezone.now()) | Q(publication_date__isnull=True) ) if settings.CMS_SHOW_END_DATE: pub = pub.filter( Q(publication_end_date__gte=timezone.now()) | Q(publication_end_date__isnull=True) ) return pub
def update_moderation_message(page, message): """This is bit special.. It updates last page state made from current user for given page. Its called after page is saved - page state is created when page gets saved (in signal), so this might have a concurrency issue, but probably will work in 99,999%. If any page state isn't found in last UPDATE_TOLERANCE seconds, a new state will be created instead of affecting old message. """ UPDATE_TOLERANCE = 30 # max in last 30 seconds from cms.utils.permissions import get_current_user user = get_current_user() created = timezone.now() - datetime.timedelta(seconds=UPDATE_TOLERANCE) try: state = page.pagemoderatorstate_set.filter(user=user, created__gt=created).order_by('-created')[0] except IndexError: state = None if not state or state.message: # If no state was found or it already has a message, create a new one state = PageModeratorState(user=user, page=page, action=PageModeratorState.ACTION_CHANGED) state.message = message state.save()
def get_timetravel_date(): """ Returns the timetravel date from ``thread_locals`` as datetime object. If no date is set, it'll return the current date and time according to its appropriate timezone. """ timetravel_date = getattr(_thread_locals, 'timetravel', timezone.now()) return timetravel_date
def test_sitemap_uses_publication_date_when_later_than_modification(self): now = timezone.now() one_day_ago = now - datetime.timedelta(days=1) page = create_page("page", "nav_playground.html", "en", published=True, publication_date=now) page.creation_date = one_day_ago page.changed_date = one_day_ago sitemap = CMSSitemap() actual_last_modification_time = sitemap.lastmod(page) self.assertEqual(actual_last_modification_time, now)
def test_sitemap_includes_last_modification_date(self): one_day_ago = timezone.now() - datetime.timedelta(days=1) page = create_page("page", "nav_playground.html", "en", published=True, publication_date=one_day_ago) page.creation_date = one_day_ago page.save() sitemap = CMSSitemap() self.assertEqual(sitemap.items().count(), 1) actual_last_modification_time = sitemap.lastmod(sitemap.items()[0]) self.assertTrue(actual_last_modification_time > one_day_ago)
def test_page_already_expired(self): """ Test that a page which has a end date in the past gives a 404, not a 500. """ yesterday = timezone.now() - datetime.timedelta(days=1) with SettingsOverride(CMS_PERMISSION=False): page = create_page('page', 'nav_playground.html', 'en', publication_end_date=yesterday, published=True) resp = self.client.get(page.get_absolute_url('en')) self.assertEqual(resp.status_code, 404)
def test_editing_plugin_changes_page_modification_time_in_sitemap(self): now = timezone.now() one_day_ago = now - datetime.timedelta(days=1) page = create_page("page", "nav_playground.html", "en", published=True, publication_date=now) page.creation_date = one_day_ago page.changed_date = one_day_ago plugin_id = self._create_text_plugin_on_page(page) plugin = self._edit_text_plugin(plugin_id, "fnord") actual_last_modification_time = CMSSitemap().lastmod(page) self.assertEqual(plugin.changed_date, actual_last_modification_time)
def test_editing_plugin_changes_page_modification_time_in_sitemap(self): now = timezone.now() one_day_ago = now - datetime.timedelta(days=1) page = create_page("page", "nav_playground.html", "en", published=True, publication_date=now) page.creation_date = one_day_ago page.changed_date = one_day_ago plugin_id = self._create_text_plugin_on_page(page) plugin = self._edit_text_plugin(plugin_id, "fnord") actual_last_modification_time = CMSSitemap().lastmod(page) self.assertEqual( plugin.changed_date - datetime.timedelta(microseconds=plugin.changed_date.microsecond), actual_last_modification_time - datetime.timedelta( microseconds=actual_last_modification_time.microsecond))
def _set_session(self): # Work-around in var. self.client.session isn't mutable # See also: https://code.djangoproject.com/ticket/10899 session = self.client.session session['timetravel_date'] = timezone.now() session.save()
def expired(self): return self.on_site().filter( publication_end_date__lte=timezone.now())
def _date(self, date=timezone.now()): return date.strftime('%Y-%m-%d')
def expired(self): return self.on_site().filter(publication_end_date__lte=timezone.now())
def save( self, no_signals=False, change_state=True, commit=True, force_with_moderation=False, force_state=None, **kwargs ): """ Args: commit: True if model should be really saved force_with_moderation: can be true when new object gets added under some existing page and this new page will require moderation; this is because of how this adding works - first save, then move """ # delete template cache if hasattr(self, "_template_cache"): delattr(self, "_template_cache") # Published pages should always have a publication date publish_directly, under_moderation = False, False if self.publisher_is_draft: # publisher specific stuff, but only on draft model, this is here # because page initializes publish process if settings.CMS_MODERATOR: under_moderation = force_with_moderation or self.pk and bool(self.get_moderator_queryset().count()) created = not bool(self.pk) if settings.CMS_MODERATOR: if change_state: if created: # new page.... self.moderator_state = Page.MODERATOR_CHANGED elif not self.requires_approvement(): # always change state to need approvement when there is some change self.moderator_state = Page.MODERATOR_NEED_APPROVEMENT if not under_moderation and (self.published or self.publisher_public): # existing page without moderator - publish it directly if # published is True publish_directly = True elif change_state: self.moderator_state = Page.MODERATOR_CHANGED # publish_directly = True - no publisher, no publishing!! - we just # use draft models in this case if force_state is not None: self.moderator_state = force_state # if the page is published we set the publish date if not set yet. if self.publication_date is None and self.published: self.publication_date = timezone.now() if self.reverse_id == "": self.reverse_id = None from cms.utils.permissions import _thread_locals user = getattr(_thread_locals, "user", None) if user: self.changed_by = user.username else: self.changed_by = "script" if not self.pk: self.created_by = self.changed_by if commit: if no_signals: # ugly hack because of mptt self.save_base(cls=self.__class__, **kwargs) else: super(Page, self).save(**kwargs) # if commit and (publish_directly or created and not under_moderation): if self.publisher_is_draft: if self.published: if commit and publish_directly: self.publish()
def save(self, no_signals=False, change_state=True, commit=True, force_with_moderation=False, force_state=None, **kwargs): """ Args: commit: True if model should be really saved force_with_moderation: can be true when new object gets added under some existing page and this new page will require moderation; this is because of how this adding works - first save, then move """ # delete template cache if hasattr(self, '_template_cache'): delattr(self, '_template_cache') # Published pages should always have a publication date publish_directly, under_moderation = False, False if self.publisher_is_draft: # publisher specific stuff, but only on draft model, this is here # because page initializes publish process if settings.CMS_MODERATOR: under_moderation = force_with_moderation or self.pk and bool( self.get_moderator_queryset().count()) created = not bool(self.pk) if settings.CMS_MODERATOR: if change_state: if created: # new page.... self.moderator_state = Page.MODERATOR_CHANGED elif not self.requires_approvement(): # always change state to need approvement when there is some change self.moderator_state = Page.MODERATOR_NEED_APPROVEMENT if not under_moderation and (self.published or self.publisher_public): # existing page without moderator - publish it directly if # published is True publish_directly = True elif change_state: self.moderator_state = Page.MODERATOR_CHANGED #publish_directly = True - no publisher, no publishing!! - we just # use draft models in this case if force_state is not None: self.moderator_state = force_state # if the page is published we set the publish date if not set yet. if self.publication_date is None and self.published: self.publication_date = timezone.now() if self.reverse_id == "": self.reverse_id = None from cms.utils.permissions import _thread_locals user = getattr(_thread_locals, "user", None) if user: self.changed_by = user.username else: self.changed_by = "script" if not self.pk: self.created_by = self.changed_by if commit: if no_signals: # ugly hack because of mptt self.save_base(cls=self.__class__, **kwargs) else: super(Page, self).save(**kwargs) #if commit and (publish_directly or created and not under_moderation): if self.publisher_is_draft: if self.published: if commit and publish_directly: self.publish()
def get_initial(self): initial = super(TimetravelView, self).get_initial() initial['timetravel_date'] = self.request.session.get('timetravel_date', timezone.now()) initial['auto_redirect'] = self.request.session.get('auto_redirect', True) return initial