|
|
@ -13,6 +13,7 @@ from restapi.resource import Resource |
|
|
|
class ApiView(View): |
|
|
|
class ApiView(View): |
|
|
|
model = None |
|
|
|
model = None |
|
|
|
authentication_methods = [] |
|
|
|
authentication_methods = [] |
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, model): |
|
|
|
def __init__(self, model): |
|
|
|
self.start = time.time() |
|
|
|
self.start = time.time() |
|
|
|
self.model = model |
|
|
|
self.model = model |
|
|
@ -44,7 +45,12 @@ class ApiView(View): |
|
|
|
return {'error': str(e)}, '404 Not Found' |
|
|
|
return {'error': str(e)}, '404 Not Found' |
|
|
|
|
|
|
|
|
|
|
|
def get(self, *args, **kwargs): |
|
|
|
def get(self, *args, **kwargs): |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
TODO: check permissions |
|
|
|
|
|
|
|
:param args: |
|
|
|
|
|
|
|
:param kwargs: |
|
|
|
|
|
|
|
:return: |
|
|
|
|
|
|
|
""" |
|
|
|
if 'pk' in kwargs: |
|
|
|
if 'pk' in kwargs: |
|
|
|
try: |
|
|
|
try: |
|
|
|
count, data = self.resource.to_json(pk=kwargs.get('pk')) |
|
|
|
count, data = self.resource.to_json(pk=kwargs.get('pk')) |
|
|
@ -79,6 +85,13 @@ class ApiView(View): |
|
|
|
return jsonify(response), kwargs.get('code', 200) |
|
|
|
return jsonify(response), kwargs.get('code', 200) |
|
|
|
|
|
|
|
|
|
|
|
def post(self, *args, **kwargs): |
|
|
|
def post(self, *args, **kwargs): |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
TODO: check permissions |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
:param args: |
|
|
|
|
|
|
|
:param kwargs: |
|
|
|
|
|
|
|
:return: |
|
|
|
|
|
|
|
""" |
|
|
|
try: |
|
|
|
try: |
|
|
|
item = self.model(**request.json) |
|
|
|
item = self.model(**request.json) |
|
|
|
item.validate() |
|
|
|
item.validate() |
|
|
@ -97,10 +110,17 @@ class ApiView(View): |
|
|
|
return self.get(pk=data.id, code=201) |
|
|
|
return self.get(pk=data.id, code=201) |
|
|
|
|
|
|
|
|
|
|
|
def put(self, *args, **kwargs): |
|
|
|
def put(self, *args, **kwargs): |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
TODO: check permissions |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
:param args: |
|
|
|
|
|
|
|
:param kwargs: |
|
|
|
|
|
|
|
:return: |
|
|
|
|
|
|
|
""" |
|
|
|
if "pk" not in kwargs: |
|
|
|
if "pk" not in kwargs: |
|
|
|
return jsonify({ |
|
|
|
return jsonify({ |
|
|
|
'status': False, |
|
|
|
'status': False, |
|
|
|
"error":"Method not allowed" |
|
|
|
"error": "Method not allowed" |
|
|
|
}), 403 |
|
|
|
}), 403 |
|
|
|
else: |
|
|
|
else: |
|
|
|
try: |
|
|
|
try: |
|
|
@ -118,5 +138,17 @@ class ApiView(View): |
|
|
|
'errors': str(e) |
|
|
|
'errors': str(e) |
|
|
|
}), 400 |
|
|
|
}), 400 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def has_read_permission(self, request, qs): |
|
|
|
|
|
|
|
return qs |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def has_add_permission(self, request, obj): |
|
|
|
|
|
|
|
return True |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def has_change_permission(self, request, obj): |
|
|
|
|
|
|
|
return True |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def has_delete_permission(self, request, obj): |
|
|
|
|
|
|
|
return True |
|
|
|
|
|
|
|
|
|
|
|
def delete(self, *args, **kwargs): |
|
|
|
def delete(self, *args, **kwargs): |
|
|
|
"delete method" |
|
|
|
"delete method" |
|
|
|