From 7c3cfbd89635407b9124f32d6f88806deb19cad9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Fri, 10 Aug 2018 11:31:50 +0200 Subject: [PATCH] wcs: add new cell for backoffice submission (#23536) --- .../0016_backofficesubmissioncell.py | 38 +++++++++++++++++++ combo/apps/wcs/models.py | 25 ++++++++++++ .../combo/wcs/backoffice_submission.html | 18 +++++++++ tests/test_wcs.py | 25 +++++++++++- 4 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 combo/apps/wcs/migrations/0016_backofficesubmissioncell.py create mode 100644 combo/apps/wcs/templates/combo/wcs/backoffice_submission.html diff --git a/combo/apps/wcs/migrations/0016_backofficesubmissioncell.py b/combo/apps/wcs/migrations/0016_backofficesubmissioncell.py new file mode 100644 index 0000000..4c545cf --- /dev/null +++ b/combo/apps/wcs/migrations/0016_backofficesubmissioncell.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.12 on 2018-08-10 08:05 +from __future__ import unicode_literals + +import combo.apps.wcs.models +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('data', '0035_page_related_cells'), + ('wcs', '0015_wcscurrentformscell_categories'), + ] + + operations = [ + migrations.CreateModel( + name='BackofficeSubmissionCell', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('placeholder', models.CharField(max_length=20)), + ('order', models.PositiveIntegerField()), + ('slug', models.SlugField(blank=True, verbose_name='Slug')), + ('extra_css_class', models.CharField(blank=True, max_length=100, verbose_name='Extra classes for CSS styling')), + ('public', models.BooleanField(default=True, verbose_name='Public')), + ('restricted_to_unlogged', models.BooleanField(default=False, verbose_name='Restrict to unlogged users')), + ('last_update_timestamp', models.DateTimeField(auto_now=True)), + ('wcs_site', models.CharField(blank=True, max_length=50, verbose_name='Site')), + ('groups', models.ManyToManyField(blank=True, to='auth.Group', verbose_name='Groups')), + ('page', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='data.Page')), + ], + options={ + 'verbose_name': 'Backoffice Submission', + }, + bases=(models.Model, combo.apps.wcs.models.WcsBlurpMixin), + ), + ] diff --git a/combo/apps/wcs/models.py b/combo/apps/wcs/models.py index 056613b..71f9681 100644 --- a/combo/apps/wcs/models.py +++ b/combo/apps/wcs/models.py @@ -503,3 +503,28 @@ class TrackingCodeInputCell(CellBase): self.wcs_site = list(get_wcs_services().keys())[0] extra_context['url'] = get_wcs_services().get(self.wcs_site).get('url') return extra_context + + +@register_cell_class +class BackofficeSubmissionCell(WcsDataBaseCell): + api_url = '/api/formdefs/?backoffice-submission=on' + variable_name = 'all_formdefs' + template_name = 'combo/wcs/backoffice_submission.html' + cache_duration = 600 + + class Meta: + verbose_name = _('Backoffice Submission') + + def get_concerned_user(self, context): + # always return connected user; the selected user will be used in the + # query string when creating a new formdata. + return getattr(context.get('request'), 'user', None) + + def get_cell_extra_context(self, context): + context = super(BackofficeSubmissionCell, self).get_cell_extra_context(context) + # add a fake category where it's missing + for site_formdefs in context['all_formdefs'].values(): + for formdef in site_formdefs['data']: + if not 'category' in formdef: + formdef['category'] = _('Misc') + return context diff --git a/combo/apps/wcs/templates/combo/wcs/backoffice_submission.html b/combo/apps/wcs/templates/combo/wcs/backoffice_submission.html new file mode 100644 index 0000000..51305f6 --- /dev/null +++ b/combo/apps/wcs/templates/combo/wcs/backoffice_submission.html @@ -0,0 +1,18 @@ +{% load i18n %} +{% block cell-content %} +

{% trans "New Form" %}

+{% for site_formdefs in all_formdefs.values %} + +{% endfor %} +{% endblock %} diff --git a/tests/test_wcs.py b/tests/test_wcs.py index db85cde..363271d 100644 --- a/tests/test_wcs.py +++ b/tests/test_wcs.py @@ -20,7 +20,7 @@ from django.utils.six.moves.urllib import parse as urlparse from combo.data.models import Page from combo.apps.wcs.models import (WcsFormCell, WcsCurrentFormsCell, WcsFormsOfCategoryCell, WcsCurrentDraftsCell, WcsCategoryCell, - TrackingCodeInputCell) + TrackingCodeInputCell, BackofficeSubmissionCell) from combo.utils import NothingInCacheException @@ -143,6 +143,7 @@ formdef = FormDef() formdef.name = 'a private form' formdef.category_id = cats[2].id formdef.roles = [role.id] +formdef.backoffice_submission_roles = [role.id] formdef.fields = [] formdef.store() @@ -652,3 +653,25 @@ def test_tracking_code_search(app): assert len(app.get('/api/search/tracking-code/?q=BBCCDDFF').json.get('data')) == 0 assert len(app.get('/api/search/tracking-code/?q=CNPHNTFB').json.get('data')) == 1 assert len(app.get('/api/search/tracking-code/?q=BBCCDDFFG').json.get('data')) == 0 + +@wcsctl_present +def test_backoffice_submission_cell_render(context): + page = Page(title='xxx', slug='test_backoffice_submission_cell_render', template_name='standard') + page.save() + cell = BackofficeSubmissionCell(page=page, placeholder='content', order=0) + cell.wcs_site = 'default' + cell.save() + + context['synchronous'] = True # to get fresh content + + result = cell.render(context) + assert '/backoffice/submission/a-private-form/' not in result + + class MockUser(object): + email = 'foo@example.net' + def is_authenticated(self): + return True + context['request'].user = MockUser() + + result = cell.render(context) + assert '/backoffice/submission/a-private-form/' in result -- 2.18.0