Esempio n. 1
0
    def get_form(self, request, obj=None, **kwargs):
        """Get a :class:`Page <pages.admin.forms.PageForm>` for the
        :class:`Page <pages.models.Page>` and modify its fields depending on
        the request."""
        form = super(PageAdmin, self).get_form(request, obj, **kwargs)

        language = get_language_from_request(request)
        form.base_fields['language'].initial = language
        if obj:
            initial_slug = obj.slug(language=language, fallback=False)
            initial_title = obj.title(language=language, fallback=False)
            form.base_fields['slug'].initial = initial_slug
            form.base_fields['title'].initial = initial_title
            form.base_fields['slug'].label = _('Slug')

        template = get_template_from_request(request, obj)
        page_templates = settings.get_page_templates()
        if len(page_templates) > 0:
            template_choices = list(page_templates)
            template_choices.insert(0, (settings.PAGE_DEFAULT_TEMPLATE,
                    _('Default template')))
            form.base_fields['template'].choices = template_choices
            form.base_fields['template'].initial = force_unicode(template)

        for placeholder in get_placeholders(template):
            name = placeholder.name
            if obj:
                initial = placeholder.get_content(obj, language, name)
            else:
                initial = None
            form.base_fields[name] = placeholder.get_field(obj,
                language, initial=initial)

        return form
Esempio n. 2
0
    def get_form(self, request, obj=None, **kwargs):
        """Get a :class:`Page <pages.admin.forms.PageForm>` for the
        :class:`Page <pages.models.Page>` and modify its fields depending on
        the request."""
        form = super(PageAdmin, self).get_form(request, obj, **kwargs)

        language = get_language_from_request(request)
        form.base_fields['language'].initial = language
        if obj:
            initial_slug = obj.slug(language=language, fallback=False)
            initial_title = obj.title(language=language, fallback=False)
            form.base_fields['slug'].initial = initial_slug
            form.base_fields['title'].initial = initial_title
            form.base_fields['slug'].label = _('Slug')

        template = get_template_from_request(request, obj)
        page_templates = settings.get_page_templates()
        if len(page_templates) > 0:
            template_choices = list(page_templates)
            template_choices.insert(
                0, (settings.PAGE_DEFAULT_TEMPLATE, _('Default template')))
            form.base_fields['template'].choices = template_choices
            form.base_fields['template'].initial = force_unicode(template)

        for placeholder in get_placeholders(template):
            name = placeholder.name
            if obj:
                initial = placeholder.get_content(obj, language, name)
            else:
                initial = None
            form.base_fields[name] = placeholder.get_field(obj,
                                                           language,
                                                           initial=initial)

        return form
Esempio n. 3
0
 def get_template_name(self):
     """
     Get the template name of this page if defined or if a closer
     parent has a defined template or
     :data:`pages.settings.PAGE_DEFAULT_TEMPLATE otherwise.
     """
     template = self.get_template()
     page_templates = settings.get_page_templates()
     for t in page_templates:
         if t[0] == template:
             return t[1]
     return template
Esempio n. 4
0
 def get_template_name(self):
     """
     Get the template name of this page if defined or if a closer
     parent has a defined template or
     :data:`pages.settings.PAGE_DEFAULT_TEMPLATE` otherwise.
     """
     template = self.get_template()
     page_templates = settings.get_page_templates()
     for t in page_templates:
         if t[0] == template:
             return t[1]
     return template
Esempio n. 5
0
def page_templates_loading_check(app_configs, **kwargs):
    """ Check if any page template can't be loaded. """
    errors = []

    for page_template in settings.get_page_templates():
        try:
            loader.get_template(page_template[0])
        except template.TemplateDoesNotExist:
            errors.append(checks.Warning(
                    'Django cannot find template %s' % page_template[0],
                    obj=page_template, id='pages.W001'))

    return errors
Esempio n. 6
0
def get_template_from_request(request, page=None):
    """
    Gets a valid template from different sources or falls back to the
    default template.
    """
    page_templates = settings.get_page_templates()
    if len(page_templates) == 0:
        return settings.DEFAULT_PAGE_TEMPLATE
    template = request.REQUEST.get("template", None)
    if template is not None and (template in dict(page_templates).keys() or template == settings.DEFAULT_PAGE_TEMPLATE):
        return template
    if page is not None:
        return page.get_template()
    return settings.DEFAULT_PAGE_TEMPLATE
Esempio n. 7
0
def page_templates_loading_check(app_configs, **kwargs):
    """ Check if any page template can't be loaded. """
    errors = []

    for page_template in settings.get_page_templates():
        try:
            loader.get_template(page_template[0])
        except template.TemplateDoesNotExist:
            errors.append(
                checks.Warning('Django cannot find template %s' %
                               page_template[0],
                               obj=page_template,
                               id='pages.W001'))

    return errors
Esempio n. 8
0
def get_template_from_request(request, page=None):
    """
    Gets a valid template from different sources or falls back to the
    default template.
    """
    page_templates = settings.get_page_templates()
    if len(page_templates) == 0:
        return settings.PAGE_DEFAULT_TEMPLATE
    template = request.POST.get('template', request.GET.get('template', None))
    if template is not None and \
            (template in list(dict(page_templates).keys()) or
            template == settings.PAGE_DEFAULT_TEMPLATE):
        return template
    if page is not None:
        return page.get_template()
    return settings.PAGE_DEFAULT_TEMPLATE
Esempio n. 9
0
def get_template_from_request(request, page=None):
    """
    Gets a valid template from different sources or falls back to the
    default template.
    """
    page_templates = settings.get_page_templates()
    if len(page_templates) == 0:
        return settings.PAGE_DEFAULT_TEMPLATE
    template = request.POST.get('template', request.GET.get('template', None))
    if template is not None and \
            (template in list(dict(page_templates).keys()) or
            template == settings.PAGE_DEFAULT_TEMPLATE):
        return template
    if page is not None:
        return page.get_template()
    return settings.PAGE_DEFAULT_TEMPLATE
