Projet

Général

Profil

0001-api-newsletters-retrieval-endpoint-10794.patch

Voir les différences:

Subject: [PATCH 1/3] api: newsletters retrieval endpoint (#10794)

 corbo/api_urls.py  | 23 +++++++++++++++++++++++
 corbo/api_views.py | 32 ++++++++++++++++++++++++++++++++
 corbo/channels.py  | 13 +++++++++----
 corbo/settings.py  |  1 +
 corbo/urls.py      |  4 +++-
 jenkins.sh         | 13 +++++++++++++
 requirements.txt   |  1 +
 setup.py           |  3 ++-
 tests/conftest.py  |  9 +++++++++
 tests/test_api.py  | 45 +++++++++++++++++++++++++++++++++++++++++++++
 tox.ini            | 23 +++++++++++++++++++++++
 11 files changed, 161 insertions(+), 6 deletions(-)
 create mode 100644 corbo/api_urls.py
 create mode 100644 corbo/api_views.py
 create mode 100755 jenkins.sh
 create mode 100644 tests/conftest.py
 create mode 100644 tests/test_api.py
 create mode 100644 tox.ini
corbo/api_urls.py
1
# corbo - Announces Manager
2
# Copyright (C) 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, include, url
18

  
19
from .api_views import NewslettersView
20

  
21
urlpatterns = patterns('',
22
            url(r'^newsletters/', NewslettersView.as_view(), name='newsletters'),
23
)
corbo/api_views.py
1
# corbo - Announces Manager
2
# Copyright (C) 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 rest_framework.views import APIView
18
from rest_framework.response import Response
19

  
20
from .models import Category, Subscription
21
from .channels import get_channels
22

  
23

  
24
class NewslettersView(APIView):
25

  
26
    def get(self, request):
27
        newsletters = []
28
        for c in Category.objects.all():
29
            newsletter = {'id': str(c.pk), 'text': c.name,
30
                          'transports': get_channels()}
31
            newsletters.append(newsletter)
32
        return Response({'data': newsletters})
corbo/channels.py
9 9
        for identifier, display_name in channel.get_choices():
10 10
            yield (identifier, display_name)
11 11

  
12
def get_channels():
13
    return [{'id': c_id, 'text': unicode(c_name)}  for c_id, c_name in get_channel_choices()]
14

  
15

  
12 16
class HomepageChannel(object):
13 17
    identifier = 'homepage'
14 18

  
15 19
    @classmethod
16 20
    def get_choices(self):
17
        return (('homepage', _('Homepage')),)
21
        return ((self.identifier, _('Homepage')),)
18 22

  
19 23
class SMSChannel(object):
24
    identifier = 'mobile'
20 25

  
21 26
    @classmethod
22 27
    def get_choices(self):
23
        return (('sms', _('SMS')),)
28
        return ((self.identifier, _('SMS')),)
24 29

  
25 30
    def send(self, announce):
26 31
        pass
27 32

  
28 33
class EmailChannel(object):
29
    identifier = 'email'
34
    identifier = 'mail'
30 35

  
31 36
    @classmethod
32 37
    def get_choices(self):
33
        return (('email', _('Email')),)
38
        return ((self.identifier, _('Email')),)
34 39

  
35 40
    def send(self, announce):
36 41
        pass
corbo/settings.py
41 41
    'django.contrib.sessions',
42 42
    'django.contrib.messages',
43 43
    'django.contrib.staticfiles',
44
    'rest_framework',
44 45
)
45 46

  
46 47
MIDDLEWARE_CLASSES = (
corbo/urls.py
8 8
from .views import homepage, atom
9 9

  
10 10
from manage_urls import urlpatterns as manage_urls
11
from api_urls import urlpatterns as api_urls
11 12

  
12 13
urlpatterns = patterns('',
13 14
    url(r'^$', homepage, name='home'),
......
15 16
    url(r'^manage/', decorated_includes(manager_required,
16 17
                    include(manage_urls))),
17 18
    url(r'^ckeditor/', include('ckeditor.urls')),
18
    url(r'^admin/', include(admin.site.urls))
19
    url(r'^admin/', include(admin.site.urls)),
20
    url(r'^api/', include(api_urls))
19 21
)
20 22

  
21 23
if 'mellon' in settings.INSTALLED_APPS:
jenkins.sh
1
#!/bin/sh
2

  
3
set -e
4

  
5
rm -f coverage.xml
6
rm -f test_results.xml
7

  
8
pip install --upgrade tox
9
pip install --upgrade pylint pylint-django
10
tox -r
11
test -f pylint.out && cp pylint.out pylint.out.prev
12
(pylint -f parseable --rcfile /var/lib/jenkins/pylint.django.rc corbo/ | tee pylint.out) || /bin/true
13
test -f pylint.out.prev && (diff pylint.out.prev pylint.out | grep '^[><]' | grep .py) || /bin/true
requirements.txt
1 1
Django>=1.7, <1.8
2 2
django-ckeditor<4.5.3
3
djangorestframework
3 4
-e git+http://repos.entrouvert.org/gadjo.git/#egg=gadjo
setup.py
94 94
        'Programming Language :: Python :: 2',
95 95
    ],
