Ejemplo n.º 1
0
def create_page(request, model, parent_pk):
    try:
        page = HelpBasePage.objects.get(pk=parent_pk)
    except HelpBasePage.DoesNotExist:
        raise Http404

    try:
        model_ct = ContentType.objects.get_by_natural_key(app_label="cms",
                                                          model=model)
    except ContentType.DoesNotExist:
        raise Http404

    form_class = get_page_form(model_ct)

    if request.method == "POST":
        form = form_class(data=request.POST, files=request.FILES)
        form.instance.parent_id = parent_pk
        form.instance.content_type = model_ct
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(
                reverse("admin:index", kwargs={"page_pk": parent_pk}))
    else:
        form = form_class()

    return TemplateResponse(request, "cms/admin/create_page.html", {
        "form": form,
        "breadcrumbs": breadcrumb_iterator(page),
    })
Ejemplo n.º 2
0
def edit_page(request, page_pk):
    try:
        page = HelpBasePage.objects.get(pk=page_pk).specific
    except HelpBasePage.DoesNotExist:
        raise Http404

    model_ct = ContentType.objects.get_for_id(page.content_type_id)
    form_class = get_page_form(model_ct)

    if request.method == "POST":
        form = form_class(data=request.POST,
                          files=request.FILES,
                          instance=page)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(
                reverse("admin:index", kwargs={"page_pk": page.pk}))
    else:
        form = form_class(instance=page)

    return TemplateResponse(request, "cms/admin/edit_page.html", {
        "form": form,
        "page": page,
        "breadcrumbs": breadcrumb_iterator(page),
    })
Ejemplo n.º 3
0
    def test_valid_model(self):
        model_ct = ContentType.objects.get_for_model(models.HelpPage)
        form = forms.get_page_form(model_ct)

        self.assertEqual(form._meta.model, models.HelpPage)
        self.assertEqual(form.model_ct, model_ct)
        self.assertEqual(form.base_fields.keys(),
                         [i for i in models.HelpPage.admin_fields])
Ejemplo n.º 4
0
    def test_invalid_model(self):
        model_ct = ContentType.objects.get_for_model(models.HelpBasePage)
        with self.assertRaises(AssertionError):
            forms.get_page_form(model_ct)

        model_ct = ContentType.objects.get_for_model(ContentType)
        with self.assertRaises(AssertionError):
            forms.get_page_form(model_ct)

        model_ct = ContentType.objects.get_for_model(models.HelpPage)
        with mock.patch.object(forms, "PAGE_TYPES",
                               []), self.assertRaises(AssertionError):
            forms.get_page_form(model_ct)

        # double check the mock above is correctly undone itself
        self.assertTrue(len(forms.PAGE_TYPES))