Esempio n. 10
0
    def get_form(self, request, obj=None, **kwargs):
        """Get a :class:`Page <pages.admin.forms.PageForm>` for the
        :class:`Page <pages.models.Page>` and modify its fields depending on
        the request."""

        template = get_template_from_request(request, obj)

        #model = create_page_model(get_placeholders(template))

        form = make_form(self.model, get_placeholders(template))

        # bound the form
        language = get_language_from_request(request)
        form.base_fields['language'].initial = language
        if obj:
            initial_slug = obj.slug(language=language, fallback=False)
            initial_title = obj.title(language=language, fallback=False)
            form.base_fields['slug'].initial = initial_slug
            form.base_fields['title'].initial = initial_title

        template = get_template_from_request(request, obj)
        page_templates = settings.get_page_templates()
        template_choices = list(page_templates)
        # is default template is not in the list add it
        if not [
                tpl for tpl in template_choices
                if tpl[0] == settings.PAGE_DEFAULT_TEMPLATE
        ]:
            template_choices.insert(
                0, (settings.PAGE_DEFAULT_TEMPLATE, _('Default template')))
        form.base_fields['template'].choices = template_choices
        form.base_fields['template'].initial = force_text(template)

        for placeholder in get_placeholders(template):
            ctype = placeholder.ctype
            if obj:
                initial = placeholder.get_content(obj,
                                                  language,
                                                  lang_fallback=False)
            else:
                initial = None
            form.base_fields[ctype] = placeholder.get_field(obj,
                                                            language,
                                                            initial=initial)

        return form
Esempio n. 11
0
    def get_form(self, request, obj=None, **kwargs):
        """Get a :class:`Page <pages.admin.forms.PageForm>` for the
        :class:`Page <pages.models.Page>` and modify its fields depending on
        the request."""

        template = get_template_from_request(request, obj)

        #model = create_page_model(get_placeholders(template))

        form = make_form(self.model, get_placeholders(template))

        # bound the form
        language = get_language_from_request(request)
        form.base_fields['language'].initial = language
        if obj:
            initial_slug = obj.slug(language=language, fallback=False)
            initial_title = obj.title(language=language, fallback=False)
            form.base_fields['slug'].initial = initial_slug
            form.base_fields['title'].initial = initial_title

        template = get_template_from_request(request, obj)
        page_templates = settings.get_page_templates()
        template_choices = list(page_templates)
        # is default template is not in the list add it
        if not [tpl for tpl in template_choices if tpl[0] == settings.PAGE_DEFAULT_TEMPLATE]:
            template_choices.insert(0, (settings.PAGE_DEFAULT_TEMPLATE,
                    _('Default template')))
        form.base_fields['template'].choices = template_choices
        form.base_fields['template'].initial = force_text(template)

        for placeholder in get_placeholders(template):
            ctype = placeholder.ctype
            if obj:
                initial = placeholder.get_content(obj, language, lang_fallback=False)
            else:
                initial = None
            form.base_fields[ctype] = placeholder.get_field(obj,
                language, initial=initial)

        return form
Esempio n. 12
0
class PageAdmin(admin.ModelAdmin):
    """Page Admin class."""

    form = PageForm
    exclude = ['author', 'parent']
    # these mandatory fields are not versioned
    mandatory_placeholders = ('title', 'slug')
    general_fields = [
        'title', 'slug', 'status', 'target', 'position', 'freeze_date'
    ]

    if settings.PAGE_USE_SITE_ID and not settings.PAGE_HIDE_SITES:
        general_fields.append('sites')
    insert_point = general_fields.index('status') + 1

    # Strange django behavior. If not provided, django will try to find
    # 'page' foreign key in all registered models
    inlines = []

    if settings.PAGE_TAGGING:
        general_fields.insert(insert_point, 'tags')

    # Add support for future dating and expiration based on settings.
    if settings.PAGE_SHOW_END_DATE:
        general_fields.insert(insert_point, 'publication_end_date')
    if settings.PAGE_SHOW_START_DATE:
        general_fields.insert(insert_point, 'publication_date')

    from pages.urlconf_registry import registry
    if (len(registry)):
        general_fields.append('delegate_to')
        insert_point = general_fields.index('status') + 1

    normal_fields = ['language']
    page_templates = settings.get_page_templates()
    if len(page_templates) > 0:
        normal_fields.append('template')
    normal_fields.append('redirect_to')
    normal_fields.append('redirect_to_url')
    fieldsets = (
        [
            _('General'), {
                'fields': general_fields,
                'classes': ('module-general', ),
            }
        ],
        (_('Options'), {
            'fields': normal_fields,
            'classes': ('module-options', ),
        }),
    )

    class Media:
        css = {
            'all': [
                join(settings.PAGES_MEDIA_URL, path)
                for path in ('css/rte.css', 'css/pages.css')
            ]
        }
        js = [
            join(settings.PAGES_MEDIA_URL, path) for path in (
                'javascript/jquery.js',
                'javascript/jquery.rte.js',
                'javascript/jquery.query.js',
                'javascript/pages.js',
                'javascript/pages_form.js',
            )
        ]

    def urls(self):
        from django.conf.urls.defaults import patterns, url, include

        # Admin-site-wide views.
        urlpatterns = patterns(
            '',
            url(r'^$', self.list_pages, name='page-index'),
            url(r'^(?P<page_id>[0-9]+)/traduction/(?P<language_id>[-\w]+)/$',
                traduction,
                name='page-traduction'),
            url(r'^(?P<page_id>[0-9]+)/get-content/(?P<content_id>[0-9]+)/$',
                get_content,
                name='page-get-content'),
            url(r'^(?P<page_id>[0-9]+)/modify-content/(?P<content_type>[-\w]+)/(?P<language_id>[-\w]+)/$',
                modify_content,
                name='page-modify-content'),
            url(r'^(?P<page_id>[0-9]+)/delete-content/(?P<language_id>[-\w]+)/$',
                delete_content,
                name='page-delete-content'),
            url(r'^(?P<page_id>[0-9]+)/sub-menu/$',
                sub_menu,
                name='page-sub-menu'),
            url(r'^(?P<page_id>[0-9]+)/move-page/$',
                move_page,
                name='page-move-page'),
            url(r'^(?P<page_id>[0-9]+)/change-status/$',
                change_status,
                name='page-change-status'),
        )
        urlpatterns += super(PageAdmin, self).urls

        return urlpatterns

    urls = property(urls)

    def i18n_javascript(self, request):
        """Displays the i18n JavaScript that the Django admin
        requires.

        This takes into account the ``USE_I18N`` setting. If it's set to False, the
        generated JavaScript will be leaner and faster.
        """
        return javascript_catalog(request, packages='pages')

    def save_model(self, request, page, form, change):
        """Move the page in the tree if necessary and save every
        placeholder :class:`Content <pages.models.Content>`.
        """
        language = form.cleaned_data['language']
        target = form.data.get('target', None)
        position = form.data.get('position', None)
        page.save()

        # if True, we need to move the page
        if target and position:
            try:
                target = self.model.objects.get(pk=target)
            except self.model.DoesNotExist:
                pass
            else:
                target.invalidate()
                page.move_to(target, position)

        for name in self.mandatory_placeholders:
            data = form.cleaned_data[name]
            placeholder = PlaceholderNode(name)
            extra_data = placeholder.get_extra_data(form.data)
            placeholder.save(page,
                             language,
                             data,
                             change,
                             extra_data=extra_data)

        for placeholder in get_placeholders(page.get_template()):
            if (placeholder.name in form.cleaned_data
                    and placeholder.name not in self.mandatory_placeholders):
                data = form.cleaned_data[placeholder.name]
                extra_data = placeholder.get_extra_data(form.data)
                placeholder.save(page,
                                 language,
                                 data,
                                 change,
                                 extra_data=extra_data)

        page.invalidate()

    def get_fieldsets(self, request, obj=None):
        """
        Add fieldsets of placeholders to the list of already
        existing fieldsets.
        """
        general_fields = list(self.general_fields)
        perms = PagePermission(request.user)

        # some ugly business to remove freeze_date
        # from the field list
        general_module = {
            'fields': list(self.general_fields),
            'classes': ('module-general', ),
        }

        default_fieldsets = list(self.fieldsets)
        if not perms.check('freeze'):
            general_module['fields'].remove('freeze_date')
        if not perms.check('publish'):
            general_module['fields'].remove('status')

        default_fieldsets[0][1] = general_module

        placeholder_fieldsets = []
        template = get_template_from_request(request, obj)
        for placeholder in get_placeholders(template):
            if placeholder.name not in self.mandatory_placeholders:
                placeholder_fieldsets.append(placeholder.name)

        additional_fieldsets = []
        additional_fieldsets.append((_('Content'), {
            'fields': placeholder_fieldsets,
            'classes': ('module-content', ),
        }))

        return default_fieldsets + additional_fieldsets

    def save_form(self, request, form, change):
        """Given a ModelForm return an unsaved instance. ``change`` is True if
        the object is being changed, and False if it's being added."""
        instance = super(PageAdmin, self).save_form(request, form, change)
        instance.template = form.cleaned_data['template']
        if not change:
            instance.author = request.user
        return instance

    def get_form(self, request, obj=None, **kwargs):
        """Get a :class:`Page <pages.admin.forms.PageForm>` for the
        :class:`Page <pages.models.Page>` and modify its fields depending on
        the request."""
        form = super(PageAdmin, self).get_form(request, obj, **kwargs)

        language = get_language_from_request(request)
        form.base_fields['language'].initial = language
        if obj:
            initial_slug = obj.slug(language=language, fallback=False)
            initial_title = obj.title(language=language, fallback=False)
            form.base_fields['slug'].initial = initial_slug
            form.base_fields['title'].initial = initial_title
            form.base_fields['slug'].label = _('Slug')

        template = get_template_from_request(request, obj)
        page_templates = settings.get_page_templates()
        if len(page_templates) > 0:
            template_choices = list(page_templates)
            template_choices.insert(
                0, (settings.PAGE_DEFAULT_TEMPLATE, _('Default template')))
            form.base_fields['template'].choices = template_choices
            form.base_fields['template'].initial = force_unicode(template)

        for placeholder in get_placeholders(template):
            name = placeholder.name
            if obj:
                initial = placeholder.get_content(obj, language, name)
            else:
                initial = None
            form.base_fields[name] = placeholder.get_field(obj,
                                                           language,
                                                           initial=initial)

        return form

    def change_view(self, request, object_id, extra_context=None):
        """The ``change`` admin view for the
        :class:`Page <pages.models.Page>`."""
        language = get_language_from_request(request)
        extra_context = {
            'language': language,
            # don't see where it's used
            #'lang': current_lang,
            'page_languages': settings.PAGE_LANGUAGES,
        }
        try:
            int(object_id)
        except ValueError:
            raise Http404('The "%s" part of the location is invalid.' %
                          str(object_id))
        try:
            obj = self.model.objects.get(pk=object_id)
        except self.model.DoesNotExist:
            # Don't raise Http404 just yet, because we haven't checked
            # permissions yet. We don't want an unauthenticated user to be able
            # to determine whether a given object exists.
            obj = None
        else:
            template = get_template_from_request(request, obj)
            extra_context['placeholders'] = get_placeholders(template)
            extra_context['traduction_languages'] = [
                l for l in settings.PAGE_LANGUAGES
                if Content.objects.get_content(obj, l[0], "title")
                and l[0] != language
            ]
        extra_context['page'] = obj
        return super(PageAdmin, self).change_view(request, object_id,
                                                  extra_context)

    def add_view(self, request, form_url='', extra_context=None):
        """The ``add`` admin view for the :class:`Page <pages.models.Page>`."""
        extra_context = {
            'language': get_language_from_request(request),
            'page_languages': settings.PAGE_LANGUAGES,
        }
        template = get_template_from_request(request)
        #extra_context['placeholders'] = get_placeholders(template)
        return super(PageAdmin, self).add_view(request, form_url,
                                               extra_context)

    def has_add_permission(self, request):
        """Return ``True`` if the current user has permission to add a new
        page."""
        lang = get_language_from_request(request)
        return PagePermission(request.user).check('add', lang=lang)

    def has_change_permission(self, request, obj=None):
        """Return ``True`` if the current user has permission
        to change the page."""
        lang = get_language_from_request(request)
        return PagePermission(request.user).check('change',
                                                  page=obj,
                                                  lang=lang,
                                                  method=request.method)

    def has_delete_permission(self, request, obj=None):
        """Return ``True`` if the current user has permission on the page."""
        lang = get_language_from_request(request)
        return PagePermission(request.user).check('change',
                                                  page=obj,
                                                  lang=lang)

    def list_pages(self, request, template_name=None, extra_context=None):
        """List root pages"""
        if not admin.site.has_permission(request):
            return admin.site.login(request)
        language = get_language_from_request(request)

        query = request.POST.get('q', '').strip()

        if query:
            page_ids = list(
                set([
                    c.page.pk
                    for c in Content.objects.filter(body__icontains=query)
                ]))
            pages = Page.objects.filter(pk__in=page_ids)
        else:
            pages = Page.objects.filter(parent__isnull=True)
        if settings.PAGE_HIDE_SITES:
            pages = pages.filter(sites=settings.SITE_ID)

        perms = PagePermission(request.user)
        context = {
            'can_publish': perms.check('publish'),
            'language': language,
            'name': _("page"),
            'pages': pages,
            'opts': self.model._meta,
            'q': query
        }

        context.update(extra_context or {})
        change_list = self.changelist_view(request, context)

        return change_list
