Пример #1
0
def import_workout(request):
    if request.POST and request.FILES:
        json_file = request.FILES['csv_file'].file
        data = json_file.read()
        workouts = json.loads(data.decode('utf8').replace("'", '"'))
        for workout in workouts:
            # create workouts
            new_workout = Workout(
                creation_date=workouts[workout]['creation_date'],
                comment=workouts[workout]["comment"],
                user=request.user)
            new_workout.save()
            # if workout has workout days then create days
            if 'workout_days' in workouts[workout]:
                day = Day(training=new_workout,
                          description=workouts[workout]["description"])
                day.save()
                for day_name in workouts[workout]["workout_days"]:
                    day.day.add(
                        DaysOfWeek.objects.filter(
                            day_of_week=day_name).first())
                # if workout days has exercises then create them
                if 'exercises' in workouts[workout]:
                    one_set = Set(exerciseday=day)
                    one_set.save()
                    for exercise in workouts[workout]["exercises"]:
                        one_set.exercises.add(
                            Exercise.objects.filter(name=exercise).first())
    messages.success(request, _('workout(s) import was successful'))

    return HttpResponseRedirect(reverse('manager:workout:overview'))
Пример #2
0
def import_workouts(request):
    if request.POST and request.FILES:
        csvfile = TextIOWrapper(request.FILES['csv_file'].file,
                                encoding="utf-8")
        reader = csv.DictReader(csvfile)
        workouts = []
        for row in reader:
            workouts.append(dict(row))

        for each_workout in workouts:
            workout = Workout(creation_date=each_workout["Date created"],
                              comment=each_workout["Comment"],
                              user=request.user)
            workout.save()
            day = Day(training=workout,
                      description=each_workout["Description"])
            day.save()

            for day_name in each_workout["Days"].split("\n"):
                day.day.add(
                    DaysOfWeek.objects.filter(day_of_week=day_name).first())
            one_set = Set(exerciseday=day)
            one_set.save()

            for exercise in each_workout["Exercise"].split("\n"):
                one_set.exercises.add(
                    Exercise.objects.filter(name=exercise).first())

    return HttpResponseRedirect(reverse('manager:workout:overview'))
Пример #3
0
    def test_smart_repr_custom_setting(self):
        """
        Tests the representation with several settings
        """
        set_obj = Set(exerciseday_id=1, order=1, sets=4)
        set_obj.save()
        setting1 = Setting(
            set=set_obj,
            exercise_id=1,
            repetition_unit_id=1,
            reps=8,
            weight=Decimal(90),
            weight_unit_id=1,
            rir='3',
            order=1,
        )
        setting1.save()
        setting2 = Setting(
            set=set_obj,
            exercise_id=1,
            repetition_unit_id=1,
            reps=10,
            weight=Decimal(80),
            weight_unit_id=1,
            rir='2.5',
            order=2,
        )
        setting2.save()
        setting3 = Setting(
            set=set_obj,
            exercise_id=1,
            repetition_unit_id=1,
            reps=10,
            weight=Decimal(80),
            weight_unit_id=1,
            rir='2',
            order=3,
        )
        setting3.save()
        setting4 = Setting(
            set=set_obj,
            exercise_id=1,
            repetition_unit_id=1,
            reps=12,
            weight=Decimal(80),
            weight_unit_id=1,
            rir='1',
            order=4,
        )
        setting4.save()

        setting_text = set_obj.reps_smart_text(Exercise.objects.get(pk=1))
        self.assertEqual(
            setting_text,
            '8 (90 kg, 3 RiR) – 10 (80 kg, 2.5 RiR) – '
            '10 (80 kg, 2 RiR) – 12 (80 kg, 1 RiR)',
        )
Пример #4
0
    def test_synthetic_settings(self):
        set_obj = Set(exerciseday_id=1, order=1, sets=4)
        set_obj.save()
        setting1 = Setting(
            set=set_obj,
            exercise_id=1,
            repetition_unit_id=1,
            reps=8,
            weight=Decimal(90),
            weight_unit_id=1,
            rir='3',
            order=1,
        )
        setting1.save()
        setting2 = Setting(
            set=set_obj,
            exercise_id=3,
            repetition_unit_id=1,
            reps=10,
            weight=Decimal(80),
            weight_unit_id=1,
            rir='2.5',
            order=2,
        )
        setting2.save()
        settings: List[Setting] = set_obj.compute_settings

        # Check that there are 2 x 4 entries (2 Exercises x 4 Sets)
        self.assertEqual(len(settings), 8)

        # Check interleaved settings
        for i in range(0, len(settings)):
            if (i % 2) == 0:
                self.assertEqual(settings[i].exercise_id, 1)
                self.assertEqual(settings[i].reps, 8)
                self.assertEqual(settings[i].weight, Decimal(90))
                self.assertEqual(settings[i].rir, '3')
            else:
                self.assertEqual(settings[i].exercise_id, 3)
                self.assertEqual(settings[i].reps, 10)
                self.assertEqual(settings[i].weight, Decimal(80))
                self.assertEqual(settings[i].rir, '2.5')
