def users_index(): """ View function to return all messages """ values = db_session.query(User).all() results = [{'id': value.id} for value in values] return (json.dumps(results), 200, {'content_type': 'application/json'})
def index(): """ View function to return all users """ users = db_session.query(User).all() return (json.dumps(users_schema.dump(users)), 200, { 'content_type': 'application/json' })
def get_user_color(user_id, color_id): """ View function to retrieve color by user. """ color = db_session.query(Color).filter_by(id=color_id, user_id=user_id).first() if not color: error = "Color id {0} doesn't exist.".format(color_id) return (json.dumps({'message': error}), 404, {'content-type': 'application/json'}) return (json.dumps(color_schema.dump(color)), 200, {'content-type': 'application/json'})
def validates_colors_schema(self, data, **kwargs): """ Validate that hex code is unique by user """ hex_code = data.get('hex_code') user_id = data.get('user_id') if db_session.query(Color).filter_by(user_id=user_id, hex_code=hex_code).count() > 0: raise ValidationError( 'Hex code {0} already exists for user {1}'.format( hex_code, user_id))
def user_moods(user_id): """ View function to retrieve moods by user. """ if request.method == 'GET': moods = db_session.query(Mood).filter_by(user_id=user_id).all() return (json.dumps(moods_schema.dump(moods)), 200, { 'content_type': 'application/json' }) try: data = request.get_json() color = db_session.query(Color).get(data['color_id']) data['user_id'] = user_id data['color_hex'] = color.hex_code mood = mood_schema.load(data) db_session.add(mood) db_session.commit() return (json.dumps(mood_schema.dump(mood)), 200, { 'content-type': 'application/json' }) except ValidationError as err: return make_response(jsonify({'message': err.messages}), 422)
def check_user_credentials(user_name, pass_hash): x = db_session.query(UserData).\ filter(UserData.name == user_name).\ filter(UserData.password == pass_hash).first() # No user data if x is None: return False if x.name == user_name and x.password == pass_hash: return True return False
def get_user_mood(user_id, mood_id): """ View function to retrieve mood by user. """ mood = db_session.query(Mood).filter_by(id=mood_id, user_id=user_id).first() if not mood: error = "Mood id {0} doesn't exist.".format(mood_id) return (json.dumps({'message': error}), 404, { 'content-type': 'application/json' }) return (json.dumps(mood_schema.dump(mood)), 200, { 'content-type': 'application/json' })
def user_colors(user_id): """ View function to retrieve colors by user. """ if request.method == 'GET': colors = db_session.query(Color).filter_by(user_id=user_id).all() return (json.dumps(colors_schema.dump(colors)), 200, {'content_type': 'application/json'}) try: data = request.get_json() data['user_id'] = user_id color = color_schema.load(data) db_session.add(color) db_session.commit() return (json.dumps(color_schema.dump(color)), 200, {'content-type': 'application/json'}) except ValidationError as err: return make_response(jsonify({'message': err.messages}), 422)
def get(user_id): """ View function to GET user """ user = db_session.query(User).get(user_id) if not user: error = "User id {0} doesn't exist.".format(user_id) return (json.dumps({'message': error}), 404, { 'content-type': 'application/json' }) return (json.dumps(user_schema.dump(user)), 200, { 'content-type': 'application/json' })
def validates_email_exists(self, data): """ Validate uniqueness of email """ if db_session.query(User).filter_by(email=data).count() > 0: raise ValidationError('Email is already registered.')
def validates_user_id(self, data): """ Validate existence of user """ if not db_session.query(User).get(data): raise ValidationError('User does not exist.')
def validates_color_id(self, data): """ Validate existence of color """ if not db_session.query(Color).get(data): raise ValidationError('Color does not exist.')