2015-02-08 15:14:58 +01:00
|
|
|
# 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/>.
|
2015-02-08 11:32:29 +01:00
|
|
|
|
2015-02-08 16:04:03 +01:00
|
|
|
from decimal import Decimal
|
2015-02-08 15:14:58 +01:00
|
|
|
import json
|
|
|
|
|
2015-02-08 16:04:03 +01:00
|
|
|
from django.contrib.auth.models import User
|
2015-02-08 15:14:58 +01:00
|
|
|
from django.http import HttpResponse
|
2015-02-08 16:04:03 +01:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
from django.views.generic import View, ListView
|
2015-02-08 15:14:58 +01:00
|
|
|
|
2015-02-08 16:04:03 +01:00
|
|
|
from .models import Regie, BasketItem
|
2015-02-08 15:14:58 +01:00
|
|
|
|
|
|
|
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
|
2015-02-08 16:04:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
class AddBasketItemApiView(View):
|
|
|
|
http_method_names = ['post', 'options']
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
|
return super(AddBasketItemApiView, self).dispatch(*args, **kwargs)
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
item = BasketItem()
|
|
|
|
item.amount = Decimal(request.GET.get('amount'))
|
|
|
|
|
|
|
|
# XXX: fill with real values
|
|
|
|
item.user = User.objects.all()[0]
|
|
|
|
item.regie = Regie.objects.all()[0]
|
|
|
|
item.subject = 'foo'
|
|
|
|
item.source_url = 'http://www.example.net'
|
|
|
|
|
|
|
|
item.save()
|
|
|
|
response = HttpResponse(content_type='application/json')
|
|
|
|
response.write(json.dumps({'result': 'success'}))
|
|
|
|
return response
|