Projet

Général

Profil

0001-wcs-search-for-tracking-code-on-multiple-sites-9320.patch

Frédéric Péters, 15 février 2018 10:16

Télécharger (9,29 ko)

Voir les différences:

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
combo/apps/wcs/__init__.py
21 21
    name = 'combo.apps.wcs'
22 22
    verbose_name = _('Forms')
23 23

  
24
    def get_before_urls(self):
25
        from . import urls
26
        return urls.urlpatterns
27

  
24 28
default_app_config = 'combo.apps.wcs.AppConfig'
combo/apps/wcs/templates/combo/wcs/tracking_code_input.html
1 1
{% load i18n %}
2 2
<div class="wcs-tracking-code-input">
3 3
 <h2>{% trans 'Tracking Code' %}</h2>
4
 <form data-wcs-url="{{ url }}">
4
 <form data-wcs-url="{{ url }}" method="post" action="{{ site_base }}{% url 'wcs-tracking-code' %}">
5 5
  <p>
6 6
    {% blocktrans %}
7 7
    A tracking code is attached to all your forms, it is there to help you in
......
9 9
    a tracking code, you can enter the code in the entry below:
10 10
    {% endblocktrans %}
11 11
  </p>
12
  <input id="tracking-code" placeholder="{% trans 'ex: CNPHNTFB' %}"/>
12
  <input id="_cell_url_{{ cell.id }}" name="url" value="" type="hidden"/>
13
  <input name="cell" value="{{ cell.id }}" type="hidden"/>
14
  <div id="_cell_error_{{ cell.id }}" class="errornotice" style="display: none">
15
    {% trans "The tracking code could not been found." %}
16
  </div>
17
  <input id="tracking-code" name="code" placeholder="{% trans 'ex: CNPHNTFB' %}"/>
13 18
  <button>{% trans 'Submit' %}</button>
14
 </form>
15
 <script type="text/javascript">
