add /api/agenda/<agenda>/datetimes/ API endpoint

This commit is contained in:
Frédéric Péters 2016-02-13 16:31:14 +01:00
parent 349d4baaed
commit 2e25f21dd0
9 changed files with 103 additions and 0 deletions

View File

@ -4,6 +4,7 @@ recursive-include chrono/locale *.po *.mo
# static
# templates
recursive-include chrono/api/templates *.html
recursive-include chrono/manager/templates *.html
include COPYING README

0
chrono/api/__init__.py Normal file
View File

View File

@ -0,0 +1,6 @@
{% extends "rest_framework/base.html" %}
{% block title %}Chrono API{% endblock %}
{% block branding %}<a class='navbar-brand' rel="nofollow">Chrono API</a>{% endblock %}
{% block userlinks %}{% endblock %}

23
chrono/api/urls.py Normal file
View File

@ -0,0 +1,23 @@
# chrono - agendas system
# Copyright (C) 2016 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 patterns, url
from . import views
urlpatterns = patterns('',
url(r'agenda/(?P<pk>\w+)/datetimes/', views.datetimes),
)

32
chrono/api/views.py Normal file
View File

@ -0,0 +1,32 @@
# chrono - agendas system
# Copyright (C) 2016 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.utils.translation import ugettext as _
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
from ..agendas.models import Event
class Datetimes(GenericAPIView):
def get(self, request, pk=None, format=None):
response = {'data': [{'id': x.id,
'text': x.start_datetime.strftime(_('%Y-%m-%d %H:%M'))}
for x in Event.objects.filter(agenda=pk)]}
return Response(response)
datetimes = Datetimes.as_view()

View File

@ -54,7 +54,9 @@ INSTALLED_APPS = (
'django.contrib.staticfiles',
'gadjo',
'chrono.agendas',
'chrono.api',
'chrono.manager',
'rest_framework',
)
MIDDLEWARE_CLASSES = (

View File

@ -20,6 +20,7 @@ from django.conf.urls import patterns, include, url
from .urls_utils import decorated_includes, manager_required
from .views import homepage, login, logout
from .api.urls import urlpatterns as chrono_api_urls
from .manager.urls import urlpatterns as chrono_manager_urls
@ -27,6 +28,7 @@ urlpatterns = patterns('',
url(r'^$', homepage, name='home'),
url(r'^manage/', decorated_includes(manager_required,
include(chrono_manager_urls))),
url(r'^api/', include(chrono_api_urls)),
url(r'^logout/$', logout, name='auth_logout'),
url(r'^login/$', login, name='auth_login'),
)

View File

@ -104,6 +104,7 @@ setup(
],
install_requires=['django>=1.7, <1.8',
'gadjo',
'djangorestframework>=3.1',
],
zip_safe=False,
cmdclass={

36
tests/test_api.py Normal file
View File

@ -0,0 +1,36 @@
import datetime
import pytest
from webtest import TestApp
from chrono.wsgi import application
from chrono.agendas.models import Agenda, Event
pytestmark = pytest.mark.django_db
@pytest.fixture
def some_data():
agenda = Agenda(label=u'Foo bar')
agenda.save()
first_date = datetime.datetime.now().replace(hour=17, minute=0, second=0)
first_date += datetime.timedelta(days=1)
for i in range(3):
event = Event(start_datetime=first_date + datetime.timedelta(days=i),
places=20, agenda=agenda)
event.save()
agenda2 = Agenda(label=u'Foo bar2')
agenda2.save()
first_date = datetime.datetime.now().replace(hour=20, minute=0, second=0)
first_date += datetime.timedelta(days=1)
for i in range(2):
event = Event(start_datetime=first_date + datetime.timedelta(days=i),
places=20, agenda=agenda2)
event.save()
def test_datetimes_api(some_data):
app = TestApp(application)
resp = app.get('/api/agenda/1/datetimes/')
assert 'data' in resp.json
assert len(resp.json['data']) == 3