def get_user_by_user_key(user_key): user = User.search_by_key(user_key) status = 200 if not isinstance(user, dict): status = 404 user = f'Error 404. User with id: {user_key} not found' return Response(json.dumps(user), mimetype=mimetype, status=status)
def get_ratings_by_user(user_key): user = User.search_by_key(user_key) status = 200 if not isinstance(user, dict): status = 404 user = f'Error 404. User with id: {user_key} not found' return Response(json.dumps(user), mimetype=mimetype, status=status) else: ratings = Rating.search_by_user(user_key) if not bool(ratings): status = 404 ratings = f'Error 404. User with id: {user_key} has not ratings yet' return Response(json.dumps(ratings), mimetype=mimetype, status=status)
def delete_user_by_id(): if not request.json: return Response(json.dumps({'error': f'Bad request.'}), mimetype=mimetype, status=400) user_key = request.json['user'] user = User.search_by_key(user_key) status = 200 if user_key is not None and isinstance(user, dict): User(user_key).delete(user_key) result = {'result': f'Status 200. The user: {user_key} was deleted.'} else: status = 400 result = { 'Error': f'Error 400. Bad request. The provided data is not correct.' } return Response(json.dumps(result), mimetype=mimetype, status=status)
def create_rating(): if not request.json: return Response(json.dumps({'error': f'Bad request.'}), mimetype=mimetype, status=400) user_key = request.json['user'] leisure_id = request.json['leisure'] grade = request.json['grade'] text = request.json['text'] user = User.search_by_key(user_key) leisure = '' for leisure_type in leisures_type: ls = LeisureList(leisure_type) leisure = ls.get_by_id(leisure_id) print(leisure) if isinstance(leisure, dict): break status = 200 if isinstance(user, dict) and isinstance(leisure, dict) and 5 >= grade >= 0: rating = Rating.search_by_user_and_leisure(user_key, leisure_id) if not isinstance(rating, dict): Rating(user=user_key, leisure=leisure_id, grade=grade, text=text) result = {'result': f'Status 200. The rating was created.'} else: status = 400 result = { 'error': f'Error 400. The rating with user_key:{user_key} and Leisure_ID:{leisure_id} is already in the' f'database.' } else: status = 400 result = { 'error': f'Error 400. The user_key:{user_key} or Leisure_ID:{leisure_id} is wrong or does not exists in the' f'system.' } return Response(json.dumps(result), mimetype=mimetype, status=status)
def update_user_password(): if not request.json: return Response(json.dumps({'error': f'Bad request.'}), mimetype=mimetype, status=400) user_key = request.json['user'] password = request.json['password'] status = 200 user = User.search_by_key(user_key) if isinstance(user, dict): user = User(user_key) user.set_password(user_key, password) result = { 'result': f'Status 200. The user: {user_key} was updated with password: {password}.' } else: status = 400 result = { 'Error': f'Error 400. Bad request. The provided data is not correct.' } return Response(json.dumps(result), mimetype=mimetype, status=status)
def update_user_username(): if not request.json: return Response(json.dumps({'error': f'Bad request.'}), mimetype=mimetype, status=400) user_key = request.json['user'] username = request.json['username'] status = 200 user = User.search_by_key(user_key) if isinstance(user, dict): user = User(user_key) user.set_username(user_key, username) result = { 'result': f'Status 200. The user: {user_key} was updated with name {username}.' } else: status = 404 result = { 'Error': f'Error 404. User with key: {user_key} not found and cannot be updated' } return Response(json.dumps(result), mimetype=mimetype, status=status)