96 96
    install_requires=['django>=1.7, <1.8',
97
        'django-ckeditor<4.5.3'
97
        'django-ckeditor<4.5.3',
98
        'djangorestframework',
98 99
        'gadjo'
99 100
        ],
100 101
    zip_safe=False,
tests/conftest.py
1
import pytest
2
import django_webtest
3

  
4
@pytest.fixture
5
def app(request):
6
    wtm = django_webtest.WebTestMixin()
7
    wtm._patch_settings()
8
    request.addfinalizer(wtm._unpatch_settings)
9
    return django_webtest.DjangoTestApp()
tests/test_api.py
1
import pytest
2
import json
3

  
4

  
5
from django.core.urlresolvers import reverse
6

  
7
from corbo.models import Category, Announce, Broadcast
8
from corbo.channels import get_channels
9

  
10
pytestmark = pytest.mark.django_db
11

  
12
CATEGORIES = ('Alerts', 'News')
13

  
14

  
15
@pytest.fixture
16
def categories():
17
    categories = []
18
    for category in CATEGORIES:
19
        c, created = Category.objects.get_or_create(name=category)
20
        categories.append(c)
21
    return categories
22

  
23
@pytest.fixture
24
def announces():
25
    announces = []
26
    for category in Category.objects.all():
27
        a = Announce.objects.create(category=category, title='By email')
28
        Broadcast.objects.create(announce=a, channel='mail')
29
        announces.append(a)
30
        a = Announce.objects.create(category=category, title='On homepage')
31
        Broadcast.objects.create(announce=a, channel='homepage')
32
        announces.append(a)
33
    return announces
34

  
35

  
36
def test_get_newsletters(app, categories, announces):
37
    resp = app.get(reverse('newsletters'), status=200)
38
    data = resp.json
39
    assert data['data']
40
    for category in data['data']:
41
        assert 'id' in category
42
        assert 'text' in category
43
        assert category['text'] in CATEGORIES
44
        assert 'transports' in category
45
        assert category['transports'] == get_channels()
tox.ini
1
[tox]
2
envlist = coverage-{django17,django18}
3

  
4
[testenv]
5
usedevelop =
6
  coverage: True
7
setenv =
8
  DJANGO_SETTINGS_MODULE=corbo.settings
9
  coverage: COVERAGE=--junitxml=test_results.xml --cov-report xml --cov=corbo/ --cov-config .coveragerc
10
deps =
11
  django17: django>1.7,<1.8
12
  django18: django>=1.8,<1.9
13
  pytest-cov
14
  pytest-django
15
  pytest
16
  pytest-capturelog
17
  django-webtest
18
  django-ckeditor<4.5.3
19
  djangorestframework
20
  pylint==1.4.0
21
  astroid==1.3.2
22
commands =
23
  py.test {env:COVERAGE:} {posargs:tests/}
0
-