示例#1
0
文件: apps.py 项目: nada/feincms3
    def clean_fields(self, exclude=None):
        """
        Checks that application nodes do not have any descendants, and that
        required fields for the selected application (if any) are filled out,
        and that app instances with the same instance namespace and same
        language only exist once on a site.
        """
        exclude = [] if exclude is None else exclude
        super(AppsMixin, self).clean_fields(exclude)

        if self.parent and (
                self.parent.application
                or self.parent.ancestors().exclude(application='').exists()):
            error = _('Apps may nove have any descendants.')
            raise validation_error(_('Invalid parent: %s') % (error, ),
                                   field='parent',
                                   exclude=exclude)

        if self.application and not self.is_leaf():
            raise validation_error(
                _('Apps may not have any descendants in the tree.'),
                field='application',
                exclude=exclude)

        app_config = self.application_config()
        if app_config and app_config.get('required_fields'):
            missing = [
                field for field in app_config['required_fields']
                if not getattr(self, field)
            ]
            if missing:
                error = _('This field is required for the application %s.') % (
                    self.get_application_display(), )
                errors = {}
                for field in missing:
                    if field in exclude:
                        errors.setdefault('__all__',
                                          []).append('%s: %s' % (field, error))
                    else:
                        errors[field] = error

                raise ValidationError(errors)

        if app_config:
            app_instance_namespace = app_config.get(
                'app_instance_namespace',
                lambda instance: instance.application,
            )(self)

            if self.__class__._base_manager.filter(
                    Q(app_instance_namespace=app_instance_namespace),
                    Q(language_code=self.language_code),
                    ~Q(pk=self.pk or 0),
            ).exists():
                fields = ['application']
                fields.extend(app_config.get('required_fields', ()))
                raise ValidationError({
                    field: _(_('This exact app already exists.'), )
                    for field in fields
                })
示例#2
0
    def clean_fields(self, exclude=None):
        super(RedirectMixin, self).clean_fields(exclude)

        if self.redirect_to_url and self.redirect_to_page_id:
            raise validation_error(
                _('Only set one redirect value.'),
                field='redirect_to_url',
                exclude=exclude,
            )

        if self.redirect_to_page_id:
            if self.redirect_to_page_id == self.pk:
                raise validation_error(
                    _('Cannot redirect to self.'),
                    field='redirect_to_page',
                    exclude=exclude,
                )

            if self.redirect_to_page.redirect_to_page_id:
                raise validation_error(
                    _('Do not chain redirects. The selected page redirects'
                      ' to %(title)s (%(path)s).') % {
                          'title': self.redirect_to_page,
                          'path': self.redirect_to_page.get_absolute_url(),
                      },
                    field='redirect_to_page',
                    exclude=exclude,
                )
示例#3
0
    def clean_fields(self, exclude=None):
        """
        Implement the following validation rules:

        - Objects in the primary language cannot be the translation of another object
        - Objects in other languages can only reference objects in the primary language
        """

        super().clean_fields(exclude)

        if self.language_code == settings.LANGUAGES[0][
                0] and self.translation_of:
            raise validation_error(
                _("Objects in the primary language cannot be"
                  " the translation of another object."),
                field="translation_of",
                exclude=exclude,
            )

        if (self.translation_of and
                self.translation_of.language_code != settings.LANGUAGES[0][0]):
            raise validation_error(
                _("Objects may only be the translation of"
                  " objects in the primary language."),
                field="translation_of",
                exclude=exclude,
            )
示例#4
0
    def clean_fields(self, exclude=None):
        """
        Check for path uniqueness problems.
        """
        super(AbstractPage, self).clean_fields(exclude)

        if self.static_path:
            if not self.path:
                raise validation_error(
                    _('Static paths cannot be empty. Did you mean \'/\'?'),
                    field='path',
                    exclude=exclude)
        else:
            self.path = '{}{}/'.format(
                self.parent.path if self.parent else '/', self.slug)

        super(AbstractPage, self).clean()

        # Skip if we don't exist yet.
        if not self.pk:
            return

        clash_candidates = self.__class__._default_manager.exclude(
            Q(pk__in=self.descendants()) | Q(pk=self.pk), )
        for pk, node in self._branch_for_update().items():
            if clash_candidates.filter(path=node.path).exists():
                raise validation_error(
                    _('The page %(page)s\'s new path %(path)s would'
                      ' not be unique.') % {
                          'page': node,
                          'path': node.path,
                      },
                    field='path',
                    exclude=exclude)