Esempio n. 13
0
class PageAdmin(admin.ModelAdmin):
    """Page Admin class."""

    actions = extra_actions_registery

    # these mandatory fields are not versioned
    mandatory_placeholders = ('title', 'slug')
    general_fields = [
        'title', 'slug', 'status', 'target',
        'position', 'freeze_date', 'template', 'language',
        'redirect_to', 'redirect_to_url']

    if settings.PAGE_USE_SITE_ID and not settings.PAGE_HIDE_SITES:
        general_fields.append('sites')
    insert_point = general_fields.index('status') + 1

    # Strange django behavior. If not provided, django will try to find
    # 'page' foreign key in all registered models
    inlines = []

    if settings.PAGE_TAGGING:
        general_fields.insert(insert_point, 'tags')

    # Add support for future dating and expiration based on settings.
    if settings.PAGE_SHOW_END_DATE:
        general_fields.insert(insert_point, 'publication_end_date')
    if settings.PAGE_SHOW_START_DATE:
        general_fields.insert(insert_point, 'publication_date')

    general_fields.append('delegate_to')
    insert_point = general_fields.index('status') + 1

    page_templates = settings.get_page_templates()

    fieldsets = (
        [_('General'), {
            'fields': general_fields,
            'classes': ('module-general',),
        }],
    )

    class Media:
        css = {
            'all': [join(settings.PAGES_STATIC_URL, path) for path in (
                'css/rte.css',
                'css/pages.css'
            )]
        }
        js = [join(settings.PAGES_STATIC_URL, path) for path in (
            'javascript/jquery.js',
            'javascript/jquery.rte.js',
            'javascript/pages.js',
            'javascript/pages_list.js',
            'javascript/pages_form.js',
            'javascript/jquery.query-2.1.7.js',
            'javascript/iframeResizer.min.js',
        )]

    def get_urls(self):
        urls = super(PageAdmin, self).get_urls()
        from django.conf.urls import url

        pages_urls = [
            url(r'^$', self.list_pages, name='page-changelist'),
            url(r'^(?P<page_id>[0-9]+)/traduction/(?P<language_id>[-\w]+)/$',
                views.traduction, name='page-traduction'),
            url(r'^(?P<page_id>[0-9]+)/get-content/(?P<content_id>[0-9]+)/$',
                views.get_content, name='page-get-content'),
            url(r'^(?P<page_id>[0-9]+)/modify-content/(?P<content_type>[-\w]+)/(?P<language_id>[-\w]+)/$',
                views.modify_content, name='page-modify-content'),
            url(r'^(?P<page_id>[0-9]+)/modify-placeholder/$',
                views.modify_placeholder, name='page-modify-placeholder'),
            url(r'^(?P<page_id>[0-9]+)/get-last-content/$',
                views.get_last_content, name='page-get-last-content'),
            url(r'^(?P<page_id>[0-9]+)/delete-content/(?P<language_id>[-\w]+)/$',
                views.delete_content, name='page-delete-content'),
            url(r'^(?P<page_id>[0-9]+)/sub-menu/$',
                views.sub_menu, name='page-sub-menu'),
            url(r'^(?P<page_id>[0-9]+)/move-page/$',
                views.move_page, name='page-move-page'),
            url(r'^(?P<page_id>[0-9]+)/change-status/$',
                views.change_status, name='page-change-status'),
            url(r'^(?P<media_id>[0-9]+)/media-url/$',
                views.get_media_url, name='get-media-url'),
        ]

        return pages_urls + urls

    def save_model(self, request, page, form, change):
        """Move the page in the tree if necessary and save every
        placeholder :class:`Content <pages.models.Content>`.
        """
        language = form.cleaned_data['language']
        target = form.data.get('target', None)
        position = form.data.get('position', None)
        page.save()

        # if True, we need to move the page
        if target and position:
            try:
                target = self.model.objects.get(pk=target)
            except self.model.DoesNotExist:
                pass
            else:
                target.invalidate()
                page.move_to(target, position)

        for name in self.mandatory_placeholders:
            data = form.cleaned_data[name]
            placeholder = PlaceholderNode(name)
            extra_data = placeholder.get_extra_data(form.data)
            placeholder.save(page, language, data, change,
                extra_data=extra_data)

        for placeholder in get_placeholders(page.get_template()):
            if(placeholder.ctype in form.cleaned_data and placeholder.ctype
                    not in self.mandatory_placeholders):
                data = form.cleaned_data[placeholder.ctype]
                extra_data = placeholder.get_extra_data(form.data)
                placeholder.save(page, language, data, change,
                    extra_data=extra_data)

        page.invalidate()

    def get_fieldsets(self, request, obj=None):
        """
        Add fieldsets of placeholders to the list of already
        existing fieldsets.
        """

        # some ugly business to remove freeze_date
        # from the field list
        general_module = {
            'fields': list(self.general_fields),
            'classes': ('module-general',),
        }

        default_fieldsets = list(self.fieldsets)
        if not request.user.has_perm('pages.can_freeze'):
            general_module['fields'].remove('freeze_date')
        if not request.user.has_perm('pages.can_publish'):
            general_module['fields'].remove('status')
        if not len(registry):
            general_module['fields'].remove('delegate_to')

        default_fieldsets[0][1] = general_module

        placeholder_fieldsets = []
        section_placeholder_fieldsets = defaultdict(list)
        template = get_template_from_request(request, obj)
        for placeholder in get_placeholders(template):
            if placeholder.ctype not in self.mandatory_placeholders and not placeholder.section:
                placeholder_fieldsets.append(placeholder.ctype)
            elif placeholder.section:
                section_placeholder_fieldsets[placeholder.section].append(placeholder.ctype)

        additional_fieldsets = []
        for title, fieldset in section_placeholder_fieldsets.items():
            additional_fieldsets.append((_(title), {
                'fields': fieldset,
                'classes': ('module-content collapse grp-collapse grp-closed',),
            }))
        additional_fieldsets.append((_('Content'), {
            'fields': placeholder_fieldsets,
            'classes': ('module-content',),
        }))
        return default_fieldsets + additional_fieldsets

    def save_form(self, request, form, change):
        """Given a ModelForm return an unsaved instance. ``change`` is True if
        the object is being changed, and False if it's being added."""
        instance = super(PageAdmin, self).save_form(request, form, change)
        instance.template = form.cleaned_data['template']
        if not change:
            instance.author = request.user
        return instance

    def get_form(self, request, obj=None, **kwargs):
        """Get a :class:`Page <pages.admin.forms.PageForm>` for the
        :class:`Page <pages.models.Page>` and modify its fields depending on
        the request."""

        template = get_template_from_request(request, obj)

        #model = create_page_model(get_placeholders(template))

        form = make_form(self.model, get_placeholders(template))

        # bound the form
        language = get_language_from_request(request)
        form.base_fields['language'].initial = language
        if obj:
            initial_slug = obj.slug(language=language, fallback=False)
            initial_title = obj.title(language=language, fallback=False)
            form.base_fields['slug'].initial = initial_slug
            form.base_fields['title'].initial = initial_title

        template = get_template_from_request(request, obj)
        page_templates = settings.get_page_templates()
        template_choices = list(page_templates)
        # is default template is not in the list add it
        if not [tpl for tpl in template_choices if tpl[0] == settings.PAGE_DEFAULT_TEMPLATE]:
            template_choices.insert(0, (settings.PAGE_DEFAULT_TEMPLATE,
                    _('Default template')))
        form.base_fields['template'].choices = template_choices
        form.base_fields['template'].initial = force_text(template)

        for placeholder in get_placeholders(template):
            ctype = placeholder.ctype
            if obj:
                initial = placeholder.get_content(obj, language, lang_fallback=False)
            else:
                initial = None
            form.base_fields[ctype] = placeholder.get_field(obj,
                language, initial=initial)

        return form

    def change_view(self, request, object_id, form_url='', extra_context=None):
        """The ``change`` admin view for the
        :class:`Page <pages.models.Page>`."""
        language = get_language_from_request(request)
        extra_context = {
            'language': language,
            'page_languages': settings.PAGE_LANGUAGES,
        }
        try:
            int(object_id)
        except ValueError:
            raise Http404('The "%s" part of the location is invalid.'
                % str(object_id))
        try:
            obj = self.model.objects.get(pk=object_id)
        except self.model.DoesNotExist:
            # Don't raise Http404 just yet, because we haven't checked
            # permissions yet. We don't want an unauthenticated user to be able
            # to determine whether a given object exists.
            obj = None
        else:
            template = get_template_from_request(request, obj)
            extra_context['placeholders'] = get_placeholders(template)
            extra_context['traduction_languages'] = [l for l in
                settings.PAGE_LANGUAGES if Content.objects.get_content(obj,
                                    l[0], "title") and l[0] != language]
        extra_context['page'] = obj

        response = super(PageAdmin, self).change_view(request, object_id,
            form_url=form_url, extra_context=extra_context)
        if request.method == 'POST' and isinstance(response, HttpResponseRedirect):
            if '_continue' in request.POST or '_saveasnew' in request.POST or '_addanother' in request.POST:
                addlanguage = True
            else:
                addlanguage = False
            if addlanguage:
                splitted = list(urllib.parse.urlparse(response.url))
                query = urllib.parse.parse_qs(splitted[4])
                query['language'] = language
                splitted[4] = urllib.parse.urlencode(query)
                response = HttpResponseRedirect(urllib.parse.urlunparse(splitted))
        return response

    def add_view(self, request, form_url='', extra_context=None):
        """The ``add`` admin view for the :class:`Page <pages.models.Page>`."""
        extra_context = {
            'language': get_language_from_request(request),
            'page_languages': settings.PAGE_LANGUAGES,
        }
        return super(PageAdmin, self).add_view(request, form_url,
                                               extra_context)

    def has_add_permission(self, request):
        """Return ``True`` if the current user has permission to add a new
        page."""
        return request.user.has_perm('pages.add_page')

    def has_change_permission(self, request, obj=None):
        """Return ``True`` if the current user has permission
        to change the page."""
        return request.user.has_perm('pages.change_page')

    def has_delete_permission(self, request, obj=None):
        """Return ``True`` if the current user has permission on the page."""
        return request.user.has_perm('pages.delete_page')

    def list_pages(self, request, template_name=None, extra_context=None):
        """List root pages"""
        if not self.admin_site.has_permission(request):
            return self.admin_site.login(request)
        language = get_language_from_request(request)

        query = request.POST.get('q', '').strip()

        if query:
            page_ids = list(set([c.page.pk for c in
                Content.objects.filter(body__icontains=query)]))
            pages = Page.objects.filter(pk__in=page_ids)
        else:
            pages = Page.objects.root()
        if settings.PAGE_HIDE_SITES:
            pages = pages.filter(sites=global_settings.SITE_ID)

        context = {
            'can_publish': request.user.has_perm('pages.can_publish'),
            'can_import': settings.PAGE_IMPORT_ENABLED,
            'lang': language,  # lang is the key used by show_content
            'pages': pages,
            'opts': self.model._meta,
            'q': query
        }

        context.update(extra_context or {})
        change_list = self.changelist_view(request, context)

        return change_list
