Esempio n. 1
0
def delete_user_photo(user_id):
    """
    Delete user photo

    **sample request**

        CURL -X DELETE 'http://localhost:5000/users/12/photos'
        --header "X-TOKEN: MDhjOTliMzg1Y2Q2NDA5ZTgwNzg4NGY3NjM1NTQ0M2U"

    **sample response**

        {
            "success": true
        }

    """

    user = user_service.get_user_by_user_id(user_id)

    if user.profile_photo:
        os.remove(
            os.path.join(Config.UPLOAD_FOLDER, 'users', user.profile_photo))

    # update user
    user_service.update_user(user, {'profile_photo': None})

    return jsonify_response(success=True)
Esempio n. 2
0
def hard_delete_user(user_id):
    """
    Hard delete a user by user id

    !!!Important!!!

    This end point should not be exposed to public

    **sample request**

        curl -X DELETE 'http://localhost:5000/users/1'
        --header "Content-Type: application/json"
        --header "X-TOKEN: MDhjOTliMzg1Y2Q2NDA5ZTgwNzg4NGY3NjM1NTQ0M2U"

    **sample response**

        {
            "success": true
        }

    """

    user = user_service.get_user_by_user_id(user_id, include_deleted=True)

    if not user:
        raise Exception('Invalid user id.')

    result = user_service.hard_delete_user(user)
    return jsonify_response(success=result)
Esempio n. 3
0
def authenticate_user():
    """
    Authenticate user

    **sample request**

        curl -X POST 'http://localhost:5000/users/authenticate/'
        --header "Content-Type: application/json"
        --data '{
                    "email": "test_user_email",
                    "password": "******"
                }


    **sample response**

        {
            "token": "Y2QyYjJlYTMxMjA1NDMwMTg5ZDJhMDhlYjk1MTU1Yjg"
        }

    """

    user_email = request.json['email']
    password = request.json['password']

    token = auth_service.authenticate_user(user_email, password)
    return jsonify_response(token=token)
Esempio n. 4
0
def catch_all(exc):
    """
    Catch all exceptions to format a user friendly response

    :param exc: exception
    :return:
    """
    current_app.logger.error('Unhandled Exception: %s', (exc))

    error = {
        'type': 'generic',
        'msg': str(exc)
    }

    return jsonify_response(status_code=400, error=error)
Esempio n. 5
0
def get_user(user_id):
    """
    Get a user by user id

    **sample request**

        curl -X GET 'http://*****:*****@gmail.com",
                "short_description": null,
                "gender": null,
                "long_description": null,
                "age_last_modified": "2017-12-17T03:59:16.782865",
                "budget_max": null,
                "user_name": "test_user_name",
                "age": null,
                "phone": null,
                "deleted": false,
                "last_deleted_time": "2017-12-17T03:59:16.782865",
                "profile_photo": "example/path/to/photo.png",
                "preference": {
                    "gender": "F",
                    "education": "H",
                    "age_group": "25-30"
                }
            }
        }

    """

    user = user_service.get_user_by_user_id(user_id)

    if user is None:
        raise Exception('Invalid user id.')

    return jsonify_response(user=user.to_json(filter_hidden_columns=True))
Esempio n. 6
0
def update_user(user_id):
    """
    Update a user

    **sample request**

        curl -X PUT 'http://*****:*****@email.com",
                    "user_name": "test_user_name",
                    "preference": {
                                      "gender": "F",
                                      "education": "H",
                                      "age_group": "25-30"
                                  }
                }'

    **sample response**

        {
            "success": true
        }

    """

    # to update password, user '/reset_password/' end point
    if 'password' in request.json:
        raise Exception(
            'Please refer to /reset_password/ end point for password update.')

    user = user_service.get_user_by_user_id(user_id)

    if not user:
        raise Exception('Invalid user id.')

    user_info = request.json
    data_helper.clean_info(UserModel, user_info)

    user_service.update_user(user, user_info)

    return jsonify_response(success=True)
Esempio n. 7
0
def send_password_reset_email():
    """
    Send user password reset email if the email exists

    **sample request**

        curl -X GET 'http://*****:*****@test.com'

    **sample response**

        {
            "success"=True
        }

    """

    user_email = request.args.get('email', default=None)

    user = user_service.get_user_by_user_email(user_email)

    if user and Config.SEND_EMAIL:
        # create password reset token
        token = string_helper.generate_guid()

        # store password reset token in redis
        cache_helper.cache_password_reset_key(user, token)

        # generate the password reset url to be included in the email
        password_reset_url = 'http://localhost:5000/reset_password/?token={token}'.format(
            token=token)

        # send email
        email_helper.send_email(
            [user_email], render_template('password_reset_email_subject.txt'),
            render_template('password_reset_email_body.txt',
                            user_name=user.user_name,
                            link=password_reset_url),
            render_template('password_reset_email_body.html',
                            user_name=user.user_name,
                            link=password_reset_url))

    return jsonify_response(success=True)