Пример #5
0
                day = Day(training=workout,
                          description='Dummy day - {0}'.format(uid[0]))
                day.save()
                day.day.add(weekday)

                # Select a random number of exercises
                nr_of_exercises = random.randint(3, 10)
                random.shuffle(exercise_list)
                day_exercises = exercise_list[0:nr_of_exercises]
                order = 1
                for exercise in day_exercises:
                    reps = random.choice([1, 3, 5, 8, 10, 12, 15])
                    sets = random.randint(2, 4)

                    day_set = Set(exerciseday=day, sets=sets, order=order)
                    day_set.save()
                    day_set.exercises.add(exercise)

                    setting = Setting(set=day_set,
                                      exercise=exercise,
                                      reps=reps,
                                      order=order)
                    setting.save()

                    order += 1

        # Schedules
        nr_of_schedules = random.randint(1, 5)
        user_workouts = [i for i in Workout.objects.filter(user=user)]
        for i in range(0, nr_of_schedules):
Пример #6
0
def create_demo_entries(user):
    """
    Creates some demo data for temporary users
    """

    # (this is a bit ugly and long...)
    language = load_language()

    #
    # Workout and exercises
    #
    setting_list = []
    weight_log = []
    workout = Workout(user=user, name=_('Sample workout'))
    workout.save()
    monday = DaysOfWeek.objects.get(pk=1)
    wednesday = DaysOfWeek.objects.get(pk=3)
    day = Day(training=workout, description=_('Sample day'))
    day.save()
    day.day.add(monday)

    day2 = Day(training=workout, description=_('Another sample day'))
    day2.save()
    day2.day.add(wednesday)

    # Biceps curls with dumbbell
    if language.short_name == 'de':
        exercise = Exercise.objects.get(pk=26)
    else:
        exercise = Exercise.objects.get(pk=81)
    day_set = Set(exerciseday=day, sets=4, order=2)
    day_set.save()

    setting = Setting(set=day_set, exercise=exercise, reps=8, order=1)
    setting.save()

    # Weight log entries
    for reps in (8, 10, 12):
        for i in range(1, 8):
            log = WorkoutLog(user=user,
                             exercise=exercise,
                             workout=workout,
                             reps=reps,
                             weight=18 - reps + random.randint(1, 4),
                             date=datetime.date.today() -
                             datetime.timedelta(weeks=i))
            weight_log.append(log)

    # French press
    if language.short_name == 'de':
        exercise = Exercise.objects.get(pk=25)
    else:
        exercise = Exercise.objects.get(pk=84)
    day_set = Set(exerciseday=day, sets=4, order=2)
    day_set.save()

    setting_list.append(
        Setting(set=day_set, exercise=exercise, reps=8, order=1))

    # Weight log entries
    for reps in (7, 10):
        for i in range(1, 8):
            log = WorkoutLog(user=user,
                             exercise=exercise,
                             workout=workout,
                             reps=reps,
                             weight=30 - reps + random.randint(1, 4),
                             date=datetime.date.today() -
                             datetime.timedelta(weeks=i))
            weight_log.append(log)

    # Squats
    if language.short_name == 'de':
        exercise = Exercise.objects.get(pk=6)
    else:
        exercise = Exercise.objects.get(pk=111)
    day_set = Set(exerciseday=day, sets=4, order=3)
    day_set.save()

    setting_list.append(
        Setting(set=day_set, exercise=exercise, reps=10, order=1))

    # Weight log entries
    for reps in (5, 10, 12):
        for i in range(1, 8):
            log = WorkoutLog(user=user,
                             exercise=exercise,
                             workout=workout,
                             reps=reps,
                             weight=110 - reps + random.randint(1, 10),
                             date=datetime.date.today() -
                             datetime.timedelta(weeks=i))
            weight_log.append(log)

    # Crunches
    if language.short_name == 'de':
        exercise = Exercise.objects.get(pk=4)
    else:
        exercise = Exercise.objects.get(pk=91)
    day_set = Set(exerciseday=day, sets=4, order=4)
    day_set.save()

    setting_list.append(
        Setting(set=day_set, exercise=exercise, reps=30, order=1))
    setting_list.append(
        Setting(set=day_set, exercise=exercise, reps=99, order=2))
    setting_list.append(
        Setting(set=day_set, exercise=exercise, reps=35, order=3))

    # Leg raises, supersets with crunches
    if language.short_name == 'de':
        exercise = Exercise.objects.get(pk=35)
    else:
        exercise = Exercise.objects.get(pk=126)

    setting_list.append(
        Setting(set=day_set, exercise=exercise, reps=30, order=1))
    setting_list.append(
        Setting(set=day_set, exercise=exercise, reps=40, order=2))
    setting_list.append(
        Setting(set=day_set, exercise=exercise, reps=99, order=3))

    Setting.objects.bulk_create(setting_list)

    # Save all the log entries
    WorkoutLog.objects.bulk_create(weight_log)

    #
    # (Body) weight entries
    #
    temp = []
    existing_entries = [i.date for i in WeightEntry.objects.filter(user=user)]
    for i in range(1, 20):
        creation_date = datetime.date.today() - datetime.timedelta(days=i)
        if creation_date not in existing_entries:
            entry = WeightEntry(user=user,
                                weight=80 + 0.5 * i + random.randint(1, 3),
                                date=creation_date)
            temp.append(entry)
    WeightEntry.objects.bulk_create(temp)

    #
    # Nutritional plan
    #
    plan = NutritionPlan()
    plan.user = user
    plan.language = language
    plan.description = _('Sample nutrional plan')
    plan.save()

    # Breakfast
    meal = Meal()
    meal.plan = plan
    meal.order = 1
    meal.time = datetime.time(7, 30)
    meal.save()

    # Oatmeal
    if language.short_name == 'de':
        ingredient = Ingredient.objects.get(pk=8197)
    else:
        ingredient = Ingredient.objects.get(pk=2126)

    mealitem = MealItem()
    mealitem.meal = meal
    mealitem.ingredient = ingredient
    mealitem.order = 1
    mealitem.amount = 100
    mealitem.save()

    # Milk
    if language.short_name == 'de':
        ingredient = Ingredient.objects.get(pk=8198)
    else:
        ingredient = Ingredient.objects.get(pk=154)

    mealitem = MealItem()
    mealitem.meal = meal
    mealitem.ingredient = ingredient
    mealitem.order = 2
    mealitem.amount = 100
    mealitem.save()

    # Protein powder
    if language.short_name == 'de':
        ingredient = Ingredient.objects.get(pk=8244)
    else:
        ingredient = Ingredient.objects.get(pk=196)

    mealitem = MealItem()
    mealitem.meal = meal
    mealitem.ingredient = ingredient
    mealitem.order = 3
    mealitem.amount = 30
    mealitem.save()

    #
    # 11 o'clock meal
    meal = Meal()
    meal.plan = plan
    meal.order = 2
    meal.time = datetime.time(11, 0)
    meal.save()

    # Bread, in slices
    if language.short_name == 'de':
        ingredient = Ingredient.objects.get(pk=8225)
        unit = None
        amount = 80
    else:
        ingredient = Ingredient.objects.get(pk=5370)
        unit = IngredientWeightUnit.objects.get(pk=9874)
        amount = 2

    mealitem = MealItem()
    mealitem.meal = meal
    mealitem.ingredient = ingredient
    mealitem.weight_unit = unit
    mealitem.order = 1
    mealitem.amount = amount
    mealitem.save()

    # Turkey
    if language.short_name == 'de':
        ingredient = Ingredient.objects.get(pk=8201)
    else:
        ingredient = Ingredient.objects.get(pk=1643)

    mealitem = MealItem()
    mealitem.meal = meal
    mealitem.order = 2
    mealitem.ingredient = ingredient
    mealitem.amount = 100
    mealitem.save()

    # Cottage cheese
    if language.short_name == 'de':
        ingredient = Ingredient.objects.get(pk=8222)  # TODO: check this!
    else:
        ingredient = Ingredient.objects.get(pk=17)

    mealitem = MealItem()
    mealitem.meal = meal
    mealitem.ingredient = ingredient
    mealitem.order = 3
    mealitem.amount = 50
    mealitem.save()

    # Tomato, one
    if language.short_name == 'de':
        ingredient = Ingredient.objects.get(pk=8217)
        unit = None
        amount = 120
    else:
        ingredient = Ingredient.objects.get(pk=3208)
        unit = IngredientWeightUnit.objects.get(pk=5950)
        amount = 1

    mealitem = MealItem()
    mealitem.meal = meal
    mealitem.weight_unit = unit
    mealitem.ingredient = ingredient
    mealitem.order = 4
    mealitem.amount = amount
    mealitem.save()

    #
    # Lunch (leave empty so users can add their own ingredients)
    meal = Meal()
    meal.plan = plan
    meal.order = 3
    meal.time = datetime.time(13, 0)
    meal.save()

    #
    # Workout schedules
    #

    # create some empty workouts to fill the list
    workout2 = Workout(
        user=user, name=_('Placeholder workout nr {0} for schedule').format(1))
    workout2.save()
    workout3 = Workout(
        user=user, name=_('Placeholder workout nr {0} for schedule').format(2))
    workout3.save()
    workout4 = Workout(
        user=user, name=_('Placeholder workout nr {0} for schedule').format(3))
    workout4.save()

    schedule = Schedule()
    schedule.user = user
    schedule.name = _('My cool workout schedule')
    schedule.start_date = datetime.date.today() - datetime.timedelta(weeks=4)
    schedule.is_active = True
    schedule.is_loop = True
    schedule.save()

    # Add the workouts
    step = ScheduleStep()
    step.schedule = schedule
    step.workout = workout2
    step.duration = 2
    step.order = 1
    step.save()

    step = ScheduleStep()
    step.schedule = schedule
    step.workout = workout
    step.duration = 4
    step.order = 2
    step.save()

    step = ScheduleStep()
    step.schedule = schedule
    step.workout = workout3
    step.duration = 1
    step.order = 3
    step.save()

    step = ScheduleStep()
    step.schedule = schedule
    step.workout = workout4
    step.duration = 6
    step.order = 4
    step.save()

    #
    # Add two more schedules, to make the overview more interesting
    schedule = Schedule()
    schedule.user = user
    schedule.name = _('Empty placeholder schedule')
    schedule.start_date = datetime.date.today() - datetime.timedelta(weeks=15)
    schedule.is_active = False
    schedule.is_loop = False
    schedule.save()

    step = ScheduleStep()
    step.schedule = schedule
    step.workout = workout2
    step.duration = 2
    step.order = 1
    step.save()

    schedule = Schedule()
    schedule.user = user
    schedule.name = _('Empty placeholder schedule')
    schedule.start_date = datetime.date.today() - datetime.timedelta(weeks=30)
    schedule.is_active = False
    schedule.is_loop = False
    schedule.save()

    step = ScheduleStep()
    step.schedule = schedule
    step.workout = workout4
    step.duration = 2
    step.order = 1
    step.save()
Пример #7
0
    def test_smart_repr_custom_setting(self):
        """
        Tests the representation with several settings
        """
        set_obj = Set(exerciseday_id=1, order=1, sets=4)
        setting1 = Setting(set=set_obj,
                           exercise_id=1,
                           repetition_unit_id=1,
                           reps=8,
                           weight=Decimal(90),
                           weight_unit_id=1,
                           rir='3')
        setting2 = Setting(set=set_obj,
                           exercise_id=1,
                           repetition_unit_id=1,
                           reps=10,
                           weight=Decimal(80),
                           weight_unit_id=1,
                           rir='2.5')
        setting3 = Setting(set=set_obj,
                           exercise_id=1,
                           repetition_unit_id=1,
                           reps=10,
                           weight=Decimal(80),
                           weight_unit_id=1,
                           rir='2')
        setting4 = Setting(set=set_obj,
                           exercise_id=1,
                           repetition_unit_id=1,
                           reps=12,
                           weight=Decimal(80),
                           weight_unit_id=1,
                           rir='1')

        setting_text, setting_list, out = reps_smart_text(
            [setting1, setting2, setting3, setting4], set_obj)
        self.assertEqual(len(out['list']), 4)
        self.assertEqual(
            setting_text, '8 (90 kg, 3 RiR) – 10 (80 kg, 2.5 RiR) – '
            '10 (80 kg, 2 RiR) – 12 (80 kg, 1 RiR)')
        self.assertEqual(
            out['list'][0], {
                'reps': 8,
                'reps_unit': 'Repetitions',
                'weight': Decimal('90'),
                'weight_unit': 'kg',
                'rir': '3'
            })
        self.assertEqual(
            out['list'][1], {
                'reps': 10,
                'reps_unit': 'Repetitions',
                'weight': Decimal('80'),
                'weight_unit': 'kg',
                'rir': '2.5'
            })
        self.assertEqual(
            out['list'][2], {
                'reps': 10,
                'reps_unit': 'Repetitions',
                'weight': Decimal('80'),
                'weight_unit': 'kg',
                'rir': '2'
            })
        self.assertEqual(
            out['list'][3], {
                'reps': 12,
                'reps_unit': 'Repetitions',
                'weight': Decimal('80'),
                'weight_unit': 'kg',
                'rir': '1'
            })