示例#1
0
 def create_user(self, incoming_user):
     check_unique = userCollection.find_one({'email': incoming_user['email']})
     if check_unique is None:
         return {"error": "Internal Server Error: email already exists"}
     check_unique = userCollection.find_one({'username': incoming_user['username']})
     if check_unique is None:
         return {"error": "Internal Server Error: username already exists"}
     res = userCollection.insert_one(incoming_user)
     if res is None:
         return {}
     return newUser
示例#2
0
def buyTokens():

    if not request.is_json:
        return jsonify({'errors': {
            'general': 'format error (expected JSON)'
        }}), 400

    payload = ctx_stack.top.jwtPayload
    user = userCollection.find_one({"email": payload["email"]})

    try:
        params = request.get_json()
        tokensBought = params["tokensBought"]

        productPresent = False

        # User already has some tokens for the product add more
        for product in user["tokens_owned"]:
            if (product["product_name"] == tokensBought["product_name"]):
                product["noTokensBought"] += tokensBought["noTokensBought"]
                productPresent = True

        # If not bought append to list of products
        if (not productPresent):
            user["tokens_owned"].append(tokensBought)

        newvalues = {"$set": {"tokens_owned": user["tokens_owned"]}}
        userCollection.update_one({"email": payload["email"]}, newvalues)

        return jsonify({"msg": "successful update"}), 200
    except Exception as e:
        print(e)
        return jsonify({'errors': {
            'general': 'Please provide all details'
        }}), 400
示例#3
0
def listProduct():

    payload = ctx_stack.top.jwtPayload
    user = userCollection.find_one()
    productInfo = {"products": []}
    try:
        user = userCollection.find_one({"email": payload["email"]})
        print(user)
        for product in user["tokens_owned"]:
            elem = productCollection.find_one(
                {"product_name": product["product_name"]})
            elem.pop("_id")
            print(product["product_name"])
            productInfo["products"].append(elem)

    except Exception as e:
        print(e)
        return jsonify({'errors': {'general': 'Server error'}}), 500

    return jsonify(productInfo), 200
示例#4
0
def getUserInfo():
    try:
        payload = ctx_stack.top.jwtPayload
        user = userCollection.find_one({"email": payload["email"]})
        user.pop('_id')
        user.pop('password')
        return jsonify(user), 200

    except Exception as e:
        print(e)
        return jsonify({'errors': {'general': 'Server error'}}), 500
示例#5
0
def login():

    if not request.is_json:
        return jsonify({'errors': {
            'general': 'format error (expected JSON)'
        }}), 400

    try:
        params = request.get_json()

        email = params['email']
        password = params['password']

        # Check if user exist
        user = userCollection.find_one({"email": email})
        if (user == None):
            return jsonify({'errors': {'general': 'User does not exist'}}), 400

        # verify password
        if (not bcrypt.checkpw(password.encode(), user["password"])):
            return jsonify({'errors': {
                'general': 'Wrong email or password'
            }}), 400

        # Generate token
        token = jwt.encode({
            'email': email,
            'exp': time() + 36000
        },
                           "secretforjwttoken",
                           algorithm='HS256').decode('utf-8')

        return jsonify({"token": token}), 200

    # catch JSON format and missing keys (email / password)
    except Exception as e:
        print(e)
        return jsonify(
            {'errors': {
                'general': 'Please provide both email and password'
            }}), 400
示例#6
0
 def get_by_username(self, username):
     res = userCollection.find_one({'username': username})
     if res is None:
         return{}
     return res
示例#7
0
 def get_by_object_id(self, objectId):
     res = userCollection.find_one({'_id': ObjectId(id)})
     if res is None:
         return {}
     return res
示例#8
0
 def get_by_id_string(self, id):
     # will return None if not found
     res = userCollection.find_one({'_id': ObjectId(id)})
     if res is None:
         return {}
     return res