Esempio n. 1
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"'})
Esempio n. 2
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)
Esempio n. 3
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)
Esempio n. 4
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)
Esempio n. 5
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)