Update: issues fixed

parent d3a9b45e
...@@ -23,17 +23,23 @@ def check_for_token(func): ...@@ -23,17 +23,23 @@ def check_for_token(func):
return Response( return Response(
response= json.dumps({"message": "Missing token"}), response= json.dumps({"message": "Missing token"}),
status=200, status=200,
# need to find a way to continue the rest of called API, currently it shows the above message
) )
try: try:
data=jwt.decode(token, 'app.SECRET_KEY', ['HS256']) data=jwt.decode(token, 'app.SECRET_KEY', ['HS256'])
return Response( # return "123"
response= json.dumps({"message": data}), # return Response(
status=200, # response= json.dumps({"message": data}),
) # status=200,
# need to find a way to continue the rest of called API, currently it shows the above message
# )
except: except:
return Response( return Response(
response= json.dumps({"message": "invalid token"}), response= json.dumps({"message": "invalid token"}),
status=200, status=200,
# need to find a way to continue the rest of called API, currently it shows the above message
) )
return func(*args, **kwargs) return func(*args, **kwargs)
return wrapped return wrapped
...@@ -66,7 +72,7 @@ def public(): ...@@ -66,7 +72,7 @@ def public():
@app.route("/login", methods=["POST"]) @app.route("/login", methods=["POST"])
def login(): def login():
if request.form['email'] and request.form["password"] == 'password': if request.form['email'] and request.form["password"]:
session['user'] = request.form['email'] session['user'] = request.form['email']
token = jwt.encode({ token = jwt.encode({
'user': request.form['email'] 'user': request.form['email']
......
...@@ -5,7 +5,8 @@ from flask import Response, request ...@@ -5,7 +5,8 @@ from flask import Response, request
# import pymongo # import pymongo
import json import json
from bson.objectid import ObjectId from bson.objectid import ObjectId
import Agripreneur_App.Auth.Token from Agripreneur_App.Auth.Token import check_for_token
...@@ -17,7 +18,7 @@ def add_points_earns(id): ...@@ -17,7 +18,7 @@ def add_points_earns(id):
try: try:
# points 1-10 # points 1-10
# points = 4000 # points = 4000
earns = 2000 earns = 8000
# points calculation algorithm # points calculation algorithm
if earns >= 5000: if earns >= 5000:
points = 10 points = 10
......
...@@ -4,8 +4,13 @@ from flask import Response, request ...@@ -4,8 +4,13 @@ from flask import Response, request
# import pymongo # import pymongo
import json import json
from bson.objectid import ObjectId from bson.objectid import ObjectId
import Agripreneur_App.Auth.Token from Agripreneur_App.Auth.Token import check_for_token
from bson.json_util import dumps
# Python3 code to convert a tuple
# into a string using reduce() function
# import functools
# import operator
...@@ -14,8 +19,12 @@ import redis ...@@ -14,8 +19,12 @@ import redis
redisClient = redis.Redis(host='localhost', port=6379, db=0) redisClient = redis.Redis(host='localhost', port=6379, db=0)
print(redisClient) print(redisClient)
# def convertTuple(tup):
# str = functools.reduce(operator.add, (tup))
# return str
@app.route("/getDetails", methods=["POST"]) @app.route("/getDetails", methods=["POST"])
# @check_for_token @check_for_token
def get_point_redis(): def get_point_redis():
earn_gain = "Earn_gained" earn_gain = "Earn_gained"
...@@ -39,15 +48,17 @@ def get_point_redis(): ...@@ -39,15 +48,17 @@ def get_point_redis():
print(response) print(response)
my_json = response.decode("utf-8") # my_json = response.decode('utf-8')
print(my_json) # print(my_json)
# stringOBJ = convertTuple(response)
# print(stringOBJ)
data = json.loads(my_json) # data = json.loads(my_json)
return Response( return Response(
response= json.dumps({"message": data}), response= json.dumps({"message": json.loads(dumps(response))}),
status=200, status=200,
) )
......
...@@ -4,21 +4,24 @@ from flask import Response, request ...@@ -4,21 +4,24 @@ from flask import Response, request
# import pymongo # import pymongo
import json import json
from bson.objectid import ObjectId from bson.objectid import ObjectId
import Agripreneur_App.Auth.Token from Agripreneur_App.Auth.Token import check_for_token
from bson.json_util import dumps
# from json import stringify
@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):
# 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_one({"_id": ObjectId(id)}) user_earns = db.CropData.find_one({"_id": ObjectId(id)})
print(user_earns) # print(user_earns)
user_points = user_earns['points'] user_points = user_earns['points']
print(user_points) # print(user_points)
if user_points == 1: if user_points == 1:
discount = 1 discount = 1
...@@ -41,11 +44,15 @@ def calculate_discount(id): ...@@ -41,11 +44,15 @@ def calculate_discount(id):
elif user_points == 10: elif user_points == 10:
discount = 10 discount = 10
payload = user_earns["userID"]
print(payload)
# abc = json.stringify(payload)
# print(abc)
# TypeError: exceptions must derive from BaseException
return Response( return Response(
response= json.dumps({"Discount percentage": discount, "payload": payload}), response= json.dumps({"Discount percentage": discount, "payload": json.loads(dumps(payload))}),
status=200, status=200,
) )
\ No newline at end of file
...@@ -10,7 +10,7 @@ from flask import Response, request ...@@ -10,7 +10,7 @@ from flask import Response, request
import json import json
from bson.objectid import ObjectId from bson.objectid import ObjectId
import Agripreneur_App.Auth.Token import Agripreneur_App.Auth.Token
from werkzeug.security import generate_password_hash, check_password_hash
...@@ -21,9 +21,14 @@ import Agripreneur_App.Auth.Token ...@@ -21,9 +21,14 @@ import Agripreneur_App.Auth.Token
@app.route("/users", methods=["POST"]) @app.route("/users", methods=["POST"])
def create_user(): def create_user():
try: try:
password = request.form["password"]
hashed_value = generate_password_hash(password, method='sha256')
user = {"name": request.form["name"], user = {"name": request.form["name"],
"email": request.form["email"], "email": request.form["email"],
"password": request.form["password"], "password": hashed_value,
"district": request.form["district"]} "district": request.form["district"]}
dbResponse = db.users.insert_one(user) dbResponse = db.users.insert_one(user)
print(dbResponse.inserted_id) print(dbResponse.inserted_id)
......
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