def post(self): json_data = request.get_json() current_user = get_jwt_identity() data, errors = workout_schema.load(data=json_data) if errors: return { 'message': 'Validation errors', 'errors': errors }, HTTPStatus.BAD_REQUEST workout = Workout(**data) workout.user_id = current_user workout.save() return workout_schema.dump(workout), HTTPStatus.CREATED
def patch(self, workout_id): json_data = request.get_json() data, errors = workout_schema.load(data=json_data, partial=('name', )) if errors: return { 'message': 'Validation errors', 'errors': errors }, HTTPStatus.BAD_REQUEST workout = Workout.get_by_id(workout_id=workout_id) if workout is None: return {'message': 'Workout not found'}, HTTPStatus.NOT_FOUND current_user = get_jwt_identity() if current_user != workout.user_id: return {'message': 'Access is not allowed'}, HTTPStatus.FORBIDDEN workout.name = data.get('name') or workout.name workout.length = data.get('length') or workout.length workout.directions = data.get('directions') or workout.directions workout.body_part = data.get('body_part') or workout.body_part workout.save() return workout_schema.dump(workout), HTTPStatus.OK
def update_workout(id): # Form data name = request.form['name'] category = request.form['category'] capacity = request.form['capacity'] # Format date date = request.form['date'] # Split the date into a list split_date = date.split('-') # create a new date object date = datetime.date(int(split_date[0]), int(split_date[1]), int(split_date[2])) # Format time start_time = request.form['start_time'] # Split the time into a list split_start_time = start_time.split(':') # create new time object start_time = datetime.time(int(split_start_time[0]), int(split_start_time[1])) # Create new Workout object workout = Workout(name, category, date, start_time, capacity, id=id) # Update in db workout_repository.update(workout) # Redirect return redirect('/workouts')
def setUp(self): self.member_1 = Member("Dario", "Cologna", "male", 34) self.date = datetime.date(2020, 12, 16) self.time = datetime.time(6, 15) self.workout_1 = Workout("Just Skate", "freestyle", self.date, self.time, 3) self.booking_1 = Booking(self.member_1, self.workout_1)
def update_workout(id): name = request.form['name'] type = request.form['type'] duration = request.form['duration'] date = request.form['date'] capacity = request.form['capacity'] workout = Workout(name, type, duration, date, capacity, id) workout_repository.update(workout) return redirect('/workouts')
def select(id): workout = None sql = "SELECT * FROM workouts WHERE id = %s" values = [id] result = run_sql(sql, values)[0] if result is not None: workout = Workout(result['name'], result['category'], result['date'], result['start_time'], result['capacity'], result['booked'], result['id']) return workout
def select(id): workout = None sql = "SELECT * FROM workouts WHERE id = %s" values = [id] result = run_sql(sql, values)[0] if result is not None: activity = activity_repository.select(result['activity_id']) workout = Workout(activity, result['day'], result['time'], result['capacity'], result['id']) return workout
def select_all(): workouts = [] sql = "SELECT * FROM workouts" results = run_sql(sql) for row in results: activity = activity_repository.select(row['activity_id']) workout = Workout(activity, row['day'], row['time'], row['capacity'], row['id']) workouts.append(workout) return workouts
def get(self, workout_id): workout = Workout.get_by_id(workout_id=workout_id) if workout is None: return {'message': 'workout not found'}, HTTPStatus.NOT_FOUND current_user = get_jwt_identity() if workout.is_publish == False and workout_id != current_user: return {'message': 'Access is not allowed'}, HTTPStatus.FORBIDDEN return workout_schema.dump(workout).data, HTTPStatus.OK
def delete(self, workout_id): workout = Workout.get_by_id(workout_id=workout_id) if workout is None: return {'message': 'Workout not found'}, HTTPStatus.NOT_FOUND current_user = get_jwt_identity() if current_user != workout.user_id: return {'message': 'Access is not allowed'}, HTTPStatus.FORBIDDEN workout.delete() return {}, HTTPStatus.NO_CONTENT
def get(self): workouts = Workout.get_all_published() return workout_list_schema.dump(workouts), HTTPStatus.OK
def setUp(self): self.date = datetime.date(2020, 12, 16) self.time = datetime.time(6, 15) self.workout_1 = Workout("Just Skate", "freestyle", self.date, self.time, 3)
class TestWorkout(unittest.TestCase): def setUp(self): self.date = datetime.date(2020, 12, 16) self.time = datetime.time(6, 15) self.workout_1 = Workout("Just Skate", "freestyle", self.date, self.time, 3) def test_workout_has_name(self): self.assertEqual("Just Skate", self.workout_1.name) def test_workout_has_category(self): self.assertEqual("freestyle", self.workout_1.category) def test_workout_has_no_members_booked(self): self.assertEqual(0, self.workout_1.booked) def test_workout_has_capacity(self): self.assertEqual(3, self.workout_1.capacity) # Date and time tests def test_workout_has_date(self): self.assertEqual(self.date, self.workout_1.date) def test_workout_has_start_time(self): self.assertEqual(self.time, self.workout_1.start_time) def test_date_time__returns_date_and_time(self): self.assertEqual("2020-12-16 06:15:00", self.workout_1.date_time()) def test_check_upcoming__returns_true(self): self.assertEqual(True, self.workout_1.check_upcoming()) def test_check_upcoming__returns_false(self): self.workout_1.date = datetime.date(2020, 12, 13) self.assertEqual(False, self.workout_1.check_upcoming()) # Capacity tests def test_increment_booked__returns_1(self): self.workout_1.increment_booked() self.assertEqual(1, self.workout_1.booked) def test_decrease_booked__returns_0(self): self.workout_1.increment_booked() self.workout_1.decrease_booked() self.assertEqual(0, self.workout_1.booked) def test_check_capacity__returns_true(self): self.assertEqual(True, self.workout_1.check_capacity()) def test_check_capacity__returns_false(self): self.workout_1.increment_booked() self.workout_1.increment_booked() self.workout_1.increment_booked() self.assertEqual(False, self.workout_1.check_capacity())