示例#5
0
文件: apps.py 项目: hancush/feincms3
    def clean_fields(self, exclude=None):
        """
        Checks that application nodes do not have any descendants, and that
        required fields for the selected application (if any) are filled out,
        and that app instances with the same instance namespace and same
        language only exist once on a site.
        """
        exclude = [] if exclude is None else exclude
        super(AppsMixin, self).clean_fields(exclude)

        if self.parent and (
                self.parent.application
                or self.parent.ancestors().exclude(application="").exists()):
            error = _("Apps may not have any descendants.")
            raise validation_error(_("Invalid parent: %s") % (error, ),
                                   field="parent",
                                   exclude=exclude)

        if self.application and self.children.exists():
            raise validation_error(
                _("Apps may not have any descendants in the tree."),
                field="application",
                exclude=exclude,
            )

        app_config = self.application_config()
        if app_config and app_config.get("required_fields"):
            missing = [
                field for field in app_config["required_fields"]
                if not getattr(self, field)
            ]
            if missing:
                error = _("This field is required for the application %s.") % (
                    self.get_application_display(), )
                errors = {}
                for field in missing:
                    if field in exclude:
                        errors.setdefault("__all__",
                                          []).append("%s: %s" % (field, error))
                    else:
                        errors[field] = error

                raise ValidationError(errors)

        if app_config:
            app_instance_namespace = app_config.get(
                "app_instance_namespace",
                lambda instance: instance.application)(self)

            if self.__class__._default_manager.filter(
                    Q(app_instance_namespace=app_instance_namespace),
                    Q(language_code=self.language_code),
                    ~Q(pk=self.pk),
            ).exists():
                fields = ["__all__", "application"]
                fields.extend(app_config.get("required_fields", ()))
                raise ValidationError({
                    field: _("This exact app already exists.")
                    for field in fields if field not in exclude
                })
示例#6
0
    def clean_fields(self, exclude=None):
        """
        Ensure that redirects are configured properly.
        """
        super().clean_fields(exclude)

        if self.redirect_to_url and self.redirect_to_page_id:
            raise validation_error(
                _("Only set one redirect value."),
                field="redirect_to_url",
                exclude=exclude,
            )

        if self.redirect_to_page_id:
            if self.redirect_to_page_id == self.pk:
                raise validation_error(
                    _("Cannot redirect to self."),
                    field="redirect_to_page",
                    exclude=exclude,
                )

            if (
                self.redirect_to_page.redirect_to_page_id
                or self.redirect_to_page.redirect_to_url
            ):
                raise validation_error(
                    _(
                        'Do not chain redirects. The selected page "%(title)s"'
                        ' redirects to "%(path)s".'
                    )
                    % {
                        "title": self.redirect_to_page,
                        "path": (
                            self.redirect_to_page.redirect_to_page.get_absolute_url()
                            if self.redirect_to_page.redirect_to_page
                            else self.redirect_to_page.redirect_to_url
                        ),
                    },
                    field="redirect_to_page",
                    exclude=exclude,
                )

        if self.pk and (self.redirect_to_url or self.redirect_to_page_id):
            # Any page redirects to this page?
            other = self.__class__._default_manager.filter(redirect_to_page=self)
            if other:
                raise validation_error(
                    _(
                        "Do not chain redirects. The following pages already"
                        " redirect to this page: %(pages)s"
                    )
                    % {
                        "pages": ", ".join(
                            "%s (%s)" % (page, page.get_absolute_url())
                            for page in other
                        )
                    },
                    field="redirect_to_page",
                    exclude=exclude,
                )
示例#7
0
    def clean_fields(self, exclude=None):
        """
        Check for path uniqueness problems.
        """
        super().clean_fields(exclude)

        if self.static_path:
            if not self.path:
                raise validation_error(
                    _("Static paths cannot be empty. Did you mean '/'?"),
                    field="path",
                    exclude=exclude,
                )
        else:
            self.path = "%s%s/" % (self.parent.path if self.parent else "/", self.slug)

        super().clean()

        # Skip if we don't exist yet.
        if not self.pk:
            return

        clash_candidates = dict(self._path_clash_candidates().values_list("path", "id"))
        for pk, node in self._branch_for_update().items():
            if (
                node.path in clash_candidates
                and not clash_candidates[node.path] == node.pk
            ):
                raise validation_error(
                    _("The page %(page)s's new path %(path)s would not be unique.")
                    % {"page": node, "path": node.path},
                    field="path",
                    exclude=exclude,
                )
示例#8
0
    def clean_fields(self, exclude=None):
        """
        Ensure that redirects are configured properly.
        """
        super(RedirectMixin, self).clean_fields(exclude)

        if self.redirect_to_url and self.redirect_to_page_id:
            raise validation_error(
                _("Only set one redirect value."),
                field="redirect_to_url",
                exclude=exclude,
            )

        if self.redirect_to_page_id:
            if self.redirect_to_page_id == self.pk:
                raise validation_error(
                    _("Cannot redirect to self."),
                    field="redirect_to_page",
                    exclude=exclude,
                )

            if self.redirect_to_page.redirect_to_page_id:
                raise validation_error(
                    _("Do not chain redirects. The selected page redirects"
                      " to %(title)s (%(path)s).") % {
                          "title": self.redirect_to_page,
                          "path": self.redirect_to_page.get_absolute_url(),
                      },
                    field="redirect_to_page",
                    exclude=exclude,
                )

        if self.redirect_to_url or self.redirect_to_page_id:
            # Any page redirects to this page?
            other = self.__class__._default_manager.filter(
                redirect_to_page=self)
            if other:
                raise validation_error(
                    _("Do not chain redirects. The page %(page)s already"
                      " redirects to this page.") %
                    {"page": ", ".join("%s" % page for page in other)},
                    field="redirect_to_page",
                    exclude=exclude,
                )