def get(self, id): """ Return all the calories for the specified user """ current_user = User.find_by_username(get_jwt_identity()['username']) if current_user.is_admin() or current_user.has_child(id): user = User.query.filter_by(id=id).first() if user is None: return custom_response( 404, "User {} not found".format(id) ) calories = Calorie.query.with_parent(user).all() calories = list_to_array(calories) return custom_response( 200, "{} calories".format(user.username), calories ) else: return custom_response( 401, "Permission denied. User {} not a child".format(id) )
def post(self): """ Add a new image for the caller user """ current_user = User.find_by_username(get_jwt_identity()['username']) current_dir = "{}{}/".format(image_save_path, current_user.id) if not os.path.exists(current_dir): os.makedirs(current_dir, exist_ok=True) data = request.get_json() image = base64.b64decode(data['image']) timestamp = calendar.timegm(time.gmtime()) image_name = str(timestamp) + ".jpg" current_path = "{}/{}".format(current_dir, image_name) with open(current_path, 'wb') as f: f.write(image) custom_link = "{}/images/{}/{}".format(server_url, current_user.id, image_name) return custom_response(200, "Image saved", custom_link)
def get(self): """ Return the information for the caller user """ user = User.find_by_username(get_jwt_identity()['username']) return custom_response(200, "User {}".format(user.username), user.to_dict())
def get(self): """ Return all the steps for the caller user """ current_user = User.find_by_username(get_jwt_identity()['username']) steps = Step.query.with_parent(current_user).all() steps = list_to_array(steps) return custom_response(200, "Your steps", steps)
def get(self): """ Return all the meters for the caller user """ current_user = User.find_by_username(get_jwt_identity()['username']) meters = Meter.query.with_parent(current_user).all() meters = list_to_array(meters) return custom_response(200, "Your meters", meters)
def get(self): """ Return all activities for the caller user """ current_user = User.find_by_username(get_jwt_identity()['username']) activities = Activity.query.with_parent(current_user).all() activities = list_to_array(activities) return custom_response(200, "Your activities", activities)
def get(self): """ Return all the heart rates for the caller user """ current_user = User.find_by_username(get_jwt_identity()['username']) heart_rates = HeartRate.query.with_parent(current_user).all() heart_rates = list_to_array(heart_rates) return custom_response(200, "Your heart rates", heart_rates)
def get(self): """ Return all the personal info for the caller user """ current_user = User.find_by_username(get_jwt_identity()['username']) personal_info = PersonalInfo.query.with_parent(current_user).order_by( PersonalInfo.timestamp.desc()).all() personal_info = list_to_array(personal_info) return custom_response(200, "Your personal info", personal_info)
def get(self): """ Return all the children for the caller user """ current_user = User.find_by_username(get_jwt_identity()['username']) children = current_user.family_members.all() children = list_to_array(children) return custom_response(200, "{}'s children".format(current_user.username), children)
def decorated_function(*args, **kwargs): user = get_jwt_identity() current_user = User.find_by_username(user["username"]) if not current_user: return {'message': "Permission denied. User not found"}, 401 if current_user.role > access_level: return {'message': "Permission denied."}, 401 return f(*args, **kwargs)
def put(self): """ Update the information of the caller user """ user = User.find_by_username(get_jwt_identity()['username']) new_user = request.get_json() user.full_name = new_user['full_name'] user.date_of_birth = new_user['date_of_birth'] user.gender = new_user['gender'] user.update() return custom_response(200, "Your information was updated", user.to_dict())
def post(self): """ Add a new activity for the caller user """ current_user = User.find_by_username(get_jwt_identity()['username']) activity = request.get_json() new_activity = Activity(name=activity['name'], datetime=activity['datetime'], duration=activity['duration'], user=current_user) new_activity.save() return custom_response(200, "Activity added", new_activity.id)
def get(self): """ Return all the calories for the caller user """ current_user = User.find_by_username(get_jwt_identity()['username']) calories = Calorie.query.with_parent(current_user).all() calories = list_to_array(calories) return custom_response( 200, "Your calories", calories )
def get(self, user, image): """ Return the image requested """ current_user = User.find_by_username(get_jwt_identity()['username']) if current_user.id != user: return custom_response(401, "Permission denied") current_dir = "{}{}".format(image_save_path, user) return send_from_directory(current_dir, image)
def post(self): """ Refresh token """ current_user = User.find_by_username(get_jwt_identity()['username']) tmp_user = current_user.to_dict() tmp_user.update({ 'access_token': create_access_token(create_identity(current_user), expires_delta=False), 'refresh_token': create_refresh_token(create_identity(current_user)) }) return custom_response(200, 'Access token refreshed', tmp_user)
def delete(self, id): """ Delete an activity by its id """ current_user = User.find_by_username(get_jwt_identity()['username']) activity = Activity.query.with_parent(current_user).filter_by( id=id).first() if activity is None: return custom_response(404, "Activity with id {} not found".format(id)) activity.delete() return custom_response(200, "Activity {} deleted".format(id))
def post(self): """ Add a new personal info for the caller user """ current_user = User.find_by_username(get_jwt_identity()['username']) personal_info = request.get_json() new_personal_info = PersonalInfo(weight=personal_info['weight'], height=personal_info['height'], user=current_user) new_personal_info.save() return custom_response(200, "Personal Info added", new_personal_info.id)
def post(self): """ Add a new calorie for the caller user """ current_user = User.find_by_username(get_jwt_identity()['username']) calorie = request.get_json() current_date = datetime.now() day_stat = current_date.replace(hour=0, minute=0, second=0, microsecond=0) day_end = current_date.replace(hour=23, minute=59, second=59, microsecond=59) old_calorie = Calorie.query.filter(User.id == current_user.id, Calorie.timestamp >= day_stat, Calorie.timestamp <= day_end).first() if old_calorie is None: new_calorie = Calorie( timestamp=current_date, value=calorie['value'], user=current_user ) new_calorie.save() return custom_response( 200, "Calorie added", new_calorie.id ) else: if old_calorie.value <= calorie['value']: old_calorie.value = calorie['value'] old_calorie.update() return custom_response( 200, "Calorie updated", old_calorie.id ) else: return custom_response( 200, "Calorie already updated", old_calorie.id )
def post(self): """ Add a new user """ body = request.get_json() if User.find_by_username(body['username']): return custom_response( 401, 'User {} already exists'.format(body['username'])) if (body['username'] == '' or body['password'] == '' or body['password'] != body['confirm_password'] or body['full_name'] == '' or body['gender'] > 1 or body['date_of_birth'] == ''): return custom_response(400, "Invalid parameters") user = User( username=body['username'], password=User.generate_hash(body['password']), role=USER_ROLE['user'], full_name=body['full_name'], gender=body['gender'], date_of_birth=body['date_of_birth'], ) try: user.save() access_token = create_access_token(identity=create_identity(user)) refresh_token = create_refresh_token( identity=create_identity(user)) return custom_response(200, 'User {} was created'.format(user.username), { 'access_token': access_token, 'refresh_token': refresh_token }) except: return custom_response(500, 'Something went wrong')
def post(self): """ Add new step for the caller user """ current_user = User.find_by_username(get_jwt_identity()['username']) step = request.get_json() current_date = datetime.now() day_stat = current_date.replace(hour=0, minute=0, second=0, microsecond=0) day_end = current_date.replace(hour=23, minute=59, second=59, microsecond=59) old_step = Step.query.filter(User.id == current_user.id, Step.timestamp >= day_stat, Step.timestamp <= day_end).first() if old_step is None: new_step = Step(timestamp=current_date, value=step['value'], user=current_user) new_step.save() return custom_response(200, "Step added", new_step.id) else: if old_step.value <= step['value']: old_step.value = step['value'] old_step.update() return custom_response(200, "Step updated", old_step.id) else: return custom_response(200, "Step already updated", old_step.id)
def post(self): """ Create new user session """ body = request.get_json() user = User.find_by_username(body['username']) if not user: return custom_response( 401, 'User {} doesn\'t exist'.format(body['username'])) if User.verify_hash(body['password'], user.password): tmp_user = user.to_dict() tmp_user.update({ 'access_token': create_access_token(create_identity(user), expires_delta=False), 'refresh_token': create_refresh_token(create_identity(user)) }) return custom_response(200, 'Logged in as {}'.format(user.username), tmp_user) else: return custom_response(401, 'Username or password wrong')