updated: discount model

parent 8f2fdda6
from extensions import mongo, db from extensions import mongo, db
from Main import app from Main import app
from flask import Response, request, send_from_directory from flask import Response, request, send_from_directory, session
# import pymongo # import pymongo
import json import json
from bson.objectid import ObjectId from bson.objectid import ObjectId
...@@ -18,47 +18,64 @@ def get_image(filename): ...@@ -18,47 +18,64 @@ def get_image(filename):
return send_from_directory(os.path.join(root_dir, "Agri_Backend"), filename, as_attachment=True) return send_from_directory(os.path.join(root_dir, "Agri_Backend"), filename, as_attachment=True)
@app.route("/discount/<id>", methods=["GET"]) @app.route("/discount/<id>", methods=["GET"])
@check_for_token @check_for_token
def calculate_discount(id): def calculate_discount(id):
request.headers
id = session['Auth']['_id']
# should have payload wi9th the user detaisl and it is send to FE # should have payload wi9th the user detaisl and it is send to FE
user_earns = db.CropData.find({"userID._id": ObjectId(id)},)
user_earns = db.CropData.find_one({"_id": ObjectId(id)})
# print(user_earns) # print(user_earns)
mongores = list(user_earns)
print(mongores)
user_points = user_earns['points'] for user in mongores:
user["points"] = str(user["points"])
print(user["points"])
earnsUser = int(user["points"])
# print(user_points) # print(user_points)
if user_points == 1: try:
discount = 1
elif user_points == 2:
discount = 2
elif user_points == 3:
discount = 3
elif user_points == 4:
discount = 4
elif user_points == 5:
discount = 5
elif user_points == 6:
discount = 6
elif user_points == 7:
discount = 7
elif user_points == 8:
discount = 8
elif user_points == 9:
discount = 9
elif user_points == 10:
discount = 10
payload = user_earns["userID"]
print(payload)
# abc = json.stringify(payload) if earnsUser == 1:
# print(abc) discount = 1
# TypeError: exceptions must derive from BaseException elif earnsUser == 2:
discount = 2
elif earnsUser == 3:
discount = 3
elif earnsUser == 4:
discount = 4
elif earnsUser == 5:
discount = 5
elif earnsUser == 6:
discount = 6
elif earnsUser == 7:
discount = 7
elif earnsUser == 8:
discount = 8
elif earnsUser == 9:
discount = 9
elif earnsUser == 10:
discount = 10
print(discount)
return Response( return Response(
response= json.dumps({"Discount percentage": discount, "payload": json.loads(dumps(payload))}), response= json.dumps({"message": discount}),
status=200, status=200,
)
except Exception as Ex:
print("**********")
print(Ex)
return Response(
response= json.dumps({"message": "error discount"}),
status=500,
) )
\ No newline at end of file \ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment