Skip to content

Commit

Permalink
Merge f2075c3 into 8aebf47
Browse files Browse the repository at this point in the history
  • Loading branch information
Anto59290 committed Jan 2, 2018
2 parents 8aebf47 + f2075c3 commit 22ef575
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 52 deletions.
4 changes: 2 additions & 2 deletions zds/utils/admin.py
Expand Up @@ -12,8 +12,8 @@ def parent_category(self, obj):
parent_category.admin_order_field = 'categorysubcategory__category'
parent_category.short_description = 'Parent category'

list_display = ('parent_category', 'title', 'subtitle')
ordering = ('categorysubcategory__category', 'title')
list_display = ('parent_category', 'title', 'subtitle', 'position')
ordering = ('categorysubcategory__category', 'position', 'title')


class AlertAdmin(admin.ModelAdmin):
Expand Down
24 changes: 24 additions & 0 deletions zds/utils/migrations/0019_auto_20180102_1659.py
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.8 on 2018-01-02 16:59
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('utils', '0018_auto_20171006_2126'),
]

operations = [
migrations.AlterModelOptions(
name='subcategory',
options={'ordering': ['position', 'title'], 'verbose_name': 'Sous-categorie', 'verbose_name_plural': 'Sous-categories'},
),
migrations.AddField(
model_name='subcategory',
name='position',
field=models.IntegerField(db_index=True, default=0, verbose_name='Position'),
),
]
1 change: 1 addition & 0 deletions zds/utils/models.py
Expand Up @@ -79,6 +79,7 @@ class Meta:

title = models.CharField('Titre', max_length=80, unique=True)
subtitle = models.CharField('Sous-titre', max_length=200)
position = models.IntegerField('Position', db_index=True, default=0)

image = models.ImageField(upload_to=image_path_category, blank=True, null=True)

Expand Down
49 changes: 48 additions & 1 deletion zds/utils/templatetags/tests/tests_topbar_tags.py
Expand Up @@ -9,10 +9,13 @@

from zds.forum.factories import CategoryFactory, ForumFactory, TopicFactory
from zds.member.factories import ProfileFactory, StaffProfileFactory
from zds.tutorialv2.factories import PublishedContentFactory
from zds.tutorialv2.factories import PublishedContentFactory, PublishableContentFactory, SubCategoryFactory
from zds.tutorialv2.publication_utils import publish_content
from zds.utils.factories import CategoryFactory as ContentCategoryFactory
from zds.utils.templatetags.topbar import top_categories, top_categories_content
from copy import deepcopy


overridden_zds_app = deepcopy(settings.ZDS_APP)
overridden_zds_app['content']['repo_private_path'] = os.path.join(settings.BASE_DIR, 'contents-private-test')
overridden_zds_app['content']['repo_public_path'] = os.path.join(settings.BASE_DIR, 'contents-public-test')
Expand Down Expand Up @@ -124,6 +127,50 @@ def test_top_tags_content(self):
self.assertEqual(list(top_tags_tuto), list(tags_tuto))
self.assertEqual(list(top_tags_article), list(tags_article))

def test_content_ordering(self):
category_1 = ContentCategoryFactory()
category_2 = ContentCategoryFactory()
subcategory_1 = SubCategoryFactory(category=category_1)
subcategory_1.position = 5
subcategory_1.save()
subcategory_2 = SubCategoryFactory(category=category_1)
subcategory_2.position = 1
subcategory_2.save()
subcategory_3 = SubCategoryFactory(category=category_2)

tuto_1 = PublishableContentFactory(type='TUTORIAL')
tuto_1.subcategory.add(subcategory_1)
tuto_1_draft = tuto_1.load_version()
publish_content(tuto_1, tuto_1_draft, is_major_update=True)

top_categories_tuto = top_categories_content('TUTORIAL').get('categories')
expected = [(subcategory_1.title, subcategory_1.slug, category_1.slug)]
self.assertEqual(top_categories_tuto[category_1.title], expected)

tuto_2 = PublishableContentFactory(type='TUTORIAL')
tuto_2.subcategory.add(subcategory_2)
tuto_2_draft = tuto_2.load_version()
publish_content(tuto_2, tuto_2_draft, is_major_update=True)

top_categories_tuto = top_categories_content('TUTORIAL').get('categories')
# New subcategory is now first is the list
expected.insert(0, (subcategory_2.title, subcategory_2.slug, category_1.slug))
self.assertEqual(top_categories_tuto[category_1.title], expected)

article_1 = PublishableContentFactory(type='TUTORIAL')
article_1.subcategory.add(subcategory_3)
article_1_draft = tuto_2.load_version()
publish_content(article_1, article_1_draft, is_major_update=True)

