def test_mezzanine_form(self):
        page = Form.objects.create(slug='my-form')
        page.save()

        field = Field.objects.create(
            form=page,
            field_type=3,
            required=False)
        field.save()

        page_middleware = PageMiddleware()

        request = test.RequestFactory().post(
            '/my-form', data={'field_1': '*****@*****.**'})
        request.user = User.objects.get_or_create(pk=1)[0]

        SessionMiddleware().process_request(request)
        request.session.save()

        self.assertEqual(len(mail.outbox), 0,)

        page_middleware.process_view(request, mezzanine_page_view, {}, {})
        self.assertEqual(len(mail.outbox), 1)
        self.assertNotIn('access your downloads', mail.outbox[0].body)

        download = Download.objects.create()
        download.forms.add(page)
        download.save()

        page_middleware.process_view(request, mezzanine_page_view, {}, {})
        self.assertEqual(len(mail.outbox), 2)
        self.assertIn('access your downloads', mail.outbox[1].body)
Esempio n. 2
0
def page(request, template=u"pages/versionpage.html", extra_context=None):
    """
    Select a template for a page and render it. The request
    object should have a ``page`` attribute that's added via
    ``mezzanine.pages.middleware.PageMiddleware``. The page is loaded
    earlier via middleware to perform various other functions.
    The urlpattern that maps to this view is a catch-all pattern, in
    which case the page attribute won't exist, so raise a 404 then.

    For template selection, a list of possible templates is built up
    based on the current page. This list is order from most granular
    match, starting with a custom template for the exact page, then
    adding templates based on the page's parent page, that could be
    used for sections of a site (eg all children of the parent).
    Finally at the broadest level, a template for the page's content
    type (it's model class) is checked for, and then if none of these
    templates match, the default pages/page.html is used.
    """

    from mezzanine.pages.middleware import PageMiddleware
    if not PageMiddleware.installed():
        raise ImproperlyConfigured("mezzanine.pages.middleware.PageMiddleware "
                                   "(or a subclass of it) is missing from " +
                                   "settings.MIDDLEWARE_CLASSES or " +
                                   "settings.MIDDLEWARE")
    return TemplateResponse(request, [template], extra_context or {})
Esempio n. 3
0
def page(request, slug, template=u"pages/page.html", extra_context=None):
    """
    Select a template for a page and render it. The request
    object should have a ``page`` attribute that's added via
    ``mezzanine.pages.middleware.PageMiddleware``. The page is loaded
    earlier via middleware to perform various other functions.
    The urlpattern that maps to this view is a catch-all pattern, in
    which case the page attribute won't exist, so raise a 404 then.

    For template selection, a list of possible templates is built up
    based on the current page. This list is order from most granular
    match, starting with a custom template for the exact page, then
    adding templates based on the page's parent page, that could be
    used for sections of a site (eg all children of the parent).
    Finally at the broadest level, a template for the page's content
    type (it's model class) is checked for, and then if none of these
    templates match, the default pages/page.html is used.
    """

    from mezzanine.pages.middleware import PageMiddleware
    if not PageMiddleware.installed():
        raise ImproperlyConfigured("mezzanine.pages.middleware.PageMiddleware "
                                   "(or a subclass of it) is missing from " +
                                   "settings.MIDDLEWARE_CLASSES or " +
                                   "settings.MIDDLEWARE")

    if not hasattr(request, "page") or request.page.slug != slug:
        raise Http404

    # Check for a template name matching the page's slug. If the homepage
    # is configured as a page instance, the template "pages/index.html" is
    # used, since the slug "/" won't match a template name.
    template_name = str(slug) if slug != home_slug() else "index"
    templates = [u"pages/%s.html" % template_name]
    method_template = request.page.get_content_model().get_template_name()
    if method_template:
        templates.insert(0, method_template)
    if request.page.content_model is not None:
        templates.append(u"pages/%s/%s.html" %
                         (template_name, request.page.content_model))
    for parent in request.page.get_ascendants(for_user=request.user):
        parent_template_name = str(parent.slug)
        # Check for a template matching the page's content model.
        if request.page.content_model is not None:
            templates.append(
                u"pages/%s/%s.html" %
                (parent_template_name, request.page.content_model))
    # Check for a template matching the page's content model.
    if request.page.content_model is not None:
        templates.append(u"pages/%s.html" % request.page.content_model)
    templates.append(template)
    if request.is_ajax() or (template != "pages/unifypage.html"
                             and template != "pages/page.html"):
        templates = template
    return TemplateResponse(request, templates, extra_context or {})
Esempio n. 4
0
def page(request, slug, template=u"pages/page.html", extra_context=None):
    """
    Select a template for a page and render it. The ``extra_context``
    arg will include a ``page`` object that's added via
    ``mezzanine.pages.middleware.PageMiddleware``. The page is loaded
    via the middleware so that other apps with urlpatterns that match
    the current page can include a page in their template context.
    The urlpattern that maps to this view is a catch-all pattern, in
    which case the page instance will be None, so raise a 404 then.

    For template selection, a list of possible templates is built up
    based on the current page. This list is order from most granular
    match, starting with a custom template for the exact page, then
    adding templates based on the page's parent page, that could be
    used for sections of a site (eg all children of the parent).
    Finally at the broadest level, a template for the page's content
    type (it's model class) is checked for, and then if none of these
    templates match, the default pages/page.html is used.
    """

    from mezzanine.pages.middleware import PageMiddleware
    if not PageMiddleware.installed():
        raise ImproperlyConfigured("mezzanine.pages.middleware.PageMiddleware "
                                   "(or a subclass of it) is missing from " +
                                   "settings.MIDDLEWARE_CLASSES")

    extra_context = extra_context or {}
    try:
        page = extra_context["page"]
    except KeyError:
        raise Http404

    # Check for a template name matching the page's slug. If the homepage
    # is configured as a page instance, the template "pages/index.html" is
    # used, since the slug "/" won't match a template name.
    template_name = str(slug) if slug != home_slug() else "index"
    templates = [u"pages/%s.html" % template_name]
    method_template = page.get_content_model().get_template_name()
    if method_template:
        templates.insert(0, method_template)
    if page.content_model is not None:
        templates.append(u"pages/%s/%s.html" % (template_name,
            page.content_model))
    for parent in page.get_ascendants(for_user=request.user):
        parent_template_name = str(parent.slug)
        # Check for a template matching the page's content model.
        if page.content_model is not None:
            templates.append(u"pages/%s/%s.html" % (parent_template_name,
                page.content_model))
    # Check for a template matching the page's content model.
    if page.content_model is not None:
        templates.append(u"pages/%s.html" % page.content_model)
    templates.append(template)
    return render(request, templates, extra_context)
Esempio n. 5
0
    def test_mezzanine_form(self):
        page = Form.objects.create(slug='my-form')
        page.save()

        field = Field.objects.create(form=page, field_type=3, required=False)
        field.save()

        page_middleware = PageMiddleware()

        request = test.RequestFactory().post(
            '/my-form', data={'field_1': '*****@*****.**'})
        request.user = User.objects.get_or_create(pk=1)[0]

        SessionMiddleware().process_request(request)
        request.session.save()

        self.assertEqual(
            len(mail.outbox),
            0,
        )

        page_middleware.process_view(request, mezzanine_page_view, {}, {})
        self.assertEqual(len(mail.outbox), 1)
        self.assertNotIn('access your downloads', mail.outbox[0].body)

        download = Download.objects.create()
        download.forms.add(page)
        download.save()

        page_middleware.process_view(request, mezzanine_page_view, {}, {})
        self.assertEqual(len(mail.outbox), 2)
        self.assertIn('access your downloads', mail.outbox[1].body)
Esempio n. 6
0
    def test_exact_page_processor_for(self):
        """
        Test that passing exact_page=True works with the PageMiddleware
        """
        from mezzanine.pages.middleware import PageMiddleware
        from mezzanine.pages.page_processors import processor_for
        from mezzanine.pages.views import page as page_view

        @processor_for('foo/bar', exact_page=True)
        def test_page_processor(request, page):
            return HttpResponse("bar")

        foo, _ = RichTextPage.objects.get_or_create(title="foo")
        bar, _ = RichTextPage.objects.get_or_create(title="bar", parent=foo)

        request = self._request_factory.get('/foo/bar/')
        request.user = self._user
        response = PageMiddleware().process_view(request, page_view, [], {})

        self.assertTrue(isinstance(response, HttpResponse))
        self.assertContains(response, "bar")
