def favourites(current_user): chars = FavouriteCharacter.get_characters_from_user(current_user.id) quotes = FavouriteQuotes.get_quotes_from_user(current_user.id) response = { 'characters': [item.as_dict() for item in chars], 'quotes': [item.as_dict() for item in quotes] } # print(chars.text) return res(response)
def register_user(): json_dict = request.json print(json_dict) user = User() user.update_from_dict(json_dict, ['id']) existing_user = User.get_user_by_email(json_dict['email']) if existing_user: return Responses.OBJECT_EXIST() error = user.update() if len(error) > 0: return Responses.OPERATION_FAILED() return res(user.as_dict())
def request_token(): """ request_token takes in email and password, returns the authentication token Returns: [type]: [description] """ if request.json is None: return Responses.OPERATION_FAILED() email = request.json.get('email', None) password = request.json.get('password', None) user = User.authenticate(email, password) if user: token = user.token return res({'token':token}) return Responses.AUTHENTICATION_FAILED()
def favourite_quotes(current_user, char_id, quo_id): item = FavouriteQuotes.query.filter_by(_id=quo_id).first() if item: return Responses.FAVOURITE_CHARACTER_EXISTS() quote_conn = HTTPConnector('%s/quote/%s' % (base_url, quo_id), None, headers, 'GET') quote_resp = quote_conn.trigger_request() if quote_resp.status_code == 200: if quote_resp.json()['docs']: quote_result = quote_resp.json()['docs'][0] if quote_result['character'] != char_id: return res({ 'message': 'character id does not match with the provided one' }) quote_result.update({'user_id': current_user.id}) quote_item = FavouriteQuotes() quote_error = quote_item.update(quote_result) print(quote_result) if len(quote_error) == 0: char_item = FavouriteCharacter.query.filter_by( _id=char_id).first() if not char_item: char_conn = HTTPConnector( '%s/character/%s' % (base_url, char_id), None, headers, 'GET') char_resp = conn.trigger_request() if char_resp.status_code == 200: char_result = char_resp.json()['docs'][0] char_result.update({'user_id': current_user.id}) char_item = FavouriteCharacter() char_error = char_item.update(char_result) if len(char_error) == 0: return Responses.SUCCESS() return Responses.SUCCESS() else: return Responses.NOT_EXIST() # print(chars.text) return Responses.OPERATION_FAILED()
def BLACKLISTED(): return res('', Messages.BLACKLISTED, 400)
def TOKEN_EXPIRED(): return res('', Messages.TOKEN_EXPIRED, 400)
def FAVOURITE_QUOTE_EXISTS(): return res('', Messages.FAVOURITE_QUOTE_EXISTS, 400)
def FAVOURITE_CHARACTER_EXISTS(): return res('', Messages.FAVOURITE_CHARACTER_EXISTS, 400)
def NEEDED_FIELD_EMPTY(): return res('', Messages.NEEDED_FIELD_EMPTY, 400)
def AUTHORISATION_FAILED(): return res('', Messages.AUTHORISATION_FAILED, 403)
def AUTHENTICATION_FAILED(): return res('', Messages.AUTHENTICATION_FAILED, 401)
def OPERATION_FAILED(err=Messages.OPERATION_FAILED): return res('', err, 400)
def OBJECT_EXIST(err=Messages.OBJECT_EXIST): return res('', err, 409)
def SUCCESS(): return res(Messages.SUCCESS)
def NOT_EXIST(): return res('', Messages.NOT_EXIST, 404)