Esempio n. 14
0
    class PageForm(forms.ModelForm):
        """Form for page creation"""
        def __init__(self, *args, **kwargs):
            super(PageForm, self).__init__(*args, **kwargs)
            for p in placeholders:
                if not self.fields[p.ctype]:
                    self.fields[p.ctype] = forms.TextField()

        target = forms.IntegerField(required=False, widget=forms.HiddenInput)
        position = forms.CharField(required=False, widget=forms.HiddenInput)

        class Meta:
            model = model_
            exclude = ('author', 'last_modification_date', 'parent')

        title = forms.CharField(
            label=_('Title'),
            widget=forms.TextInput(),
        )
        slug = forms.CharField(
            label=_('Slug'),
            widget=forms.TextInput(),
            help_text=
            _('The slug will be used to create the page URL, it must be unique among the other pages of the same level.'
              ))

        language = forms.ChoiceField(label=_('Language'),
                                     choices=settings.PAGE_LANGUAGES,
                                     widget=LanguageChoiceWidget())
        template = forms.ChoiceField(
            required=False,
            label=_('Template'),
            choices=settings.get_page_templates(),
        )
        delegate_to = forms.ChoiceField(
            required=False,
            label=_('Delegate to application'),
            choices=get_choices(),
        )
        freeze_date = forms.DateTimeField(
            required=False,
            label=_('Freeze'),
            help_text=
            _("Don't publish any content after this date. Format is 'Y-m-d H:M:S'"
              )
            # those make tests fail miserably
            #widget=widgets.AdminSplitDateTime()
            #widget=widgets.AdminTimeWidget()
        )

        def clean_slug(self):
            """Handle move action on the pages"""

            slug = slugify(self.cleaned_data['slug'])
            target = self.data.get('target', None)
            position = self.data.get('position', None)

            # this enforce a unique slug for every page
            if settings.PAGE_AUTOMATIC_SLUG_RENAMING:

                def is_slug_safe(slug):
                    content = Content.objects.get_content_slug_by_slug(slug)
                    if content is None:
                        return True
                    if self.instance.id:
                        if content.page.id == self.instance.id:
                            return True
                    else:
                        return False

                return automatic_slug_renaming(slug, is_slug_safe)

            if settings.PAGE_UNIQUE_SLUG_REQUIRED:
                # We can return here as not futher checks
                # are necessary
                return unique_slug_required(self, slug)

            intersects_sites = intersect_sites_method(self)

            if not settings.PAGE_UNIQUE_SLUG_REQUIRED:
                if target and position:
                    target = Page.objects.get(pk=target)
                    if position in ['right', 'left']:
                        slugs = [
                            sibling.slug()
                            for sibling in target.get_siblings()
                            if intersects_sites(sibling)
                        ]
                        slugs.append(target.slug())
                        if slug in slugs:
                            raise forms.ValidationError(
                                error_dict['sibling_position_error'])
                    if position == 'first-child':
                        if slug in [
                                sibling.slug()
                                for sibling in target.get_children()
                                if intersects_sites(sibling)
                        ]:
                            raise forms.ValidationError(
                                error_dict['child_error'])
                else:
                    if self.instance.id:
                        if (slug in [
                                sibling.slug() for sibling in self.instance.
                                get_siblings().exclude(id=self.instance.id)
                                if intersects_sites(sibling)
                        ]):
                            raise forms.ValidationError(
                                error_dict['sibling_error'])
                    else:
                        if slug in [
                                sibling.slug()
                                for sibling in Page.objects.root()
                                if intersects_sites(sibling)
                        ]:
                            raise forms.ValidationError(
                                error_dict['sibling_root_error'])
            return slug
