wcs: search for tracking code on multiple sites (#9320)

This commit is contained in:
Frédéric Péters 2018-02-15 09:23:05 +01:00
parent f959056b73
commit 6c34aa524a
5 changed files with 161 additions and 12 deletions

View File

@ -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'

View File

@ -1,7 +1,7 @@
{% load i18n %}
<div class="wcs-tracking-code-input">
<h2>{% trans 'Tracking Code' %}</h2>
<form data-wcs-url="{{ url }}">
<form data-wcs-url="{{ url }}" method="post" action="{{ site_base }}{% url 'wcs-tracking-code' %}">
<p>
{% 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 %}
</p>
<input id="tracking-code" placeholder="{% trans 'ex: CNPHNTFB' %}"/>
<input id="_cell_url_{{ cell.id }}" name="url" value="" type="hidden"/>
<input name="cell" value="{{ cell.id }}" type="hidden"/>
<div id="_cell_error_{{ cell.id }}" class="errornotice" style="display: none">
{% trans "The tracking code could not been found." %}
</div>
<input id="tracking-code" name="code" placeholder="{% trans 'ex: CNPHNTFB' %}"/>
<button>{% trans 'Submit' %}</button>
</form>
<script type="text/javascript">
$(function() {
$('.wcs-tracking-code-input form').on('submit', function() {
var url = $(this).data('wcs-url');
window.location = url + 'code/' + $(this).find('input').val() + '/load';
return false;
<script>
$(function() {
$('#_cell_url_{{ cell.id }}').val(window.location);
if (window.location.search.indexOf('unknown-tracking-code') != -1) {
$('#_cell_error_{{ cell.id }}').show();
}
});
});
</script>
</script>
</form>
</div>

23
combo/apps/wcs/urls.py Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
from django.conf.urls import url
from .views import TrackingCodeView
urlpatterns = [
url('^tracking-code/', TrackingCodeView.as_view(), name='wcs-tracking-code'),
]

67
combo/apps/wcs/views.py Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
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)

View File

@ -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 '<li class="error">The tracking code could not been found.</li>' 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 '<li class="error">The tracking code could not been found.</li>' 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'