Beispiel #1
0
def workout_info(request):

    if request.POST.get('slug'):
        workouts = Workout.objects.filter(slug=request.POST.get('slug'))
        if workouts:
            workout = workouts[0]
            if workout:
                return JSONResponse(
                    {'num_exercises': len(workout.exercise_set.all())})

    return JSONResponse({'num_exercises': 0})
Beispiel #2
0
def send_message_to_user(request, pk):
    if request.method == 'POST':
        to_user = get_object_or_404(User, pk=int(pk))

        print to_user.display_name, request.user.display_name
        thread = get_thread_for_users(request.user, to_user)
        message_content = str(request.POST.get('message_content', ''))
        message = create_new_message(thread, request.user, message_content)

        return JSONResponse({
            'from_user': str(request.user),
            'to_user': str(to_user),
            'message_content': message_content
        })
    else:
        return JSONResponse({'error': 'Please use POST method!'})
Beispiel #3
0
def undo_macro_day(request):
    week = int(request.POST.get('week'))
    day_index = int(request.POST.get('day_index'))
    client = request.user.client
    blitz = request.user.blitz
    date = blitz.get_date_for_day_index(week, day_index)
    md = MacroDay.objects.get(client=client, day=date)
    md.delete()
    return JSONResponse(
        macro_utils.get_macro_meta_for_day(client, week, day_index))
Beispiel #4
0
def inbox_feed(request):
    user_threads = UserThread.objects.filter(
        user=request.user).order_by('-thread__last_message_date')
    possible_recipients = possible_recipients_for_user(request.user)
    try:
        new_message_to_user = request.session.pop('new_message_to_user')
    except KeyError:
        new_message_to_user = None

    ret = {'html': get_inboxfeed_html(user_threads)}
    return JSONResponse(ret)
Beispiel #5
0
def workout_desc(request):

    if request.POST.get('workout'):
        workout = get_object_or_404(Workout, pk=request.POST.get('workout'))

        if workout:
            if request.POST.get('desc'):
                workout.description = request.POST.get('desc')
                workout.save()
                print "Description for workout pk=%s : %s" % (
                    request.POST.get('workout'), request.POST.get('desc'))

    return JSONResponse({})
Beispiel #6
0
def get_macros_for_blitz_week(request):
    """
    List of macros for a given week
    """
    week = int(request.GET['week'])
    elements = []
    for i in range(7):
        elements.append(
            macro_utils.get_macro_meta_for_day(request.user.client, week, i))
    ret = {
        'week_macros': elements,
    }
    return JSONResponse(ret)
Beispiel #7
0
def workoutplan_rename(request):

    if request.POST.get('workoutplan'):
        workoutplan = get_object_or_404(WorkoutPlan,
                                        pk=request.POST.get('workoutplan'))

        if workoutplan:
            if request.POST.get('name'):
                workoutplan.name = request.POST.get('name')
                workoutplan.save()
                print "Rename workoutplan pk=%s : %s" % (
                    request.POST.get('workoutplan'), request.POST.get('name'))

    return JSONResponse({})
Beispiel #8
0
def save_macro_day(request):
    week = int(request.POST.get('week'))
    day_index = int(request.POST.get('day_index'))
    client = request.user.client
    blitz = request.user.blitz
    date = blitz.get_date_for_day_index(week, day_index)

    # TODO better error handling, need client's cooperation tho
    if not MacroDay.objects.filter(client=client, day=date).exists():
        md = MacroDay.objects.create(
            client=client,
            day=date,
            calories=request.POST.get('calories') == 'yes',
            carbs=request.POST.get('carbs') == 'yes',
            protein=request.POST.get('protein') == 'yes',
            fat=request.POST.get('fat') == 'yes',
        )

    return JSONResponse(
        macro_utils.get_macro_meta_for_day(client, week, day_index))
