示例#1
0
 def decorated(*args, **kwargs):
     token = request.headers["Authorization"]
     if not token:
         return "token is missing", 401
     user_wrapper = MongoDB.getUser({"token": token})
     if not user_wrapper.found:
         return "token is invalid", 403
     return f(*args, **kwargs)
示例#2
0
def manageOneUserDisplay():
    user=request.get_json() #get user for display
    #connection with mongo getting the current user entry
    try:
        user_wrapper: UserWrapper = MongoDB.getUser(user_id)
    except TypeError as type_err: #Checking for errors
        return str(type_err), 422
    except ValueError as value_err:
        return str(value_err), 422
    except:
        return "Bad error", 500
    else:
        if user_wrapper.user is None:
            return "Something is wrong with the database", 500
        if type(user_wrapper.user) is dict and not user_wrapper.operationDone and not user_wrapper.found:
            return "Couldn't find user", 500
        return jsonify(user=user_wrapper.user)
示例#3
0
def displayMyprofile():
    user=request.get_json()
    #connection with mongo sending the user and modifying the profile's details
    try:
        user_wrapper: UserWrapper = MongoDB.getUser(user)
    except TypeError as type_err: #Checking for errors
        return str(type_err), 422
    except ValueError as value_err:
        return str(value_err), 422
    except:
        return "Bad error", 500
    else:
        if user_wrapper.user is None:
            return "Something is wrong with the database", 500
        if type(user_wrapper.user) is dict and not user_wrapper.found and not user_wrapper.operationDone:
            return "User does not exist", 404
        return jsonify(user=user_wrapper.user)
示例#4
0
def manageBusinessAdd():
    """
   {
       "file"
       "name"
       "category"
       "country"
       "city"
       "address"
       "postalCode"
       "phoneNumber"
       "email"
       "ownerEmail"
   }
   """
    if request.method == "POST":
        # check if the post request has the file part
        if "file" in request.files:
            file = request.files["file"]
            # if user does not select file, browser also
            # submit an empty part without filename
            if file.filename == '':
                return "No selected file", 422
            if file and allowed_file(file.filename):
                file_name = secure_filename(file.filename).replace(".", str(time()).replace(".","") + ".")
                if not os.path.exists(UPLOAD_FOLDER):
                    os.makedirs(UPLOAD_FOLDER)
                file.save(os.path.join(app.config['UPLOAD_FOLDER'], file_name))
        else:
            file_name = 'image_placeholder.jpg'
 
        business = request.form.to_dict()
        if "ownerEmail" in business:
            owner: UserWrapper = MongoDB.getUser({"email": business["ownerEmail"]})
            if not owner.found:
                return "owner email invalid", 422
            business["ownerId"] = owner.user["_id"] 
            del business["ownerEmail"]
        if "file" in business:
            del business["file"]
        if "services" in business:
            business["services"] = business["services"].split(",")
        if "products" in business:
            business["products"] = business["products"].split(",")
        business["imgPath"] = file_name
        # connection with mongo sending the user and modifying the profile's details
        try:
            business_wrapper: BusinessWrapper = MongoDB.createNewBusiness(business)
        except TypeError as type_err:  # Checking for errors
            return str(type_err), 422
        except ValueError as value_err:
            return str(value_err), 422
        except:
            return "Bad error", 500
        else:
            if type(business_wrapper.business) is not dict:
                return "Something is wrong with the database", 500
            if business_wrapper.found:
                return "Business already exists", 409
            if business_wrapper.operationDone:
                return jsonify("Business addition successful!")
            return "Unexpected Error!", 500
    return "Not a POST request", 422