add api view to get list of regies

This commit is contained in:
Frédéric Péters 2015-02-08 15:14:58 +01:00
parent 86e3a5607c
commit 7337f37005
4 changed files with 65 additions and 2 deletions

View File

@ -17,3 +17,7 @@
class Plugin(object):
def get_apps(self):
return [__name__]
def get_before_urls(self):
from . import urls
return urls.urlpatterns

View File

@ -45,3 +45,8 @@ class Regie(models.Model):
def __unicode__(self):
return self.label
def as_api_dict(self):
return {'slug': self.slug,
'label': self.label,
'description': self.description}

23
lingo/urls.py Normal file
View File

@ -0,0 +1,23 @@
# lingo - basket and payment system
# Copyright (C) 2015 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, include, url
from .views import RegiesApiView
urlpatterns = patterns('',
url('^api/lingo/regies$', RegiesApiView.as_view(), name='api-regies'),
)

View File

@ -1,3 +1,34 @@
from django.shortcuts import render
# lingo - basket and payment system
# Copyright (C) 2015 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/>.
# Create your views here.
import json
from django.http import HttpResponse
from django.views.generic import ListView
from .models import Regie
class RegiesApiView(ListView):
model = Regie
def get(self, request, *args, **kwargs):
response = HttpResponse(content_type='application/json')
data = {'data': [x.as_api_dict() for x in self.get_queryset()]}
json_str = json.dumps(data)
if 'jsonpCallback' in request.GET:
json_str = '%s(%s);' % (request.GET['jsonpCallback'], json_str)
response.write(json_str)
return response