Beispiel #1
0
    def target_content_type(cls):
        if cls._target_content_type is None:
            if cls.page_type:
                target_models = []

                for page_type in cls.page_type:
                    try:
                        target_models.append(resolve_model_string(page_type))
                    except LookupError:
                        raise ImproperlyConfigured(
                            "{0}.page_type must be of the form 'app_label.model_name', given {1!r}".format(
                                cls.__name__, page_type
                            )
                        )
                    except ValueError:
                        raise ImproperlyConfigured(
                            "{0}.page_type refers to model {1!r} that has not been installed".format(
                                cls.__name__, page_type
                            )
                        )

                cls._target_content_type = list(ContentType.objects.get_for_models(*target_models).values())
            else:
                target_model = cls.model._meta.get_field(cls.field_name).rel.to
                cls._target_content_type = [ContentType.objects.get_for_model(target_model)]

        return cls._target_content_type
Beispiel #2
0
def page_model_from_string(string):
    page_model = resolve_model_string(string)

    if not issubclass(page_model, Page):
        raise ValueError("Model is not a page")

    return page_model
Beispiel #3
0
 def get_invoice_model(cls):
     if isinstance(cls.invoice_model, models.Model):
         return cls.invoice_model
     elif isinstance(cls.invoice_model, string_types):
         return resolve_model_string(cls.invoice_model, cls._meta.app_label)
     else:
         raise ValueError('Can not resolve {0}.invoice_model in to a model: {1!r}'.format(
             cls.__name__, cls.invoice_model))
Beispiel #4
0
    def get_model(self, request):
        if 'type' not in request.GET:
            return Page

        model_name = request.GET['type']
        try:
            return resolve_model_string(model_name)
        except LookupError:
            raise self.BadRequestError("type doesn't exist")
Beispiel #5
0
    def target_models(self):
        target_models = []

        for target_model in self._target_models:
            target_models.append(
                resolve_model_string(target_model)
            )

        return target_models
Beispiel #6
0
    def target_model(self):
        """
        Defines the model used by the base ChooserBlock for ID <-> instance
        conversions. If a single page type is specified in target_model,
        we can use that to get the more specific instance "for free"; otherwise
        use the generic Page model.
        """
        if len(self.target_models) == 1:
            return self.target_models[0]

        return resolve_model_string('wagtailcore.Page')
Beispiel #7
0
def page_models_from_string(string):
    page_models = []

    for sub_string in string.split(','):
        page_model = resolve_model_string(sub_string)

        if not issubclass(page_model, Page):
            raise ValueError("Model is not a page")

        page_models.append(page_model)

    return tuple(page_models)
Beispiel #8
0
def page_models_from_string(string):
    page_models = []

    for sub_string in string.split(','):
        page_model = resolve_model_string(sub_string)

        if not issubclass(page_model, Page):
            raise ValueError("Model is not a page")

        page_models.append(page_model)

    return tuple(page_models)
Beispiel #9
0
    def get_allowed_subpage_types(self):
        """
        Returns the list of manually selected subpage types, normalised as model classes.
        """
        if self.allowed_subpage_types is None or len(
                self.allowed_subpage_types) == 0:
            return []

        return [
            resolve_model_string(model_string)
            for model_string in self.allowed_subpage_types
        ]
Beispiel #10
0
    def alter_can_exist_under(self, child_page_class, can_exist):
        """
        Alter what pages may appear as children for Static Pages.
        """
        if len(self.allowed_subpage_types) > 0:
            allowed_subpage_type_models = [
                resolve_model_string(model_string)
                for model_string in self.allowed_subpage_types
            ]
            return child_page_class in allowed_subpage_type_models

        return can_exist
Beispiel #11
0
    def get_model(self, request):
        if 'type' not in request.GET:
            return Page

        model_name = request.GET['type']
        try:
            model = resolve_model_string(model_name)

            if not issubclass(model, Page):
                raise BadRequestError("type doesn't exist")

            return model
        except LookupError:
            raise BadRequestError("type doesn't exist")
Beispiel #12
0
    def target_model(cls):
        if cls._target_model is None:
            if cls.snippet_type:
                try:
                    cls._target_model = resolve_model_string(cls.snippet_type)
                except LookupError:
                    raise ImproperlyConfigured("{0}.snippet_type must be of the form 'app_label.model_name', given {1!r}".format(
                        cls.__name__, cls.snippet_type))
                except ValueError:
                    raise ImproperlyConfigured("{0}.snippet_type refers to model {1!r} that has not been installed".format(
                        cls.__name__, cls.snippet_type))
            else:
                cls._target_model = cls.model._meta.get_field(cls.field_name).rel.to

        return cls._target_model