Esempio n. 15
0
def validate_pages_json_data(d, preferred_lang):
    """
    Check if an import of d will succeed, and return errors.

    errors is a list of strings.  The import should proceed only if errors
    is empty.
    """
    from pages.models import Page
    errors = []

    seen_complete_slugs = dict(
        (lang[0], set()) for lang in settings.PAGE_LANGUAGES)

    valid_templates = set(t[0] for t in settings.get_page_templates())
    valid_templates.add(settings.PAGE_DEFAULT_TEMPLATE)

    if d[JSON_PAGE_EXPORT_NAME] != JSON_PAGE_EXPORT_VERSION:
        return [
            _('Unsupported file version: %s') % repr(d[JSON_PAGE_EXPORT_NAME])
        ], []
    pages = d['pages']
    for p in pages:
        # use the complete slug as a way to identify pages in errors
        slug = p['complete_slug'].get(preferred_lang, None)
        seen_parent = False
        for lang, s in list(p['complete_slug'].items()):
            if lang not in seen_complete_slugs:
                continue
            seen_complete_slugs[lang].add(s)

            if '/' not in s:  # root level, no parent req'd
                seen_parent = True
            if not seen_parent:
                parent_slug, ignore = s.rsplit('/', 1)
                if parent_slug in seen_complete_slugs[lang]:
                    seen_parent = True
                else:
                    parent = Page.objects.from_path(parent_slug,
                                                    lang,
                                                    exclude_drafts=False)
                    if parent and parent.get_complete_slug(
                            lang) == parent_slug:
                        # parent not included, but exists on site
                        seen_parent = True
            if not slug:
                slug = s

        if not slug:
            errors.append(
                _("%s has no common language with this site") %
                (list(p['complete_slug'].values())[0], ))
            continue

        if not seen_parent:
            errors.append(
                _("%s did not include its parent page and a matching"
                  " one was not found on this site") % (slug, ))

        if p['template'] not in valid_templates:
            errors.append(
                _("%s uses a template not found on this site: %s") %
                (slug, p['template']))
            continue

        if set(p.ctype for p in get_placeholders(p['template'])
               if p.ctype not in ('title', 'slug')) != set(
                   p['content'].keys()):
            errors.append(
                _("%s template contents are different than our "
                  "template: %s") % (slug, p['template']))
            continue

    return errors
Esempio n. 16
0
class PageForm(forms.ModelForm):
    """Form for page creation"""

    title = forms.CharField(
        label=_('Title'),
        widget=forms.TextInput(),
    )
    slug = forms.CharField(
        label=_('Slug'),
        widget=forms.TextInput(),
        help_text=_('The slug will be used to create the page URL, \
it must be unique among the other pages of the same level.'))
    language = forms.ChoiceField(label=_('Language'),
                                 choices=settings.PAGE_LANGUAGES,
                                 widget=LanguageChoiceWidget())
    template = forms.ChoiceField(
        required=False,
        label=_('Template'),
        choices=settings.get_page_templates(),
    )
    delegate_to = forms.ChoiceField(
        required=False,
        label=_('Delegate to application'),
        choices=get_choices(),
    )
    freeze_date = forms.DateTimeField(
        required=False,
        label=_('Freeze'),
        help_text=_(
            "Don't publish any content after this date. Format is 'Y-m-d H:M:S'"
        )
        # those make tests fail miserably
        #widget=widgets.AdminSplitDateTime()
        #widget=widgets.AdminTimeWidget()
    )

    target = forms.IntegerField(required=False, widget=forms.HiddenInput)
    position = forms.CharField(required=False, widget=forms.HiddenInput)

    class Meta:
        model = Page

    def clean_slug(self):
        """Handle move action on the pages"""

        slug = slugify(self.cleaned_data['slug'])
        target = self.data.get('target', None)
        position = self.data.get('position', None)

        if settings.PAGE_UNIQUE_SLUG_REQUIRED:
            if self.instance.id:
                if Content.objects.exclude(page=self.instance).filter(
                        body=slug, type="slug").count():
                    raise forms.ValidationError(another_page_error)
            elif Content.objects.filter(body=slug, type="slug").count():
                raise forms.ValidationError(another_page_error)

        if settings.PAGE_USE_SITE_ID:
            if settings.PAGE_HIDE_SITES:
                site_ids = [settings.SITE_ID]
            else:
                site_ids = [int(x) for x in self.data.getlist('sites')]

            def intersects_sites(sibling):
                return sibling.sites.filter(id__in=site_ids).count() > 0
        else:

            def intersects_sites(sibling):
                return True

        if not settings.PAGE_UNIQUE_SLUG_REQUIRED:
            if target and position:
                target = Page.objects.get(pk=target)
                if position in ['right', 'left']:
                    slugs = [
                        sibling.slug() for sibling in target.get_siblings()
                        if intersects_sites(sibling)
                    ]
                    slugs.append(target.slug())
                    if slug in slugs:
                        raise forms.ValidationError(sibling_position_error)
                if position == 'first-child':
                    if slug in [
                            sibling.slug()
                            for sibling in target.get_children()
                            if intersects_sites(sibling)
                    ]:
                        raise forms.ValidationError(child_error)
            else:
                if self.instance.id:
                    if (slug in [
                            sibling.slug() for sibling in self.instance.
                            get_siblings().exclude(id=self.instance.id)
                            if intersects_sites(sibling)
                    ]):
                        raise forms.ValidationError(sibling_error)
                else:
                    if slug in [
                            sibling.slug() for sibling in Page.objects.root()
                            if intersects_sites(sibling)
                    ]:
                        raise forms.ValidationError(sibling_root_error)
        return slug
