2015-06-26 07:59:53 +00:00
|
|
|
# coding=utf-8
|
2016-10-29 18:17:35 +00:00
|
|
|
import json
|
2015-12-22 16:31:43 +00:00
|
|
|
import logging
|
2016-10-29 18:17:35 +00:00
|
|
|
import random
|
2015-08-10 04:25:35 +00:00
|
|
|
|
2016-10-29 18:17:35 +00:00
|
|
|
from django.http import HttpResponse
|
|
|
|
from django.views.generic import View
|
2015-08-05 00:44:28 +00:00
|
|
|
|
2016-10-29 18:17:35 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2015-06-29 04:43:17 +00:00
|
|
|
|
2015-12-22 16:31:43 +00:00
|
|
|
|
2016-10-29 18:17:35 +00:00
|
|
|
def JSONResponse(data, content_type="application/json"):
|
|
|
|
resp = HttpResponse(json.dumps(data, indent=4), content_type=content_type)
|
|
|
|
resp.data = data
|
|
|
|
return resp
|
2015-12-22 16:31:43 +00:00
|
|
|
|
2015-08-14 02:22:32 +00:00
|
|
|
|
2016-10-29 18:17:35 +00:00
|
|
|
class APIView(View):
|
|
|
|
def _get_request_json(self, request):
|
|
|
|
if request.method != "GET":
|
|
|
|
body = request.body
|
|
|
|
if body:
|
|
|
|
return json.loads(body.decode("utf-8"))
|
|
|
|
return {}
|
|
|
|
return request.GET
|
2015-08-14 02:22:32 +00:00
|
|
|
|
2016-10-29 18:17:35 +00:00
|
|
|
def success(self, data=None):
|
|
|
|
return JSONResponse({"error": None, "data": data})
|
2015-06-29 04:43:17 +00:00
|
|
|
|
2016-10-30 15:29:22 +00:00
|
|
|
def error(self, message, error="error"):
|
|
|
|
return JSONResponse({"error": error, "data": message})
|
2015-06-29 04:43:17 +00:00
|
|
|
|
2016-10-29 18:17:35 +00:00
|
|
|
def invalid_serializer(self, serializer):
|
|
|
|
for k, v in serializer.errors.items():
|
2016-10-30 15:29:22 +00:00
|
|
|
return self.error(k + ": " + v[0], error="invalid-data-format")
|
2015-06-29 04:43:17 +00:00
|
|
|
|
2016-10-29 18:17:35 +00:00
|
|
|
def server_error(self):
|
|
|
|
return self.error("Server Error")
|
2015-06-29 04:43:17 +00:00
|
|
|
|
2016-10-29 18:17:35 +00:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
try:
|
|
|
|
request.data = self._get_request_json(self.request)
|
|
|
|
except ValueError:
|
|
|
|
return self.error("Invalid JSON")
|
|
|
|
try:
|
|
|
|
return super(APIView, self).dispatch(request, *args, **kwargs)
|
|
|
|
except Exception as e:
|
|
|
|
logging.exception(e)
|
|
|
|
return self.server_error()
|
2015-08-05 00:44:28 +00:00
|
|
|
|
|
|
|
|
2015-10-08 06:03:50 +00:00
|
|
|
def paginate_data(request, query_set, object_serializer):
|
2015-08-05 00:44:28 +00:00
|
|
|
"""
|
2016-10-30 16:29:00 +00:00
|
|
|
function used to paginate data
|
2015-08-05 00:44:28 +00:00
|
|
|
"""
|
|
|
|
need_paginate = request.GET.get("paging", None)
|
2016-10-30 16:29:00 +00:00
|
|
|
# if paging=true not in request.GET, then we return all data
|
2015-08-05 00:44:28 +00:00
|
|
|
if need_paginate != "true":
|
2015-10-08 06:03:50 +00:00
|
|
|
if object_serializer:
|
|
|
|
return object_serializer(query_set, many=True).data
|
|
|
|
else:
|
|
|
|
return query_set
|
2015-08-05 00:44:28 +00:00
|
|
|
|
|
|
|
try:
|
2016-10-30 16:06:47 +00:00
|
|
|
limit = int(request.GET.get("limit", "100"))
|
2016-10-30 16:18:17 +00:00
|
|
|
except ValueError:
|
|
|
|
limit = 100
|
|
|
|
if limit < 0:
|
2016-10-30 16:06:47 +00:00
|
|
|
limit = 100
|
2015-08-05 00:44:28 +00:00
|
|
|
|
|
|
|
try:
|
2016-10-30 16:18:17 +00:00
|
|
|
offset = int(request.GET.get("offset", "0"))
|
|
|
|
except ValueError:
|
|
|
|
offset = 0
|
|
|
|
if offset < 0:
|
|
|
|
offset = 0
|
|
|
|
|
|
|
|
results = query_set[offset:offset + limit]
|
|
|
|
if object_serializer:
|
|
|
|
count = query_set.count()
|
|
|
|
results = object_serializer(results, many=True).data
|
|
|
|
else:
|
|
|
|
count = len(query_set)
|
2015-08-05 00:44:28 +00:00
|
|
|
|
2016-10-30 16:06:47 +00:00
|
|
|
data = {"results": results,
|
|
|
|
"count": count}
|
2015-08-05 00:44:28 +00:00
|
|
|
|
2015-10-08 06:03:50 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
|
2016-10-29 18:17:35 +00:00
|
|
|
def rand_str(length=32, type="lower_hex"):
|
|
|
|
"""
|
2016-10-30 16:29:00 +00:00
|
|
|
generate types of random string or number with specific length
|
|
|
|
DO NOT USE TO GENERATE SECRET KEY!
|
2016-10-29 18:17:35 +00:00
|
|
|
"""
|
|
|
|
if type == "str":
|
|
|
|
return ''.join(random.choice("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") for i in range(length))
|
|
|
|
elif type == "lower_str":
|
|
|
|
return ''.join(random.choice("abcdefghijklmnopqrstuvwxyz0123456789") for i in range(length))
|
|
|
|
elif type == "lower_hex":
|
|
|
|
return ''.join(random.choice("0123456789abcdef") for i in range(length))
|
|
|
|
else:
|
|
|
|
return random.choice("123456789") + ''.join(random.choice("0123456789") for i in range(length - 1))
|