Beispiel #1
0
 def test_new_article_navigation_leaf(self):
     initial_data = {'title': "test", 'content': "this is my article content"}
     article_class = get_article_class()
     art1 = article_class.objects.create(publication=BaseArticle.PUBLISHED, **initial_data)
     
     tree = get_navtree_class().objects.create()
     ct = ContentType.objects.get_for_model(article_class)
     node1 = NavNode.objects.create(content_type=ct, object_id=art1.id, tree=tree, parent=None)
     
     self._log_as_editor()
     data = {
         'title': "Un titre",
         'publication': BaseArticle.PUBLISHED,
         'template': get_article_templates(None, self.user)[0][0],
         'navigation_parent': node1.id,
         'sites': [settings.SITE_ID]
     }
     
     response = self.client.post(reverse('coop_cms_new_article'), data=data, follow=True)
     self.assertEqual(response.status_code, 200)
     self.assertEqual(article_class.objects.exclude(id=art1.id).count(), 1)
     art2 = article_class.objects.exclude(id=art1.id)[0]
     
     self.assertEqual(art2.title, data['title'])
     self.assertEqual(art2.publication, data['publication'])
     self.assertEqual(art2.template, data['template'])
     self.assertEqual(art2.navigation_parent, node1.id)
     
     self.assertEqual(NavNode.objects.count(), 2)
     node2 = NavNode.objects.exclude(id=node1.id)[0]
     self.assertEqual(node2.content_object, art2)
     self.assertEqual(node2.parent, node1)
Beispiel #2
0
 def test_new_article_navigation(self):
     article_class = get_article_class()
     
     tree = get_navtree_class().objects.create()
     
     self._log_as_editor()
     data = {
         'title': "Un titre",
         'publication': BaseArticle.PUBLISHED,
         'template': get_article_templates(None, self.user)[0][0],
         'navigation_parent': -tree.id,
         'sites': [settings.SITE_ID]
     }
     
     response = self.client.post(reverse('coop_cms_new_article'), data=data, follow=True)
     self.assertEqual(response.status_code, 200)
     
     self.assertEqual(article_class.objects.count(), 1)
     article = article_class.objects.all()[0]
     
     self.assertEqual(article.title, data['title'])
     self.assertEqual(article.publication, data['publication'])
     self.assertEqual(article.template, data['template'])
     self.assertEqual(article.navigation_parent, -tree.id)
     
     self.assertEqual(NavNode.objects.count(), 1)
     node = NavNode.objects.all()[0]
     self.assertEqual(node.content_object, article)
     self.assertEqual(node.parent, None)
     self.assertEqual(node.tree, tree)
    def test_new_article_navigation(self):
        article_class = get_article_class()

        tree = get_navtree_class().objects.create()

        self._log_as_editor()
        data = {
            'title': "Un titre",
            'publication': BaseArticle.PUBLISHED,
            'template': get_article_templates(None, self.user)[0][0],
            'navigation_parent': -tree.id,
            'sites': [settings.SITE_ID]
        }

        response = self.client.post(reverse('coop_cms_new_article'),
                                    data=data,
                                    follow=True)
        self.assertEqual(response.status_code, 200)

        self.assertEqual(article_class.objects.count(), 1)
        article = article_class.objects.all()[0]

        self.assertEqual(article.title, data['title'])
        self.assertEqual(article.publication, data['publication'])
        self.assertEqual(article.template, data['template'])
        self.assertEqual(article.navigation_parent, -tree.id)

        self.assertEqual(NavNode.objects.count(), 1)
        node = NavNode.objects.all()[0]
        self.assertEqual(node.content_object, article)
        self.assertEqual(node.parent, None)
        self.assertEqual(node.tree, tree)
Beispiel #4
0
def get_node_choices():
    """used for node selection in article settings form"""
    prefix = "--"
    choices = [(None, _('<not in navigation>'))]
    for tree in get_navtree_class().objects.all():
        choices.append((-tree.id, tree.name))
        for root_node in NavNode.objects.filter(tree=tree, parent__isnull=True).order_by('ordering'):
            for (progeny, level) in root_node.get_progeny():
                choices.append((progeny.id, prefix * (level + 1) + progeny.label))
    return choices
Beispiel #5
0
    def setUp(self):
        super(CmsEditTagTest, self).setUp()
        self.user = None
        current_site = Site.objects.get_current()

        self.link1 = Link.objects.create(url='http://www.google.fr')
        self.link1.sites.add(current_site)
        self.link1.save()

        self.tree = tree = get_navtree_class().objects.create()
        NavNode.objects.create(tree=tree, label=self.link1.url, content_object=self.link1, ordering=1, parent=None)