16
$(function() {
17
  $('.wcs-tracking-code-input form').on('submit', function() {
18
    var url = $(this).data('wcs-url');
19
    window.location = url + 'code/' + $(this).find('input').val() + '/load';
20
    return false;
19
  <script>
20
  $(function() {
21
    $('#_cell_url_{{ cell.id }}').val(window.location);
22
    if (window.location.search.indexOf('unknown-tracking-code') != -1) {
23
      $('#_cell_error_{{ cell.id }}').show();
24
    }
21 25
  });
22
});
23
 </script>
26
  </script>
27
 </form>
24 28
</div>
combo/apps/wcs/urls.py
1
# combo - content management system
2
# Copyright (C) 2014-2018  Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

  
17
from django.conf.urls import url
18

  
19
from .views import TrackingCodeView
20

  
21
urlpatterns = [
22
    url('^tracking-code/', TrackingCodeView.as_view(), name='wcs-tracking-code'),
23
]
combo/apps/wcs/views.py
1
# combo - content management system
2
# Copyright (C) 2014-2018  Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

  
17
import urlparse
18

  
19
from django.contrib import messages
20
from django.http import HttpResponseRedirect
21
from django.utils.translation import ugettext_lazy as _
22
from django.views.decorators.csrf import csrf_exempt
23
from django.views.generic import View
24

  
25
from .models import TrackingCodeInputCell
26
from .utils import get_wcs_services
27

  
28
from combo.utils import requests
29

  
30

  
31
class TrackingCodeView(View):
32
    http_method_names = ['post']
33

  
34
    @csrf_exempt
35
    def dispatch(self, *args, **kwargs):
36
        # CSRF check must be disabled as the cell may be distributed to other
37
        # sites in a skeleton.
38
        return super(TrackingCodeView, self).dispatch(*args, **kwargs)
39

  
40
    def post(self, request, *args, **kwargs):
41
        cell = TrackingCodeInputCell.objects.get(id=request.POST['cell'])
42
        code = request.POST['code']
43
        if cell.wcs_site:
44
            wcs_sites = [get_wcs_services().get(cell.wcs_site)]
45
        else:
46
            wcs_sites = get_wcs_services().values()
47

  
48
        for wcs_site in wcs_sites:
49
            response = requests.get('/api/code/' + code,
50
                    remote_service=wcs_site, log_errors=False)
51
            if response.status_code == 200 and response.json().get('err') == 0:
52
                url = response.json().get('load_url')
53
                return HttpResponseRedirect(url)
54

  
55
        next_url = request.POST.get('url') or '/'
56
        next_netloc = urlparse.urlparse(next_url).netloc
57
        if not (next_netloc and next_netloc != urlparse.urlparse(request.build_absolute_uri()).netloc):
58
            messages.error(self.request,
59
                    _(u'The tracking code could not been found.'))
60
        else:
61
            if '?' in next_url:
62
                next_url += '&'
63
            else:
64
                next_url += '?'
65
            next_url += 'unknown-tracking-code'
66

  
67
        return HttpResponseRedirect(next_url)
tests/test_wcs.py
16 16

  
17 17
from combo.data.models import Page
18 18
from combo.apps.wcs.models import (WcsFormCell, WcsCurrentFormsCell,
19
        WcsFormsOfCategoryCell, WcsCurrentDraftsCell, WcsCategoryCell)
19
        WcsFormsOfCategoryCell, WcsCurrentDraftsCell, WcsCategoryCell,
20
        TrackingCodeInputCell)
20 21

  
21 22
from combo.utils import NothingInCacheException
22 23

  
......
125 126
formdata.user_id = user.id
126 127
formdata.store()
127 128

  
129
if '127.0.0.2' in get_publisher().get_frontoffice_url():
130
    # create a tracking code on second website only
131
    code = get_publisher().tracking_code_class()
132
    code.id = 'CNPHNTFB'
133
    code.formdata = formdata
134

  
128 135
# a private formdef
129 136
role = Role(name='Blah')
130 137
role.store()
......
535 542
    finally:
536 543
        # restore original settings
537 544
        settings.KNOWN_SERVICES = temp_settings
545

  
546

  
547
@wcsctl_present
548
def test_tracking_code_cell(app):
549
    Page.objects.all().delete()
550
    page = Page(title='One', slug='index', template_name='standard')
551
    page.save()
552
    cell = TrackingCodeInputCell(page=page, placeholder='content', order=0)
553
    cell.save()
554

  
555
    resp = app.get('/')
556
    resp.form['code'] = 'FOOBAR'
557
    resp = resp.form.submit()
558
    assert resp.status_code == 302
559
    resp = resp.follow()
560
    assert '<li class="error">The tracking code could not been found.</li>' in resp.body
561

  
562
    resp = app.get('/')
563
    resp.form['code'] = 'CNPHNTFB'
564
    resp = resp.form.submit()
565
    assert resp.status_code == 302
566
    assert resp.location == 'http://127.0.0.2:8999/code/CNPHNTFB/load'
567

  
568
    # lock cell to a single site
569
    cell.wcs_site = 'default'
570
    cell.save()
571
    resp = app.get('/')
572
    resp.form['code'] = 'CNPHNTFB'
573
    resp = resp.form.submit()
574
    resp = resp.follow()
575
    assert '<li class="error">The tracking code could not been found.</li>' in resp.body
576

  
577
    # simulate cell being displayed on a different site
578
    resp = app.get('/')
579
    resp.form['url'] = 'http://example.net/'
580
    resp.form['code'] = 'CNPHNTFB'
581
    resp = resp.form.submit()
582
    assert resp.location == 'http://example.net/?unknown-tracking-code'
583

  
584
    resp = app.get('/')
585
    resp.form['url'] = 'http://example.net/?foo=bar'
586
    resp.form['code'] = 'CNPHNTFB'
587
    resp = resp.form.submit()
588
    assert resp.location == 'http://example.net/?foo=bar&unknown-tracking-code'
538
-