From 292083cb17c36f3c15874b833e5a9990b598a34c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Thu, 15 Feb 2018 09:23:05 +0100 Subject: [PATCH] wcs: search for tracking code on multiple sites (#9320) --- combo/apps/wcs/__init__.py | 4 ++ .../templates/combo/wcs/tracking_code_input.html | 26 +++++---- combo/apps/wcs/urls.py | 23 ++++++++ combo/apps/wcs/views.py | 67 ++++++++++++++++++++++ tests/test_wcs.py | 53 ++++++++++++++++- 5 files changed, 161 insertions(+), 12 deletions(-) create mode 100644 combo/apps/wcs/urls.py create mode 100644 combo/apps/wcs/views.py diff --git a/combo/apps/wcs/__init__.py b/combo/apps/wcs/__init__.py index 60e584c..9b19e61 100644 --- a/combo/apps/wcs/__init__.py +++ b/combo/apps/wcs/__init__.py @@ -21,4 +21,8 @@ class AppConfig(django.apps.AppConfig): name = 'combo.apps.wcs' verbose_name = _('Forms') + def get_before_urls(self): + from . import urls + return urls.urlpatterns + default_app_config = 'combo.apps.wcs.AppConfig' diff --git a/combo/apps/wcs/templates/combo/wcs/tracking_code_input.html b/combo/apps/wcs/templates/combo/wcs/tracking_code_input.html index cdbe73e..bf93aaa 100644 --- a/combo/apps/wcs/templates/combo/wcs/tracking_code_input.html +++ b/combo/apps/wcs/templates/combo/wcs/tracking_code_input.html @@ -1,7 +1,7 @@ {% load i18n %}

{% trans 'Tracking Code' %}

-
+

{% blocktrans %} A tracking code is attached to all your forms, it is there to help you in @@ -9,16 +9,20 @@ a tracking code, you can enter the code in the entry below: {% endblocktrans %}

- + + + + -
- + +
diff --git a/combo/apps/wcs/urls.py b/combo/apps/wcs/urls.py new file mode 100644 index 0000000..7c78727 --- /dev/null +++ b/combo/apps/wcs/urls.py @@ -0,0 +1,23 @@ +# combo - content management system +# Copyright (C) 2014-2018 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from django.conf.urls import url + +from .views import TrackingCodeView + +urlpatterns = [ + url('^tracking-code/', TrackingCodeView.as_view(), name='wcs-tracking-code'), +] diff --git a/combo/apps/wcs/views.py b/combo/apps/wcs/views.py new file mode 100644 index 0000000..10d2522 --- /dev/null +++ b/combo/apps/wcs/views.py @@ -0,0 +1,67 @@ +# combo - content management system +# Copyright (C) 2014-2018 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +import urlparse + +from django.contrib import messages +from django.http import HttpResponseRedirect +from django.utils.translation import ugettext_lazy as _ +from django.views.decorators.csrf import csrf_exempt +from django.views.generic import View + +from .models import TrackingCodeInputCell +from .utils import get_wcs_services + +from combo.utils import requests + + +class TrackingCodeView(View): + http_method_names = ['post'] + + @csrf_exempt + def dispatch(self, *args, **kwargs): + # CSRF check must be disabled as the cell may be distributed to other + # sites in a skeleton. + return super(TrackingCodeView, self).dispatch(*args, **kwargs) + + def post(self, request, *args, **kwargs): + cell = TrackingCodeInputCell.objects.get(id=request.POST['cell']) + code = request.POST['code'] + if cell.wcs_site: + wcs_sites = [get_wcs_services().get(cell.wcs_site)] + else: + wcs_sites = get_wcs_services().values() + + for wcs_site in wcs_sites: + response = requests.get('/api/code/' + code, + remote_service=wcs_site, log_errors=False) + if response.status_code == 200 and response.json().get('err') == 0: + url = response.json().get('load_url') + return HttpResponseRedirect(url) + + next_url = request.POST.get('url') or '/' + next_netloc = urlparse.urlparse(next_url).netloc + if not (next_netloc and next_netloc != urlparse.urlparse(request.build_absolute_uri()).netloc): + messages.error(self.request, + _(u'The tracking code could not been found.')) + else: + if '?' in next_url: + next_url += '&' + else: + next_url += '?' + next_url += 'unknown-tracking-code' + + return HttpResponseRedirect(next_url) diff --git a/tests/test_wcs.py b/tests/test_wcs.py index 081acff..0a6bc7a 100644 --- a/tests/test_wcs.py +++ b/tests/test_wcs.py @@ -16,7 +16,8 @@ from django.test.client import RequestFactory from combo.data.models import Page from combo.apps.wcs.models import (WcsFormCell, WcsCurrentFormsCell, - WcsFormsOfCategoryCell, WcsCurrentDraftsCell, WcsCategoryCell) + WcsFormsOfCategoryCell, WcsCurrentDraftsCell, WcsCategoryCell, + TrackingCodeInputCell) from combo.utils import NothingInCacheException @@ -125,6 +126,12 @@ formdata.receipt_time = datetime.datetime(2015, 1, 1).timetuple() formdata.user_id = user.id formdata.store() +if '127.0.0.2' in get_publisher().get_frontoffice_url(): + # create a tracking code on second website only + code = get_publisher().tracking_code_class() + code.id = 'CNPHNTFB' + code.formdata = formdata + # a private formdef role = Role(name='Blah') role.store() @@ -535,3 +542,47 @@ def test_manager_current_forms(app, admin_user): finally: # restore original settings settings.KNOWN_SERVICES = temp_settings + + +@wcsctl_present +def test_tracking_code_cell(app): + Page.objects.all().delete() + page = Page(title='One', slug='index', template_name='standard') + page.save() + cell = TrackingCodeInputCell(page=page, placeholder='content', order=0) + cell.save() + + resp = app.get('/') + resp.form['code'] = 'FOOBAR' + resp = resp.form.submit() + assert resp.status_code == 302 + resp = resp.follow() + assert '
  • The tracking code could not been found.
  • ' in resp.body + + resp = app.get('/') + resp.form['code'] = 'CNPHNTFB' + resp = resp.form.submit() + assert resp.status_code == 302 + assert resp.location == 'http://127.0.0.2:8999/code/CNPHNTFB/load' + + # lock cell to a single site + cell.wcs_site = 'default' + cell.save() + resp = app.get('/') + resp.form['code'] = 'CNPHNTFB' + resp = resp.form.submit() + resp = resp.follow() + assert '
  • The tracking code could not been found.
  • ' in resp.body + + # simulate cell being displayed on a different site + resp = app.get('/') + resp.form['url'] = 'http://example.net/' + resp.form['code'] = 'CNPHNTFB' + resp = resp.form.submit() + assert resp.location == 'http://example.net/?unknown-tracking-code' + + resp = app.get('/') + resp.form['url'] = 'http://example.net/?foo=bar' + resp.form['code'] = 'CNPHNTFB' + resp = resp.form.submit() + assert resp.location == 'http://example.net/?foo=bar&unknown-tracking-code' -- 2.16.1