Beispiel #9
0
def workoutplan_ajax(request):

    if not 'mode' in request.POST:
        return False

    if request.POST.get('mode') == 'save_day':
        workoutplan_day_mgr(request=request,
                            workoutplan=request.POST.get('workoutplan'),
                            key=request.POST.get('exercise'),
                            workout=request.POST.get('workout'),
                            day_char=request.POST.get('day'))

    # save description for exercise
    # TODO: modularize the exercise lookup with handling of provisional records
    elif request.POST.get('mode') == 'save_desc':
        exercise = request.POST.get('exercise')
        desc = request.POST.get('desc')
        if len(desc) == 0:
            return JSONResponse({'is_error': False})

        if len(exercise.split(
                '_')) == 1:  # handle the case of exercise param by itself
            exercise_pk = exercise
            exercises = Exercise.objects.filter(pk=exercise_pk)

        elif len(
                exercise.split('_')
        ) == 3:  # handle the case of exercise as 3-part param: week, day, exercise
            exercise_pk = exercise.split('_')[2]
            day_pk = request.POST.get('exercise').split('_')[1]  # day param
            workoutplan = get_object_or_404(WorkoutPlan,
                                            pk=request.POST.get('workoutplan'))
            day = WorkoutPlanDay.objects.filter(pk=day_pk)[0]
            workout = day.workout

            exercises = Exercise.objects.filter(pk=exercise_pk)

            if not exercises or exercises[
                    0].workout != workout:  # check for provisional exercise
                session_key = "exercise_%s" % exercise_pk
                if session_key in request.session:
                    exercises = Exercise.objects.filter(
                        pk=int(request.session[session_key]))
                else:
                    exercises = None

        else:
            exercise_pk = None

        if exercises:
            exercise = exercises[0]
            exercise.description = desc
            exercise.save()

        print 'EDIT EXERCISE DESCRIPTION:', exercise, desc

    elif request.POST.get('mode') == 'save_exercise':

        week_pk = request.POST.get('exercise').split('_')[
            0]  # split key into week, day, exercise
        day_pk = request.POST.get('exercise').split('_')[1]
        workoutplan = get_object_or_404(WorkoutPlan,
                                        pk=request.POST.get('workoutplan'))
        week = get_object_or_404(WorkoutPlanWeek, pk=week_pk)

        days = WorkoutPlanDay.objects.filter(pk=day_pk)
        if not days or days[
                0].workout_plan_week.workout_plan != workoutplan:  # check for provisional day
            session_key = "day_%s" % day_pk
            day = WorkoutPlanDay.objects.get(
                pk=int(request.session[session_key]))
            print "SAVE EXERCISE REDIRECT WORKOUTDAY:", day_pk, request.session[
                session_key]
        else:
            day = days[0]

        workout = day.workout

        lifts = Lift.objects.filter(name=request.POST.get('lift'))
        if lifts:
            lift = lifts[0]

            if len(request.POST.get('exercise').split('_')) > 2:
                exercise_pk = request.POST.get('exercise').split('_')[2]
                exercises = Exercise.objects.filter(pk=exercise_pk)

                if not exercises or exercises[
                        0].workout != workout:  # check for provisional exercise
                    session_key = "exercise_%s" % exercise_pk
                    if session_key in request.session:
                        exercises = Exercise.objects.filter(
                            pk=int(request.session[session_key]))
                    else:
                        exercises = None

                    if not exercises:  # must be new exercise
                        exercise = Exercise.objects.create(lift=lift,
                                                           workout=workout)
                        request.session[session_key] = exercise.pk
                        print "SAVE/NEW EXERCISE REDIRECT", request.POST.get(
                            'exercise'), exercise.pk
                    else:
                        exercise = exercises[0]
                        exercise.lift = lift
                        print "SAVE/EDIT EXERCISE REDIRECT", request.POST.get(
                            'exercise'), exercise.pk
                else:
                    exercise = exercises[0]
                    exercise.lift = lift
                    print "SAVE/EDIT EXERCISE", request.POST.get('exercise')

            else:
                print "NO EXERCISE IN KEY", request.POST.get('exercise')

            exercise.sets_display = request.POST.get('display')
            exercise.save()
            sets = exercise.workoutset_set.all()

            for set_num in range(6):
                if request.POST.get('set' + str(set_num + 1)).isdigit():
                    num_reps = int(request.POST.get('set' + str(set_num + 1)))
                else:
                    num_reps = 0

                if len(request.POST.get('set' + str(set_num + 1))) > 0 and len(
                        sets) > set_num:
                    if sets[set_num]:
                        ws = WorkoutSet.objects.get(id=sets[set_num].id)
                        if num_reps > 0:
                            ws.num_reps = num_reps
                            ws.save()
                            print "CHANGED SET:", set_num, ws
                        elif ws:
                            ws.delete()
                            print "DELETED SET:", set_num, ws
                elif num_reps > 0:
                    ws = WorkoutSet.objects.create(lift=lift,
                                                   workout=day.workout,
                                                   exercise=exercise,
                                                   num_reps=num_reps)
                    print "NEW SET:", ws

            print "SAVE EXERCISE:", request.POST.get(
                'workoutplan'), request.POST.get('exercise'), request.POST.get(
                    'lift'), request.POST.get('display'), request.POST.get(
                        'set1'), request.POST.get('set2'), request.POST.get(
                            'set3'), request.POST.get(
                                'set4'), request.POST.get(
                                    'set5'), request.POST.get('set6')

    elif request.POST.get(
            'mode'
    ) == 'delete_workoutplan_day' and request.POST.get('key') != None:

        workoutplan = get_object_or_404(WorkoutPlan,
                                        pk=request.POST.get('workoutplan'))

        key = request.POST.get('key')
        if '_' in key:
            day = workoutplan_day_mgr(
                request=request,
                workoutplan=request.POST.get('workoutplan'),
                key=key)
        else:
            return JSONResponse({'is_error': True})

        workout = day.workout
        week = day.workout_plan_week

        # purge leftover records with safechecks to make sure there are no dependencies
        if not day.gymsession_set.all() and not day.traineralert_set.all():
            day.delete()  # delete day IFF it's no longer used
            print "DELETE DAY:", day

        if not workout.exercise_set.all() and not workout.workoutset_set.all(
        ) and not workout.workoutplanday_set.all():
            workout.delete()  # delete workout IFF it's no longer used
            print "UNUSED WORKOUT DELETED", workout.slug

        if not week.workoutplanday_set.all():
            week.delete()  # delete week IFF it's no longer used

            for w in WorkoutPlanWeek.objects.filter(workout_plan=workoutplan):
                if w.week >= week.week:
                    w.week -= 1
                    w.save()

            print "UNUSED WEEK DELETED", week
            return JSONResponse({
                'redirect':
                '/spotter/edit-workoutplan?plan=' + str(workoutplan.pk)
            })

    elif request.POST.get('mode') == 'delete_workoutplan_exercise':

        if request.POST.get('key') != None:
            week_pk = request.POST.get('key').split('_')[
                0]  # split key into week, day, exercise
            day_pk = request.POST.get('key').split('_')[1]
            workoutplan = get_object_or_404(WorkoutPlan,
                                            pk=request.POST.get('workoutplan'))
            week = get_object_or_404(WorkoutPlanWeek, pk=week_pk)
            days = WorkoutPlanDay.objects.filter(pk=day_pk)
            if not days or days[
                    0].workout_plan_week.workout_plan != workoutplan:  # check for provisional day
                session_key = "day_%s" % day_pk
                day = WorkoutPlanDay.objects.get(
                    pk=int(request.session[session_key]))
                print "DELETE EXERCISE REDIRECT WORKOUTDAY:", day_pk, request.session[
                    session_key]
            else:
                day = days[0]

            workout = day.workout

            exercise_pk = request.POST.get('key').split('_')[2]

            exercises = Exercise.objects.filter(pk=exercise_pk)

            if not exercises or exercises[
                    0].workout != workout:  # check for provisional exercise
                session_key = "exercise_%s" % exercise_pk
                if session_key in request.session:
                    exercises = Exercise.objects.filter(
                        pk=int(request.session[session_key]))
                    print "DELETE EXERCISE REDIRECT", request.session[
                        session_key], exercise_pk

            if exercises:
                exercise = exercises[0]
                exercise.delete()  # delete exercise and associated workoutsets
                print "DELETE EXERCISE", request.POST.get('key')

    elif request.POST.get('mode') == 'add_week':
        workoutplan = get_object_or_404(WorkoutPlan,
                                        pk=request.POST.get('workoutplan'))

        if request.POST.get('exercise') == '999':  # add week to end
            weeks = WorkoutPlanWeek.objects.filter(workout_plan=workoutplan)
            if weeks:
                week = weeks[len(weeks) - 1]  # get last week
                print "ADD WEEK after:", week
                WorkoutPlanWeek.objects.create(workout_plan=workoutplan,
                                               week=week.week + 1)
            else:
                print "ADD WEEK 1"
                WorkoutPlanWeek.objects.create(workout_plan=workoutplan,
                                               week=1)

        else:
            week = get_object_or_404(WorkoutPlanWeek,
                                     pk=request.POST.get('exercise'))

            for w in WorkoutPlanWeek.objects.filter(workout_plan=workoutplan):
                if w.week >= week.week:
                    w.week += 1
                    w.save()
            WorkoutPlanWeek.objects.create(workout_plan=workoutplan,
                                           week=week.week)

            print "ADD WEEK before:", week

        return JSONResponse({
            'redirect':
            '/spotter/edit-workoutplan?plan=' + str(workoutplan.pk)
        })

    return JSONResponse({'is_error': False})