From 8109a2a5c98f67465d60b4593ed6acb10c4d0c30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= Date: Fri, 8 Nov 2019 12:09:14 +0100 Subject: [PATCH] wcs: add a filter categories to current drafts cell (#37116) --- combo/apps/wcs/forms.py | 45 ++++++++++++++----- .../0018_wcscurrentdraftscell_categories.py | 20 +++++++++ combo/apps/wcs/models.py | 24 +++++++++- tests/test_wcs.py | 9 ++++ 4 files changed, 85 insertions(+), 13 deletions(-) create mode 100644 combo/apps/wcs/migrations/0018_wcscurrentdraftscell_categories.py diff --git a/combo/apps/wcs/forms.py b/combo/apps/wcs/forms.py index 1aee7146..120006db 100644 --- a/combo/apps/wcs/forms.py +++ b/combo/apps/wcs/forms.py @@ -23,7 +23,7 @@ from django.utils.translation import ugettext_lazy as _ from combo.utils.forms import MultiSortWidget from .models import (WcsFormCell, WcsCategoryCell, WcsFormsOfCategoryCell, - WcsCurrentFormsCell) + WcsCurrentFormsCell, WcsCurrentDraftsCell) from .utils import get_wcs_options, get_wcs_services @@ -83,13 +83,8 @@ class CategoriesSelectMultiple(forms.SelectMultiple): return data.get(name, None) -class WcsCurrentFormsCellForm(forms.ModelForm): - class Meta: - model = WcsCurrentFormsCell - fields = ['wcs_site', 'categories', 'current_forms', 'done_forms'] - - def __init__(self, *args, **kwargs): - super(WcsCurrentFormsCellForm, self).__init__(*args, **kwargs) +class WcsFormsMixin(object): + def _init_wcs_site(self): if len(get_wcs_services()) == 1: self.fields['wcs_site'].widget = forms.HiddenInput() else: @@ -97,9 +92,35 @@ class WcsCurrentFormsCellForm(forms.ModelForm): wcs_sites = [(x, y.get('title')) for x, y in get_wcs_services().items()] wcs_sites.sort(key=lambda x: x[1]) combo_wcs_sites.extend(wcs_sites) - self.fields['wcs_site'].widget = forms.Select(choices=combo_wcs_sites, - attrs={'class': 'wcs-site-select'}) + self.fields['wcs_site'].widget = forms.Select( + choices=combo_wcs_sites, + attrs={'class': 'wcs-site-select'}) + + def _init_categories(self): categories = get_wcs_options('/api/categories/') self.fields['categories'].help_text = _('By default forms from all categories are displayed.') - self.fields['categories'].widget = CategoriesSelectMultiple(choices=categories, - attrs={'class': 'categories-select'}) + self.fields['categories'].widget = CategoriesSelectMultiple( + choices=categories, + attrs={'class': 'categories-select'}) + + +class WcsCurrentFormsCellForm(WcsFormsMixin, forms.ModelForm): + class Meta: + model = WcsCurrentFormsCell + fields = ['wcs_site', 'categories', 'current_forms', 'done_forms'] + + def __init__(self, *args, **kwargs): + super(WcsCurrentFormsCellForm, self).__init__(*args, **kwargs) + self._init_wcs_site() + self._init_categories() + + +class WcsCurrentDraftsCellForm(WcsFormsMixin, forms.ModelForm): + class Meta: + model = WcsCurrentDraftsCell + fields = ['wcs_site', 'categories'] + + def __init__(self, *args, **kwargs): + super(WcsCurrentDraftsCellForm, self).__init__(*args, **kwargs) + self._init_wcs_site() + self._init_categories() diff --git a/combo/apps/wcs/migrations/0018_wcscurrentdraftscell_categories.py b/combo/apps/wcs/migrations/0018_wcscurrentdraftscell_categories.py new file mode 100644 index 00000000..277cb67d --- /dev/null +++ b/combo/apps/wcs/migrations/0018_wcscurrentdraftscell_categories.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations +import jsonfield.fields + + +class Migration(migrations.Migration): + + dependencies = [ + ('wcs', '0017_wcscareformscell'), + ] + + operations = [ + migrations.AddField( + model_name='wcscurrentdraftscell', + name='categories', + field=jsonfield.fields.JSONField(blank=True, default=dict, verbose_name='Categories'), + ), + ] diff --git a/combo/apps/wcs/models.py b/combo/apps/wcs/models.py index 755c9f54..7995fc12 100644 --- a/combo/apps/wcs/models.py +++ b/combo/apps/wcs/models.py @@ -387,9 +387,15 @@ class WcsCurrentDraftsCell(WcsUserDataBaseCell): variable_name = 'current_drafts' template_name = 'combo/wcs/current_drafts.html' + categories = JSONField(_('Categories'), blank=True) + class Meta: verbose_name = _('Current Drafts') + def get_default_form_class(self): + from .forms import WcsCurrentDraftsCellForm + return WcsCurrentDraftsCellForm + def get_api_url(self, context): user = self.get_concerned_user(context) if user: @@ -401,7 +407,22 @@ class WcsCurrentDraftsCell(WcsUserDataBaseCell): def get_cell_extra_context(self, context): context = super(WcsCurrentDraftsCell, self).get_cell_extra_context(context) - # regroup all drafts in a flat list + categories_filter = {} + if self.categories: + for category in self.categories.get('data', []): + categories_filter[tuple(category.split(':'))] = True + + for wcs_site in context['current_drafts']: + if not context['current_drafts'].get(wcs_site): + continue + if not context['current_drafts'][wcs_site].get('data'): + continue + forms = context['current_drafts'][wcs_site]['data'] + if categories_filter: + forms = [x for x in forms if (wcs_site, x.get('category_slug')) in categories_filter] + context['current_drafts'][wcs_site]['data'] = forms # put it back + + # regroup all forms in a flat list context['drafts'] = [] for wcs_site in context['current_drafts']: if not context['current_drafts'].get(wcs_site): @@ -412,6 +433,7 @@ class WcsCurrentDraftsCell(WcsUserDataBaseCell): return context + @register_cell_class class WcsFormsOfCategoryCell(WcsCommonCategoryCell, WcsBlurpMixin): ordering = models.CharField(_('Order'), max_length=20, diff --git a/tests/test_wcs.py b/tests/test_wcs.py index ec2d31d5..c539e632 100644 --- a/tests/test_wcs.py +++ b/tests/test_wcs.py @@ -644,6 +644,15 @@ def test_current_drafts_cell_render_logged_in(context): assert len([x for x in extra_context['drafts'] if x['site_slug'] == 'default']) == 1 assert len([x for x in extra_context['drafts'] if x['site_slug'] == 'other']) == 1 + # limit to a category + cell.categories = {'data': ['default:test-3']} + extra_context = cell.get_cell_extra_context(context) + assert len(extra_context['drafts']) == 1 + cell.categories = {'data': ['default:test-9']} + extra_context = cell.get_cell_extra_context(context) + assert len(extra_context['drafts']) == 0 + + @wcs_present def test_manager_forms_of_category_cell(app, admin_user): Page.objects.all().delete()