Пример #1
0
def authenticate():
    try:
        email = request.get_json().get("email")
        password_res = application.db.db.users.distinct(
            "password", {"email": email})[0]
    except Exception as e:
        email = 0

    try:
        password = request.get_json().get("password")
    except Exception as e:
        password = 0

    if email == 0 or password == 0:
        return utils.res({
            "status": "Fail",
            "message": "email or password are blank!!!!!!"
        })
    elif check_password_hash(password_res, password):
        access_token = create_access_token(identity=email)
        refresh_token = create_refresh_token(identity=email)
        return utils.res({
            'message': 'Logged in as {}'.format(email),
            'access_token': access_token,
            'refresh_token': refresh_token
        })
    else:
        return utils.res({
            "status": "Fail",
            "message": "email or password is Invalid"
        })
Пример #2
0
def add_user():
    response = {}
    collection = application.db.db.users
    result = {}

    try:
        response["name"] = request.get_json().get(
            "name") if not request.get_json().get("name") == "" else 0
    except Exception as e:
        response["name"] = 0

    try:
        response["email"] = request.get_json().get(
            "email") if not request.get_json().get("email") == "" else 0
    except Exception as e:
        response["email"] = 0

    try:
        response["password"] = generate_password_hash(
            request.get_json().get("password"), method='sha256'
        ) if not request.get_json().get("password") == "" else 0
    except Exception as e:
        response["password"] = 0

    try:
        response["mobile"] = int(request.get_json().get(
            "mobile")) if not request.get_json().get("mobile") == "" else 0
    except Exception as e:
        response["mobile"] = 0

    #role_id 1 = Admin
    #role_id 2 = Super Admin
    try:
        response["role_id"] = int(request.get_json().get(
            "role_id")) if not request.get_json().get("mobile") == "" else 2
    except Exception as e:
        response["role_id"] = 0

    ## we need _id in normal number not ObjectID format
    response["_id"] = utils.autoIncrementId(collection)

    ### checking if email already exist or not
    email_exist = collection.distinct("email", {"email": response["email"]})

    if response["name"] == 0 or response["password"] == 0 or response[
            "email"] == 0 or response["role_id"] == 0:
        result["message"] = "Please Enter Name,Password,Email,Role"
        result["status"] = "Fail"
    elif email_exist:
        result["message"] = "Email Already Exits"
        result["status"] = "Fail"
    else:
        collection.insert(response)
        result["message"] = "User Added Successfully!!!!! "
        result["status"] = "Success"

    return utils.res(result)
Пример #3
0
def delete_user():
    result = {}
    try:
        user_id = int(request.get_json().get(
            "user_id")) if not request.get_json().get("user_id") == "" else 0
    except Exception as e:
        user_id = 0

    if user_id == 0:
        return utils.res({"message": "User Id is Invalid", "status": "fail"})

    collection = application.db.db.users
    if collection.find({'_id': user_id}).count() > 0:
        response = collection.delete_one({"_id": user_id})
        result["message"] = "Deleted successfully"
        result["status"] = "Success"
    else:
        result["message"] = "No Such Record Found"
        result["status"] = "fail"
    return utils.res(result)
Пример #4
0
def expense_update(user_expense_id):
    response = {}
    result = {}
    # collection = application.db.db.user_expense
    try:
        response["user_id"] = int(request.get_json().get(
            "user_id")) if not request.get_json().get("user_id") == "" else 0
    except Exception as e:
        response["user_id"] = 0

    try:
        response["cost"] = float(request.get_json().get(
            "cost")) if not request.get_json().get("cost") == "" else 0
    except Exception as e:
        response["cost"] = 0

    try:
        response["item"] = request.get_json().get(
            "item") if not request.get_json().get("item") == "" else 0
    except Exception as e:
        response["item"] = 0

    response["_id"] = user_expense_id

    try:
        ## input date must be in the format of dd/mm/yyyy
        response["date"] = utils.dateConvrsnToDateObject(
            request.get_json().get("date")) if not request.get_json().get(
                "date") == "" else datetime.combine(datetime.today(), time.min)
    except Exception as e:
        response["date"] = datetime.combine(datetime.today(), time.min)

    if response["user_id"] == 0:
        result["message"] = "Please Enter Valid UserId"
        result["status"] = "Fail"
    elif response["cost"] == 0:
        result["message"] = "Please Enter Valid Cost"
        result["status"] = "Fail"
    elif response["item"] == 0:
        result["message"] = "Please Enter Item"
        result["status"] = "Fail"
    else:
        res = collection.update({"_id": response["_id"]},
                                response,
                                upsert=True)
        print(res)
        result["message"] = "User Updated Successfully!!!!! "
        result["status"] = "Success"

    return utils.res(result)
Пример #5
0
def add_expense():
    response = {}
    result = {}
    collection = application.db.db.user_expense
    try:
        response["user_id"] = int(request.get_json().get(
            "user_id")) if not request.get_json().get("user_id") == "" else 0
    except Exception as e:
        response["user_id"] = 0

    try:
        response["cost"] = float(request.get_json().get(
            "cost")) if not request.get_json().get("cost") == "" else 0
    except Exception as e:
        response["cost"] = 0

    try:
        response["item"] = request.get_json().get(
            "item") if not request.get_json().get("item") == "" else 0
    except Exception as e:
        response["item"] = 0

    try:
        ## input date must be in the format of dd/mm/yyyy
        response["date"] = utils.dateConvrsnToDateObject(
            request.get_json().get("date")) if not request.get_json().get(
                "date") == "" else datetime.combine(datetime.today(), time.min)
    except Exception as e:
        response["date"] = datetime.combine(datetime.today(), time.min)

    ## we need _id in normal number not ObjectID format
    response["_id"] = utils.autoIncrementId(collection)

    if response["user_id"] == 0:
        result["message"] = "Please Enter Valid UserId"
        result["status"] = "Fail"
    elif response["cost"] == 0:
        result["message"] = "Please Enter Valid Cost"
        result["status"] = "Fail"
    elif response["item"] == 0:
        result["message"] = "Please Enter Item"
        result["status"] = "Fail"
    else:
        collection.insert(response)
        result["message"] = "User Added Successfully!!!!! "
        result["status"] = "Success"

    return utils.res(result)
Пример #6
0
def list_all_users_expense():
    collection = application.db.db.user_expense
    projection = {
        "_id": 1,
        "user_id": 1,
        "cost": 1,
        "date": 1,
        "item": 1,
        "user_name": "$user_details.name",
        "user_email": "$user_details.email"
    }
    cursor = collection.aggregate([{
        "$lookup": {
            "from": "users",
            "localField": "user_id",
            "foreignField": "_id",
            "as": "user_details"
        }
    }, {
        "$project": projection
    }])
    final_data = [document for document in cursor]
    return utils.res(final_data)
Пример #7
0
def expense_edit(user_expense_id):
    cursor = collection.find({"_id": user_expense_id})
    final_data = [document for document in cursor]
    return utils.res(final_data)
Пример #8
0
def list_expense(user_id):
    collection = application.db.db.user_expense
    cursor = collection.aggregate([{"$match": {"user_id": user_id}}])
    final_data = [document for document in cursor]
    return utils.res(final_data)