You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
184 lines
6.6 KiB
Python
184 lines
6.6 KiB
Python
import json
|
|
from django.http import JsonResponse
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
from api import api
|
|
from utils import decimal_to_float, format_token
|
|
|
|
@csrf_exempt
|
|
async def shop(request):
|
|
try:
|
|
products = None
|
|
if request.method == 'GET':
|
|
products = api.get_products()
|
|
user_agent = request.headers.get('User-Agent')
|
|
products = decimal_to_float(products)
|
|
print("get_shop", user_agent)
|
|
return JsonResponse({"success": products}, status=200)
|
|
except Exception as error:
|
|
return JsonResponse({"error": format(error)}, status=500)
|
|
|
|
@csrf_exempt
|
|
async def user(request):
|
|
try:
|
|
user1 = dict()
|
|
if request.method == 'POST':
|
|
|
|
if request.body:
|
|
body: dict = json.loads(request.body)
|
|
|
|
if body["register"]:
|
|
token = api.registration(body["register"]["nickname"],
|
|
body["register"]["password"],
|
|
body["register"]["email"])
|
|
elif body["login"]:
|
|
token = api.login(body["login"]["email"],
|
|
body["login"]["password"])
|
|
elif body["unregister"]:
|
|
token = format_token(request.headers.get("Authorization"))
|
|
api.unregister(token)
|
|
elif body["logout"]:
|
|
token = format_token(request.headers.get("Authorization"))
|
|
api.logout(token)
|
|
elif body["add_money"]:
|
|
token = format_token(request.headers.get("Authorization"))
|
|
api.add_money(token, body["add_money"]["money"])
|
|
else:
|
|
token = format_token(request.headers.get("Authorization"))
|
|
|
|
user1 = api.get_user(token)
|
|
|
|
return JsonResponse({"success": [user1]}, status=200)
|
|
except Exception as error:
|
|
return JsonResponse({"error": format(error)}, status=500)
|
|
|
|
|
|
@csrf_exempt
|
|
async def basket(request):
|
|
try:
|
|
basket1 = None
|
|
if request.method == 'POST':
|
|
token = format_token(request.headers.get("Authorization"))
|
|
|
|
if request.body:
|
|
body: dict = json.loads(request.body)
|
|
if body["add_product"]:
|
|
api.add_product_to_basket(token, body["add_product"]["product_id"])
|
|
elif body["delete_product"]:
|
|
api.delete_product_from_basket(token, body["delete_product"]["product_id"])
|
|
elif body["clear"]:
|
|
api.clear_basket(token)
|
|
elif body["buy_products"]:
|
|
api.buy_products(token)
|
|
|
|
products_id = api.get_products_id(token, "basket")
|
|
basket1 = api.get_products_by_id(products_id)
|
|
|
|
return JsonResponse({"success": basket1}, status=200)
|
|
except Exception as error:
|
|
return JsonResponse({"error": format(error)}, status=500)
|
|
|
|
@csrf_exempt
|
|
async def history(request):
|
|
try:
|
|
histories = None
|
|
if request.method == 'POST':
|
|
token = format_token(request.headers.get("Authorization"))
|
|
histories = api.get_histories_with_products(token)
|
|
return JsonResponse({"success": histories}, status=200)
|
|
except Exception as error:
|
|
return JsonResponse({"error": format(error)}, status=500)
|
|
|
|
@csrf_exempt
|
|
async def login(request):
|
|
try:
|
|
token = None
|
|
if request.method == 'POST':
|
|
body: dict = json.loads(request.body)
|
|
token = api.login(body["email"], body["password"])
|
|
return JsonResponse({"success": token}, status=200)
|
|
except Exception as error:
|
|
return JsonResponse({"error": format(error)}, status=500)
|
|
|
|
@csrf_exempt
|
|
async def logout(request):
|
|
try:
|
|
token = None
|
|
if request.method == 'POST':
|
|
token = format_token(request.headers.get("Authorization"))
|
|
api.logout(token)
|
|
return JsonResponse({"success": token}, status=200)
|
|
except Exception as error:
|
|
return JsonResponse({"error": format(error)}, status=500)
|
|
|
|
@csrf_exempt
|
|
async def register(request):
|
|
try:
|
|
token = None
|
|
if request.method == 'POST':
|
|
body: dict = json.loads(request.body)
|
|
token = api.registration(body["nickname"], body["password"], body["email"])
|
|
return JsonResponse({"success": token}, status=200)
|
|
except Exception as error:
|
|
return JsonResponse({"error": format(error)}, status=500)
|
|
|
|
@csrf_exempt
|
|
async def unregister(request):
|
|
try:
|
|
token = None
|
|
if request.method == 'POST':
|
|
token = format_token(request.headers.get("Authorization"))
|
|
api.unregister(token)
|
|
return JsonResponse({"success": token}, status=200)
|
|
except Exception as error:
|
|
return JsonResponse({"error": format(error)}, status=500)
|
|
|
|
@csrf_exempt
|
|
async def add_product_to_basket(request):
|
|
try:
|
|
token = None
|
|
if request.method == 'POST':
|
|
body: dict = json.loads(request.body)
|
|
token = format_token(request.headers.get("Authorization"))
|
|
api.add_product_to_basket(token, body["product_id"])
|
|
return JsonResponse({"success": token}, status=200)
|
|
except Exception as error:
|
|
return JsonResponse({"error": format(error)}, status=500)
|
|
|
|
@csrf_exempt
|
|
async def delete_product_from_basket(request):
|
|
try:
|
|
token = None
|
|
if request.method == 'POST':
|
|
body: dict = json.loads(request.body)
|
|
token = format_token(request.headers.get("Authorization"))
|
|
api.delete_product_from_basket(token, body["product_id"])
|
|
return JsonResponse({"success": token}, status=200)
|
|
except Exception as error:
|
|
return JsonResponse({"error": format(error)}, status=500)
|
|
|
|
@csrf_exempt
|
|
async def buy_products(request):
|
|
try:
|
|
token = None
|
|
if request.method == 'POST':
|
|
body: dict = json.loads(request.body)
|
|
token = format_token(request.headers.get("Authorization"))
|
|
api.buy_products(token)
|
|
return JsonResponse({"success": token}, status=200)
|
|
except Exception as error:
|
|
return JsonResponse({"error": format(error)}, status=500)
|
|
|
|
@csrf_exempt
|
|
async def clear_basket(request):
|
|
try:
|
|
token = None
|
|
if request.method == 'POST':
|
|
body: dict = json.loads(request.body)
|
|
token = format_token(request.headers.get("Authorization"))
|
|
api.clear_basket(token)
|
|
return JsonResponse({"success": token}, status=200)
|
|
except Exception as error:
|
|
return JsonResponse({"error": format(error)}, status=500)
|
|
|