Esempio n. 8
0
def reset_password(user_id):
    """
    Reset password

    **sample request**

        curl -X PUT 'http://localhost:5000/users/121/password_reset/'
        --header "Content-Type: application/json"
        --header "X-TOKEN: MDhjOTliMzg1Y2Q2NDA5ZTgwNzg4NGY3NjM1NTQ0M2U"
        --data '{
                    "password": "******"
                }'

    **sample response**

        {
            "success": true
        }

    """

    is_valid_token = cache_helper.validate_password_reset_token(
        user_id, g.token)

    if not is_valid_token:
        raise Exception(
            'The password reset token is not valid for user {0}'.format(
                user_id))

    user = user_service.get_user_by_user_id(user_id)

    if not user:
        raise Exception('Invalid user id.')

    password = request.json['password']

    user_service.update_user(user, {'password': password})

    return jsonify_response(success=True)
Esempio n. 9
0
def upload_user_photo(user_id):
    """
    Upload user photo

    **sample request**

        CURL -X POST 'http://localhost:5000/users/12/photos'
        -F [email protected]
        --header "X-TOKEN: MDhjOTliMzg1Y2Q2NDA5ZTgwNzg4NGY3NjM1NTQ0M2U"

    **sample response**

        {
            "success": true
        }

    """

    user = user_service.get_user_by_user_id(user_id)

    # save file on the server
    file_dest_rel_dir = u'user_{0}/photos/'.format(user_id)
    file_dest_full_dir = os.path.join(Config.UPLOAD_FOLDER, 'users',
                                      file_dest_rel_dir)
    os.makedirs(file_dest_full_dir, exist_ok=True)

    file = request.files['file']
    filename = secure_filename(file.filename)
    file_rel_path = os.path.join(file_dest_rel_dir, filename)
    file_full_path = os.path.join(file_dest_full_dir, filename)
    file.save(file_full_path)

    # update user
    user_service.update_user(user, {'profile_photo': file_rel_path})

    return jsonify_response(success=True)
Esempio n. 10
0
def get_qualified_users():
    """
    Get all users that fit the filter criteria

    **sample request**

        curl -X GET 'http://*****:*****@gmail.com",
                        "short_description": null,
                        "gender": null,
                        "long_description": null,
                        "age_last_modified": "2017-12-17T03:59:16.782865",
                        "budget_max": null,
                        "user_name": "test_user_name",
                        "age": null,
                        "phone": null,
                        "deleted": false,
                        "last_deleted_time": "2017-12-17T03:59:16.782865",
                        "profile_photo": "example/path/to/photo.png",
                        "preference": {
                                          "gender": "F",
                                          "education": "H",
                                          "age_group": "25-30"
                                      }
                    },
                    {
                        "education": null,
                        "created_time": "2017-12-17T03:59:16.782856",
                        "budget_min": null,
                        "id": 1,
                        "email": "*****@*****.**",
                        "short_description": null,
                        "gender": null,
                        "long_description": null,
                        "age_last_modified": "2017-12-17T03:59:16.782865",
                        "budget_max": null,
                        "user_name": "test_user_name",
                        "age": null,
                        "phone": null,
                        "deleted": false,
                        "last_deleted_time": "2017-12-17T03:59:16.782865",
                        "profile_photo": "example/path/to/photo.png",
                        "preference": {
                                          "gender": "F",
                                          "education": "H",
                                          "age_group": "25-30"
                                      }
                    }
                ]
            }
        }

    """
    individual_preference = {}
    shared_preference = {}

    for filter in PreferenceModel.individual_preference_columns():
        filter_value = request.args.get(filter, default=None)
        if filter_value:
            individual_preference[filter] = filter_value

    for filter in PreferenceModel.shared_preference_columns():
        filter_value = request.args.get(filter, default=None)
        if filter_value:
            shared_preference[filter] = filter_value

    page = request.args.get('page', default=1)

    users = user_service.get_qualified_users(individual_preference,
                                             shared_preference,
                                             page=page)
    return jsonify_response(
        users=[user.to_json(filter_hidden_columns=True) for user in users])
Esempio n. 11
0
def create_user():
    """
    Create a new user

    **sample request**

        curl -X POST 'http://*****:*****@email.com",
                    "user_name": "test_user_name",
                    "preference": {
                                      "gender": "F",
                                      "education": "H",
                                      "age_group": "25-30"
                                  }
                }'

    **sample response**

        {
            "user": {
                "education": null,
                "created_time": "2017-12-17T03:59:16.782856",
                "budget_min": null,
                "id": 1,
                "email": "*****@*****.**",
                "short_description": null,
                "gender": null,
                "long_description": null,
                "age_last_modified": "2017-12-17T03:59:16.782865",
                "budget_max": null,
                "user_name": "test_user_name",
                "age": null,
                "phone": null,
                "deleted": false,
                "last_deleted_time": "2017-12-17T03:59:16.782865",
                "profile_photo": "example/path/to/photo.png",
                "preference": {
                    "gender": "F",
                    "education": "H",
                    "age_group": "25-30"
                }
            }
        }

    """

    user_info = request.json
    data_helper.clean_info(UserModel, user_info)
    new_user = user_service.create_user(user_info)

    if new_user and Config.SEND_EMAIL:
        email_helper.send_email([new_user.email],
                                render_template('welcome_email_subject.txt'),
                                render_template('welcome_email_body.txt',
                                                user=new_user),
                                render_template('welcome_email_body.html',
                                                user=new_user))

    return jsonify_response(user=new_user.to_json(filter_hidden_columns=True))