wcs: add a filter categories to current drafts cell (#37116)
This commit is contained in:
parent
be7053850f
commit
8109a2a5c9
|
@ -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()
|
||||
|
|
|
@ -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'),
|
||||
),
|
||||
]
|
|
@ -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,
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue