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 09:35

Télécharger (8,1 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   | 20 ++++----
 combo/apps/wcs/urls.py                             | 23 +++++++++
 combo/apps/wcs/views.py                            | 56 ++++++++++++++++++++++
 tests/test_wcs.py                                  | 40 +++++++++++++++-
 5 files changed, 131 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
  <input id="tracking-code" name="code" placeholder="{% trans 'ex: CNPHNTFB' %}"/>
13 15
  <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;
16
  <script>
17
  $(function() {
18
    $('#_cell_url_{{ cell.id }}').val(window.location);
21 19
  });
22
});
23
 </script>
20
  </script>
21
 </form>
24 22
</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
from django.contrib import messages
18
from django.http import HttpResponseRedirect
19
from django.utils.translation import ugettext_lazy as _
20
from django.views.decorators.csrf import csrf_exempt
21
from django.views.generic import View
22

  
23
from .models import TrackingCodeInputCell
24
from .utils import get_wcs_services
25

  
26
from combo.utils import requests
27

  
28

  
29
class TrackingCodeView(View):
30
    http_method_names = ['post']
31

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

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

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

  
53
        messages.error(self.request,
54
                _(u'This tracking code could not been found.'))
55

  
56
        return HttpResponseRedirect(request.POST.get('url') or '/')
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 'This tracking code could not been found.' 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 'This tracking code could not been found.' in resp.body
538
-