0001-wcs-add-option-to-limit-current-forms-cell-to-some-c.patch
combo/apps/wcs/forms.py | ||
---|---|---|
18 | 18 |
from django import forms |
19 | 19 |
from django.utils.datastructures import MultiValueDict |
20 | 20 |
from django.utils.safestring import mark_safe |
21 |
from django.utils.translation import ugettext_lazy as _ |
|
21 | 22 | |
22 |
from .models import WcsFormCell, WcsCategoryCell, WcsFormsOfCategoryCell |
|
23 |
from .utils import get_wcs_options |
|
23 |
from .models import (WcsFormCell, WcsCategoryCell, WcsFormsOfCategoryCell, |
|
24 |
WcsCurrentFormsCell) |
|
25 |
from .utils import get_wcs_options, get_wcs_services |
|
24 | 26 | |
25 | 27 |
class WcsFormCellForm(forms.ModelForm): |
26 | 28 |
class Meta: |
... | ... | |
98 | 100 |
self.fields['category_reference'].widget = forms.Select(choices=references, |
99 | 101 |
attrs={'class': 'category-select'}) |
100 | 102 |
self.fields['manual_order'].widget = MultiSortWidget(choices=formdef_references) |
103 | ||
104 | ||
105 |
class CategoriesSelectMultiple(forms.SelectMultiple): |
|
106 |
def format_value(self, value): |
|
107 |
# this converts data dictionary to list, for django >=1.11 |
|
108 |
return super(CategoriesSelectMultiple, self).format_value( |
|
109 |
value.get('data') or []) |
|
110 | ||
111 |
def render_options(self, choices, value): |
|
112 |
# this converts data dictionary to list, for django <1.11 |
|
113 |
value = value.get('data') or [] |
|
114 |
return super(CategoriesSelectMultiple, self).render_options(choices, value) |
|
115 | ||
116 |
def value_from_datadict(self, data, files, name): |
|
117 |
if isinstance(data, MultiValueDict): |
|
118 |
return {'data': data.getlist(name)} |
|
119 |
return data.get(name, None) |
|
120 | ||
121 | ||
122 |
class WcsCurrentFormsCellForm(forms.ModelForm): |
|
123 |
class Meta: |
|
124 |
model = WcsCurrentFormsCell |
|
125 |
fields = ['wcs_site', 'categories', 'current_forms', 'done_forms'] |
|
126 | ||
127 |
def __init__(self, *args, **kwargs): |
|
128 |
super(WcsCurrentFormsCellForm, self).__init__(*args, **kwargs) |
|
129 |
if len(get_wcs_services()) == 1: |
|
130 |
self.fields['wcs_site'].widget = forms.HiddenInput() |
|
131 |
else: |
|
132 |
combo_wcs_sites = [('', _('All'))] |
|
133 |
wcs_sites = [(x, y.get('title')) for x, y in get_wcs_services().items()] |
|
134 |
wcs_sites.sort(key=lambda x: x[1]) |
|
135 |
combo_wcs_sites.extend(wcs_sites) |
|
136 |
self.fields['wcs_site'].widget = forms.Select(choices=combo_wcs_sites, |
|
137 |
attrs={'class': 'wcs-site-select'}) |
|
138 |
categories = get_wcs_options('/api/categories/') |
|
139 |
self.fields['categories'].help_text = _('By default forms from all categories are displayed.') |
|
140 |
self.fields['categories'].widget = CategoriesSelectMultiple(choices=categories, |
|
141 |
attrs={'class': 'categories-select'}) |
combo/apps/wcs/models.py | ||
---|---|---|
263 | 263 |
api_url = '/api/user/forms' |
264 | 264 |
variable_name = 'user_forms' |
265 | 265 | |
266 |
categories = JSONField(blank=True) |
|
266 | 267 |
current_forms = models.BooleanField(_('Current Forms'), default=True) |
267 | 268 |
done_forms = models.BooleanField(_('Done Forms'), default=False) |
268 | 269 | |
269 | 270 |
class Meta: |
270 | 271 |
verbose_name = _('User Forms') |
271 | 272 | |
272 |
def get_form_fields(self): |
|
273 |
fields = super(WcsCurrentFormsCell, self).get_form_fields() |
|
274 |
fields.extend(['current_forms', 'done_forms']) |
|
275 |
return fields |
|
273 |
def get_default_form_class(self): |
|
274 |
from .forms import WcsCurrentFormsCellForm |
|
275 |
return WcsCurrentFormsCellForm |
|
276 | 276 | |
277 | 277 |
@property |
278 | 278 |
def template_name(self): |
... | ... | |
296 | 296 | |
297 | 297 |
def get_cell_extra_context(self, context): |
298 | 298 |
context = super(WcsCurrentFormsCell, self).get_cell_extra_context(context) |
299 | ||
300 |
categories_filter = {} |
|
301 |
if self.categories: |
|
302 |
for category in self.categories.get('data', []): |
|
303 |
categories_filter[tuple(category.split(':'))] = True |
|
304 | ||
299 | 305 |
if not (self.current_forms and self.done_forms): |
300 | 306 |
for wcs_site in context['user_forms']: |
301 | 307 |
if not context['user_forms'].get(wcs_site): |
302 | 308 |
continue |
303 | 309 |
if not context['user_forms'][wcs_site].get('data'): |
304 | 310 |
continue |
311 |
forms = context['user_forms'][wcs_site]['data'] |
|
312 |
if categories_filter: |
|
313 |
forms = [x for x in forms if (wcs_site, x.get('category_slug')) in categories_filter] |
|
305 | 314 |
if self.current_forms: |
306 |
context['user_forms'][wcs_site]['data'] = [x for x in |
|
307 |
context['user_forms'][wcs_site]['data'] if not x.get('form_status_is_endpoint')] |
|
315 |
forms = [x for x in forms if not x.get('form_status_is_endpoint')] |
|
308 | 316 |
elif self.done_forms: |
309 |
context['user_forms'][wcs_site]['data'] = [x for x in |
|
310 |
context['user_forms'][wcs_site]['data'] if x.get('form_status_is_endpoint')] |
|
317 |
forms = [x for x in forms if x.get('form_status_is_endpoint')] |
|
318 |
context['user_forms'][wcs_site]['data'] = forms # put it back |
|
319 | ||
311 | 320 |
context['current_forms'] = context['user_forms'] # legacy |
312 | 321 | |
313 | 322 |
# regroup all forms in a flat list |
tests/test_wcs.py | ||
---|---|---|
294 | 294 |
cell.done_forms = True |
295 | 295 |
assert cell.get_additional_label() == 'All Sites - Done Forms' |
296 | 296 | |
297 |
try: |
|
298 |
# check there is not wcs_site field if there's a single one defined in |
|
299 |
# the configuration |
|
300 |
temp_settings = settings.KNOWN_SERVICES.copy() |
|
301 |
default = settings.KNOWN_SERVICES['wcs']['default'] |
|
302 |
settings.KNOWN_SERVICES = {'wcs': {'default': default}} |
|
303 |
form_class = cell.get_default_form_class() |
|
304 |
form = form_class() |
|
305 |
assert not 'wcs_site' in form.fields.keys() |
|
306 |
assert cell.get_additional_label() == 'Done Forms' |
|
307 |
finally: |
|
308 |
# restore original settings |
|
309 |
settings.KNOWN_SERVICES = temp_settings |
|
310 | ||
311 | 297 |
@wcsctl_present |
312 | 298 |
def test_current_forms_cell_render(context): |
313 | 299 |
page = Page(title='xxx', slug='test_current_forms_cell_render', template_name='standard') |
... | ... | |
354 | 340 |
assert len([x for x in extra_context['forms'] if x['site_slug'] == 'default']) == 5 |
355 | 341 |
assert len([x for x in extra_context['forms'] if x['site_slug'] == 'other']) == 5 |
356 | 342 | |
343 |
# limit to a category |
|
344 |
cell.categories = {'data': ['default:test-3']} |
|
345 |
extra_context = cell.get_cell_extra_context(context) |
|
346 |
assert len(extra_context['forms']) == 0 |
|
347 |
cell.categories = {'data': ['default:test-9']} |
|
348 |
extra_context = cell.get_cell_extra_context(context) |
|
349 |
assert len(extra_context['forms']) == 5 |
|
350 | ||
357 | 351 |
@wcsctl_present |
358 | 352 |
def test_current_forms_cell_render_single_site(context): |
359 | 353 |
page = Page(title='xxx', slug='test_current_forms_cell_render', template_name='standard') |
... | ... | |
493 | 487 |
resp.forms[0]['c%s-ordering' % cells[0].get_reference()].value = 'manual' |
494 | 488 |
resp = resp.forms[0].submit() |
495 | 489 |
assert resp.status_int == 302 |
490 | ||
491 |
@wcsctl_present |
|
492 |
def test_manager_current_forms(app, admin_user): |
|
493 |
Page.objects.all().delete() |
|
494 |
page = Page(title='One', slug='one', template_name='standard') |
|
495 |
page.save() |
|
496 |
app = login(app) |
|
497 |
resp = app.get('/manage/pages/%s/' % page.id) |
|
498 |
resp = app.get(resp.html.find('option', |
|
499 |
**{'data-add-url': re.compile('wcscurrentformscell')})['data-add-url']) |
|
500 | ||
501 |
cells = page.get_cells() |
|
502 |
assert len(cells) == 1 |
|
503 |
assert isinstance(cells[0], WcsCurrentFormsCell) |
|
504 | ||
505 |
resp = app.get('/manage/pages/%s/' % page.id) |
|
506 |
assert ('data-cell-reference="%s"' % cells[0].get_reference()) in resp.body |
|
507 |
assert len(resp.form['c%s-categories' % cells[0].get_reference()].options) == 4 |
|
508 |
resp.form['c%s-categories' % cells[0].get_reference()].value = ['default:test-3', 'default:test-9'] |
|
509 |
resp = resp.form.submit().follow() |
|
510 |
assert resp.form['c%s-categories' % cells[0].get_reference()].value == ['default:test-3', 'default:test-9'] |
|
511 | ||
512 |
# check wcs_site field is a select box |
|
513 |
assert resp.form['c%s-wcs_site' % cells[0].get_reference()].tag == 'select' |
|
514 | ||
515 |
try: |
|
516 |
# check the wcs_site field is hidden if there's a single one defined in |
|
517 |
# the configuration |
|
518 |
temp_settings = settings.KNOWN_SERVICES.copy() |
|
519 |
default = settings.KNOWN_SERVICES['wcs']['default'] |
|
520 |
settings.KNOWN_SERVICES = {'wcs': {'default': default}} |
|
521 |
resp = app.get('/manage/pages/%s/' % page.id) |
|
522 |
assert resp.form['c%s-wcs_site' % cells[0].get_reference()].attrs['type'] == 'hidden' |
|
523 |
finally: |
|
524 |
# restore original settings |
|
525 |
settings.KNOWN_SERVICES = temp_settings |
|
496 |
- |