Esempio n. 7
0
def page(request, slug, template="pages/page.html", extra_context=None):
    """
    Select a template for a page and render it. The request
    object should have a ``page`` attribute that's added via
    ``mezzanine.pages.middleware.PageMiddleware``. The page is loaded
    earlier via middleware to perform various other functions.
    The urlpattern that maps to this view is a catch-all pattern, in
    which case the page attribute won't exist, so raise a 404 then.

    For template selection, a list of possible templates is built up
    based on the current page. This list is order from most granular
    match, starting with a custom template for the exact page, then
    adding templates based on the page's parent page, that could be
    used for sections of a site (eg all children of the parent).
    Finally at the broadest level, a template for the page's content
    type (it's model class) is checked for, and then if none of these
    templates match, the default pages/page.html is used.
    """

    from mezzanine.pages.middleware import PageMiddleware

    if not PageMiddleware.installed():
        raise ImproperlyConfigured(
            "mezzanine.pages.middleware.PageMiddleware "
            "(or a subclass of it) is missing from " + "settings.MIDDLEWARE_CLASSES"
        )

    if not hasattr(request, "page") or request.page.slug != slug:
        raise Http404

    # Check for a template name matching the page's slug. If the homepage
    # is configured as a page instance, the template "pages/index.html" is
    # used, since the slug "/" won't match a template name.
    template_name = str(slug) if slug != home_slug() else "index"
    templates = ["pages/%s.html" % template_name]
    method_template = request.page.get_content_model().get_template_name()
    if method_template:
        templates.insert(0, method_template)
    if request.page.content_model is not None:
        templates.append("pages/%s/%s.html" % (template_name, request.page.content_model))
    for parent in request.page.get_ascendants(for_user=request.user):
        parent_template_name = str(parent.slug)
        # Check for a template matching the page's content model.
        if request.page.content_model is not None:
            templates.append("pages/%s/%s.html" % (parent_template_name, request.page.content_model))
    # Check for a template matching the page's content model.
    if request.page.content_model is not None:
        templates.append("pages/%s.html" % request.page.content_model)
    templates.append(template)

    # added by RobinWang
    ss = slug.split("/")
    if len(ss) == 1:
        categorys = BlogCategory.objects.all()
        categoryslug = ""
        for c in categorys:
            if c.slug == ss[0]:
                categoryslug = ss[0]
                break
        if categoryslug != "":
            blogs = getposts(request, category=categoryslug)
            extra_context["blog_posts"] = blogs

    return render(request, templates, extra_context or {})
Esempio n. 8
0
def page(request, slug, template=u"pages/page.html", extra_context=None):
    """
    Select a template for a page and render it. The request
    object should have a ``page`` attribute that's added via
    ``mezzanine.pages.middleware.PageMiddleware``. The page is loaded
    earlier via middleware to perform various other functions.
    The urlpattern that maps to this view is a catch-all pattern, in
    which case the page attribute won't exist, so raise a 404 then.

    For template selection, a list of possible templates is built up
    based on the current page. This list is order from most granular
    match, starting with a custom template for the exact page, then
    adding templates based on the page's parent page, that could be
    used for sections of a site (eg all children of the parent).
    Finally at the broadest level, a template for the page's content
    type (it's model class) is checked for, and then if none of these
    templates match, the default pages/page.html is used.
    """
    
    from mezzanine.pages.middleware import PageMiddleware
    if not PageMiddleware.installed():
        raise ImproperlyConfigured("mezzanine.pages.middleware.PageMiddleware "
                                   "(or a subclass of it) is missing from " +
                                   "settings.MIDDLEWARE_CLASSES")
    obj=None
    if not hasattr(request, "page") or request.page.slug != slug:
        
        split=slug.split('/')
        article=split.pop(-1)
        
        obj=get_object_or_404(Article,slug=article)
        if obj:
            slug="/".join(split)+"/detail"
            

    # Check for a template name matching the page's slug. If the homepage
    # is configured as a page instance, the template "pages/index.html" is
    # used, since the slug "/" won't match a template name.
    template_name = str(slug) if slug != home_slug() else "index"
    templates = [u"pages/%s.html" % template_name]
    if obj:
        templates.append("pages/detail.html")
        obj.prepare_content(request)
        return TemplateResponse(request, templates, {'article':obj})
    method_template = request.page.get_content_model().get_template_name()
    
    if method_template:
        templates.insert(0, method_template)
    if request.page.content_model is not None:
        templates.append(u"pages/%s/%s.html" % (template_name,
            request.page.content_model))
    for parent in request.page.get_ascendants(for_user=request.user):
        parent_template_name = str(parent.slug)
        # Check for a template matching the page's content model.
        if request.page.content_model is not None:
            templates.append(u"pages/%s/%s.html" % (parent_template_name,
                request.page.content_model))
    # Check for a template matching the page's content model.
    if request.page.content_model is not None:
        templates.append(u"pages/%s.html" % request.page.content_model)
    templates.append(template)
    return TemplateResponse(request, templates, extra_context or {})