Beispiel #13
0
    def target_content_type(cls):
        if cls._target_content_type is None:
            if cls.page_type:
                try:
                    model = resolve_model_string(cls.page_type)
                except LookupError:
                    raise ImproperlyConfigured("{0}.page_type must be of the form 'app_label.model_name', given {1!r}".format(
                        cls.__name__, cls.page_type))
                except ValueError:
                    raise ImproperlyConfigured("{0}.page_type refers to model {1!r} that has not been installed".format(
                        cls.__name__, cls.page_type))

                cls._target_content_type = ContentType.objects.get_for_model(model)
            else:
                # TODO: infer the content type by introspection on the foreign key
                cls._target_content_type = ContentType.objects.get_by_natural_key('wagtailcore', 'page')

        return cls._target_content_type
Beispiel #14
0
    def target_model(cls):
        if cls._target_model is None:
            if cls.snippet_type:
                try:
                    cls._target_model = resolve_model_string(cls.snippet_type)
                except LookupError:
                    raise ImproperlyConfigured(
                        "{0}.snippet_type must be of the form 'app_label.model_name', given {1!r}"
                        .format(cls.__name__, cls.snippet_type))
                except ValueError:
                    raise ImproperlyConfigured(
                        "{0}.snippet_type refers to model {1!r} that has not been installed"
                        .format(cls.__name__, cls.snippet_type))
            else:
                cls._target_model = cls.model._meta.get_field(
                    cls.field_name).rel.to

        return cls._target_model
Beispiel #15
0
    def target_content_type(cls):
        if cls._target_content_type is None:
            if cls.page_type:
                try:
                    model = resolve_model_string(cls.page_type)
                except LookupError:
                    raise ImproperlyConfigured("{0}.page_type must be of the form 'app_label.model_name', given {1!r}".format(
                        cls.__name__, cls.page_type))
                except ValueError:
                    raise ImproperlyConfigured("{0}.page_type refers to model {1!r} that has not been installed".format(
                        cls.__name__, cls.page_type))

                cls._target_content_type = ContentType.objects.get_for_model(model)
            else:
                # TODO: infer the content type by introspection on the foreign key
                cls._target_content_type = ContentType.objects.get_by_natural_key('wagtailcore', 'page')

        return cls._target_content_type
Beispiel #16
0
    def target_content_type(cls):
        if cls._target_content_type is None:
            if cls.page_type:
                try:
                    model = resolve_model_string(cls.page_type)
                except LookupError:
                    raise ImproperlyConfigured("{0}.page_type must be of the form 'app_label.model_name', given {1!r}".format(
                        cls.__name__, cls.page_type))
                except ValueError:
                    raise ImproperlyConfigured("{0}.page_type refers to model {1!r} that has not been installed".format(
                        cls.__name__, cls.page_type))

                cls._target_content_type = ContentType.objects.get_for_model(model)
            else:
                target_model = cls.model._meta.get_field(cls.field_name).rel.to
                cls._target_content_type = ContentType.objects.get_for_model(target_model)

        return cls._target_content_type
Beispiel #17
0
    def target_models(cls):
        if cls.page_type:
            target_models = []

            for page_type in cls.page_type:
                try:
                    target_models.append(resolve_model_string(page_type))
                except LookupError:
                    raise ImproperlyConfigured(
                        "{0}.page_type must be of the form 'app_label.model_name', given {1!r}"
                        .format(cls.__name__, page_type))
                except ValueError:
                    raise ImproperlyConfigured(
                        "{0}.page_type refers to model {1!r} that has not been installed"
                        .format(cls.__name__, page_type))

            return target_models
        else:
            return [cls.model._meta.get_field(cls.field_name).rel.to]
Beispiel #18
0
def search(request, parent_page_id=None):
    page_types = request.GET.get('page_types')
    content_types = []

    # Convert page_types string into list of ContentType objects
    if page_types:
        try:
            content_types = ContentType.objects.get_for_models(*[
                resolve_model_string(page_type) for page_type in page_types.split(',')])
        except LookupError:
            raise Http404

    search_form = SearchForm(request.GET)
    if search_form.is_valid() and search_form.cleaned_data['q']:
        pages = Page.objects.exclude(
            depth=1  # never include root
        )

        # Restrict content types
        if content_types:
            pages = pages.filter(content_type__in=content_types)

        # Do search
        pages = pages.filter(title__icontains=search_form.cleaned_data['q'])

        # Truncate results
        pages = pages[:10]
    else:
        pages = Page.objects.none()

    shown_pages = []
    for page in pages:
        page.can_choose = True
        shown_pages.append(page)

    return render(
        request, 'wagtailadmin/chooser/_search_results.html',
        shared_context(request, {
            'searchform': search_form,
            'pages': shown_pages,
        })
    )
Beispiel #19
0
    def target_model(cls):
        if cls._target_model is None:
            if cls.snippet_type:
                # RemovedInWagtail16Warning: The target_model is automatically
                # detected from the relation, so snippet_type is deprecated.
                try:
                    cls._target_model = resolve_model_string(cls.snippet_type)
                except LookupError:
                    raise ImproperlyConfigured(
                        "{0}.snippet_type must be of the form 'app_label.model_name', given {1!r}"
                        .format(cls.__name__, cls.snippet_type))
                except ValueError:
                    raise ImproperlyConfigured(
                        "{0}.snippet_type refers to model {1!r} that has not been installed"
                        .format(cls.__name__, cls.snippet_type))
            else:
                cls._target_model = cls.model._meta.get_field(
                    cls.field_name).rel.to

        return cls._target_model
Beispiel #20
0
    def target_model(cls):
        if cls._target_model is None:
            if cls.snippet_type:
                # RemovedInWagtail16Warning: The target_model is automatically
                # detected from the relation, so snippet_type is deprecated.
                try:
                    cls._target_model = resolve_model_string(cls.snippet_type)
                except LookupError:
                    raise ImproperlyConfigured(
                        "{0}.snippet_type must be of the form 'app_label.model_name', given {1!r}"
                        .format(cls.__name__, cls.snippet_type)
                    )
                except ValueError:
                    raise ImproperlyConfigured(
                        "{0}.snippet_type refers to model {1!r} that has not been installed"
                        .format(cls.__name__, cls.snippet_type)
                    )
            else:
                cls._target_model = cls.model._meta.get_field(cls.field_name).rel.to

        return cls._target_model
Beispiel #21
0
    def get_queryset(self):
        request = self.request

        # Allow pages to be filtered to a specific type
        if 'type' not in request.GET:
            model = Page
        else:
            model_name = request.GET['type']
            try:
                model = resolve_model_string(model_name)
            except LookupError:
                raise BadRequestError("type doesn't exist")
            if not issubclass(model, Page):
                raise BadRequestError("type doesn't exist")

        # Get live pages that are not in a private section
        queryset = model.objects.public().live()

        # Filter by site
        queryset = queryset.descendant_of(request.site.root_page, inclusive=True)

        return queryset
Beispiel #22
0
    def clean_parent_page_types(cls):
        """
        Returns the list of parent page types, with strings converted to class
        objects where required
        """
        if cls._clean_parent_page_types is None:
            parent_page_types = getattr(cls, 'parent_page_types', None)
            if parent_page_types is None:
                # if parent_page_types is not specified on the Page class, allow all page types as subpages
                res = get_page_types()
            else:
                try:
                    models = [resolve_model_string(model_string, cls._meta.app_label)
                              for model_string in parent_page_types]
                except LookupError as err:
                    raise ImproperlyConfigured("{0}.parent_page_types must be a list of 'app_label.model_name' strings, given {1!r}".format(
                        cls.__name__, err.args[1]))
                res = list(map(ContentType.objects.get_for_model, models))

            cls._clean_parent_page_types = res

        return cls._clean_parent_page_types
Beispiel #23
0
    def clean_parent_page_types(cls):
        """
        Returns the list of parent page types, with strings converted to class
        objects where required
        """
        if cls._clean_parent_page_types is None:
            parent_page_types = getattr(cls, 'parent_page_types', None)
            if parent_page_types is None:
                # if parent_page_types is not specified on the Page class, allow all page types as subpages
                res = get_page_types()
            else:
                try:
                    models = [resolve_model_string(model_string, cls._meta.app_label)
                              for model_string in parent_page_types]
                except LookupError as err:
                    raise ImproperlyConfigured("{0}.parent_page_types must be a list of 'app_label.model_name' strings, given {1!r}".format(
                        cls.__name__, err.args[1]))
                res = list(map(ContentType.objects.get_for_model, models))

            cls._clean_parent_page_types = res

        return cls._clean_parent_page_types
