def page_index_factory(lang, lang_name): if isinstance(lang_name, basestring): lang_name = ugettext_lazy(lang_name) def get_absolute_url(self): return '/%s%s' % (lang, Page.get_absolute_url(self)) class Meta: proxy = True app_label = 'cms' verbose_name = string_concat(Page._meta.verbose_name, ' (', lang_name, ')') verbose_name_plural = string_concat(Page._meta.verbose_name_plural, ' (', lang_name, ')') attrs = { '__module__': Page.__module__, 'Meta': Meta, 'objects': PageManager(), 'get_absolute_url': get_absolute_url } _PageProxy = type("Page%s" % lang.title(), (Page, ), attrs) _PageProxy._meta.parent_attr = 'parent' _PageProxy._meta.left_attr = 'lft' _PageProxy._meta.right_attr = 'rght' _PageProxy._meta.tree_id_attr = 'tree_id' class _PageIndex(indexes.SearchIndex): language = lang text = indexes.CharField(document=True, use_template=False) pub_date = indexes.DateTimeField(model_attr='publication_date') login_required = indexes.BooleanField(model_attr='login_required') url = indexes.CharField(stored=True, indexed=False, model_attr='get_absolute_url') title = indexes.CharField(stored=True, indexed=False, model_attr='get_title') def prepare(self, obj): self.prepared_data = super(_PageIndex, self).prepare(obj) plugins = obj.cmsplugin_set.filter(language=lang) text = '' for plugin in plugins: instance, _ = plugin.get_plugin_instance() if hasattr(instance, 'search_fields'): text += ''.join( getattr(instance, field) for field in instance.search_fields) self.prepared_data['text'] = text return self.prepared_data def get_queryset(self): return _PageProxy.objects.published().filter( title_set__language=lang, publisher_is_draft=False).distinct() return _PageProxy, _PageIndex
def page_proxy_factory(language_code, language_name): def get_absolute_url(self): if 'cms.middleware.multilingual.MultilingualURLMiddleware' in settings.MIDDLEWARE_CLASSES: old_language = get_language() try: activate(language_code) return '/%s%s' % (language_code, Page.get_absolute_url(self)) finally: activate(old_language) else: return Page.get_absolute_url(self) class Meta: proxy = True app_label = 'cms_search' if len(settings.LANGUAGES) > 1: verbose_name = string_concat(Page._meta.verbose_name, ' (', language_name, ')') verbose_name_plural = string_concat(Page._meta.verbose_name_plural, ' (', language_name, ')') else: verbose_name = Page._meta.verbose_name verbose_name_plural = Page._meta.verbose_name_plural attrs = { '__module__': Page.__module__, 'Meta': Meta, 'objects': PageManager(), 'get_absolute_url': get_absolute_url } _PageProxy = type(proxy_name(language_code), (Page, ), attrs) _PageProxy._meta.parent_attr = 'parent' _PageProxy._meta.left_attr = 'lft' _PageProxy._meta.right_attr = 'rght' _PageProxy._meta.tree_id_attr = 'tree_id' return _PageProxy
class Page(MPTTModel): """ A simple hierarchical page model """ __metaclass__ = PageMetaClass MODERATOR_CHANGED = 0 MODERATOR_NEED_APPROVEMENT = 1 MODERATOR_NEED_DELETE_APPROVEMENT = 2 MODERATOR_APPROVED = 10 # special case - page was approved, but some of page parents are not approved yet MODERATOR_APPROVED_WAITING_FOR_PARENTS = 11 moderator_state_choices = ( (MODERATOR_CHANGED, _('changed')), (MODERATOR_NEED_APPROVEMENT, _('req. app.')), (MODERATOR_NEED_DELETE_APPROVEMENT, _('delete')), (MODERATOR_APPROVED, _('approved')), (MODERATOR_APPROVED_WAITING_FOR_PARENTS, _('app. par.')), ) LIMIT_VISIBILITY_IN_MENU_CHOICES = ( (1, _('for logged in users only')), (2, _('for anonymous users only')), ) PUBLISHER_STATE_DEFAULT = 0 PUBLISHER_STATE_DIRTY = 1 PUBLISHER_STATE_DELETE = 2 template_choices = [(x, _(y)) for x, y in settings.CMS_TEMPLATES] created_by = models.CharField(_("created by"), max_length=70, editable=False) changed_by = models.CharField(_("changed by"), max_length=70, editable=False) parent = models.ForeignKey('self', null=True, blank=True, related_name='children', db_index=True) creation_date = models.DateTimeField(auto_now_add=True) changed_date = models.DateTimeField(auto_now=True) publication_date = models.DateTimeField( _("publication date"), null=True, blank=True, help_text= _('When the page should go live. Status must be "Published" for page to go live.' ), db_index=True) publication_end_date = models.DateTimeField( _("publication end date"), null=True, blank=True, help_text=_('When to expire the page. Leave empty to never expire.'), db_index=True) in_navigation = models.BooleanField(_("in navigation"), default=True, db_index=True) soft_root = models.BooleanField( _("soft root"), db_index=True, default=False, help_text=_("All ancestors will not be displayed in the navigation")) reverse_id = models.CharField( _("id"), max_length=40, db_index=True, blank=True, null=True, help_text= _("An unique identifier that is used with the page_url templatetag for linking to this page" )) navigation_extenders = models.CharField(_("attached menu"), max_length=80, db_index=True, blank=True, null=True) published = models.BooleanField(_("is published"), blank=True) template = models.CharField( _("template"), max_length=100, choices=template_choices, help_text=_('The template used to render the content.')) site = models.ForeignKey( Site, help_text=_('The site the page is accessible at.'), verbose_name=_("site")) moderator_state = models.SmallIntegerField( _('moderator state'), choices=moderator_state_choices, default=MODERATOR_NEED_APPROVEMENT, blank=True) level = models.PositiveIntegerField(db_index=True, editable=False) lft = models.PositiveIntegerField(db_index=True, editable=False) rght = models.PositiveIntegerField(db_index=True, editable=False) tree_id = models.PositiveIntegerField(db_index=True, editable=False) login_required = models.BooleanField(_("login required"), default=False) limit_visibility_in_menu = models.SmallIntegerField( _("menu visibility"), default=None, null=True, blank=True, choices=LIMIT_VISIBILITY_IN_MENU_CHOICES, db_index=True, help_text=_("limit when this page is visible in the menu")) # Placeholders (plugins) placeholders = models.ManyToManyField(Placeholder, editable=False) # Publisher fields publisher_is_draft = models.BooleanField(default=1, editable=False, db_index=True) publisher_public = models.OneToOneField('self', related_name='publisher_draft', null=True, editable=False) publisher_state = models.SmallIntegerField(default=0, editable=False, db_index=True) # Managers objects = PageManager() permissions = PagePermissionsPermissionManager() class Meta: permissions = (('view_page', 'Can view page'), ) verbose_name = _('page') verbose_name_plural = _('pages') ordering = ('site', 'tree_id', 'lft') app_label = 'cms' class PublisherMeta: exclude_fields_append = [ 'id', 'publisher_is_draft', 'publisher_public', 'publisher_state', 'moderator_state', 'placeholders', 'lft', 'rght', 'tree_id', 'parent' ] def __unicode__(self): title = self.get_menu_title(fallback=True) if title is None: title = u"" return u'%s' % (title, ) def get_absolute_url(self, language=None, fallback=True): if self.is_home(): return reverse('pages-root') if settings.CMS_FLAT_URLS: path = self.get_slug(language, fallback) return urlutils.urljoin(reverse('pages-root'), path) # else path = self.get_path(language, fallback) return urlutils.urljoin(reverse('pages-root'), path) def move_page(self, target, position='first-child'): """Called from admin interface when page is moved. Should be used on all the places which are changing page position. Used like an interface to mptt, but after move is done page_moved signal is fired. """ # make sure move_page does not break when using INHERIT template if (position in ('left', 'right') and not target.parent and self.template == settings.CMS_TEMPLATE_INHERITANCE_MAGIC): self.template = self.get_template() self.move_to(target, position) # fire signal from cms.models.moderatormodels import PageModeratorState self.force_moderation_action = PageModeratorState.ACTION_MOVE import cms.signals as cms_signals cms_signals.page_moved.send( sender=Page, instance=self) #titles get saved before moderation self.save(change_state=True ) # always save the page after move, because of publisher # check the slugs page_utils.check_title_slugs(self) def copy_page(self, target, site, position='first-child', copy_permissions=True, copy_moderation=True, public_copy=False): """ copy a page [ and all its descendants to a new location ] Doesn't checks for add page permissions anymore, this is done in PageAdmin. Note: public_copy was added in order to enable the creation of a copy for creating the public page during the publish operation as it sets the publisher_is_draft=False. """ from cms.utils.moderator import update_moderation_message page_copy = None if public_copy: # create a copy of the draft page - existing code loops through pages so added it to a list pages = [copy.copy(self)] else: pages = [self] + list(self.get_descendants().order_by('-rght')) if not public_copy: site_reverse_ids = Page.objects.filter( site=site, reverse_id__isnull=False).values_list('reverse_id', flat=True) if target: target.old_pk = -1 if position == "first-child": tree = [target] elif target.parent_id: tree = [target.parent] else: tree = [] else: tree = [] if tree: tree[0].old_pk = tree[0].pk first = True # loop over all affected pages (self is included in descendants) for page in pages: titles = list(page.title_set.all()) # get all current placeholders (->plugins) placeholders = list(page.placeholders.all()) origin_id = page.id # create a copy of this page by setting pk = None (=new instance) page.old_pk = page.pk page.pk = None page.level = None page.rght = None page.lft = None page.tree_id = None page.published = False page.moderator_state = Page.MODERATOR_CHANGED page.publisher_public_id = None # only set reverse_id on standard copy if not public_copy: if page.reverse_id in site_reverse_ids: page.reverse_id = None if first: first = False if tree: page.parent = tree[0] else: page.parent = None page.insert_at(target, position) else: count = 1 found = False for prnt in tree: if prnt.old_pk == page.parent_id: page.parent = prnt tree = tree[0:count] found = True break count += 1 if not found: page.parent = None tree.append(page) page.site = site # override default page settings specific for public copy if public_copy: page.published = True page.publisher_is_draft = False page.moderator_state = Page.MODERATOR_APPROVED # we need to set relate this new public copy to its draft page (self) page.publisher_public = self # code taken from Publisher publish() overridden here as we need to save the page # before we are able to use the page object for titles, placeholders etc.. below # the method has been modified to return the object after saving the instance variable page = self._publisher_save_public(page) page_copy = page # create a copy used in the return else: # only need to save the page if it isn't public since it is saved above otherwise page.save() # copy moderation, permissions if necessary if settings.CMS_PERMISSION and copy_permissions: from cms.models.permissionmodels import PagePermission for permission in PagePermission.objects.filter( page__id=origin_id): permission.pk = None permission.page = page permission.save() if settings.CMS_MODERATOR and copy_moderation: from cms.models.moderatormodels import PageModerator for moderator in PageModerator.objects.filter( page__id=origin_id): moderator.pk = None moderator.page = page moderator.save() # update moderation message for standard copy if not public_copy: update_moderation_message(page, unicode(_('Page was copied.'))) # copy titles of this page for title in titles: title.pk = None # setting pk = None creates a new instance title.publisher_public_id = None title.published = False title.page = page # create slug-copy for standard copy if not public_copy: title.slug = page_utils.get_available_slug(title) title.save() # copy the placeholders (and plugins on those placeholders!) for ph in placeholders: plugins = list(ph.cmsplugin_set.all().order_by( 'tree_id', '-rght')) try: ph = page.placeholders.get(slot=ph.slot) except Placeholder.DoesNotExist: ph.pk = None # make a new instance ph.save() page.placeholders.add(ph) if plugins: copy_plugins_to(plugins, ph) # invalidate the menu for this site menu_pool.clear(site_id=site.pk) return page_copy # return the page_copy or None 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 """ # 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 = datetime.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_base(self, *args, **kwargs): """Overriden save_base. If an instance is draft, and was changed, mark it as dirty. Dirty flag is used for changed nodes identification when publish method takes place. After current changes are published, state is set back to PUBLISHER_STATE_DEFAULT (in publish method). """ keep_state = getattr(self, '_publisher_keep_state', None) if self.publisher_is_draft and not keep_state: self.publisher_state = self.PUBLISHER_STATE_DIRTY if keep_state: delattr(self, '_publisher_keep_state') ret = super(Page, self).save_base(*args, **kwargs) return ret def publish(self): """Overrides Publisher method, because there may be some descendants, which are waiting for parent to publish, so publish them if possible. IMPORTANT: @See utils.moderator.approve_page for publishing permissions Returns: True if page was successfully published. """ # Publish can only be called on moderated and draft pages if not self.publisher_is_draft: return # publish, but only if all parents are published!! published = None if not self.pk: self.save() if self._publisher_can_publish(): ######################################################################## # Assign the existing public page in old_public and mark it as # PUBLISHER_STATE_DELETE # the draft version was being deleted if I replaced the save() # below with a delete() directly so the deletion is handle at the end old_public = self.get_public_object() if old_public: old_public.publisher_state = self.PUBLISHER_STATE_DELETE # store old public on self, pass around instead self.old_public = old_public old_public.publisher_public = None # remove the reference to the publisher_draft version of the page so it does not get deleted old_public.save() # we hook into the modified copy_page routing to do the heavy lifting of copying the draft page to a new public page new_public = self.copy_page(target=None, site=self.site, copy_moderation=False, position=None, copy_permissions=False, public_copy=True) # taken from Publisher - copy_page needs to call self._publisher_save_public(copy) for mptt insertion # insert_at() was maybe calling _create_tree_space() method, in this # case may tree_id change, so we must update tree_id from db first # before save if getattr(self, 'tree_id', None): me = self._default_manager.get(pk=self.pk) self.tree_id = me.tree_id self.published = True self.publisher_public = new_public self.moderator_state = Page.MODERATOR_APPROVED self.publisher_state = self.PUBLISHER_STATE_DEFAULT self._publisher_keep_state = True published = True else: self.moderator_state = Page.MODERATOR_APPROVED_WAITING_FOR_PARENTS self.save(change_state=False) if not published: # was not published, escape return # clean moderation log self.pagemoderatorstate_set.all().delete() # we delete the old public page - this only deletes the public page as we # have removed the old_public.publisher_public=None relationship to the draft page above if old_public: # reparent public child pages before delete so they don't get purged as well for child_page in old_public.children.order_by('lft'): child_page.move_to(new_public, 'last-child') child_page.save(change_state=False) # reload old_public to get correct tree attrs old_public = Page.objects.get(pk=old_public.pk) old_public.move_to(None, 'last-child') # moving the object out of the way berore deleting works, but why? # finally delete the old public page old_public.delete() # page was published, check if there are some childs, which are waiting # for publishing (because of the parent) publish_set = self.children.filter( moderator_state=Page.MODERATOR_APPROVED_WAITING_FOR_PARENTS) for page in publish_set: # recursive call to all childrens.... page.moderator_state = Page.MODERATOR_APPROVED page.save(change_state=False) page.publish() # fire signal after publishing is done import cms.signals as cms_signals cms_signals.post_publish.send(sender=Page, instance=self) return published def delete(self): """Mark public instance for deletion and delete draft. """ placeholders = self.placeholders.all() for ph in placeholders: plugin = CMSPlugin.objects.filter(placeholder=ph) plugin.delete() ph.delete() if self.publisher_public_id: # mark the public instance for deletion self.publisher_public.publisher_state = self.PUBLISHER_STATE_DELETE self.publisher_public.save() super(Page, self).delete() def delete_with_public(self): placeholders = list(self.placeholders.all()) if self.publisher_public_id: placeholders = placeholders + list( self.publisher_public.placeholders.all()) for ph in placeholders: plugin = CMSPlugin.objects.filter(placeholder=ph) plugin.delete() ph.delete() if self.publisher_public_id: self.publisher_public.delete() super(Page, self).delete() def get_draft_object(self): return self def get_public_object(self): return self.publisher_public def get_languages(self): """ get the list of all existing languages for this page """ from cms.models.titlemodels import Title if not hasattr(self, "all_languages"): self.all_languages = Title.objects.filter(page=self).values_list( "language", flat=True).distinct() self.all_languages = list(self.all_languages) self.all_languages.sort() return self.all_languages def get_cached_ancestors(self, ascending=True): if ascending: if not hasattr(self, "ancestors_ascending"): self.ancestors_ascending = list(self.get_ancestors(ascending)) return self.ancestors_ascending else: if not hasattr(self, "ancestors_descending"): self.ancestors_descending = list(self.get_ancestors(ascending)) return self.ancestors_descending def get_title_obj(self, language=None, fallback=True, version_id=None, force_reload=False): """Helper function for accessing wanted / current title. If wanted title doesn't exists, EmptyTitle instance will be returned. """ language = self._get_title_cache(language, fallback, version_id, force_reload) if language in self.title_cache: return self.title_cache[language] from cms.models.titlemodels import EmptyTitle return EmptyTitle() def get_title_obj_attribute(self, attrname, language=None, fallback=True, version_id=None, force_reload=False): """Helper function for getting attribute or None from wanted/current title. """ try: attribute = getattr( self.get_title_obj(language, fallback, version_id, force_reload), attrname) return attribute except AttributeError: return None def get_path(self, language=None, fallback=True, version_id=None, force_reload=False): """ get the path of the page depending on the given language """ return self.get_title_obj_attribute("path", language, fallback, version_id, force_reload) def get_slug(self, language=None, fallback=True, version_id=None, force_reload=False): """ get the slug of the page depending on the given language """ return self.get_title_obj_attribute("slug", language, fallback, version_id, force_reload) def get_title(self, language=None, fallback=True, version_id=None, force_reload=False): """ get the title of the page depending on the given language """ return self.get_title_obj_attribute("title", language, fallback, version_id, force_reload) def get_menu_title(self, language=None, fallback=True, version_id=None, force_reload=False): """ get the menu title of the page depending on the given language """ menu_title = self.get_title_obj_attribute("menu_title", language, fallback, version_id, force_reload) if not menu_title: return self.get_title(language, True, version_id, force_reload) return menu_title def get_page_title(self, language=None, fallback=True, version_id=None, force_reload=False): """ get the page title of the page depending on the given language """ page_title = self.get_title_obj_attribute("page_title", language, fallback, version_id, force_reload) if not page_title: return self.get_title(language, True, version_id, force_reload) return page_title def get_meta_description(self, language=None, fallback=True, version_id=None, force_reload=False): """ get content for the description meta tag for the page depending on the given language """ return self.get_title_obj_attribute("meta_description", language, fallback, version_id, force_reload) def get_meta_keywords(self, language=None, fallback=True, version_id=None, force_reload=False): """ get content for the keywords meta tag for the page depending on the given language """ return self.get_title_obj_attribute("meta_keywords", language, fallback, version_id, force_reload) def get_application_urls(self, language=None, fallback=True, version_id=None, force_reload=False): """ get application urls conf for application hook """ return self.get_title_obj_attribute("application_urls", language, fallback, version_id, force_reload) def get_redirect(self, language=None, fallback=True, version_id=None, force_reload=False): """ get redirect """ return self.get_title_obj_attribute("redirect", language, fallback, version_id, force_reload) def _get_title_cache(self, language, fallback, version_id, force_reload): if not language: language = get_language() load = False if not hasattr(self, "title_cache") or force_reload: load = True self.title_cache = {} elif not language in self.title_cache: if fallback: fallback_langs = i18n.get_fallback_languages(language) for lang in fallback_langs: if lang in self.title_cache: return lang load = True if load: from cms.models.titlemodels import Title if version_id: from reversion.models import Version version = get_object_or_404(Version, pk=version_id) revs = [ related_version.object_version for related_version in version.revision.version_set.all() ] for rev in revs: obj = rev.object if obj.__class__ == Title: self.title_cache[obj.language] = obj else: title = Title.objects.get_title(self, language, language_fallback=fallback) if title: self.title_cache[title.language] = title language = title.language return language def get_template(self): """ get the template of this page if defined or if closer parent if defined or DEFAULT_PAGE_TEMPLATE otherwise """ template = None if self.template: if self.template != settings.CMS_TEMPLATE_INHERITANCE_MAGIC: template = self.template else: for p in self.get_ancestors(ascending=True): template = p.get_template() if template: break if not template: template = settings.CMS_TEMPLATES[0][0] return template def get_template_name(self): """ get the textual name (2nd parameter in settings.CMS_TEMPLATES) of the template of this page or of the nearest ancestor. failing to find that, return the name of the default template. """ template = self.get_template() for t in settings.CMS_TEMPLATES: if t[0] == template: return t[1] return _("default") def has_view_permission(self, request): from cms.models.permissionmodels import PagePermission, GlobalPagePermission from cms.utils.plugins import current_site # staff is allowed to see everything if request.user.is_staff and settings.CMS_PUBLIC_FOR in ('staff', 'all'): return True if not self.publisher_is_draft and self.publisher_public: return self.publisher_public.has_view_permission(request) # does any restriction exist? # direct # inherited and direct is_restricted = PagePermission.objects.for_page(self).filter( can_view=True).exists() if request.user.is_authenticated(): site = current_site(request) global_perms_q = Q(can_view=True) & Q( Q(sites__in=[site]) | Q(sites__isnull=True)) global_view_perms = GlobalPagePermission.objects.with_user( request.user).filter(global_perms_q).exists() # a global permission was given to the request's user if global_view_perms: return True # authenticated user, no restriction and public for all if (not is_restricted and not global_view_perms and settings.CMS_PUBLIC_FOR == 'all'): return True else: #anonymous user if is_restricted or not settings.CMS_PUBLIC_FOR == 'all': # anyonymous user, page has restriction and global access is permitted return False else: # anonymous user, no restriction saved in database return True # Authenticated user # Django wide auth perms "can_view" or cms auth perms "can_view" opts = self._meta codename = '%s.view_%s' % (opts.app_label, opts.object_name.lower()) return (request.user.has_perm(codename) or self.has_generic_permission(request, "view")) def has_change_permission(self, request): opts = self._meta if request.user.is_superuser: return True return request.user.has_perm(opts.app_label + '.' + opts.get_change_permission()) and \ self.has_generic_permission(request, "change") def has_delete_permission(self, request): opts = self._meta if request.user.is_superuser: return True return request.user.has_perm(opts.app_label + '.' + opts.get_delete_permission()) and \ self.has_generic_permission(request, "delete") def has_publish_permission(self, request): return self.has_generic_permission(request, "publish") def has_advanced_settings_permission(self, request): return self.has_generic_permission(request, "advanced_settings") def has_change_permissions_permission(self, request): """ Has user ability to change permissions for current page? """ return self.has_generic_permission(request, "change_permissions") def has_add_permission(self, request): """ Has user ability to add page under current page? """ return self.has_generic_permission(request, "add") def has_move_page_permission(self, request): """Has user ability to move current page? """ return self.has_generic_permission(request, "move_page") def has_moderate_permission(self, request): """ Has user ability to moderate current page? If moderation isn't installed, nobody can moderate. """ if not settings.CMS_MODERATOR: return False return self.has_generic_permission(request, "moderate") def has_generic_permission(self, request, perm_type): """ Return true if the current user has permission on the page. Return the string 'All' if the user has all rights. """ att_name = "permission_%s_cache" % perm_type if not hasattr(self, "permission_user_cache") or not hasattr(self, att_name) \ or request.user.pk != self.permission_user_cache.pk: from cms.utils.permissions import has_generic_permission self.permission_user_cache = request.user setattr( self, att_name, has_generic_permission(self.id, request.user, perm_type, self.site_id)) if getattr(self, att_name): self.permission_edit_cache = True return getattr(self, att_name) def is_home(self): if self.parent_id: return False else: try: return self.home_pk_cache == self.pk except NoHomeFound: pass return False def get_home_pk_cache(self): attr = "%s_home_pk_cache_%s" % (self.publisher_is_draft and "draft" or "public", self.site_id) if not hasattr(self, attr): setattr(self, attr, self.get_object_queryset().get_home(self.site).pk) return getattr(self, attr) def set_home_pk_cache(self, value): attr = "%s_home_pk_cache_%s" % (self.publisher_is_draft and "draft" or "public", self.site_id) setattr(self, attr, value) home_pk_cache = property(get_home_pk_cache, set_home_pk_cache) def get_media_path(self, filename): """ Returns path (relative to MEDIA_ROOT/MEDIA_URL) to directory for storing page-scope files. This allows multiple pages to contain files with identical names without namespace issues. Plugins such as Picture can use this method to initialise the 'upload_to' parameter for File-based fields. For example: image = models.ImageField(_("image"), upload_to=CMSPlugin.get_media_path) where CMSPlugin.get_media_path calls self.page.get_media_path This location can be customised using the CMS_PAGE_MEDIA_PATH setting """ return join(settings.CMS_PAGE_MEDIA_PATH, "%d" % self.id, filename) def last_page_states(self): """Returns last five page states, if they exist, optimized, calls sql query only if some states available """ # TODO: optimize SQL... 1 query per page if settings.CMS_MODERATOR: has_moderator_state = getattr(self, '_has_moderator_state_chache', None) if has_moderator_state == False: return self.pagemoderatorstate_set.none() return self.pagemoderatorstate_set.all().order_by('created', )[:5] return self.pagemoderatorstate_set.none() def get_moderator_queryset(self): """Returns ordered set of all PageModerator instances, which should moderate this page """ from cms.models.moderatormodels import PageModerator if not settings.CMS_MODERATOR or not self.tree_id: return PageModerator.objects.get_empty_query_set() q = Q(page__tree_id=self.tree_id, page__level__lt=self.level, moderate_descendants=True) | \ Q(page__tree_id=self.tree_id, page__level=self.level - 1, moderate_children=True) | \ Q(page__pk=self.pk, moderate_page=True) return PageModerator.objects.distinct().filter(q).order_by( 'page__level') def is_under_moderation(self): return bool(self.get_moderator_queryset().count()) def is_approved(self): """Returns true, if page is approved and published, or approved, but parents are missing.. """ return self.moderator_state in ( Page.MODERATOR_APPROVED, Page.MODERATOR_APPROVED_WAITING_FOR_PARENTS) def is_public_published(self): """Returns true if public model is published. """ if hasattr(self, 'public_published_cache'): # if it was cached in change list, return cached value return self.public_published_cache # othervise make db lookup if self.publisher_public_id: return self.publisher_public.published #return is_public_published(self) return False def reload(self): """ Reload a page from the database """ return Page.objects.get(pk=self.pk) def requires_approvement(self): return self.moderator_state in (Page.MODERATOR_NEED_APPROVEMENT, Page.MODERATOR_NEED_DELETE_APPROVEMENT) def get_moderation_value(self, user): """Returns page moderation value for given user, moderation value is sum of moderations. """ moderation_value = getattr(self, '_moderation_value_cahce', None) if moderation_value is not None and self._moderation_value_cache_for_user_id == user.pk: return moderation_value try: page_moderator = self.pagemoderator_set.get(user=user) except ObjectDoesNotExist: return 0 moderation_value = page_moderator.get_decimal() self._moderation_value_cahce = moderation_value self._moderation_value_cache_for_user_id = user return moderation_value def get_object_queryset(self): """Returns smart queryset depending on object type - draft / public """ qs = self.__class__.objects return self.publisher_is_draft and qs.drafts() or qs.public() def _publisher_can_publish(self): """Is parent of this object already published? """ if self.parent_id: try: return bool(self.parent.publisher_public_id) except AttributeError: raise MpttPublisherCantPublish return True def _publisher_get_public_copy(self): """This is here because of the relation between CMSPlugins - model inheritance. eg. Text.objects.get(pk=1).publisher_public returns instance of CMSPlugin instead of instance of Text, thats why this method must be overriden in CMSPlugin. """ return self.publisher_public def get_next_filtered_sibling(self, **filters): """Very simillar to original mptt method, but adds support for filters. Returns this model instance's next sibling in the tree, or ``None`` if it doesn't have a next sibling. """ opts = self._meta if self.is_root_node(): filters.update({ '%s__isnull' % opts.parent_attr: True, '%s__gt' % opts.tree_id_attr: getattr(self, opts.tree_id_attr), }) else: filters.update({ opts.parent_attr: getattr(self, '%s_id' % opts.parent_attr), '%s__gt' % opts.left_attr: getattr(self, opts.right_attr), }) # publisher stuff filters.update({'publisher_is_draft': self.publisher_is_draft}) # multisite filters.update({'site__id': self.site_id}) sibling = None try: sibling = self._tree_manager.filter(**filters)[0] except IndexError: pass return sibling def get_previous_filtered_sibling(self, **filters): """Very simillar to original mptt method, but adds support for filters. Returns this model instance's previous sibling in the tree, or ``None`` if it doesn't have a previous sibling. """ opts = self._meta if self.is_root_node(): filters.update({ '%s__isnull' % opts.parent_attr: True, '%s__lt' % opts.tree_id_attr: getattr(self, opts.tree_id_attr), }) order_by = '-%s' % opts.tree_id_attr else: filters.update({ opts.parent_attr: getattr(self, '%s_id' % opts.parent_attr), '%s__lt' % opts.right_attr: getattr(self, opts.left_attr), }) order_by = '-%s' % opts.right_attr # publisher stuff filters.update({'publisher_is_draft': self.publisher_is_draft}) # multisite filters.update({'site__id': self.site_id}) sibling = None try: sibling = self._tree_manager.filter( **filters).order_by(order_by)[0] except IndexError: pass return sibling def _publisher_save_public(self, obj): """Mptt specific stuff before the object can be saved, overrides original publisher method. Args: obj - public variant of `self` to be saved. """ prev_sibling = self.get_previous_filtered_sibling( publisher_public__isnull=False) if not self.publisher_public_id: # is there anybody on left side? if prev_sibling: obj.insert_at(prev_sibling.publisher_public, position='right', save=False) else: # it is a first time published object, perform insert_at: parent, public_parent = self.parent, None if parent: public_parent = parent.publisher_public if public_parent: obj.insert_at(public_parent, save=False) else: # check if object was moved / structural tree change prev_public_sibling = self.old_public.get_previous_filtered_sibling( ) if not self.level == self.old_public.level or \ not (self.level > 0 and self.parent.publisher_public == self.old_public.parent) or \ not prev_sibling == prev_public_sibling == None or \ (prev_sibling and prev_sibling.publisher_public_id == prev_public_sibling.id): if prev_sibling: obj.insert_at(prev_sibling.publisher_public, position="right") elif self.parent: # move as a first child to parent target = self.parent.publisher_public obj.insert_at(target, position='first-child') else: # it is a move from the right side or just save next_sibling = self.get_next_filtered_sibling() if next_sibling and next_sibling.publisher_public_id: obj.insert_at(next_sibling.publisher_public, position="left") else: # insert at last public position prev_sibling = self.old_public.get_previous_filtered_sibling() if prev_sibling: obj.insert_at(prev_sibling, position="right") elif self.old_public.parent: # move as a first child to parent target = self.old_public.parent obj.insert_at(target, position='first-child') else: # it is a move from the right side or just save next_sibling = self.old_public.get_next_filtered_sibling() if next_sibling and next_sibling.publisher_public_id: obj.insert_at(next_sibling, position="left") # or none structural change, just save obj.save() return obj def rescan_placeholders(self): """ Rescan and if necessary create placeholders in the current template. """ # inline import to prevent circular imports from cms.utils.plugins import get_placeholders placeholders = get_placeholders(self.get_template()) found = {} for placeholder in self.placeholders.all(): if placeholder.slot in placeholders: found[placeholder.slot] = placeholder for placeholder_name in placeholders: if not placeholder_name in found: placeholder = Placeholder.objects.create(slot=placeholder_name) self.placeholders.add(placeholder) found[placeholder_name] = placeholder
class Page(MPTTModel): """ A simple hierarchical page model """ __metaclass__ = PageMetaClass LIMIT_VISIBILITY_IN_MENU_CHOICES = ( (1, _('for logged in users only')), (2, _('for anonymous users only')), ) PUBLISHER_STATE_DEFAULT = 0 PUBLISHER_STATE_DIRTY = 1 PUBLISHER_STATE_DELETE = 2 # Page was marked published, but some of page parents are not. PUBLISHER_STATE_PENDING = 4 template_choices = [(x, _(y)) for x, y in get_cms_setting('TEMPLATES')] created_by = models.CharField(_("created by"), max_length=70, editable=False) changed_by = models.CharField(_("changed by"), max_length=70, editable=False) parent = models.ForeignKey('self', null=True, blank=True, related_name='children', db_index=True) creation_date = models.DateTimeField(auto_now_add=True) changed_date = models.DateTimeField(auto_now=True) publication_date = models.DateTimeField( _("publication date"), null=True, blank=True, help_text= _('When the page should go live. Status must be "Published" for page to go live.' ), db_index=True) publication_end_date = models.DateTimeField( _("publication end date"), null=True, blank=True, help_text=_('When to expire the page. Leave empty to never expire.'), db_index=True) in_navigation = models.BooleanField(_("in navigation"), default=True, db_index=True) soft_root = models.BooleanField( _("soft root"), db_index=True, default=False, help_text=_("All ancestors will not be displayed in the navigation")) reverse_id = models.CharField( _("id"), max_length=40, db_index=True, blank=True, null=True, help_text= _("An unique identifier that is used with the page_url templatetag for linking to this page" )) navigation_extenders = models.CharField(_("attached menu"), max_length=80, db_index=True, blank=True, null=True) published = models.BooleanField(_("is published"), blank=True) template = models.CharField( _("template"), max_length=100, choices=template_choices, help_text=_('The template used to render the content.')) site = models.ForeignKey( Site, help_text=_('The site the page is accessible at.'), verbose_name=_("site")) login_required = models.BooleanField(_("login required"), default=False) limit_visibility_in_menu = models.SmallIntegerField( _("menu visibility"), default=None, null=True, blank=True, choices=LIMIT_VISIBILITY_IN_MENU_CHOICES, db_index=True, help_text=_("limit when this page is visible in the menu")) level = models.PositiveIntegerField(db_index=True, editable=False) lft = models.PositiveIntegerField(db_index=True, editable=False) rght = models.PositiveIntegerField(db_index=True, editable=False) tree_id = models.PositiveIntegerField(db_index=True, editable=False) # Placeholders (plugins) placeholders = models.ManyToManyField(Placeholder, editable=False) # Publisher fields publisher_is_draft = models.BooleanField(default=True, editable=False, db_index=True) # This is misnamed - the one-to-one relation is populated on both ends publisher_public = models.OneToOneField('self', related_name='publisher_draft', null=True, editable=False) publisher_state = models.SmallIntegerField(default=0, editable=False, db_index=True) # Managers objects = PageManager() permissions = PagePermissionsPermissionManager() class Meta: permissions = ( ('view_page', 'Can view page'), ('publish_page', 'Can publish page'), ) verbose_name = _('page') verbose_name_plural = _('pages') ordering = ('tree_id', 'lft') app_label = 'cms' class PublisherMeta: exclude_fields_append = [ 'id', 'publisher_is_draft', 'publisher_public', 'publisher_state', 'moderator_state', 'placeholders', 'lft', 'rght', 'tree_id', 'parent' ] def __unicode__(self): title = self.get_menu_title(fallback=True) if title is None: title = u"" return unicode(title) def __repr__(self): # This is needed to solve the infinite recursion when # adding new pages. return object.__repr__(self) def is_dirty(self): return self.publisher_state == self.PUBLISHER_STATE_DIRTY def get_absolute_url(self, language=None, fallback=True): if self.is_home(): return reverse('pages-root') path = self.get_path(language, fallback) or self.get_slug( language, fallback) return reverse('pages-details-by-slug', kwargs={"slug": path}) def move_page(self, target, position='first-child'): """ Called from admin interface when page is moved. Should be used on all the places which are changing page position. Used like an interface to mptt, but after move is done page_moved signal is fired. Note for issue #1166: url conflicts are handled by updated check_title_slugs, overwrite_url on the moved page don't need any check as it remains the same regardless of the page position in the tree """ # do not mark the page as dirty after page moves self._publisher_keep_state = True # make sure move_page does not break when using INHERIT template # and moving to a top level position if (position in ('left', 'right') and not target.parent and self.template == constants.TEMPLATE_INHERITANCE_MAGIC): self.template = self.get_template() self.move_to(target, position) # fire signal import cms.signals as cms_signals cms_signals.page_moved.send(sender=Page, instance=self) self.save() # always save the page after move, because of publisher # check the slugs page_utils.check_title_slugs(self) if self.publisher_public_id: # Ensure we have up to date mptt properties public_page = Page.objects.get(pk=self.publisher_public_id) # Ensure that the page is in the right position and save it public_page = self._publisher_save_public(public_page) cms_signals.page_moved.send(sender=Page, instance=public_page) public_page.save() page_utils.check_title_slugs(public_page) def _copy_titles(self, target): """ Copy all the titles to a new page (which must have a pk). :param target: The page where the new titles should be stored """ old_titles = dict(target.title_set.values_list('language', 'pk')) for title in self.title_set.all(): # If an old title exists, overwrite. Otherwise create new title.pk = old_titles.pop(title.language, None) title.page = target title.save() if old_titles: from titlemodels import Title Title.objects.filter(id__in=old_titles.values()).delete() def _copy_contents(self, target): """ Copy all the plugins to a new page. :param target: The page where the new content should be stored """ # TODO: Make this into a "graceful" copy instead of deleting and overwriting # copy the placeholders (and plugins on those placeholders!) CMSPlugin.objects.filter(placeholder__page=target).delete() for ph in self.placeholders.all(): plugins = ph.get_plugins_list() try: ph = target.placeholders.get(slot=ph.slot) except Placeholder.DoesNotExist: ph.pk = None # make a new instance ph.save() target.placeholders.add(ph) # update the page copy if plugins: copy_plugins_to(plugins, ph) def _copy_attributes(self, target): """ Copy all page data to the target. This excludes parent and other values that are specific to an exact instance. :param target: The Page to copy the attributes to """ target.publication_date = self.publication_date target.publication_end_date = self.publication_end_date target.in_navigation = self.in_navigation target.login_required = self.login_required target.limit_visibility_in_menu = self.limit_visibility_in_menu target.soft_root = self.soft_root target.reverse_id = self.reverse_id target.navigation_extenders = self.navigation_extenders target.template = self.template target.site_id = self.site_id def copy_page(self, target, site, position='first-child', copy_permissions=True): """ Copy a page [ and all its descendants to a new location ] Doesn't checks for add page permissions anymore, this is done in PageAdmin. Note: public_copy was added in order to enable the creation of a copy for creating the public page during the publish operation as it sets the publisher_is_draft=False. Note for issue #1166: when copying pages there is no need to check for conflicting URLs as pages are copied unpublished. """ from cms.utils.moderator import update_moderation_message page_copy = None pages = [self] + list(self.get_descendants().order_by('-rght')) site_reverse_ids = Page.objects.filter( site=site, reverse_id__isnull=False).values_list('reverse_id', flat=True) if target: target.old_pk = -1 if position == "first-child": tree = [target] elif target.parent_id: tree = [target.parent] else: tree = [] else: tree = [] if tree: tree[0].old_pk = tree[0].pk first = True # loop over all affected pages (self is included in descendants) for page in pages: titles = list(page.title_set.all()) # get all current placeholders (->plugins) placeholders = list(page.placeholders.all()) origin_id = page.id # create a copy of this page by setting pk = None (=new instance) page.old_pk = page.pk page.pk = None page.level = None page.rght = None page.lft = None page.tree_id = None page.published = False page.publisher_public_id = None # only set reverse_id on standard copy if page.reverse_id in site_reverse_ids: page.reverse_id = None if first: first = False if tree: page.parent = tree[0] else: page.parent = None page.insert_at(target, position) else: count = 1 found = False for prnt in tree: if prnt.old_pk == page.parent_id: page.parent = prnt tree = tree[0:count] found = True break count += 1 if not found: page.parent = None tree.append(page) page.site = site page.save() # copy permissions if necessary if get_cms_setting('PERMISSION') and copy_permissions: from cms.models.permissionmodels import PagePermission for permission in PagePermission.objects.filter( page__id=origin_id): permission.pk = None permission.page = page permission.save() update_moderation_message(page, unicode(_('Page was copied.'))) # copy titles of this page for title in titles: title.pk = None # setting pk = None creates a new instance title.page = page # create slug-copy for standard copy title.slug = page_utils.get_available_slug(title) title.save() # copy the placeholders (and plugins on those placeholders!) for ph in placeholders: plugins = ph.get_plugins_list() try: ph = page.placeholders.get(slot=ph.slot) except Placeholder.DoesNotExist: ph.pk = None # make a new instance ph.save() page.placeholders.add(ph) # update the page copy page_copy = page if plugins: copy_plugins_to(plugins, ph) # invalidate the menu for this site menu_pool.clear(site_id=site.pk) return page_copy # return the page_copy or None 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.email 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 save_base(self, *args, **kwargs): """Overridden save_base. If an instance is draft, and was changed, mark it as dirty. Dirty flag is used for changed nodes identification when publish method takes place. After current changes are published, state is set back to PUBLISHER_STATE_DEFAULT (in publish method). """ keep_state = getattr(self, '_publisher_keep_state', None) if self.publisher_is_draft and not keep_state: self.publisher_state = self.PUBLISHER_STATE_DIRTY if keep_state: delattr(self, '_publisher_keep_state') ret = super(Page, self).save_base(*args, **kwargs) return ret def publish(self): """Overrides Publisher method, because there may be some descendants, which are waiting for parent to publish, so publish them if possible. :returns: True if page was successfully published. """ # Publish can only be called on draft pages if not self.publisher_is_draft: raise PublicIsUnmodifiable( 'The public instance cannot be published. Use draft.') # publish, but only if all parents are published!! published = None if not self.pk: self.save() if not self.parent_id: self.clear_home_pk_cache() if self._publisher_can_publish(): if self.publisher_public_id: # Ensure we have up to date mptt properties public_page = Page.objects.get(pk=self.publisher_public_id) else: public_page = Page(created_by=self.created_by) self._copy_attributes(public_page) # we need to set relate this new public copy to its draft page (self) public_page.publisher_public = self public_page.publisher_is_draft = False # Ensure that the page is in the right position and save it public_page = self._publisher_save_public(public_page) public_page.published = (public_page.parent_id is None or public_page.parent.published) public_page.save() # The target page now has a pk, so can be used as a target self._copy_titles(public_page) self._copy_contents(public_page) # invalidate the menu for this site menu_pool.clear(site_id=self.site_id) # taken from Publisher - copy_page needs to call self._publisher_save_public(copy) for mptt insertion # insert_at() was maybe calling _create_tree_space() method, in this # case may tree_id change, so we must update tree_id from db first # before save if getattr(self, 'tree_id', None): me = self._default_manager.get(pk=self.pk) self.tree_id = me.tree_id self.publisher_public = public_page published = True else: # Nothing left to do pass if self.publisher_public and self.publisher_public.published: self.publisher_state = Page.PUBLISHER_STATE_DEFAULT else: self.publisher_state = Page.PUBLISHER_STATE_PENDING self.published = True self._publisher_keep_state = True self.save() # If we are publishing, this page might have become a "home" which # would change the path if self.is_home(): for title in self.title_set.all(): if title.path != '': title.save() # clean moderation log self.pagemoderatorstate_set.all().delete() if not published: # was not published, escape return # Check if there are some children which are waiting for parents to # become published. publish_set = self.get_descendants().filter( published=True).select_related('publisher_public') for page in publish_set: if page.publisher_public: if page.publisher_public.parent.published: if not page.publisher_public.published: page.publisher_public.published = True page.publisher_public.save() if page.publisher_state == Page.PUBLISHER_STATE_PENDING: page.publisher_state = Page.PUBLISHER_STATE_DEFAULT page._publisher_keep_state = True page.save() elif page.publisher_state == Page.PUBLISHER_STATE_PENDING: page.publish() # fire signal after publishing is done import cms.signals as cms_signals cms_signals.post_publish.send(sender=Page, instance=self) return published def unpublish(self): """ Removes this page from the public site :returns: True if this page was successfully unpublished """ # Publish can only be called on draft pages if not self.publisher_is_draft: raise PublicIsUnmodifiable( 'The public instance cannot be unpublished. Use draft.') # First, make sure we are in the correct state self.published = False self.save() public_page = self.get_public_object() if public_page: public_page.published = False public_page.save() # Go through all children of our public instance descendants = public_page.get_descendants() for child in descendants: child.published = False child.save() draft = child.publisher_public if (draft and draft.published and draft.publisher_state == Page.PUBLISHER_STATE_DEFAULT): draft.publisher_state = Page.PUBLISHER_STATE_PENDING draft._publisher_keep_state = True draft.save() return True def revert(self): """Revert the draft version to the same state as the public version """ # Revert can only be called on draft pages if not self.publisher_is_draft: raise PublicIsUnmodifiable( 'The public instance cannot be reverted. Use draft.') if not self.publisher_public: # TODO: Issue an error return public = self.publisher_public public._copy_titles(self) if self.parent != (self.publisher_public.parent_id and self.publisher_public.parent.publisher_draft): # We don't send the signals here self.move_to(public.parent.publisher_draft) public._copy_contents(self) public._copy_attributes(self) self.published = True self.publisher_state = self.PUBLISHER_STATE_DEFAULT self._publisher_keep_state = True self.save() # clean moderation log self.pagemoderatorstate_set.all().delete() def delete(self): """Mark public instance for deletion and delete draft. """ placeholders = self.placeholders.all() for ph in placeholders: plugin = CMSPlugin.objects.filter(placeholder=ph) plugin.delete() ph.delete() if self.publisher_public_id: # mark the public instance for deletion self.publisher_public.publisher_state = self.PUBLISHER_STATE_DELETE self.publisher_public.save() super(Page, self).delete() def delete_with_public(self): """ Assuming this page and all its descendants have been marked for deletion, recursively deletes the entire set of pages including the public instance. """ descendants = list(self.get_descendants().order_by('level')) descendants.reverse() descendants.append(self) # Get all pages that are children of any public page that would be deleted public_children = Page.objects.public().filter( parent__publisher_public__in=descendants) public_pages = Page.objects.public().filter( publisher_public__in=descendants) if set(public_children).difference(public_pages): raise PermissionDenied('There are pages that would be orphaned. ' 'Publish their move requests first.') for page in descendants: placeholders = list(page.placeholders.all()) if page.publisher_public_id: placeholders = placeholders + list( page.publisher_public.placeholders.all()) plugins = CMSPlugin.objects.filter(placeholder__in=placeholders) plugins.delete() for ph in placeholders: ph.delete() if page.publisher_public_id: page.publisher_public.delete() super(Page, page).delete() def get_draft_object(self): if not self.publisher_is_draft: return self.publisher_draft return self def get_public_object(self): if not self.publisher_is_draft: return self return self.publisher_public def get_languages(self): """ get the list of all existing languages for this page """ from cms.models.titlemodels import Title if not hasattr(self, "all_languages"): self.all_languages = Title.objects.filter(page=self).values_list( "language", flat=True).distinct() self.all_languages = list(self.all_languages) self.all_languages.sort() self.all_languages = map(str, self.all_languages) return self.all_languages def get_cached_ancestors(self, ascending=True): if ascending: if not hasattr(self, "ancestors_ascending"): self.ancestors_ascending = list(self.get_ancestors(ascending)) return self.ancestors_ascending else: if not hasattr(self, "ancestors_descending"): self.ancestors_descending = list(self.get_ancestors(ascending)) return self.ancestors_descending # ## Title object access def get_title_obj(self, language=None, fallback=True, version_id=None, force_reload=False): """Helper function for accessing wanted / current title. If wanted title doesn't exists, EmptyTitle instance will be returned. If fallback=False is used, titlemodels.Title.DoesNotExist will be raised when a language does not exist. """ language = self._get_title_cache(language, fallback, version_id, force_reload) if language in self.title_cache: return self.title_cache[language] from cms.models.titlemodels import EmptyTitle return EmptyTitle() def get_title_obj_attribute(self, attrname, language=None, fallback=True, version_id=None, force_reload=False): """Helper function for getting attribute or None from wanted/current title. """ try: attribute = getattr( self.get_title_obj(language, fallback, version_id, force_reload), attrname) return attribute except AttributeError: return None def get_path(self, language=None, fallback=True, version_id=None, force_reload=False): """ get the path of the page depending on the given language """ return self.get_title_obj_attribute("path", language, fallback, version_id, force_reload) def get_slug(self, language=None, fallback=True, version_id=None, force_reload=False): """ get the slug of the page depending on the given language """ return self.get_title_obj_attribute("slug", language, fallback, version_id, force_reload) def get_title(self, language=None, fallback=True, version_id=None, force_reload=False): """ get the title of the page depending on the given language """ return self.get_title_obj_attribute("title", language, fallback, version_id, force_reload) def get_menu_title(self, language=None, fallback=True, version_id=None, force_reload=False): """ get the menu title of the page depending on the given language """ menu_title = self.get_title_obj_attribute("menu_title", language, fallback, version_id, force_reload) if not menu_title: return self.get_title(language, True, version_id, force_reload) return menu_title def get_page_title(self, language=None, fallback=True, version_id=None, force_reload=False): """ get the page title of the page depending on the given language """ page_title = self.get_title_obj_attribute("page_title", language, fallback, version_id, force_reload) if not page_title: return self.get_title(language, True, version_id, force_reload) return page_title def get_meta_description(self, language=None, fallback=True, version_id=None, force_reload=False): """ get content for the description meta tag for the page depending on the given language """ return self.get_title_obj_attribute("meta_description", language, fallback, version_id, force_reload) def get_meta_keywords(self, language=None, fallback=True, version_id=None, force_reload=False): """ get content for the keywords meta tag for the page depending on the given language """ return self.get_title_obj_attribute("meta_keywords", language, fallback, version_id, force_reload) def get_application_urls(self, language=None, fallback=True, version_id=None, force_reload=False): """ get application urls conf for application hook """ return self.get_title_obj_attribute("application_urls", language, fallback, version_id, force_reload) def get_redirect(self, language=None, fallback=True, version_id=None, force_reload=False): """ get redirect """ return self.get_title_obj_attribute("redirect", language, fallback, version_id, force_reload) def _get_title_cache(self, language, fallback, version_id, force_reload): if not language: language = get_language() load = False if not hasattr(self, "title_cache") or force_reload: load = True self.title_cache = {} elif not language in self.title_cache: if fallback: fallback_langs = i18n.get_fallback_languages(language) for lang in fallback_langs: if lang in self.title_cache: return lang load = True if load: from cms.models.titlemodels import Title if version_id: from reversion.models import Version version = get_object_or_404(Version, pk=version_id) revs = [ related_version.object_version for related_version in version.revision.version_set.all() ] for rev in revs: obj = rev.object if obj.__class__ == Title: self.title_cache[obj.language] = obj else: title = Title.objects.get_title(self, language, language_fallback=fallback) if title: self.title_cache[title.language] = title language = title.language return language def get_template(self): """ get the template of this page if defined or if closer parent if defined or DEFAULT_PAGE_TEMPLATE otherwise """ if hasattr(self, '_template_cache'): return self._template_cache template = None if self.template: if self.template != constants.TEMPLATE_INHERITANCE_MAGIC: template = self.template else: try: template = self.get_ancestors(ascending=True).exclude( template=constants.TEMPLATE_INHERITANCE_MAGIC ).values_list('template', flat=True)[0] except IndexError: pass if not template: template = get_cms_setting('TEMPLATES')[0][0] self._template_cache = template return template def get_template_name(self): """ get the textual name (2nd parameter in get_cms_setting('TEMPLATES')) of the template of this page or of the nearest ancestor. failing to find that, return the name of the default template. """ template = self.get_template() for t in get_cms_setting('TEMPLATES'): if t[0] == template: return t[1] return _("default") def has_view_permission(self, request): from cms.models.permissionmodels import PagePermission, GlobalPagePermission from cms.utils.plugins import current_site if not self.publisher_is_draft: return self.publisher_draft.has_view_permission(request) # does any restriction exist? # inherited and direct is_restricted = PagePermission.objects.for_page(page=self).filter( can_view=True).exists() if request.user.is_authenticated(): site = current_site(request) global_perms_q = Q(can_view=True) & Q( Q(sites__in=[site]) | Q(sites__isnull=True)) global_view_perms = GlobalPagePermission.objects.with_user( request.user).filter(global_perms_q).exists() # a global permission was given to the request's user if global_view_perms: return True elif not is_restricted: if ((get_cms_setting('PUBLIC_FOR') == 'all') or (get_cms_setting('PUBLIC_FOR') == 'staff' and request.user.is_staff)): return True # a restricted page and an authenticated user elif is_restricted: opts = self._meta codename = '%s.view_%s' % (opts.app_label, opts.object_name.lower()) user_perm = request.user.has_perm(codename) generic_perm = self.has_generic_permission(request, "view") return (user_perm or generic_perm) else: #anonymous user if is_restricted or not get_cms_setting('PUBLIC_FOR') == 'all': # anyonymous user, page has restriction and global access is permitted return False else: # anonymous user, no restriction saved in database return True # Authenticated user # Django wide auth perms "can_view" or cms auth perms "can_view" opts = self._meta codename = '%s.view_%s' % (opts.app_label, opts.object_name.lower()) return (request.user.has_perm(codename) or self.has_generic_permission(request, "view")) def has_change_permission(self, request): opts = self._meta if request.user.is_superuser: return True return request.user.has_perm(opts.app_label + '.' + opts.get_change_permission()) and \ self.has_generic_permission(request, "change") def has_delete_permission(self, request): opts = self._meta if request.user.is_superuser: return True return request.user.has_perm(opts.app_label + '.' + opts.get_delete_permission()) and \ self.has_generic_permission(request, "delete") def has_publish_permission(self, request): if request.user.is_superuser: return True opts = self._meta return request.user.has_perm(opts.app_label + '.' + "publish_page") and \ self.has_generic_permission(request, "publish") has_moderate_permission = has_publish_permission def has_advanced_settings_permission(self, request): return self.has_generic_permission(request, "advanced_settings") def has_change_permissions_permission(self, request): """ Has user ability to change permissions for current page? """ return self.has_generic_permission(request, "change_permissions") def has_add_permission(self, request): """ Has user ability to add page under current page? """ return self.has_generic_permission(request, "add") def has_move_page_permission(self, request): """Has user ability to move current page? """ return self.has_generic_permission(request, "move_page") def has_generic_permission(self, request, perm_type): """ Return true if the current user has permission on the page. Return the string 'All' if the user has all rights. """ att_name = "permission_%s_cache" % perm_type if not hasattr(self, "permission_user_cache") or not hasattr(self, att_name) \ or request.user.pk != self.permission_user_cache.pk: from cms.utils.permissions import has_generic_permission self.permission_user_cache = request.user setattr( self, att_name, has_generic_permission(self.id, request.user, perm_type, self.site_id)) if getattr(self, att_name): self.permission_edit_cache = True return getattr(self, att_name) def is_home(self): if self.parent_id: return False else: try: return self.home_pk_cache == self.pk except NoHomeFound: pass return False def get_home_pk_cache(self): attr = "%s_home_pk_cache_%s" % (self.publisher_is_draft and "draft" or "public", self.site_id) if getattr(self, attr, None) is None: setattr(self, attr, self.get_object_queryset().get_home(self.site).pk) return getattr(self, attr) def set_home_pk_cache(self, value): attr = "%s_home_pk_cache_%s" % (self.publisher_is_draft and "draft" or "public", self.site_id) setattr(self, attr, value) home_pk_cache = property(get_home_pk_cache, set_home_pk_cache) def clear_home_pk_cache(self): self.home_pk_cache = None def get_media_path(self, filename): """ Returns path (relative to MEDIA_ROOT/MEDIA_URL) to directory for storing page-scope files. This allows multiple pages to contain files with identical names without namespace issues. Plugins such as Picture can use this method to initialise the 'upload_to' parameter for File-based fields. For example: image = models.ImageField(_("image"), upload_to=CMSPlugin.get_media_path) where CMSPlugin.get_media_path calls self.page.get_media_path This location can be customised using the CMS_PAGE_MEDIA_PATH setting """ return join(get_cms_setting('PAGE_MEDIA_PATH'), "%d" % self.id, filename) def last_page_states(self): """Returns last five page states, if they exist, optimized, calls sql query only if some states available """ result = getattr(self, '_moderator_state_cache', None) if result is None: result = list( self.pagemoderatorstate_set.all().order_by('created')) self._moderator_state_cache = result return result[:5] def delete_requested(self): """ Checks whether there are any delete requests for this page. Uses the same cache as last_page_states to minimize DB requests """ from cms.models import PageModeratorState result = getattr(self, '_moderator_state_cache', None) if result is None: return self.pagemoderatorstate_set.get_delete_actions().exists() for state in result: if state.action == PageModeratorState.ACTION_DELETE: return True return False def is_public_published(self): """Returns true if public model is published. """ if hasattr(self, '_public_published_cache'): # if it was cached in change list, return cached value return self._public_published_cache # If we have a public version it will be published as well. # If it isn't published, it should be deleted. return self.published and self.publisher_public_id and self.publisher_public.published def reload(self): """ Reload a page from the database """ return Page.objects.get(pk=self.pk) def get_object_queryset(self): """Returns smart queryset depending on object type - draft / public """ qs = self.__class__.objects return self.publisher_is_draft and qs.drafts() or qs.public( ).published() def _publisher_can_publish(self): """Is parent of this object already published? """ if self.parent_id: try: return bool(self.parent.publisher_public_id) except AttributeError: raise MpttPublisherCantPublish return True def get_next_filtered_sibling(self, **filters): """Very similar to original mptt method, but adds support for filters. Returns this model instance's next sibling in the tree, or ``None`` if it doesn't have a next sibling. """ opts = self._mptt_meta if self.is_root_node(): filters.update({ '%s__isnull' % opts.parent_attr: True, '%s__gt' % opts.tree_id_attr: getattr(self, opts.tree_id_attr), }) else: filters.update({ opts.parent_attr: getattr(self, '%s_id' % opts.parent_attr), '%s__gt' % opts.left_attr: getattr(self, opts.right_attr), }) # publisher stuff filters.update({'publisher_is_draft': self.publisher_is_draft}) # multisite filters.update({'site__id': self.site_id}) sibling = None try: sibling = self._tree_manager.filter(**filters)[0] except IndexError: pass return sibling def get_previous_filtered_sibling(self, **filters): """Very similar to original mptt method, but adds support for filters. Returns this model instance's previous sibling in the tree, or ``None`` if it doesn't have a previous sibling. """ opts = self._mptt_meta if self.is_root_node(): filters.update({ '%s__isnull' % opts.parent_attr: True, '%s__lt' % opts.tree_id_attr: getattr(self, opts.tree_id_attr), }) order_by = '-%s' % opts.tree_id_attr else: filters.update({ opts.parent_attr: getattr(self, '%s_id' % opts.parent_attr), '%s__lt' % opts.right_attr: getattr(self, opts.left_attr), }) order_by = '-%s' % opts.right_attr # publisher stuff filters.update({'publisher_is_draft': self.publisher_is_draft}) # multisite filters.update({'site__id': self.site_id}) sibling = None try: sibling = self._tree_manager.filter( **filters).order_by(order_by)[0] except IndexError: pass return sibling def _publisher_save_public(self, obj): """Mptt specific stuff before the object can be saved, overrides original publisher method. Args: obj - public variant of `self` to be saved. """ public_parent = self.parent.publisher_public if self.parent_id else None filters = dict(publisher_public__isnull=False) if public_parent: filters['publisher_public__parent__in'] = [public_parent] else: filters['publisher_public__parent__isnull'] = True prev_sibling = self.get_previous_filtered_sibling(**filters) public_prev_sib = prev_sibling.publisher_public if prev_sibling else None if not self.publisher_public_id: # first time published # is there anybody on left side? if public_prev_sib: obj.insert_at(public_prev_sib, position='right', save=False) else: if public_parent: obj.insert_at(public_parent, position='first-child', save=False) else: # check if object was moved / structural tree change prev_public_sibling = obj.get_previous_filtered_sibling() if self.level != obj.level or \ public_parent != obj.parent or \ public_prev_sib != prev_public_sibling: if public_prev_sib: obj.move_to(public_prev_sib, position="right") elif public_parent: # move as a first child to parent obj.move_to(public_parent, position='first-child') else: # it is a move from the right side or just save next_sibling = self.get_next_filtered_sibling(**filters) if next_sibling and next_sibling.publisher_public_id: obj.move_to(next_sibling.publisher_public, position="left") return obj def rescan_placeholders(self): """ Rescan and if necessary create placeholders in the current template. """ # inline import to prevent circular imports from cms.utils.plugins import get_placeholders placeholders = get_placeholders(self.get_template()) found = {} for placeholder in self.placeholders.all(): if placeholder.slot in placeholders: found[placeholder.slot] = placeholder for placeholder_name in placeholders: if not placeholder_name in found: placeholder = Placeholder.objects.create(slot=placeholder_name) self.placeholders.add(placeholder) found[placeholder_name] = placeholder
class Page(MpttPublisher): """ A simple hierarchical page model """ MODERATOR_CHANGED = 0 MODERATOR_NEED_APPROVEMENT = 1 MODERATOR_NEED_DELETE_APPROVEMENT = 2 MODERATOR_APPROVED = 10 # special case - page was approved, but some of page parents are not approved yet MODERATOR_APPROVED_WAITING_FOR_PARENTS = 11 moderator_state_choices = ( (MODERATOR_CHANGED, _('changed')), (MODERATOR_NEED_APPROVEMENT, _('req. app.')), (MODERATOR_NEED_DELETE_APPROVEMENT, _('delete')), (MODERATOR_APPROVED, _('approved')), (MODERATOR_APPROVED_WAITING_FOR_PARENTS, _('app. par.')), ) LIMIT_VISIBILITY_IN_MENU_CHOICES = ( (1,_('for logged in users only')), (2,_('for anonymous users only')), ) template_choices = [(x, _(y)) for x,y in settings.CMS_TEMPLATES] created_by = models.CharField(_("created by"), max_length=70, editable=False) changed_by = models.CharField(_("changed by"), max_length=70, editable=False) parent = models.ForeignKey('self', null=True, blank=True, related_name='children', db_index=True) creation_date = models.DateTimeField(editable=False, default=datetime.now) publication_date = models.DateTimeField(_("publication date"), null=True, blank=True, help_text=_('When the page should go live. Status must be "Published" for page to go live.'), db_index=True) publication_end_date = models.DateTimeField(_("publication end date"), null=True, blank=True, help_text=_('When to expire the page. Leave empty to never expire.'), db_index=True) in_navigation = models.BooleanField(_("in navigation"), default=True, db_index=True) soft_root = models.BooleanField(_("soft root"), db_index=True, default=False, help_text=_("All ancestors will not be displayed in the navigation")) reverse_id = models.CharField(_("id"), max_length=40, db_index=True, blank=True, null=True, help_text=_("An unique identifier that is used with the page_url templatetag for linking to this page")) navigation_extenders = models.CharField(_("attached menu"), max_length=80, db_index=True, blank=True, null=True) published = models.BooleanField(_("is published"), blank=True) template = models.CharField(_("template"), max_length=100, choices=template_choices, help_text=_('The template used to render the content.')) site = models.ForeignKey(Site, help_text=_('The site the page is accessible at.'), verbose_name=_("site")) moderator_state = models.SmallIntegerField(_('moderator state'), choices=moderator_state_choices, default=MODERATOR_NEED_APPROVEMENT, blank=True) level = models.PositiveIntegerField(db_index=True, editable=False) lft = models.PositiveIntegerField(db_index=True, editable=False) rght = models.PositiveIntegerField(db_index=True, editable=False) tree_id = models.PositiveIntegerField(db_index=True, editable=False) login_required = models.BooleanField(_("login required"),default=False) limit_visibility_in_menu = models.SmallIntegerField(_("menu visibility"), default=None, null=True, blank=True, choices=LIMIT_VISIBILITY_IN_MENU_CHOICES, db_index=True, help_text=_("limit when this page is visible in the menu")) # Placeholders (plugins) placeholders = models.ManyToManyField(Placeholder, editable=False) # Managers objects = PageManager() permissions = PagePermissionsPermissionManager() class Meta: verbose_name = _('page') verbose_name_plural = _('pages') ordering = ('site','tree_id', 'lft') app_label = 'cms' class PublisherMeta: exclude_fields_append = ['moderator_state', 'placeholders'] def __unicode__(self): title = self.get_menu_title(fallback=True) if title is None: title = u"" return u'%s' % (title,) def move_page(self, target, position='first-child'): """Called from admin interface when page is moved. Should be used on all the places which are changing page position. Used like an interface to mptt, but after move is done page_moved signal is fired. """ self.move_to(target, position) # fire signal from cms.models.moderatormodels import PageModeratorState self.force_moderation_action = PageModeratorState.ACTION_MOVE import cms.signals as cms_signals cms_signals.page_moved.send(sender=Page, instance=self) #titles get saved before moderation self.save(change_state=True) # always save the page after move, because of publisher # check the slugs check_title_slugs(self) def copy_page(self, target, site, position='first-child', copy_permissions=True, copy_moderation=True, public_copy=False): """ copy a page [ and all its descendants to a new location ] Doesn't checks for add page permissions anymore, this is done in PageAdmin. Note: public_copy was added in order to enable the creation of a copy for creating the public page during the publish operation as it sets the publisher_is_draft=False. """ from cms.utils.moderator import update_moderation_message page_copy = None if public_copy: # create a copy of the draft page - existing code loops through pages so added it to a list pages = [copy.copy(self)] else: pages = [self] + list(self.get_descendants().order_by('-rght')) site_reverse_ids = Page.objects.filter(site=site, reverse_id__isnull=False).values_list('reverse_id', flat=True) if target: target.old_pk = -1 if position == "first-child": tree = [target] elif target.parent_id: tree = [target.parent] else: tree = [] else: tree = [] if tree: tree[0].old_pk = tree[0].pk first = True # loop over all affected pages (self is included in descendants) for page in pages: titles = list(page.title_set.all()) # get all current placeholders (->plugins) placeholders = list(page.placeholders.all()) origin_id = page.id # create a copy of this page by setting pk = None (=new instance) page.old_pk = page.pk page.pk = None page.level = None page.rght = None page.lft = None page.tree_id = None page.published = False page.publisher_status = Page.MODERATOR_CHANGED page.publisher_public_id = None if page.reverse_id in site_reverse_ids: page.reverse_id = None if first: first = False if tree: page.parent = tree[0] else: page.parent = None page.insert_at(target, position) else: count = 1 found = False for prnt in tree: if prnt.old_pk == page.parent_id: page.parent = prnt tree = tree[0:count] found = True break count += 1 if not found: page.parent = None tree.append(page) page.site = site # override default page settings specific for public copy if public_copy: page.published = True page.publisher_is_draft=False page.publisher_status = Page.MODERATOR_APPROVED # we need to set relate this new public copy to its draft page (self) page.publisher_public = self # code taken from Publisher publish() overridden here as we need to save the page # before we are able to use the page object for titles, placeholders etc.. below # the method has been modified to return the object after saving the instance variable page = self._publisher_save_public(page) page_copy = page # create a copy used in the return else: # only need to save the page if it isn't public since it is saved above otherwise page.save() # copy moderation, permissions if necessary if settings.CMS_PERMISSION and copy_permissions: from cms.models.permissionmodels import PagePermission for permission in PagePermission.objects.filter(page__id=origin_id): permission.pk = None permission.page = page permission.save() if settings.CMS_MODERATOR and copy_moderation: from cms.models.moderatormodels import PageModerator for moderator in PageModerator.objects.filter(page__id=origin_id): moderator.pk = None moderator.page = page moderator.save() # update moderation message for standard copy if not public_copy: update_moderation_message(page, unicode(_('Page was copied.'))) # copy titles of this page for title in titles: title.pk = None # setting pk = None creates a new instance title.publisher_public_id = None title.published = False title.page = page # create slug-copy for standard copy if not public_copy: title.slug = get_available_slug(title) title.save() # copy the placeholders (and plugins on those placeholders!) for ph in placeholders: plugins = list(ph.cmsplugin_set.all().order_by('tree_id', '-rght')) try: ph = page.placeholders.get(slot=ph.slot) except Placeholder.DoesNotExist: ph.pk = None # make a new instance ph.save() page.placeholders.add(ph) ptree = [] for p in plugins: p.copy_plugin(ph, p.language, ptree) # invalidate the menu for this site menu_pool.clear(site_id=site.pk) return page_copy # return the page_copy or None 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 """ # 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 = datetime.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 super(Page, 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 and commit and publish_directly: self.publish() @transaction.commit_manually def publish(self): """Overrides Publisher method, because there may be some descendants, which are waiting for parent to publish, so publish them if possible. IMPORTANT: @See utils.moderator.approve_page for publishing permissions Also added @transaction.commit_manually decorator as delete() was removing both draft and public versions Returns: True if page was successfully published. """ # Publish can only be called on moderated and draft pages if not self.publisher_is_draft: return # publish, but only if all parents are published!! published = None try: if not self.pk: self.save() if not self._publisher_can_publish(): raise PublisherCantPublish ######################################################################## # delete the existing public page using transaction block to ensure save() and delete() do not conflict # the draft version was being deleted if I replaced the save() below with a delete() try: old_public = self.get_public_object() old_public.publisher_state = Publisher.PUBLISHER_STATE_DELETE old_public.publisher_public = None # remove the reference to the publisher_draft version of the page so it does not get deleted old_public.save() except: transaction.rollback() else: transaction.commit() # we hook into the modified copy_page routing to do the heavy lifting of copying the draft page to a new public page new_public = self.copy_page(target=None, site=self.site, copy_moderation=False, position=None, copy_permissions=False, public_copy=True) # taken from Publisher - copy_page needs to call self._publisher_save_public(copy) for mptt insertion # insert_at() was maybe calling _create_tree_space() method, in this # case may tree_id change, so we must update tree_id from db first # before save if getattr(self, 'tree_id', None): me = self._default_manager.get(pk=self.pk) self.tree_id = me.tree_id self.published = True self.publisher_public = new_public self.moderator_state = Page.MODERATOR_APPROVED self.publisher_state = Publisher.PUBLISHER_STATE_DEFAULT self._publisher_keep_state = True published = True except PublisherCantPublish: self.moderator_state = Page.MODERATOR_APPROVED_WAITING_FOR_PARENTS self.save(change_state=False) if not published: # was not published, escape return # clean moderation log self.pagemoderatorstate_set.all().delete() # page was published, check if there are some childs, which are waiting # for publishing (because of the parent) publish_set = self.children.filter(moderator_state = Page.MODERATOR_APPROVED_WAITING_FOR_PARENTS) for page in publish_set: # recursive call to all childrens.... page.moderator_state = Page.MODERATOR_APPROVED page.save(change_state=False) page.publish() # we delete the old public page - this only deletes the public page as we # have removed the old_public.publisher_public=None relationship to the draft page above if old_public: # reparent public child pages before delete so they don't get purged as well for child_page in old_public.children.all(): if child_page.publisher_public: child_page.parent = new_public child_page.save(change_state=False) transaction.commit() old_public.delete() # manually commit the last transaction batch transaction.commit() # fire signal after publishing is done import cms.signals as cms_signals cms_signals.post_publish.send(sender=Page, instance=self) return published def get_draft_object(self): return self def get_public_object(self): return self.publisher_public def get_calculated_status(self): """ get the calculated status of the page based on published_date, published_end_date, and status """ if settings.CMS_SHOW_START_DATE: if self.publication_date > datetime.now(): return False if settings.CMS_SHOW_END_DATE and self.publication_end_date: if self.publication_end_date < datetime.now(): return True return self.published calculated_status = property(get_calculated_status) def get_languages(self): """ get the list of all existing languages for this page """ from cms.models.titlemodels import Title if not hasattr(self, "all_languages"): self.all_languages = Title.objects.filter(page=self).values_list("language", flat=True).distinct() self.all_languages = list(self.all_languages) self.all_languages.sort() return self.all_languages def get_absolute_url(self, language=None, fallback=True): try: if self.is_home(): return reverse('pages-root') except NoHomeFound: pass if settings.CMS_FLAT_URLS: path = self.get_slug(language, fallback) else: path = self.get_path(language, fallback) if hasattr(self, "home_cut_cache") and self.home_cut_cache: if not self.get_title_obj_attribute("has_url_overwrite", language, fallback) and path: path = "/".join(path.split("/")[1:]) else: home_pk = None try: home_pk = self.home_pk_cache except NoHomeFound: pass ancestors = self.get_cached_ancestors(ascending=True) if self.parent_id and ancestors[-1].pk == home_pk and not self.get_title_obj_attribute("has_url_overwrite", language, fallback) and path: path = "/".join(path.split("/")[1:]) if settings.CMS_DBGETTEXT and settings.CMS_DBGETTEXT_SLUGS: path = '/'.join([ugettext(p) for p in path.split('/')]) return urljoin(reverse('pages-root'), path) def get_cached_ancestors(self, ascending=True): if ascending: if not hasattr(self, "ancestors_ascending"): self.ancestors_ascending = list(self.get_ancestors(ascending)) return self.ancestors_ascending else: if not hasattr(self, "ancestors_descending"): self.ancestors_descending = list(self.get_ancestors(ascending)) return self.ancestors_descending def get_title_obj(self, language=None, fallback=True, version_id=None, force_reload=False): """Helper function for accessing wanted / current title. If wanted title doesn't exists, EmptyTitle instance will be returned. """ language = self._get_title_cache(language, fallback, version_id, force_reload) if language in self.title_cache: return self.title_cache[language] from cms.models.titlemodels import EmptyTitle return EmptyTitle() def get_title_obj_attribute(self, attrname, language=None, fallback=True, version_id=None, force_reload=False): """Helper function for getting attribute or None from wanted/current title. """ try: attribute = getattr(self.get_title_obj( language, fallback, version_id, force_reload), attrname) if attribute and settings.CMS_DBGETTEXT: if attrname in ('slug', 'path') and \ not settings.CMS_DBGETTEXT_SLUGS: return attribute return ugettext(attribute) return attribute except AttributeError: return None def get_path(self, language=None, fallback=True, version_id=None, force_reload=False): """ get the path of the page depending on the given language """ return self.get_title_obj_attribute("path", language, fallback, version_id, force_reload) def get_slug(self, language=None, fallback=True, version_id=None, force_reload=False): """ get the slug of the page depending on the given language """ return self.get_title_obj_attribute("slug", language, fallback, version_id, force_reload) def get_title(self, language=None, fallback=True, version_id=None, force_reload=False): """ get the title of the page depending on the given language """ return self.get_title_obj_attribute("title", language, fallback, version_id, force_reload) def get_menu_title(self, language=None, fallback=True, version_id=None, force_reload=False): """ get the menu title of the page depending on the given language """ menu_title = self.get_title_obj_attribute("menu_title", language, fallback, version_id, force_reload) if not menu_title: return self.get_title(language, True, version_id, force_reload) return menu_title def get_page_title(self, language=None, fallback=True, version_id=None, force_reload=False): """ get the page title of the page depending on the given language """ page_title = self.get_title_obj_attribute("page_title", language, fallback, version_id, force_reload) if not page_title: return self.get_title(language, True, version_id, force_reload) return page_title def get_meta_description(self, language=None, fallback=True, version_id=None, force_reload=False): """ get content for the description meta tag for the page depending on the given language """ return self.get_title_obj_attribute("meta_description", language, fallback, version_id, force_reload) def get_meta_keywords(self, language=None, fallback=True, version_id=None, force_reload=False): """ get content for the keywords meta tag for the page depending on the given language """ return self.get_title_obj_attribute("meta_keywords", language, fallback, version_id, force_reload) def get_application_urls(self, language=None, fallback=True, version_id=None, force_reload=False): """ get application urls conf for application hook """ return self.get_title_obj_attribute("application_urls", language, fallback, version_id, force_reload) def get_redirect(self, language=None, fallback=True, version_id=None, force_reload=False): """ get redirect """ return self.get_title_obj_attribute("redirect", language, fallback, version_id, force_reload) def _get_title_cache(self, language, fallback, version_id, force_reload): if not language: language = get_language() load = False if not hasattr(self, "title_cache") or force_reload: load = True self.title_cache = {} elif not language in self.title_cache: if fallback: fallback_langs = get_fallback_languages(language) for lang in fallback_langs: if lang in self.title_cache: return lang load = True if load: from cms.models.titlemodels import Title if version_id: from reversion.models import Version version = get_object_or_404(Version, pk=version_id) revs = [related_version.object_version for related_version in version.revision.version_set.all()] for rev in revs: obj = rev.object if obj.__class__ == Title: self.title_cache[obj.language] = obj else: title = Title.objects.get_title(self, language, language_fallback=fallback) if title: self.title_cache[title.language] = title language = title.language return language def get_template(self): """ get the template of this page if defined or if closer parent if defined or DEFAULT_PAGE_TEMPLATE otherwise """ template = None if self.template and len(self.template)>0 and \ self.template != settings.CMS_TEMPLATE_INHERITANCE_MAGIC: template = self.template else: for p in self.get_ancestors(ascending=True): template = p.get_template() break if not template: template = settings.CMS_TEMPLATES[0][0] return template def get_template_name(self): """ get the textual name (2nd parameter in settings.CMS_TEMPLATES) of the template of this page or of the nearest ancestor. failing to find that, return the name of the default template. """ template = self.get_template() for t in settings.CMS_TEMPLATES: if t[0] == template: return t[1] return _("default") def has_change_permission(self, request): opts = self._meta if request.user.is_superuser: return True return request.user.has_perm(opts.app_label + '.' + opts.get_change_permission()) and \ self.has_generic_permission(request, "change") def has_delete_permission(self, request): opts = self._meta if request.user.is_superuser: return True return request.user.has_perm(opts.app_label + '.' + opts.get_delete_permission()) and \ self.has_generic_permission(request, "delete") def has_publish_permission(self, request): return self.has_generic_permission(request, "publish") def has_advanced_settings_permission(self, request): return self.has_generic_permission(request, "advanced_settings") def has_change_permissions_permission(self, request): """Has user ability to change permissions for current page? """ return self.has_generic_permission(request, "change_permissions") def has_add_permission(self, request): """Has user ability to add page under current page? """ return self.has_generic_permission(request, "add") def has_move_page_permission(self, request): """Has user ability to move current page? """ return self.has_generic_permission(request, "move_page") def has_moderate_permission(self, request): """Has user ability to moderate current page? If moderation isn't installed, nobody can moderate. """ if not settings.CMS_MODERATOR: return False return self.has_generic_permission(request, "moderate") def has_generic_permission(self, request, type): """ Return true if the current user has permission on the page. Return the string 'All' if the user has all rights. """ att_name = "permission_%s_cache" % type if not hasattr(self, "permission_user_cache") or not hasattr(self, att_name) \ or request.user.pk != self.permission_user_cache.pk: from cms.utils.permissions import has_generic_permission self.permission_user_cache = request.user setattr(self, att_name, has_generic_permission(self.id, request.user, type, self.site_id)) if getattr(self, att_name): self.permission_edit_cache = True return getattr(self, att_name) def is_home(self): if self.parent_id: return False else: try: return self.home_pk_cache == self.pk except NoHomeFound: pass return False def get_home_pk_cache(self): attr = "%s_home_pk_cache_%s" % (self.publisher_is_draft and "draft" or "public", self.site.pk) if not hasattr(self, attr): setattr(self, attr, self.get_object_queryset().get_home(self.site).pk) return getattr(self, attr) def set_home_pk_cache(self, value): attr = "%s_home_pk_cache_%s" % (self.publisher_is_draft and "draft" or "public", self.site.pk) setattr(self, attr, value) home_pk_cache = property(get_home_pk_cache, set_home_pk_cache) def get_media_path(self, filename): """ Returns path (relative to MEDIA_ROOT/MEDIA_URL) to directory for storing page-scope files. This allows multiple pages to contain files with identical names without namespace issues. Plugins such as Picture can use this method to initialise the 'upload_to' parameter for File-based fields. For example: image = models.ImageField(_("image"), upload_to=CMSPlugin.get_media_path) where CMSPlugin.get_media_path calls self.page.get_media_path This location can be customised using the CMS_PAGE_MEDIA_PATH setting """ return join(settings.CMS_PAGE_MEDIA_PATH, "%d" % self.id, filename) def last_page_states(self): """Returns last five page states, if they exist, optimized, calls sql query only if some states available """ # TODO: optimize SQL... 1 query per page if settings.CMS_MODERATOR: has_moderator_state = getattr(self, '_has_moderator_state_chache', None) if has_moderator_state == False: return None return self.pagemoderatorstate_set.all().order_by('created',)[:5] return None def get_moderator_queryset(self): """Returns ordered set of all PageModerator instances, which should moderate this page """ from cms.models.moderatormodels import PageModerator if not settings.CMS_MODERATOR or not self.tree_id: return PageModerator.objects.get_empty_query_set() q = Q(page__tree_id=self.tree_id, page__level__lt=self.level, moderate_descendants=True) | \ Q(page__tree_id=self.tree_id, page__level=self.level - 1, moderate_children=True) | \ Q(page__pk=self.pk, moderate_page=True) return PageModerator.objects.distinct().filter(q).order_by('page__level') def is_under_moderation(self): return bool(self.get_moderator_queryset().count()) def is_approved(self): """Returns true, if page is approved and published, or approved, but parents are missing.. """ return self.moderator_state in (Page.MODERATOR_APPROVED, Page.MODERATOR_APPROVED_WAITING_FOR_PARENTS) def is_public_published(self): """Returns true if public model is published. """ if hasattr(self, 'public_published_cache'): # if it was cached in change list, return cached value return self.public_published_cache # othervise make db lookup if self.publisher_public_id: return self.publisher_public.published #return is_public_published(self) return False def requires_approvement(self): return self.moderator_state in (Page.MODERATOR_NEED_APPROVEMENT, Page.MODERATOR_NEED_DELETE_APPROVEMENT) def get_moderation_value(self, user): """Returns page moderation value for given user, moderation value is sum of moderations. """ moderation_value = getattr(self, '_moderation_value_cahce', None) if moderation_value is not None and self._moderation_value_cache_for_user_id == user.pk: return moderation_value try: page_moderator = self.pagemoderator_set.get(user=user) except ObjectDoesNotExist: return 0 moderation_value = page_moderator.get_decimal() self._moderation_value_cahce = moderation_value self._moderation_value_cache_for_user_id = user return moderation_value