Beispiel #1
0
def update_user(current_user, email):

    if not current_user.admin:
        return ResponseGenerator.not_authorized()

    user = User.query.filter_by(email=email).first()
    data = request.get_json()

    if not user:
        return ResponseGenerator.not_found()

    if data['admin'] == "True":
        admin = True
    else:
        admin = False

    user.name = data['name']
    user.password = data['password']
    user.email = data['email']
    user.admin = admin
    update_user = User(name=user.name,
                       email=user.email,
                       password=user.password,
                       admin=user.admin)

    User.update(update_user)

    return ResponseGenerator.generate_response(
        f"{update_user.name} has been successfully updated", 200)
Beispiel #2
0
def delete_todo(current_user, todo_id):
    todo = Todo.query.filter_by(id=todo_id, user_id=current_user.id).first()

    if not todo:
        return ResponseGenerator.not_found()

    Todo.delete(todo)

    return ResponseGenerator.generate_response(
        f"{todo.text} has been Deleted!", 200)
Beispiel #3
0
    def wrap(*args, **kwargs):
        if not request.is_json:
            return ResponseGenerator.json_data_expected()

        try:
            request.get_json()
        except Exception as _e:
            return ResponseGenerator.json_data_expected()

        return f(*args, **kwargs)
Beispiel #4
0
def user_login():
    data = request.get_json()
    existing_user = UserRepository.check_password(data['email'],
                                                  data['password'])
    if existing_user:
        token = TokenGenerator.generate_jwt({
            'name': existing_user.name,
            'email': existing_user.email
        })
        return ResponseGenerator.generate_response({'token': token}, 200)
    return ResponseGenerator.user_login_failed()
Beispiel #5
0
    def get(self):
        mandatory_fields = ["q"]
        if any(
                request.args.get(item, default=None) is None
                for item in mandatory_fields):
            return ResponseGenerator.mandatory_field(fields=mandatory_fields)

        query = request.args.get("q", default=None, type=str)
        page = request.args.get("page", default=1, type=int)

        movies = MovieSearchRepository.search(search_query=query, page=page)
        return ResponseGenerator.generate_response(data=movies, code=200)
Beispiel #6
0
    def post(self):
        data = request.get_json()

        mandatory_fields = ["imdb_id"]
        if any(data.get(item) is None for item in mandatory_fields):
            return ResponseGenerator.mandatory_field(fields=mandatory_fields)

        fav_movie = UserRepository.add_user_favorite_movie(user=current_user, imdb_id=data["imdb_id"])
        if not fav_movie:
            return ResponseGenerator.generate_response(data={"msg": "disliked"}, code=204)

        fav_movie = MovieRepository.get_movie_info(fav_movie)
        return ResponseGenerator.generate_response(data=fav_movie, code=201)
Beispiel #7
0
 def wrapper(*args, **kwargs):
     token = None
     if request.headers.has_key('Access-Token'):
         token = request.headers['Access-Token']
     if not token:
         return ResponseGenerator.forbidden_op('Missing Token')
     
     try:
         data = jwt.decode(token, app.config['SECRET_KEY'])
     except:
         return ResponseGenerator.forbidden_op('Invalid Token')
         
     return f(*args, **kwargs)
Beispiel #8
0
def delete_user(current_user, email):

    if not current_user.admin:
        return ResponseGenerator.not_authorized()

    user = User.query.filter_by(email=email).first()

    if not user:
        return ResponseGenerator.not_found()

    User.delete(user)

    return ResponseGenerator.generate_response(
        f"{user.name} deleted successfully from DB.", 200)
Beispiel #9
0
    def post(self):
        data = request.get_json()

        mandatory_fields = ["name", "email", "password"]
        if any(data.get(item) is None for item in mandatory_fields):
            return ResponseGenerator.mandatory_field(fields=mandatory_fields)

        name, email, password = data.pop("name"), data.pop("email"), data.pop("password")
        user = UserRepository.create_user(name=name, email=email, password=password, **data)
        if not user:
            return ResponseGenerator.error_response(msg="Email is already registered, try to login", code=400)

        access_token = UserToken.create_user_access_token(user=user)
        return ResponseGenerator.generate_response({
            "access_token": access_token
        }, code=201)
Beispiel #10
0
def login():
    auth = request.authorization

    if not auth or not auth.username or not auth.password:
        # return make_response('Could not verify', 401, {'WWW-Authenticate' : 'Basic realm="Login required"'})
        return ResponseGenerator.mandatory_field(["email", "password"], 401)

    user = User.query.filter_by(email=auth.username).first()

    if not user:
        return make_response(
            'Could not verify', 401,
            {'WWW-Authenticate': 'Basic realm="Login required"'})

    if User.check_password(user, auth.password):
        token = jwt.encode(
            {
                'email': user.email,
                'exp':
                datetime.datetime.utcnow() + datetime.timedelta(minutes=30)
            }, Configuration.SECRET_KEY)
        return jsonify({'token': token.decode('UTF-8')})

    return make_response('Could not verify', 401,
                         {'WWW-Authenticate': 'Basic realm="Login required"'})
