Обновление API Utils для работы с медиа
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.http import HttpResponse, HttpResponseBadRequest
|
||||
from .api_utils import *
|
||||
from .api_params import *
|
||||
from .models import *
|
||||
@@ -547,7 +548,7 @@ class ApiOrder:
|
||||
returns="ID созданного заказа, иначе одну из ошибок")
|
||||
async def create(**kwargs):
|
||||
access_token = kwargs.pop('access_token')
|
||||
ApiOrder._check_modify_permissions(access_token)
|
||||
ApiOrder._check_write_permissions(access_token)
|
||||
|
||||
city = await City.get_by_code(kwargs.pop('address_city'))
|
||||
|
||||
@@ -567,7 +568,7 @@ class ApiOrder:
|
||||
],
|
||||
returns="Обновленный объект заказа")
|
||||
async def set_published(access_token, order_id, value):
|
||||
ApiOrder._check_modify_permissions(access_token)
|
||||
ApiOrder._check_write_permissions(access_token)
|
||||
query = Order.objects.filter(id=order_id)
|
||||
order = await query.afirst()
|
||||
if order.owner_id != access_token.user.id:
|
||||
@@ -627,16 +628,35 @@ class ApiOrder:
|
||||
return api_make_response([ApiOrder._order_to_json(item) async for item in query.all()])
|
||||
|
||||
@staticmethod
|
||||
def _check_modify_permissions(access_token):
|
||||
def _check_write_permissions(access_token):
|
||||
if not access_token.user.is_completed():
|
||||
raise Exception(API_ERROR_NEED_COMPLETED_ACCOUNT)
|
||||
if access_token.user.role != Account.ROLE_CUSTOMER:
|
||||
raise Exception(API_ERROR_NOT_ALLOWED, 'you must be a customer')
|
||||
|
||||
|
||||
async def api_call_method(method_name, params: dict):
|
||||
class ApiMedia:
|
||||
# поскольку media.upload это не совсем стандартная функция, обернем фейковый метод чтоб была документация
|
||||
@staticmethod
|
||||
@api_method("media.upload",
|
||||
doc="Загрузка медиа на сервер. Вызывать методом POST.",
|
||||
params=[
|
||||
ApiRequestParam(),
|
||||
ApiParamAccessToken(),
|
||||
ApiParamStr(name="filename", description="Название файла",
|
||||
min_length=5, max_length=60),
|
||||
], returns="<code>id</code> медиа, в противном случае ошибку")
|
||||
def upload(request, access_token, filename):
|
||||
# ну шож, метод фейковый, все проверки нужно сделать руками
|
||||
if request.method != "POST":
|
||||
return make_error_object(Exception(API_ERROR_INVALID_REQUEST, "method must be executed http POST method"))
|
||||
|
||||
return HttpResponse("Да пошел ты нах со своим аплоадом")
|
||||
|
||||
|
||||
async def api_call_method(request, method_name, params: dict):
|
||||
if method_name in api_methods_dict:
|
||||
return await api_methods_dict[method_name]["func"](**params)
|
||||
return await api_methods_dict[method_name]["func"](__raw_request=request, **params)
|
||||
else:
|
||||
return make_error_object(Exception(API_ERROR_METHOD_NOT_FOUND))
|
||||
|
||||
@@ -644,10 +664,16 @@ async def api_call_method(method_name, params: dict):
|
||||
def api_get_documentation():
|
||||
out = []
|
||||
for m in api_methods_dict:
|
||||
params = []
|
||||
for p in api_methods_dict[m]["params"]:
|
||||
j = p.to_json()
|
||||
if j is not None:
|
||||
params.append(j)
|
||||
|
||||
out.append({
|
||||
"name": m,
|
||||
"doc": api_methods_dict[m]["doc"],
|
||||
"returns": api_methods_dict[m]["returns"],
|
||||
"params": [p.to_json() for p in api_methods_dict[m]["params"]]
|
||||
"params": params
|
||||
})
|
||||
return out
|
||||
|
||||
Reference in New Issue
Block a user