Commit 46b2a781 authored by Giorgos Korfiatis's avatar Giorgos Korfiatis

API calls for quotas and commissions

parent 2876f9e4
# Copyright 2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from django.utils import simplejson as json
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse
from synnefo.lib.db.transaction import commit_on_success_strict
from astakos.api.util import json_response
from astakos.im.api.user import api_method as user_api_method
from astakos.im.api.service import api_method as service_api_method
from astakos.im.api.faults import BadRequest, InternalServerError
from astakos.im.quotas import get_user_quotas
import astakos.quotaholder.exception as qh_exception
from astakos.quotaholder.callpoint import QuotaholderDjangoDBCallpoint
qh = QuotaholderDjangoDBCallpoint()
@user_api_method(http_method='GET', token_required=True)
def quotas(request, user=None):
result = get_user_quotas(user)
return json_response(result)
@csrf_exempt
def commissions(request):
method = request.method
if method == 'GET':
return get_pending_commissions(request)
elif method == 'POST':
return issue_commission(request)
else:
raise BadRequest('Method not allowed.')
@service_api_method(http_method='GET', token_required=True)
def get_pending_commissions(request):
data = request.GET
client_key = data['client_key']
result = qh.get_pending_commissions(clientkey=client_key)
return json_response(result)
@csrf_exempt
@service_api_method(http_method='POST', token_required=True)
def issue_commission(request):
data = request.raw_post_data
input_data = json.loads(data)
client_key = input_data['client_key']
provisions = input_data['provisions']
force = input_data.get('force', False)
auto_accept = input_data.get('auto_accept', False)
try:
result = _issue_commission(clientkey=client_key,
provisions=provisions,
force=force,
accept=auto_accept)
data = {"serial": result}
status_code = 201
except (qh_exception.NoCapacityError,
qh_exception.NoQuantityError) as e:
status_code = 413
body = {"message": e.message,
"code": status_code,
"data": e.data,
}
data = {"overLimit": body}
except qh_exception.NoHoldingError as e:
status_code = 404
body = {"message": e.message,
"code": status_code,
"data": e.data,
}
data = {"itemNotFound": body}
except qh_exception.InvalidDataError as e:
status_code = 400
body = {"message": e.message,
"code": status_code,
}
data = {"badRequest": body}
return json_response(data, status_code=status_code)
@commit_on_success_strict()
def _issue_commission(clientkey, provisions, force, accept):
serial = qh.issue_commission(clientkey=clientkey,
provisions=provisions,
force=force)
if accept:
done = qh.accept_commission(clientkey=clientkey,
serial=serial)
return serial
def failed_to_cloudfault(failed):
serial, reason = failed
if reason == 'NOTFOUND':
body = {"code": 404,
"message": "serial %s does not exist" % serial,
}
cloudfault = {"itemNotFound": body}
elif reason == 'CONFLICT':
body = {"code": 400,
"message": "cannot both accept and reject serial %s" % serial,
}
cloudfault = {"badRequest": body}
else:
raise InternalServerError('Unexpected error')
return (serial, cloudfault)
@csrf_exempt
@service_api_method(http_method='POST', token_required=True)
@commit_on_success_strict()
def resolve_pending_commissions(request):
data = request.raw_post_data
input_data = json.loads(data)
client_key = input_data['client_key']
accept = input_data.get('accept', [])
reject = input_data.get('reject', [])
result = qh.resolve_pending_commissions(clientkey=client_key,
accept_set=accept,
reject_set=reject)
accepted, rejected, failed = result
cloudfaults = [failed_to_cloudfault(f) for f in failed]
data = {'accepted': accepted,
'rejected': rejected,
'failed': cloudfaults
}
return json_response(data)
@service_api_method(http_method='GET', token_required=True)
def get_commission(request, serial):
data = request.GET
client_key = data['client_key']
serial = int(serial)
try:
data = qh.get_commission(clientkey=client_key,
serial=serial)
status_code = 200
return json_response(data, status_code)
except qh_exception.NoCommissionError as e:
return HttpResponse(status=404)
@csrf_exempt
@service_api_method(http_method='POST', token_required=True)
@commit_on_success_strict()
def serial_action(request, serial):
data = request.raw_post_data
input_data = json.loads(data)
serial = int(serial)
client_key = input_data['client_key']
accept = 'accept' in input_data
reject = 'reject' in input_data
if accept == reject:
raise BadRequest('Specify either accept or reject action.')
if accept:
result = qh.accept_commission(clientkey=client_key,
serial=serial)
else:
result = qh.reject_commission(clientkey=client_key,
serial=serial)
response = HttpResponse()
if not result:
response.status_code = 404
return response
# Copyright 2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from django.conf.urls.defaults import patterns, url
urlpatterns = patterns(
'astakos.api.quotas',
url(r'^quotas/?$', 'quotas'),
url(r'^commissions/?$', 'commissions'),
url(r'^commissions/action/?$', 'resolve_pending_commissions'),
url(r'^commissions/(?P<serial>\d+)/?$', 'get_commission'),
url(r'^commissions/(?P<serial>\d+)/action/?$', 'serial_action'),
)
# Copyright 2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from django.http import HttpResponse
from django.utils import simplejson as json
def json_response(content, status_code=None):
response = HttpResponse()
if status_code is not None:
response.status_code = status_code
response.content = json.dumps(content)
response['Content-Type'] = 'application/json; charset=UTF-8'
response['Content-Length'] = len(response.content)
return response
......@@ -35,6 +35,7 @@ from django.conf.urls.defaults import include, patterns
urlpatterns = patterns('',
(r'^im/', include('astakos.im.urls')),
(r'^astakos/api/', include('astakos.api.urls')),
(r'^login/?$', 'astakos.im.target.redirect.login'),
(r'^feedback/?$', 'astakos.im.api.user.send_feedback'),
(r'^user_catalogs/?$', 'astakos.im.api.user.get_uuid_displayname_catalogs'),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment