Projet

Général

Profil

0001-wcs-add-button-to-remove-draft-6936.patch

Frédéric Péters, 19 février 2016 17:09

Télécharger (7,8 ko)

Voir les différences:

Subject: [PATCH] wcs: add button to remove draft (#6936)

 combo/apps/wcs/__init__.py                         | 26 +++++++++++++++
 .../wcs/templates/combo/wcs/current_drafts.html    | 23 +++++++++++--
 combo/apps/wcs/urls.py                             | 25 ++++++++++++++
 combo/apps/wcs/views.py                            | 39 ++++++++++++++++++++++
 combo/utils.py                                     | 32 ++++++++++++++++++
 tests/test_wcs.py                                  |  2 +-
 6 files changed, 144 insertions(+), 3 deletions(-)
 create mode 100644 combo/apps/wcs/urls.py
 create mode 100644 combo/apps/wcs/views.py
combo/apps/wcs/__init__.py
1
# combo - content management system
2
# Copyright (C) 2014-2016  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 django.apps
18

  
19
class AppConfig(django.apps.AppConfig):
20
    name = 'combo.apps.wcs'
21

  
22
    def get_before_urls(self):
23
        from . import urls
24
        return urls.urlpatterns
25

  
26
default_app_config = 'combo.apps.wcs.AppConfig'
combo/apps/wcs/templates/combo/wcs/current_drafts.html
5 5
  {% if forms.data %}
6 6
  <ul>
7 7
    {% for data in forms.data %}
8
    {% if data.url and data.title and not data.form_status_is_endpoint %}
9
    <li><a href="{{ data.url }}">{{ data.title }}</a></li>
8
    {% if data.url and data.title %}
9
    <li><a href="{{ data.url }}">{{ data.title }}</a>
10
        <button class="remove-draft" data-formdata="{{ data.url }}"
11
                data-href="{% url 'combo-wcs-remove-draft' wcs_slug=slug %}">({% trans 'remove' %})</button>
12
    </li>
10 13
    {% endif %}
11 14
    {% endfor %}
12 15
  </ul>
13 16
  {% endif %}
14 17
</div>
15 18
{% endfor %}
19
<script>
20
$('button.remove-draft').on('click',
21
  function() {
22
    var $button = $(this);
23
    $.ajax({
24
      type: 'POST',
25
      url: $(this).data('href'),
26
      data: {'url': $(this).data('formdata')},
27
      dataType: 'json',
28
      success: function(data, status) {
29
        $button.parent().remove();
30
      }
31
    });
32
    return false;
33
});
34
</script>
combo/apps/wcs/urls.py
1
# combo - content management system
2
# Copyright (C) 2014-2016  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 patterns, url
18

  
19
from .views import remove_draft
20

  
21
urlpatterns = patterns('',
22
    url(r'^ajax/(?P<wcs_slug>[\w_-]+)/remove-draft$',
23
        remove_draft, name='combo-wcs-remove-draft'),
24
)
25

  
combo/apps/wcs/views.py
1
# combo - content management system
2
# Copyright (C) 2014-2016  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 json
18
import requests
19
import urlparse
20

  
21
from django.http import HttpResponse
22
from django.views.decorators.csrf import csrf_exempt
23

  
24
from .utils import get_wcs_services
25
from combo.utils import get_signed_service_url
26

  
27

  
28
@csrf_exempt
29
def remove_draft(request, wcs_slug):
30
    wcs_site = get_wcs_services().get(wcs_slug)
31
    formdata_ref = urlparse.urlparse(request.POST['url']).path
32
    formdef, form_id = formdata_ref.strip('/').split('/')
33
    url = get_signed_service_url(wcs_site, request,
34
            '/api/user/removedraft?formdef=%s&id=%s' % (formdef, form_id))
35
    r = requests.get(url)
36
    r.raise_for_status()
37
    response = HttpResponse(content_type='application/json')
38
    json.dump({'err': 0}, response, indent=2)
39
    return response
combo/utils.py
29 29
    pass
30 30

  
31 31

  
32
class AutoUser(object):
33
    pass
34

  
35
AUTO_USER = AutoUser()
36
AUTO_USER_EMAIL = AutoUser()
37
AUTO_USER_NAMEID = AutoUser()
38

  
39
def get_signed_service_url(site, request, path, user=AUTO_USER):
40
    query_params = {'orig': site.get('orig')}
41
    if isinstance(user, AutoUser):
42
        if request.user and request.user.is_authenticated():
43
            if request.session.get('mellon_session') and user is not AUTO_USER_EMAIL:
44
                mellon = request.session['mellon_session']
45
                query_params['NameID'] = mellon['name_id_content']
46
            elif user is not AUTO_USER_NAMEID:
47
                query_params['email'] = request.user.email
48
    elif user:
49
        # user must then be a dictionary
50
        query_params.update(user)
51

  
52
    url = site.get('url')
53
    scheme, netloc, old_path, params, old_query, fragment = urlparse.urlparse(url)
54
    query = urlencode(query_params)
55
    if '?' in path:
56
        path, old_query = path.split('?')
57
        query += '&' + old_query
58
    return urlparse.urlunparse((
59
            scheme, netloc, path, params,
60
            sign_query(query, key=site['secret']),
61
            fragment))
62

  
63

  
32 64
# Simple signature scheme for query strings
33 65

  
34 66
def sign_url(url, key, algo='sha256', timestamp=None, nonce=None):
tests/test_wcs.py
349 349
    # default is to get current forms from all wcs sites
350 350
    result = cell.render(context)
351 351
    assert 'http://127.0.0.1:8999/third-form-title/1' in result
352
    for url in re.findall('href="(.*)"', result):
352
    for url in re.findall(' href="(.*)"', result):
353 353
        check_wcs_open(url)
354
-