Beispiel #11
0
def get_one_user(current_user, email):
    if not current_user.admin:
        return ResponseGenerator.not_authorized()

    user = User.query.filter_by(email=email).first()

    if not user:
        return ResponseGenerator.not_found()

    user_data = {}
    user_data['id'] = user.id
    user_data['name'] = user.name
    user_data['email'] = user.email
    user_data['admin'] = user.admin

    return ResponseGenerator.generate_response(user_data, 200)
Beispiel #12
0
def get_all_users(current_user):
    if not current_user.admin:
        return ResponseGenerator.not_authorized()

    users = User.query.all()
    output = []

    for user in users:
        user_data = {}
        user_data['id'] = user.id
        user_data['name'] = user.name
        user_data['email'] = user.email
        user_data['admin'] = user.admin
        output.append(user_data)

    return ResponseGenerator.generate_response(output, 200)
Beispiel #13
0
def create_todo(current_user):
    data = request.get_json()

    new_todo = Todo(text=data['text'], complete=False, user_id=current_user.id)
    Todo.save(new_todo)

    return ResponseGenerator.generate_response(
        f"{new_todo.text} has been created!", 200)
Beispiel #14
0
    def post(self):
        data = request.get_json()

        email = data["email"]
        password = data["password"]

        user = UserRepository.get_by_email(email=email)
        if not user:
            return ResponseGenerator.not_found(msg="user not found")

        if not user.check_password(password=password):
            return ResponseGenerator.forbidden(
                msg="email/password combination is invalid")

        access_token = UserRepository.create_user_access_token(user=user)
        return ResponseGenerator.generate_response(
            {"access_token": access_token}, code=200)
Beispiel #15
0
def todo_create():
    data = request.get_json()
    new_todo = TodoRepository.create_todo(data['title'], data['isCompleted'])

    return ResponseGenerator.generate_response(
        {
            'title': new_todo.title,
            'isCompleted': new_todo.isCompleted
        }, 200)
Beispiel #16
0
def user_register():
    data = request.get_json()
    new_user = UserRepository.create_user(data['name'], data['email'],
                                          data['password'])
    print(new_user)
    return ResponseGenerator.generate_response(
        {
            'name': new_user.name,
            'email': new_user.email,
        }, 200)
Beispiel #17
0
    def post(self):
        data = request.get_json()

        mandatory_fields = ["email", "password"]
        if any(data.get(item) is None for item in mandatory_fields):
            return ResponseGenerator.mandatory_field(fields=mandatory_fields)

        email = data["email"]
        password = data["password"]

        user = UserRepository.get_by_email(email=email)
        if not user:
            return ResponseGenerator.not_found(msg="user not found")

        if not user.check_password(password=password):
            return ResponseGenerator.forbidden(msg="email/password combination is invalid")

        access_token = UserToken.create_user_access_token(user=user)
        return ResponseGenerator.generate_response({
            "access_token": access_token
        }, code=200)
Beispiel #18
0
def create_user():

    data = request.get_json()
    email = data['email']
    user = User.query.filter_by(email=email).first()

    if user:
        return ResponseGenerator.error_response(
            f"Already {email} exist in DB. Try using other email address", 409)

    if data['admin'] == "True":
        admin = True
    else:
        admin = False

    new_user = User(name=data['name'],
                    email=data['email'],
                    password=data['password'],
                    admin=admin)
    User.save(new_user)

    return ResponseGenerator.generate_response(data, 200)
Beispiel #19
0
def get_one_todo(current_user, todo_id):
    todo = Todo.query.filter_by(id=todo_id, user_id=current_user.id).first()

    if not todo:
        return jsonify({'message': 'No todo found!'})

    todo_data = {}
    todo_data['id'] = todo.id
    todo_data['text'] = todo.text
    todo_data['complete'] = todo.complete
    todo_data['user_id'] = todo.user_id

    return ResponseGenerator.generate_response(todo_data, 200)
Beispiel #20
0
def get_all_todos(current_user):

    todos = Todo.query.filter_by(user_id=current_user.id).all()

    output = []

    for todo in todos:
        todo_data = {}
        todo_data['id'] = todo.id
        todo_data['text'] = todo.text
        todo_data['complete'] = todo.complete
        todo_data['user_id'] = todo.user_id
        output.append(todo_data)

    return ResponseGenerator.generate_response(output, 200)
Beispiel #21
0
 def get(self):
     fav_movies = UserRepository.get_users_favorite_movie_details(current_user)
     return ResponseGenerator.generate_response(fav_movies, code=200)
Beispiel #22
0
def home():
    return ResponseGenerator.generate_response("hello", 200)
Beispiel #23
0
def about():
    return ResponseGenerator.generate_response("About", 200)
Beispiel #24
0
 def get(self, imdb_id: str):
     movie = MovieSearchRepository.search_movie_by_imdb_id(imdb_id=imdb_id)
     movies = UserRepository.add_attribute_to_favorite_movies(
         current_user, movie)
     return ResponseGenerator.generate_response(data=movies, code=200)
Beispiel #25
0
 def get(self):
     return ResponseGenerator.generate_response(current_user.json, code=200)
Beispiel #26
0
def flask_jwt_user_loader_error_callback(identity):
    return ResponseGenerator.not_found(
        msg="User::{} not found".format(identity))
Beispiel #27
0
def home():
    return ResponseGenerator.generate_response(
        "welcome to your personal movie database", 200)