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.
103 lines
3.7 KiB
Python
103 lines
3.7 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)
|
|
|
|
initialUser = {
|
|
"id": -1,
|
|
"nickname": '',
|
|
"email": '',
|
|
"token": '',
|
|
"token_expiry_date": '',
|
|
"money": '',
|
|
"histories_id": []
|
|
}
|
|
|
|
@csrf_exempt
|
|
async def user(request):
|
|
try:
|
|
user1 = dict()
|
|
if request.method == 'POST':
|
|
|
|
if request.body:
|
|
body: dict = json.loads(request.body)
|
|
|
|
if "register" in body.keys():
|
|
token = api.registration(body["register"]["nickname"],
|
|
body["register"]["password"],
|
|
body["register"]["email"])
|
|
elif "login" in body.keys():
|
|
token = api.login(body["login"]["email"],
|
|
body["login"]["password"])
|
|
elif "unregister" in body.keys():
|
|
token = format_token(request.headers.get("Authorization"))
|
|
api.unregister(token)
|
|
return JsonResponse({"success": [initialUser]}, status=200)
|
|
elif "logout" in body.keys():
|
|
token = format_token(request.headers.get("Authorization"))
|
|
api.logout(token)
|
|
return JsonResponse({"success": [initialUser]}, status=200)
|
|
elif "add_money" in body.keys():
|
|
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 "add_product" in body.keys():
|
|
api.add_product_to_basket(token, body["add_product"]["product_id"])
|
|
elif "delete_product" in body.keys():
|
|
api.delete_product_from_basket(token, body["delete_product"]["product_id"])
|
|
elif "clear" in body.keys():
|
|
api.clear_basket(token)
|
|
elif "buy_products" in body.keys():
|
|
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)
|