Beispiel #24
0
    def target_models(cls):
        if cls.page_type:
            target_models = []

            for page_type in cls.page_type:
                try:
                    target_models.append(resolve_model_string(page_type))
                except LookupError:
                    raise ImproperlyConfigured(
                        "{0}.page_type must be of the form 'app_label.model_name', given {1!r}".format(
                            cls.__name__, page_type
                        )
                    )
                except ValueError:
                    raise ImproperlyConfigured(
                        "{0}.page_type refers to model {1!r} that has not been installed".format(
                            cls.__name__, page_type
                        )
                    )

            return target_models
        else:
            return [cls.model._meta.get_field(cls.field_name).rel.to]
Beispiel #25
0
    def test_resolve_from_string(self):
        model = resolve_model_string('wagtailcore.Page')

        self.assertEqual(model, Page)
Beispiel #26
0
    def test_resolve_from_string_with_default_app(self):
        model = resolve_model_string("Page", default_app="wagtailcore")

        self.assertEqual(model, Page)
Beispiel #27
0
 def get_newsitem_model(cls):
     return resolve_model_string(cls.newsitem_model, cls._meta.app_label)
Beispiel #28
0
    def test_resolve_from_string_with_different_default_app(self):
        model = resolve_model_string('wagtailcore.Page',
                                     default_app='wagtailadmin')

        self.assertEqual(model, Page)
Beispiel #29
0
    def get_post_model(cls):
        if not cls.post_model:
            return

        return resolve_model_string(cls.post_model)
Beispiel #30
0
    def test_resolve_from_class(self):
        model = resolve_model_string(Page)

        self.assertEqual(model, Page)
Beispiel #31
0
 def target_model(self):
     return resolve_model_string(self._target_model)
Beispiel #32
0
    def get_post_model(cls):
        if not cls.post_model:
            return

        return resolve_model_string(cls.post_model)
Beispiel #33
0
def browse(request, parent_page_id=None):
    ITEMS_PER_PAGE = 25

    page_types = request.GET.get('page_types', 'wagtailcore.page').split(',')

    desired_classes = []
    for page_type in page_types:
        try:
            content_type = resolve_model_string(page_type)
        except LookupError:
            raise Http404

        desired_classes.append(content_type)

    if parent_page_id:
        parent_page = get_object_or_404(Page, id=parent_page_id)
    else:
        parent_page = Page.get_first_root_node()

    parent_page.can_choose = issubclass(parent_page.specific_class, tuple(desired_classes))
    search_form = SearchForm()
    pages = parent_page.get_children()

    if desired_classes == [Page]:
        # apply pagination first, since we know that the page listing won't
        # have to be filtered, and that saves us walking the entire list
        p = request.GET.get('p', 1)
        paginator = Paginator(pages, ITEMS_PER_PAGE)
        try:
            pages = paginator.page(p)
        except PageNotAnInteger:
            pages = paginator.page(1)
        except EmptyPage:
            pages = paginator.page(paginator.num_pages)

        for page in pages:
            page.can_choose = True
            page.can_descend = page.get_children_count()

    else:
        # restrict the page listing to just those pages that:
        # - are of the given content type (taking into account class inheritance)
        # - or can be navigated into (i.e. have children)

        shown_pages = []
        for page in pages:
            page.can_choose = issubclass(page.specific_class or Page, tuple(desired_classes))
            page.can_descend = page.get_children_count()

            if page.can_choose or page.can_descend:
                shown_pages.append(page)

        # Apply pagination
        p = request.GET.get('p', 1)
        paginator = Paginator(shown_pages, ITEMS_PER_PAGE)
        try:
            pages = paginator.page(p)
        except PageNotAnInteger:
            pages = paginator.page(1)
        except EmptyPage:
            pages = paginator.page(paginator.num_pages)

    return render_modal_workflow(
        request,
        'wagtailadmin/chooser/browse.html', 'wagtailadmin/chooser/browse.js',
        shared_context(request, {
            'parent_page': parent_page,
            'pages': pages,
            'search_form': search_form,
            'page_type_string': ','.join(page_types),
            'page_type_names': [desired_class.get_verbose_name() for desired_class in desired_classes],
            'page_types_restricted': (page_type != 'wagtailcore.page')
        })
    )
Beispiel #34
0
 def target_model(self):
     return resolve_model_string(self._target_model)
Beispiel #35
0
    def test_resolve_from_string(self):
        model = resolve_model_string('wagtailcore.Page')

        self.assertEqual(model, Page)
Beispiel #36
0
    def test_resolve_from_string_with_different_default_app(self):
        model = resolve_model_string('wagtailcore.Page', default_app='wagtailadmin')

        self.assertEqual(model, Page)
Beispiel #37
0
    def test_resolve_from_class(self):
        model = resolve_model_string(Page)

        self.assertEqual(model, Page)