Beispiel #6
0
def get_node_choices():
    """used for node selection in article settings form"""
    prefix = "--"
    choices = [(None, _('<not in navigation>'))]
    for tree in get_navtree_class().objects.all():
        choices.append((-tree.id, tree.name))
        for root_node in NavNode.objects.filter(
                tree=tree, parent__isnull=True).order_by('ordering'):
            for (progeny, level) in root_node.get_progeny():
                choices.append(
                    (progeny.id, prefix * (level + 1) + progeny.label))
    return choices
    def setUp(self):
        super(CmsEditTagTest, self).setUp()
        self.user = None
        current_site = Site.objects.get_current()

        self.link1 = Link.objects.create(url='http://www.google.fr')
        self.link1.sites.add(current_site)
        self.link1.save()

        self.tree = tree = get_navtree_class().objects.create()
        NavNode.objects.create(tree=tree,
                               label=self.link1.url,
                               content_object=self.link1,
                               ordering=1,
                               parent=None)
Beispiel #8
0
def django_admin_navtree(request, context):
    """show menu"""
    if request and request.user.is_staff:
        coop_cms_navtrees = context.get('coop_cms_navtrees', None)
        if coop_cms_navtrees:
            tree_class = get_navtree_class()
            admin_tree_name = "{0}_{1}".format(get_model_app(tree_class), get_model_name(tree_class))
            if len(coop_cms_navtrees) == 1:
                tree = coop_cms_navtrees[0]
                url = reverse('admin:{0}_change'.format(admin_tree_name), args=[tree.id])
                label = _('Navigation tree')
            else:
                url = reverse('admin:{0}_changelist'.format(admin_tree_name))
                label = _('Navigation trees')
            return make_link(
                url, label, 'fugue/leaf-plant.png',
                classes=['icon', 'alert_on_click']
            )
    def resolve_kwargs(self, context):
        """resolve: get value from context or from string"""
        kwargs = {}
        for (key, value) in self._kwargs.items():
            try:
                kwargs[key] = value.resolve(context)
            except VariableDoesNotExist:
                kwargs[key] = value.var  # if the variable can not be resolved, take the value as is

        if not 'tree' in kwargs:
            kwargs['tree'] = 'default'

        tree = get_navtree_class().objects.get_or_create(name=kwargs['tree'])[0]
        if 'coop_cms_navtrees' in context.dicts[0]:
            context.dicts[0]['coop_cms_navtrees'].append(tree)
        else:
            context.dicts[0]['coop_cms_navtrees'] = [tree]

        return kwargs
Beispiel #10
0
def process_nav_edition(request, tree_id):
    """This handle ajax request sent by the tree component"""
    if request.method == 'POST' and request.is_ajax() and 'msg_id' in request.POST:
        try:
            # Get the current tree
            tree_class = get_navtree_class()
            tree = get_object_or_404(tree_class, id=tree_id)

            # check permissions
            perm_name = "{0}.change_{1}".format(get_model_app(tree_class), get_model_name(tree_class))
            if not request.user.has_perm(perm_name):
                raise PermissionDenied

            functions = (
                view_navnode, rename_navnode, remove_navnode, move_navnode,
                add_navnode, get_suggest_list, navnode_in_navigation,
            )
            supported_msg = {}
            # create a map between message name and handler
            # use the function name as message id
            for fct in functions:
                supported_msg[fct.__name__] = fct

            # Call the handler corresponding to the requested message
            response = supported_msg[request.POST['msg_id']](request, tree)

            # If no exception raise: Success
            response['status'] = 'success'
            response.setdefault('message', 'Ok')  # if no message defined in response, add something

        except KeyError as msg:
            response = {'status': 'error', 'message': "Unsupported message : {0}".format(msg)}
        except PermissionDenied:
            response = {'status': 'error', 'message': "You are not allowed to add a node"}
        except ValidationError as ex:
            response = {'status': 'error', 'message': ' - '.join(ex.messages)}
        except Exception as msg:
            logger.exception("process_nav_edition")
            response = {'status': 'error', 'message': "An error occured : {0}".format(msg)}

        # return the result as json object
        return HttpResponse(json.dumps(response), content_type='application/json')
    raise Http404
def django_admin_navtree(request, context):
    """show menu"""
    if request and request.user.is_staff:
        coop_cms_navtrees = context.get('coop_cms_navtrees', None)
        if coop_cms_navtrees:
            tree_class = get_navtree_class()
            admin_tree_name = "{0}_{1}".format(get_model_app(tree_class),
                                               get_model_name(tree_class))
            if len(coop_cms_navtrees) == 1:
                tree = coop_cms_navtrees[0]
                url = reverse('admin:{0}_change'.format(admin_tree_name),
                              args=[tree.id])
                label = _('Navigation tree')
            else:
                url = reverse('admin:{0}_changelist'.format(admin_tree_name))
                label = _('Navigation trees')
            return make_link(url,
                             label,
                             'leaf',
                             classes=['icon', 'alert_on_click'])
    def test_new_article_navigation_leaf(self):
        initial_data = {
            'title': "test",
            'content': "this is my article content"
        }
        article_class = get_article_class()
        art1 = article_class.objects.create(publication=BaseArticle.PUBLISHED,
                                            **initial_data)

        tree = get_navtree_class().objects.create()
        ct = ContentType.objects.get_for_model(article_class)
        node1 = NavNode.objects.create(content_type=ct,
                                       object_id=art1.id,
                                       tree=tree,
                                       parent=None)

        self._log_as_editor()
        data = {
            'title': "Un titre",
            'publication': BaseArticle.PUBLISHED,
            'template': get_article_templates(None, self.user)[0][0],
            'navigation_parent': node1.id,
            'sites': [settings.SITE_ID]
        }

        response = self.client.post(reverse('coop_cms_new_article'),
                                    data=data,
                                    follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(article_class.objects.exclude(id=art1.id).count(), 1)
        art2 = article_class.objects.exclude(id=art1.id)[0]

        self.assertEqual(art2.title, data['title'])
        self.assertEqual(art2.publication, data['publication'])
        self.assertEqual(art2.template, data['template'])
        self.assertEqual(art2.navigation_parent, node1.id)

        self.assertEqual(NavNode.objects.count(), 2)
        node2 = NavNode.objects.exclude(id=node1.id)[0]
        self.assertEqual(node2.content_object, art2)
        self.assertEqual(node2.parent, node1)
Beispiel #13
0
def process_nav_edition(request, tree_id):
    """This handle ajax request sent by the tree component"""
    if request.method == 'POST' and request.is_ajax(
    ) and 'msg_id' in request.POST:
        try:
            # Get the current tree
            tree_class = get_navtree_class()
            tree = get_object_or_404(tree_class, id=tree_id)

            # check permissions
            perm_name = "{0}.change_{1}".format(get_model_app(tree_class),
                                                get_model_name(tree_class))
            if not request.user.has_perm(perm_name):
                raise PermissionDenied

            functions = (
                view_navnode,
                rename_navnode,
                remove_navnode,
                move_navnode,
                add_navnode,
                get_suggest_list,
                navnode_in_navigation,
            )
            supported_msg = {}
            # create a map between message name and handler
            # use the function name as message id
            for fct in functions:
                supported_msg[fct.__name__] = fct

            # Call the handler corresponding to the requested message
            response = supported_msg[request.POST['msg_id']](request, tree)

            # If no exception raise: Success
            response['status'] = 'success'
            response.setdefault(
                'message',
                'Ok')  # if no message defined in response, add something

        except KeyError as msg:
            response = {
                'status': 'error',
                'message': "Unsupported message : {0}".format(msg)
            }
        except PermissionDenied:
            response = {
                'status': 'error',
                'message': "You are not allowed to add a node"
            }
        except ValidationError as ex:
            response = {'status': 'error', 'message': ' - '.join(ex.messages)}
        except Exception as msg:
            logger.exception("process_nav_edition")
            response = {
                'status': 'error',
                'message': "An error occured : {0}".format(msg)
            }

        # return the result as json object
        return HttpResponse(json.dumps(response),
                            content_type='application/json')
    raise Http404
Beispiel #14
0
    def change_view(self, request, object_id, extra_context=None, *args, **kwargs):
        """override the change view"""
        extra_context = extra_context or {}
        try:
            object_id = int(object_id)
        except ValueError:
            #if the object_id is not a valid number, returns 404
            raise Http404
        tree = models.get_navtree_class().objects.get(id=object_id)
        extra_context['navtree'] = tree
        extra_context['navtree_nodes'] = self.nodes_li(tree)
        return super(NavTreeAdmin, self).change_view(
            request, str(object_id), extra_context=extra_context, *args, **kwargs
        )  # pylint: disable=E1002

if get_navtree_class():
    admin.site.register(get_navtree_class(), NavTreeAdmin)


class ArticleAdmin(BASE_ADMIN_CLASS):
    """Article admin"""
    form = ArticleAdminForm
    list_display = [
        'slug', 'title', 'category', 'template_name', 'publication', 'headline', 'in_newsletter', 'modified',
        'login_required', 'is_homepage'
    ]
    list_editable = ['publication', 'headline', 'in_newsletter', 'category']
    readonly_fields = ['created', 'modified', 'is_homepage']
    list_filter = [
        'publication', 'login_required', 'headline', 'in_newsletter', 'sites',
        'category', 'template'
Beispiel #15
0
    def test_article_settings(self, move_nav=False):
        initial_data = {'title': "test", 'content': "this is my article content"}
        article_class = get_article_class()
        art0 = mommy.make(article_class, slug="art0" )
        
        art1 = article_class.objects.create(publication=BaseArticle.PUBLISHED, **initial_data)
        
        tree = get_navtree_class().objects.create()
        ct = ContentType.objects.get_for_model(article_class)
        node1 = NavNode.objects.create(content_type=ct, object_id=art0.id, tree=tree, parent=None)
        node2 = NavNode.objects.create(content_type=ct, object_id=art0.id, tree=tree, parent=None)
        
        category = mommy.make(ArticleCategory)
        
        self._log_as_editor()
        data = {
            'template': get_article_templates(None, self.user)[0][0],
            'category': category.id,
            'publication': BaseArticle.PUBLISHED,
            'publication_date': "2013-01-01 12:00:00",
            'headline': True,
            'in_newsletter': True,
            'summary': 'short summary',
            'navigation_parent': node1.id,
            'sites': [settings.SITE_ID]
        }
        
        response = self.client.post(reverse('coop_cms_article_settings', args=[art1.id]), data=data, follow=True)
        self.assertEqual(response.status_code, 200)
        
        self.assertEqual(article_class.objects.exclude(id__in=(art1.id, art0.id)).count(), 0)
        art1 = article_class.objects.get(id=art1.id)
        
        self.assertEqual(art1.title, initial_data['title'])
        self.assertEqual(art1.publication, data['publication'])
        self.assertEqual(art1.navigation_parent, node1.id)
        self.assertEqual(art1.publication_date, make_dt(datetime(2013, 1, 1, 12, 0, 0)))
        self.assertEqual(art1.headline, data['headline'])
        self.assertEqual(art1.in_newsletter, data['in_newsletter'])
        self.assertEqual(art1.summary, data['summary'])
        self.assertEqual(art1.template, data['template'])
        self.assertEqual([a.id for a in art1.sites.all()], data['sites'])
        self.assertEqual(NavNode.objects.count(), 3)
        node = NavNode.objects.exclude(id__in=(node1.id, node2.id))[0]
        self.assertEqual(node.content_object, art1)
        self.assertEqual(node.parent, node1)
        
        # Update the article
        category2 = mommy.make(ArticleCategory)
        
        node_id = node2.id if move_nav else node1.id
        
        data = {
            'template': get_article_templates(None, self.user)[1][0],
            'category': category2.id,
            'publication': BaseArticle.DRAFT,
            'publication_date': "2013-01-01 18:00:00",
            'headline': False,
            'in_newsletter': False,
            'summary': 'another summary',
            'navigation_parent': node_id,
            'sites': [settings.SITE_ID]
        }
        
        response = self.client.post(reverse('coop_cms_article_settings', args=[art1.id]), data=data, follow=True)
        self.assertEqual(response.status_code, 200)
        
        self.assertEqual(article_class.objects.exclude(id__in=(art1.id, art0.id)).count(), 0)
        art1 = article_class.objects.get(id=art1.id)
        
        self.assertEqual(art1.title, initial_data['title'])
        self.assertEqual(art1.publication, data['publication'])
        self.assertEqual(art1.template, data['template'])
        self.assertEqual(art1.publication_date, make_dt(datetime(2013, 1, 1, 18, 0, 0)))
        self.assertEqual(art1.headline, data['headline'])
        self.assertEqual(art1.in_newsletter, data['in_newsletter'])
        self.assertEqual(art1.summary, data['summary'])

        if move_nav:
            self.assertEqual(NavNode.objects.count(), 4)
            node = NavNode.objects.exclude(id__in=(node1.id, node2.id, node.id))[0]
            self.assertEqual(node.content_object, art1)
            self.assertEqual(node.parent, node2)
        else:
            self.assertEqual(NavNode.objects.count(), 3)
            node = NavNode.objects.exclude(id__in=(node1.id, node2.id))[0]
            self.assertEqual(node.content_object, art1)
            self.assertEqual(node.parent, node1)
    def test_article_settings(self, move_nav=False):
        initial_data = {
            'title': "test",
            'content': "this is my article content"
        }
        article_class = get_article_class()
        art0 = mommy.make(article_class, slug="art0")

        art1 = article_class.objects.create(publication=BaseArticle.PUBLISHED,
                                            **initial_data)

        tree = get_navtree_class().objects.create()
        ct = ContentType.objects.get_for_model(article_class)
        node1 = NavNode.objects.create(content_type=ct,
                                       object_id=art0.id,
                                       tree=tree,
                                       parent=None)
        node2 = NavNode.objects.create(content_type=ct,
                                       object_id=art0.id,
                                       tree=tree,
                                       parent=None)

        category = mommy.make(ArticleCategory)

        self._log_as_editor()
        data = {
            'template': get_article_templates(None, self.user)[0][0],
            'category': category.id,
            'publication': BaseArticle.PUBLISHED,
            'publication_date': "2013-01-01 12:00:00",
            'headline': True,
            'in_newsletter': True,
            'summary': 'short summary',
            'navigation_parent': node1.id,
            'sites': [settings.SITE_ID]
        }

        response = self.client.post(reverse('coop_cms_article_settings',
                                            args=[art1.id]),
                                    data=data,
                                    follow=True)
        self.assertEqual(response.status_code, 200)

        self.assertEqual(
            article_class.objects.exclude(id__in=(art1.id, art0.id)).count(),
            0)
        art1 = article_class.objects.get(id=art1.id)

        self.assertEqual(art1.title, initial_data['title'])
        self.assertEqual(art1.publication, data['publication'])
        self.assertEqual(art1.navigation_parent, node1.id)
        self.assertEqual(art1.publication_date,
                         make_dt(datetime(2013, 1, 1, 12, 0, 0)))
        self.assertEqual(art1.headline, data['headline'])
        self.assertEqual(art1.in_newsletter, data['in_newsletter'])
        self.assertEqual(art1.summary, data['summary'])
        self.assertEqual(art1.template, data['template'])
        self.assertEqual([a.id for a in art1.sites.all()], data['sites'])
        self.assertEqual(NavNode.objects.count(), 3)
        node = NavNode.objects.exclude(id__in=(node1.id, node2.id))[0]
        self.assertEqual(node.content_object, art1)
        self.assertEqual(node.parent, node1)

        # Update the article
        category2 = mommy.make(ArticleCategory)

        node_id = node2.id if move_nav else node1.id

        data = {
            'template': get_article_templates(None, self.user)[1][0],
            'category': category2.id,
            'publication': BaseArticle.DRAFT,
            'publication_date': "2013-01-01 18:00:00",
            'headline': False,
            'in_newsletter': False,
            'summary': 'another summary',
            'navigation_parent': node_id,
            'sites': [settings.SITE_ID]
        }

        response = self.client.post(reverse('coop_cms_article_settings',
                                            args=[art1.id]),
                                    data=data,
                                    follow=True)
        self.assertEqual(response.status_code, 200)

        self.assertEqual(
            article_class.objects.exclude(id__in=(art1.id, art0.id)).count(),
            0)
        art1 = article_class.objects.get(id=art1.id)

        self.assertEqual(art1.title, initial_data['title'])
        self.assertEqual(art1.publication, data['publication'])
        self.assertEqual(art1.template, data['template'])
        self.assertEqual(art1.publication_date,
                         make_dt(datetime(2013, 1, 1, 18, 0, 0)))
        self.assertEqual(art1.headline, data['headline'])
        self.assertEqual(art1.in_newsletter, data['in_newsletter'])
        self.assertEqual(art1.summary, data['summary'])

        if move_nav:
            self.assertEqual(NavNode.objects.count(), 4)
            node = NavNode.objects.exclude(id__in=(node1.id, node2.id,
                                                   node.id))[0]
            self.assertEqual(node.content_object, art1)
            self.assertEqual(node.parent, node2)
        else:
            self.assertEqual(NavNode.objects.count(), 3)
            node = NavNode.objects.exclude(id__in=(node1.id, node2.id))[0]
            self.assertEqual(node.content_object, art1)
            self.assertEqual(node.parent, node1)