0001-wcs-add-a-filter-categories-to-current-drafts-cell-3.patch
combo/apps/wcs/forms.py | ||
---|---|---|
23 | 23 |
from combo.utils.forms import MultiSortWidget |
24 | 24 | |
25 | 25 |
from .models import (WcsFormCell, WcsCategoryCell, WcsFormsOfCategoryCell, |
26 |
WcsCurrentFormsCell) |
|
26 |
WcsCurrentFormsCell, WcsCurrentDraftsCell)
|
|
27 | 27 |
from .utils import get_wcs_options, get_wcs_services |
28 | 28 | |
29 | 29 | |
... | ... | |
83 | 83 |
return data.get(name, None) |
84 | 84 | |
85 | 85 | |
86 |
class WcsCurrentFormsCellForm(forms.ModelForm): |
|
87 |
class Meta: |
|
88 |
model = WcsCurrentFormsCell |
|
89 |
fields = ['wcs_site', 'categories', 'current_forms', 'done_forms'] |
|
90 | ||
91 |
def __init__(self, *args, **kwargs): |
|
92 |
super(WcsCurrentFormsCellForm, self).__init__(*args, **kwargs) |
|
86 |
class WcsFormsMixin(object): |
|
87 |
def _init_wcs_site(self): |
|
93 | 88 |
if len(get_wcs_services()) == 1: |
94 | 89 |
self.fields['wcs_site'].widget = forms.HiddenInput() |
95 | 90 |
else: |
... | ... | |
97 | 92 |
wcs_sites = [(x, y.get('title')) for x, y in get_wcs_services().items()] |
98 | 93 |
wcs_sites.sort(key=lambda x: x[1]) |
99 | 94 |
combo_wcs_sites.extend(wcs_sites) |
100 |
self.fields['wcs_site'].widget = forms.Select(choices=combo_wcs_sites, |
|
101 |
attrs={'class': 'wcs-site-select'}) |
|
95 |
self.fields['wcs_site'].widget = forms.Select( |
|
96 |
choices=combo_wcs_sites, |
|
97 |
attrs={'class': 'wcs-site-select'}) |
|
98 | ||
99 |
def _init_categories(self): |
|
102 | 100 |
categories = get_wcs_options('/api/categories/') |
103 | 101 |
self.fields['categories'].help_text = _('By default forms from all categories are displayed.') |
104 |
self.fields['categories'].widget = CategoriesSelectMultiple(choices=categories, |
|
105 |
attrs={'class': 'categories-select'}) |
|
102 |
self.fields['categories'].widget = CategoriesSelectMultiple( |
|
103 |
choices=categories, |
|
104 |
attrs={'class': 'categories-select'}) |
|
105 | ||
106 | ||
107 |
class WcsCurrentFormsCellForm(WcsFormsMixin, forms.ModelForm): |
|
108 |
class Meta: |
|
109 |
model = WcsCurrentFormsCell |
|
110 |
fields = ['wcs_site', 'categories', 'current_forms', 'done_forms'] |
|
111 | ||
112 |
def __init__(self, *args, **kwargs): |
|
113 |
super(WcsCurrentFormsCellForm, self).__init__(*args, **kwargs) |
|
114 |
self._init_wcs_site() |
|
115 |
self._init_categories() |
|
116 | ||
117 | ||
118 |
class WcsCurrentDraftsCellForm(WcsFormsMixin, forms.ModelForm): |
|
119 |
class Meta: |
|
120 |
model = WcsCurrentDraftsCell |
|
121 |
fields = ['wcs_site', 'categories'] |
|
122 | ||
123 |
def __init__(self, *args, **kwargs): |
|
124 |
super(WcsCurrentDraftsCellForm, self).__init__(*args, **kwargs) |
|
125 |
self._init_wcs_site() |
|
126 |
self._init_categories() |
combo/apps/wcs/migrations/0018_wcscurrentdraftscell_categories.py | ||
---|---|---|
1 |
# -*- coding: utf-8 -*- |
|
2 |
from __future__ import unicode_literals |
|
3 | ||
4 |
from django.db import migrations |
|
5 |
import jsonfield.fields |
|
6 | ||
7 | ||
8 |
class Migration(migrations.Migration): |
|
9 | ||
10 |
dependencies = [ |
|
11 |
('wcs', '0017_wcscareformscell'), |
|
12 |
] |
|
13 | ||
14 |
operations = [ |
|
15 |
migrations.AddField( |
|
16 |
model_name='wcscurrentdraftscell', |
|
17 |
name='categories', |
|
18 |
field=jsonfield.fields.JSONField(blank=True, default=dict, verbose_name='Categories'), |
|
19 |
), |
|
20 |
] |
combo/apps/wcs/models.py | ||
---|---|---|
387 | 387 |
variable_name = 'current_drafts' |
388 | 388 |
template_name = 'combo/wcs/current_drafts.html' |
389 | 389 | |
390 |
categories = JSONField(_('Categories'), blank=True) |
|
391 | ||
390 | 392 |
class Meta: |
391 | 393 |
verbose_name = _('Current Drafts') |
392 | 394 | |
395 |
def get_default_form_class(self): |
|
396 |
from .forms import WcsCurrentDraftsCellForm |
|
397 |
return WcsCurrentDraftsCellForm |
|
398 | ||
393 | 399 |
def get_api_url(self, context): |
394 | 400 |
user = self.get_concerned_user(context) |
395 | 401 |
if user: |
... | ... | |
401 | 407 |
def get_cell_extra_context(self, context): |
402 | 408 |
context = super(WcsCurrentDraftsCell, self).get_cell_extra_context(context) |
403 | 409 | |
404 |
# regroup all drafts in a flat list |
|
410 |
categories_filter = {} |
|
411 |
if self.categories: |
|
412 |
for category in self.categories.get('data', []): |
|
413 |
categories_filter[tuple(category.split(':'))] = True |
|
414 | ||
415 |
for wcs_site in context['current_drafts']: |
|
416 |
if not context['current_drafts'].get(wcs_site): |
|
417 |
continue |
|
418 |
if not context['current_drafts'][wcs_site].get('data'): |
|
419 |
continue |
|
420 |
forms = context['current_drafts'][wcs_site]['data'] |
|
421 |
if categories_filter: |
|
422 |
forms = [x for x in forms if (wcs_site, x.get('category_slug')) in categories_filter] |
|
423 |
context['current_drafts'][wcs_site]['data'] = forms # put it back |
|
424 | ||
425 |
# regroup all forms in a flat list |
|
405 | 426 |
context['drafts'] = [] |
406 | 427 |
for wcs_site in context['current_drafts']: |
407 | 428 |
if not context['current_drafts'].get(wcs_site): |
... | ... | |
412 | 433 | |
413 | 434 |
return context |
414 | 435 | |
436 | ||
415 | 437 |
@register_cell_class |
416 | 438 |
class WcsFormsOfCategoryCell(WcsCommonCategoryCell, WcsBlurpMixin): |
417 | 439 |
ordering = models.CharField(_('Order'), max_length=20, |
tests/test_wcs.py | ||
---|---|---|
644 | 644 |
assert len([x for x in extra_context['drafts'] if x['site_slug'] == 'default']) == 1 |
645 | 645 |
assert len([x for x in extra_context['drafts'] if x['site_slug'] == 'other']) == 1 |
646 | 646 | |
647 |
# limit to a category |
|
648 |
cell.categories = {'data': ['default:test-3']} |
|
649 |
extra_context = cell.get_cell_extra_context(context) |
|
650 |
assert len(extra_context['drafts']) == 1 |
|
651 |
cell.categories = {'data': ['default:test-9']} |
|
652 |
extra_context = cell.get_cell_extra_context(context) |
|
653 |
assert len(extra_context['drafts']) == 0 |
|
654 | ||
655 | ||
647 | 656 |
@wcs_present |
648 | 657 |
def test_manager_forms_of_category_cell(app, admin_user): |
649 | 658 |
Page.objects.all().delete() |
650 |
- |