Esempio n. 17
0
    class PageForm(forms.ModelForm):
        """Form for page creation"""

        target = forms.IntegerField(required=False, widget=forms.HiddenInput)
        position = forms.CharField(required=False, widget=forms.HiddenInput)

        class Meta:
            model = model_
            exclude = ('author', 'last_modification_date', 'parent')

        err_dict = {
            'another_page_error':
            _('Another page with this slug already exists'),
            'sibling_position_error':
            _('A sibling with this slug already exists at the targeted position'
              ),
            'child_error':
            _('A child with this slug already exists at the targeted position'
              ),
            'sibling_error':
            _('A sibling with this slug already exists'),
            'sibling_root_error':
            _('A sibling with this slug already exists at the root level'),
        }

        title = forms.CharField(
            label=_('Title'),
            widget=forms.TextInput(),
        )
        slug = forms.CharField(
            label=_('Slug'),
            widget=forms.TextInput(),
            help_text=
            _('The slug will be used to create the page URL, it must be unique among the other pages of the same level.'
              ))

        language = forms.ChoiceField(label=_('Language'),
                                     choices=settings.PAGE_LANGUAGES,
                                     widget=LanguageChoiceWidget())
        template = forms.ChoiceField(
            required=False,
            label=_('Template'),
            choices=settings.get_page_templates(),
        )
        delegate_to = forms.ChoiceField(
            required=False,
            label=_('Delegate to application'),
            choices=get_choices(),
        )
        freeze_date = forms.DateTimeField(
            required=False,
            label=_('Freeze'),
            help_text=
            _("Don't publish any content after this date. Format is 'Y-m-d H:M:S'"
              )
            # those make tests fail miserably
            #widget=widgets.AdminSplitDateTime()
            #widget=widgets.AdminTimeWidget()
        )

        def _clean_page_automatic_slug_renaming(self, slug, is_slug_safe):
            """Helper to add numbers to slugs"""

            if not isinstance(is_slug_safe, collections.Callable):
                raise TypeError('is_slug_safe must be callable')

            if is_slug_safe(slug):
                return slug

            count = 2
            new_slug = slug + "-" + str(count)
            while not is_slug_safe(new_slug):
                count = count + 1
                new_slug = slug + "-" + str(count)
            return new_slug

        def _clean_page_unique_slug_required(self, slug):
            """See if this slug exists already"""

            if hasattr(self, 'instance') and self.instance.id:
                if Content.objects.exclude(page=self.instance).filter(
                        body=slug, type="slug").count():
                    raise forms.ValidationError(
                        self.err_dict['another_page_error'])
            elif Content.objects.filter(body=slug, type="slug").count():
                raise forms.ValidationError(
                    self.err_dict['another_page_error'])
            return slug

        def clean_slug(self):
            """Handle move action on the pages"""

            slug = slugify(self.cleaned_data['slug'])
            target = self.data.get('target', None)
            position = self.data.get('position', None)

            # this enforce a unique slug for every page
            if settings.PAGE_AUTOMATIC_SLUG_RENAMING:

                def is_slug_safe(slug):
                    content = Content.objects.get_content_slug_by_slug(slug)
                    if content is None:
                        return True
                    if self.instance.id:
                        if content.page.id == self.instance.id:
                            return True
                    else:
                        return False

                return self._clean_page_automatic_slug_renaming(
                    slug, is_slug_safe)

            if settings.PAGE_UNIQUE_SLUG_REQUIRED:
                return self._clean_page_unique_slug_required(slug)

            if settings.PAGE_USE_SITE_ID:
                if settings.PAGE_HIDE_SITES:
                    site_ids = [global_settings.SITE_ID]
                else:
                    site_ids = [int(x) for x in self.data.getlist('sites')]

                def intersects_sites(sibling):
                    return sibling.sites.filter(id__in=site_ids).count() > 0
            else:

                def intersects_sites(sibling):
                    return True

            if not settings.PAGE_UNIQUE_SLUG_REQUIRED:
                if target and position:
                    target = Page.objects.get(pk=target)
                    if position in ['right', 'left']:
                        slugs = [
                            sibling.slug()
                            for sibling in target.get_siblings()
                            if intersects_sites(sibling)
                        ]
                        slugs.append(target.slug())
                        if slug in slugs:
                            raise forms.ValidationError(
                                self.err_dict['sibling_position_error'])
                    if position == 'first-child':
                        if slug in [
                                sibling.slug()
                                for sibling in target.get_children()
                                if intersects_sites(sibling)
                        ]:
                            raise forms.ValidationError(
                                self.err_dict['child_error'])
                else:
                    if self.instance.id:
                        if (slug in [
                                sibling.slug() for sibling in self.instance.
                                get_siblings().exclude(id=self.instance.id)
                                if intersects_sites(sibling)
                        ]):
                            raise forms.ValidationError(
                                self.err_dict['sibling_error'])
                    else:
                        if slug in [
                                sibling.slug()
                                for sibling in Page.objects.root()
                                if intersects_sites(sibling)
                        ]:
                            raise forms.ValidationError(
                                self.err_dict['sibling_root_error'])
            return slug
Esempio n. 18
0
def validate_pages_json_data(d, preferred_lang):
    """
    Check if an import of d will succeed, and return errors.

    errors is a list of strings.  The import should proceed only if errors
    is empty.
    """
    from pages.models import Page
    errors = []

    seen_complete_slugs = dict(
        (lang[0], set()) for lang in settings.PAGE_LANGUAGES)

    valid_templates = set(t[0] for t in settings.get_page_templates())
    valid_templates.add(settings.PAGE_DEFAULT_TEMPLATE)

    if d[JSON_PAGE_EXPORT_NAME] != JSON_PAGE_EXPORT_VERSION:
        return [_('Unsupported file version: %s') % repr(
            d[JSON_PAGE_EXPORT_NAME])], []
    pages = d['pages']
    for p in pages:
        # use the complete slug as a way to identify pages in errors
        slug = p['complete_slug'].get(preferred_lang, None)
        seen_parent = False
        for lang, s in list(p['complete_slug'].items()):
            if lang not in seen_complete_slugs:
                continue
            seen_complete_slugs[lang].add(s)

            if '/' not in s: # root level, no parent req'd
                seen_parent = True
            if not seen_parent:
                parent_slug, ignore = s.rsplit('/', 1)
                if parent_slug in seen_complete_slugs[lang]:
                    seen_parent = True
                else:
                    parent = Page.objects.from_path(parent_slug, lang,
                        exclude_drafts=False)
                    if parent and parent.get_complete_slug(lang) == parent_slug:
                        # parent not included, but exists on site
                        seen_parent = True
            if not slug:
                slug = s

        if not slug:
            errors.append(_("%s has no common language with this site")
                % (list(p['complete_slug'].values())[0],))
            continue

        if not seen_parent:
            errors.append(_("%s did not include its parent page and a matching"
                " one was not found on this site") % (slug,))

        if p['template'] not in valid_templates:
            errors.append(_("%s uses a template not found on this site: %s")
                % (slug, p['template']))
            continue

        if set(p.ctype for p in get_placeholders(p['template']) if
                p.ctype not in ('title', 'slug')) != set(p['content'].keys()):
            errors.append(_("%s template contents are different than our "
                "template: %s") % (slug, p['template']))
            continue

    return errors
Esempio n. 19
0
    class PageForm(SlugFormMixin):
        """Form for page creation"""

        err_dict = {
            'another_page_error':
            _('Another page with this slug already exists'),
            'sibling_position_error':
            _('A sibling with this slug already exists at the targeted position'
              ),
            'child_error':
            _('A child with this slug already exists at the targeted position'
              ),
            'sibling_error':
            _('A sibling with this slug already exists'),
            'sibling_root_error':
            _('A sibling with this slug already exists at the root level'),
        }

        language = forms.ChoiceField(label=_('Language'),
                                     choices=settings.PAGE_LANGUAGES,
                                     widget=LanguageChoiceWidget())
        groups = TreeNodeMultipleChoiceField(required=False,
                                             label=_('Page groups'),
                                             queryset=PageGroup.objects.all())
        template = forms.ChoiceField(
            required=False,
            label=_('Template'),
            choices=settings.get_page_templates(),
        )
        delegate_to = forms.ChoiceField(
            required=False,
            label=_('Delegate to application'),
            choices=get_choices(),
        )
        freeze_date = forms.DateTimeField(
            required=False,
            label=_('Freeze'),
            help_text=
            _("Don't publish any content after this date. Format is 'Y-m-d H:M:S'"
              )
            # those make tests fail miserably
            #widget=widgets.AdminSplitDateTime()
            #widget=widgets.AdminTimeWidget()
        )

        target = forms.IntegerField(required=False, widget=forms.HiddenInput)
        position = forms.CharField(required=False, widget=forms.HiddenInput)

        class Meta:
            model = model_
            exclude = ('author', 'last_modification_date', 'parent')

        def clean_slug(self):
            """Handle move action on the pages"""

            slug = slugify(self.cleaned_data['slug'])
            target = self.data.get('target', None)
            position = self.data.get('position', None)

            # this enforce a unique slug for every page
            if settings.PAGE_AUTOMATIC_SLUG_RENAMING:

                def is_slug_safe(slug):
                    content = Content.objects.get_content_slug_by_slug(slug)
                    if content is None:
                        return True
                    if self.instance.id:
                        if content.page.id == self.instance.id:
                            return True
                    else:
                        return False

                return self._clean_page_automatic_slug_renaming(
                    slug, is_slug_safe)

            if settings.PAGE_UNIQUE_SLUG_REQUIRED:
                return self._clean_page_unique_slug_required(slug)

            if settings.PAGE_USE_SITE_ID:
                if settings.PAGE_HIDE_SITES:
                    site_ids = [global_settings.SITE_ID]
                else:
                    site_ids = [int(x) for x in self.data.getlist('sites')]

                def intersects_sites(sibling):
                    return sibling.sites.filter(id__in=site_ids).count() > 0
            else:

                def intersects_sites(sibling):
                    return True

            if not settings.PAGE_UNIQUE_SLUG_REQUIRED:
                if target and position:
                    target = Page.objects.get(pk=target)
                    if position in ['right', 'left']:
                        slugs = [
                            sibling.slug()
                            for sibling in target.get_siblings()
                            if intersects_sites(sibling)
                        ]
                        slugs.append(target.slug())
                        if slug in slugs:
                            raise forms.ValidationError(
                                self.err_dict['sibling_position_error'])
                    if position == 'first-child':
                        if slug in [
                                sibling.slug()
                                for sibling in target.get_children()
                                if intersects_sites(sibling)
                        ]:
                            raise forms.ValidationError(
                                self.err_dict['child_error'])
                else:
                    if self.instance.id:
                        if (slug in [
                                sibling.slug() for sibling in self.instance.
                                get_siblings().exclude(id=self.instance.id)
                                if intersects_sites(sibling)
                        ]):
                            raise forms.ValidationError(
                                self.err_dict['sibling_error'])
                    else:
                        if slug in [
                                sibling.slug()
                                for sibling in Page.objects.root()
                                if intersects_sites(sibling)
                        ]:
                            raise forms.ValidationError(
                                self.err_dict['sibling_root_error'])
            return slug