# New article has no impact
top_categories_tuto = top_categories_content('TUTORIAL').get('categories')
self.assertEqual(top_categories_tuto[category_1.title], expected)

top_categories_contents = top_categories_content(['TUTORIAL', 'ARTICLE']).get('categories')
expected_2 = [(subcategory_3.title, subcategory_3.slug, category_2.slug)]
self.assertEqual(top_categories_contents[category_1.title], expected)
self.assertEqual(top_categories_contents[category_2.title], expected_2)

def tearDown(self):

if os.path.isdir(overridden_zds_app['content']['repo_private_path']):
Expand Down
74 changes: 25 additions & 49 deletions zds/utils/templatetags/topbar.py
Expand Up @@ -5,48 +5,36 @@
from zds.forum.models import Forum
from zds.tutorialv2.models.database import PublishedContent
from zds.utils.models import CategorySubCategory, Tag
from django.db.models import Count
from django.db.models import Count, Q

register = template.Library()


@register.filter('top_categories')
def top_categories(user):
max_tags = settings.ZDS_APP['forum']['top_tag_max']

forums_pub = Forum.objects.filter(groups__isnull=True).select_related('category').distinct().all()
if user and user.is_authenticated():
forums_private = Forum\
.objects\
.filter(groups__isnull=False, groups__in=user.groups.all())\
.select_related('category').distinct().all()
forums = list(forums_pub | forums_private)
else:
forums = list(forums_pub)
forums = (Forum.objects
.filter(Q(groups__isnull=True) | Q(groups__isnull=False, groups__in=user.groups.all()))
.select_related('category')
.distinct()
.all())

cats = defaultdict(list)
forums_pk = []
for forum in forums:
forums_pk.append(forum.pk)
cats[forum.category.position].append(forum)

tags_by_popularity = list(
sorted_cats = sorted(cats)
topbar_cats = [(cats[cat][0].category.title, cats[cat]) for cat in sorted_cats]

excluded_tags = settings.ZDS_APP['forum']['top_tag_exclu']
tags_by_popularity = (
Tag.objects
.filter(topic__forum__in=forums)
.annotate(count_topic=Count('topic'))
.order_by('-count_topic')
.exclude(title__in=excluded_tags)
.order_by('-count_topic').all()[:max_tags]
)

topbar_cats = []
sorted_cats = sorted(cats)
for cat in sorted_cats:
forums = cats[cat]
title = forums[0].category.title
topbar_cats.append((title, forums))

tags = [tag for tag in tags_by_popularity if tag.title not in settings.ZDS_APP['forum']['top_tag_exclu']][:max_tags]

return {'tags': tags, 'categories': topbar_cats}
return {'tags': tags_by_popularity, 'categories': topbar_cats}


@register.filter('top_categories_content')
Expand All @@ -55,38 +43,26 @@ def top_categories_content(_type):
The result is sorted by alphabetic order.
:param _type: type of the content
:type _type: str
:type _type: str or list
:return: a dictionary, with the title being the name of the category and the content a list of subcategories,
Each of these are stored in a tuple of the form ``title, slug``.
:rtype: OrderedDict
"""
# get subcategories from PublishedContent
if _type:
if not isinstance(_type, list):
_type = [_type]

subcategories_contents = PublishedContent.objects\
.filter(must_redirect=False)\
.filter(content_type__in=_type)\
.values('content__subcategory').all()
else:
# used in page with all content types
subcategories_contents = PublishedContent.objects\
.values('content__subcategory').all()

# get tags from PublishedContent
if _type:
tags = PublishedContent.objects.get_top_tags(_type, limit=settings.ZDS_APP['forum']['top_tag_max'])
else:
tags = PublishedContent.objects.get_top_tags(['TUTORIAL', 'ARTICLE', 'OPINION'],
limit=settings.ZDS_APP['forum']['top_tag_max'])

_type = _type if isinstance(_type, list) else [_type]
tags = PublishedContent.objects.get_top_tags(_type, limit=settings.ZDS_APP['forum']['top_tag_max'])

subcategories_contents = (PublishedContent.objects
.filter(must_redirect=False)
.filter(content_type__in=_type)
.values('content__subcategory')
.all())

# get parent categories of subcategories from PublishedContent
categories_from_subcategories = CategorySubCategory.objects\
.filter(is_main=True)\
.filter(subcategory__in=subcategories_contents)\
.order_by('category__position', 'subcategory__title')\
.select_related('subcategory', 'category')\
.order_by('category__position', 'subcategory__position', 'subcategory__title')\
.values('category__title', 'category__slug', 'subcategory__title', 'subcategory__slug')\
.all()

Expand Down

0 comments on commit 22ef575

Please sign in to comment.