Пример #1
0
    def construct_context(self, node, request):
        """ Construct the page context
        Render all blocks in a node page
        :param node: the node requested
        :param request: the request object
        :return: context dictionary
        """
        # construct the page context
        title = node.title if node.title == settings.SITE_NAME else ' | '.join((node.title, settings.SITE_NAME))
        status = "published" if node.status else "unpublished"
        page = {
            'title': title,
            'classes': self.construct_classes((node.page_type.name, 'content', status), request),
            'node': node,
            'author': settings.SITE_AUTHOR,
            'keywords': settings.SITE_KEYWORDS,
        }

        # get all elements (block instances) for this page type and append to page context
        # conveniently structure blocks to be able to access by name instead of looping in template
        for block in node.page_type.blocks.all():
            reg = slugify(block.name).replace('-', '_')
            # static node render
            if block.type == 'static':
                if block.node.language in (request.LANGUAGE_CODE, '') and block.node.status == 1:
                    page[reg] = block.node
            # menu render
            elif block.type == 'menu':
                if block.menu_item.language in (request.LANGUAGE_CODE, '') and block.menu_item.disabled == 0:
                    page[reg] = block.menu_item.get_descendants()
            # signal (view) render
            elif block.type == 'signal':
                responses = block_signal.send(sender=self.__class__, view=block.signal, node=node, request=request)
                responses = list(filter(lambda response: response[1] is not None, responses))
                if responses:
                    page[reg] = responses[-1][1]
            # contact form render
            elif block.type == 'contact':
                page[reg] = ContactForm(self.session_pop(request, 'contact_form_post', None), initial=request.GET)
            # language menu render
            elif block.type == 'language':
                page[reg] = settings.LANGUAGE_MENU_LABELS
            # login
            elif block.type == 'login':
                page[reg] = LoginForm(self.session_pop(request, 'login_form_post', None))
            # user menu
            elif block.type == 'user-menu':
                page[reg] = True
            # search form
            elif block.type == 'search':
                page[reg] = SearchForm(request.GET)
            # search results
            elif block.type == 'search-results':
                form = SearchForm(request.GET)
                form.is_valid()
                results = None
                if 'q' in form.cleaned_data:
                    q = form.cleaned_data['q']
                    results = Node.objects.filter(Q(title__icontains=q) | Q(body__icontains=q) |
                                                  Q(summary__icontains=q) | Q(highlight__icontains=q))
                    results = {'q': q, 'nodes': results}
                page[reg] = results
        return page
Пример #2
0
 def test_search_form_valid(self):
     """ Test that a search form is valid
     :return: None
     """
     form = SearchForm(data={'q': "test"})
     self.assertEqual(form.is_valid(), True)
Пример #3
0
    def construct_context(self, node, request):
        """ Construct the page context
        Render all blocks in a node page
        :param node: the node requested
        :param request: the request object
        :return: context dictionary
        """
        # construct the page context
        title = node.title if node.title == settings.SITE_NAME else ' | '.join(
            (node.title, settings.SITE_NAME))
        status = "published" if node.status else "unpublished"
        page = {
            'title':
            title,
            'classes':
            self.construct_classes((node.page_type.name, 'content', status),
                                   request),
            'node':
            node,
            'author':
            settings.SITE_AUTHOR,
            'keywords':
            settings.SITE_KEYWORDS,
        }

        # get all elements (block instances) for this page type and append to page context
        # conveniently structure blocks to be able to access by name instead of looping in template
        for block in node.page_type.blocks.all():
            reg = slugify(block.name).replace('-', '_')
            # static node render
            if block.type == 'static':
                if block.node.language in (request.LANGUAGE_CODE,
                                           '') and block.node.status == 1:
                    page[reg] = block.node
            # menu render
            elif block.type == 'menu':
                if block.menu_item.language in (
                        request.LANGUAGE_CODE,
                        '') and block.menu_item.disabled == 0:
                    page[reg] = block.menu_item.get_descendants()
            # signal (view) render
            elif block.type == 'signal':
                responses = block_signal.send(sender=self.__class__,
                                              view=block.signal,
                                              node=node,
                                              request=request)
                responses = list(
                    filter(lambda response: response[1] is not None,
                           responses))
                if responses:
                    page[reg] = responses[-1][1]
            # contact form render
            elif block.type == 'contact':
                page[reg] = ContactForm(self.session_pop(
                    request, 'contact_form_post', None),
                                        initial=request.GET)
            # language menu render
            elif block.type == 'language':
                page[reg] = settings.LANGUAGE_MENU_LABELS
            # login
            elif block.type == 'login':
                page[reg] = LoginForm(
                    self.session_pop(request, 'login_form_post', None))
            # user menu
            elif block.type == 'user-menu':
                page[reg] = True
            # search form
            elif block.type == 'search':
                page[reg] = SearchForm(request.GET)
            # search results
            elif block.type == 'search-results':
                form = SearchForm(request.GET)
                form.is_valid()
                results = None
                if 'q' in form.cleaned_data:
                    q = form.cleaned_data['q']
                    results = Node.objects.filter(
                        Q(title__icontains=q) | Q(body__icontains=q)
                        | Q(summary__icontains=q) | Q(highlight__icontains=q))
                    results = {'q': q, 'nodes': results}
                page[reg] = results
        return page
Пример #4
0
 def test_search_form_valid(self):
     """ Test that a search form is valid
     :return: None
     """
     form = SearchForm(data={'q': "test"})
     self.assertEqual(form.is_valid(), True)