Esempio n. 1
0
def view(request, id, slug=None):
    '''
    Detail view for an exercise
    '''

    template_data = {}
    template_data['comment_edit'] = False

    # Load the exercise itself
    exercise = cache.get(cache_mapper.get_exercise_key(int(id)))
    if not exercise:
        exercise = get_object_or_404(Exercise, pk=id)
        cache.set(cache_mapper.get_exercise_key(exercise), exercise)

    template_data['exercise'] = exercise

    # Create the backgrounds that show what muscles the exercise works on
    backgrounds = cache.get(cache_mapper.get_exercise_muscle_bg_key(int(id)))
    if not backgrounds:
        backgrounds_back = []
        backgrounds_front = []

        for muscle in exercise.muscles.all():
            if muscle.is_front:
                backgrounds_front.append('images/muscles/main/muscle-%s.svg' % muscle.id)
            else:
                backgrounds_back.append('images/muscles/main/muscle-%s.svg' % muscle.id)

        for muscle in exercise.muscles_secondary.all():
            if muscle.is_front:
                backgrounds_front.append('images/muscles/secondary/muscle-%s.svg' % muscle.id)
            else:
                backgrounds_back.append('images/muscles/secondary/muscle-%s.svg' % muscle.id)

        # Append the "main" background, with the silhouette of the human body
        # This has to happen as the last step, so it is rendered behind the muscles.
        backgrounds_front.append('images/muscles/muscular_system_front.svg')
        backgrounds_back.append('images/muscles/muscular_system_back.svg')
        backgrounds = (backgrounds_front, backgrounds_back)

        cache.set(cache_mapper.get_exercise_muscle_bg_key(int(id)),
                  (backgrounds_front, backgrounds_back))

    template_data['muscle_backgrounds_front'] = backgrounds[0]
    template_data['muscle_backgrounds_back'] = backgrounds[1]

    # If the user is logged in, load the log and prepare the entries for
    # rendering in the D3 chart
    entry_log = []
    chart_data = []
    if request.user.is_authenticated():
        logs = WorkoutLog.objects.filter(user=request.user, exercise=exercise)
        entry_log, chart_data = process_log_entries(logs)

    template_data['logs'] = entry_log
    template_data['json'] = chart_data
    template_data['svg_uuid'] = str(uuid.uuid4())
    template_data['reps'] = _("Reps")

    return render(request, 'exercise/view.html', template_data)
Esempio n. 2
0
    def get_context_data(self, **kwargs):

        # Call the base implementation first to get a context
        context = super(WorkoutLogDetailView, self).get_context_data(**kwargs)

        # Prepare the entries for rendering and the D3 chart
        workout_log = {}

        for day_list in self.object.canonical_representation['day_list']:
            day_id = day_list['obj'].id
            workout_log[day_id] = {}
            for set_list in day_list['set_list']:
                exercise_log = {}
                for exercise_list in set_list['exercise_list']:
                    exercise_id = exercise_list['obj'].id
                    exercise_log[exercise_id] = []

                    logs = exercise_list['obj'].workoutlog_set.filter(user=self.request.user,
                                                                      workout=self.object)
                    entry_log, chart_data = process_log_entries(logs)
                    if entry_log:
                        exercise_log[exercise_list['obj'].id].append(entry_log)

                    if exercise_log:
                        workout_log[day_id][exercise_id] = {}
                        workout_log[day_id][exercise_id]['log_by_date'] = entry_log
                        workout_log[day_id][exercise_id]['div_uuid'] = 'div-' + str(uuid.uuid4())
                        workout_log[day_id][exercise_id]['chart_data'] = chart_data

        context['workout_log'] = workout_log
        context['reps'] = _("Reps")

        return context
Esempio n. 3
0
def view(request, id, slug=None):
    '''
    Detail view for an exercise
    '''

    template_data = {}
    template_data['comment_edit'] = False
    template_data['show_shariff'] = True

    exercise = get_object_or_404(Exercise, pk=id)

    template_data['exercise'] = exercise

    # Create the backgrounds that show what muscles the exercise works on
    backgrounds = cache.get(cache_mapper.get_exercise_muscle_bg_key(int(id)))
    if not backgrounds:
        backgrounds_back = []
        backgrounds_front = []

        for muscle in exercise.muscles.all():
            if muscle.is_front:
                backgrounds_front.append('images/muscles/main/muscle-%s.svg' %
                                         muscle.id)
            else:
                backgrounds_back.append('images/muscles/main/muscle-%s.svg' %
                                        muscle.id)

        for muscle in exercise.muscles_secondary.all():
            if muscle.is_front:
                backgrounds_front.append(
                    'images/muscles/secondary/muscle-%s.svg' % muscle.id)
            else:
                backgrounds_back.append(
                    'images/muscles/secondary/muscle-%s.svg' % muscle.id)

        # Append the "main" background, with the silhouette of the human body
        # This has to happen as the last step, so it is rendered behind the muscles.
        backgrounds_front.append('images/muscles/muscular_system_front.svg')
        backgrounds_back.append('images/muscles/muscular_system_back.svg')
        backgrounds = (backgrounds_front, backgrounds_back)

        cache.set(cache_mapper.get_exercise_muscle_bg_key(int(id)),
                  (backgrounds_front, backgrounds_back))

    template_data['muscle_backgrounds_front'] = backgrounds[0]
    template_data['muscle_backgrounds_back'] = backgrounds[1]

    # If the user is logged in, load the log and prepare the entries for
    # rendering in the D3 chart
    entry_log = []
    chart_data = []
    if request.user.is_authenticated:
        logs = WorkoutLog.objects.filter(user=request.user, exercise=exercise)
        entry_log, chart_data = process_log_entries(logs)

    template_data['logs'] = entry_log
    template_data['json'] = chart_data
    template_data['svg_uuid'] = str(uuid.uuid4())

    return render(request, 'exercise/view.html', template_data)
Esempio n. 4
0
File: log.py Progetto: itsdtr/wger
def calendar(request, year=None, month=None):
    '''
    Show a calendar with all the workout logs
    '''
    if not year:
        year = datetime.date.today().year
    else:
        year = int(year)
    if not month:
        month = datetime.date.today().month
    else:
        month = int(month)

    context = {}
    logs_filtered = []
    temp_date_list = []
    logs = WorkoutLog.objects.filter(user=request.user,
                                     date__year=year,
                                     date__month=month).order_by('exercise')

    # Process the logs, 'overview' list for the calendar
    for log in logs:
        if log.date not in temp_date_list:
            temp_date_list.append(log.date)
            logs_filtered.append(log)

    (current_workout, schedule) = Schedule.objects.get_current_workout(request.user)
    context['calendar'] = WorkoutCalendar(logs_filtered).formatmonth(year, month)
    context['logs'] = process_log_entries(logs)[0]
    context['current_year'] = year
    context['current_month'] = month
    context['current_workout'] = current_workout
    context['month_list'] = WorkoutLog.objects.filter(user=request.user).dates('date', 'month')
    return render(request, 'workout/calendar.html', context)
Esempio n. 5
0
def view(request, id, slug=None):
    """
    Detail view for an exercise
    """

    template_data = {}
    template_data['comment_edit'] = False
    template_data['show_shariff'] = True

    exercise = get_object_or_404(Exercise, pk=id)

    template_data['exercise'] = exercise

    template_data["muscles_main_front"] = exercise.muscles.filter(is_front=True)
    template_data["muscles_main_back"] = exercise.muscles.filter(is_front=False)
    template_data["muscles_sec_front"] = exercise.muscles_secondary.filter(is_front=True)
    template_data["muscles_sec_back"] = exercise.muscles_secondary.filter(is_front=False)

    # If the user is logged in, load the log and prepare the entries for
    # rendering in the D3 chart
    entry_log = []
    chart_data = []
    if request.user.is_authenticated:
        logs = WorkoutLog.objects.filter(user=request.user, exercise=exercise)
        entry_log, chart_data = process_log_entries(logs)

    template_data['logs'] = entry_log
    template_data['json'] = chart_data
    template_data['svg_uuid'] = str(uuid.uuid4())
    template_data['cache_vary_on'] = "{}-{}".format(exercise.id, load_language().id)

    return render(request, 'exercise/view.html', template_data)
Esempio n. 6
0
    def log_data(self, request, pk):
        """
        Returns processed log data for graphing

        Basically, these are the logs for the workout and for a specific exercise.

        If on a day there are several entries with the same number of repetitions,
        but different weights, only the entry with the higher weight is shown in the chart
        """
        execise_id = request.GET.get('id')
        if not execise_id:
            return Response(
                "Please provide an exercise ID in the 'id' GET parameter")

        exercise = get_object_or_404(Exercise, pk=execise_id)
        logs = exercise.workoutlog_set.filter(user=self.request.user,
                                              weight_unit__in=(1, 2),
                                              repetition_unit=1,
                                              workout=self.get_object())
        entry_logs, chart_data = process_log_entries(logs)
        serialized_logs = {}
        for key, values in entry_logs.items():
            serialized_logs[str(key)] = [
                WorkoutLogSerializer(entry).data for entry in values
            ]
        return Response({
            'chart_data': json.loads(chart_data),
            'logs': serialized_logs
        })
Esempio n. 7
0
    def get_context_data(self, **kwargs):

        # Call the base implementation first to get a context
        context = super(WorkoutLogDetailView, self).get_context_data(**kwargs)
        is_owner = self.owner_user == self.request.user

        # Prepare the entries for rendering and the D3 chart
        workout_log = {}

        for day_list in self.object.canonical_representation['day_list']:
            day_id = day_list['obj'].id
            workout_log[day_id] = {}
            for set_list in day_list['set_list']:
                exercise_log = {}
                for exercise_list in set_list['exercise_list']:
                    exercise_id = exercise_list['obj'].id
                    exercise_log[exercise_id] = []

                    # Filter the logs for user and exclude all units that are
                    # not weight
                    #
                    # TODO: add the repetition_unit to the filter. For some
                    #       reason (bug in django? DB problems?) when adding
                    #       the filter there, thevexecution time explodes.
                    #       The weight unit filter works as expected. Also,
                    #       adding the unit IDs to the exclude list also has
                    #       the disadvantage that if new ones are added in a
                    #       local instance, they could "slip" through.
                    logs = exercise_list['obj'].workoutlog_set.filter(
                        user=self.owner_user,
                        weight_unit__in=(1, 2),
                        workout=self.object) \
                        .exclude(repetition_unit_id__in=(2, 3, 4, 5, 6, 7, 8))
                    entry_log, chart_data = process_log_entries(logs)
                    if entry_log:
                        exercise_log[exercise_list['obj'].id].append(entry_log)

                    if exercise_log:
                        workout_log[day_id][exercise_id] = {}
                        workout_log[day_id][exercise_id][
                            'log_by_date'] = entry_log
                        workout_log[day_id][exercise_id][
                            'div_uuid'] = 'div-' + str(uuid.uuid4())
                        workout_log[day_id][exercise_id][
                            'chart_data'] = chart_data

        context['workout_log'] = workout_log
        context['owner_user'] = self.owner_user
        context['is_owner'] = is_owner
        context['show_shariff'] = is_owner

        return context
Esempio n. 8
0
def calendar(request, year=None, month=None):
    '''
    Show a calendar with all the workout logs
    '''
    if not year:
        year = datetime.date.today().year
    else:
        year = int(year)
    if not month:
        month = datetime.date.today().month
    else:
        month = int(month)

    context = {}
    logs = WorkoutLog.objects.filter(user=request.user,
                                     date__year=year,
                                     date__month=month).order_by('exercise')
    logs_filtered = cache.get(
        cache_mapper.get_workout_log(request.user.pk, year, month))
    if not logs_filtered:
        logs_filtered = {}

        # Process the logs. Group by date and check for impressions
        for log in logs:
            if log.date not in logs_filtered:
                session = log.get_workout_session()

                if session:
                    impression = session.impression
                else:
                    # Default is 'neutral'
                    impression = WorkoutSession.IMPRESSION_NEUTRAL

                logs_filtered[log.date.day] = {
                    'impression': impression,
                    'log': log
                }
        cache.set(cache_mapper.get_workout_log(request.user.pk, year, month),
                  logs_filtered)

    (current_workout,
     schedule) = Schedule.objects.get_current_workout(request.user)
    context['calendar'] = WorkoutCalendar(logs_filtered).formatmonth(
        year, month)
    context['logs'] = process_log_entries(logs)[0]
    context['current_year'] = year
    context['current_month'] = month
    context['current_workout'] = current_workout
    context['impressions'] = WorkoutSession.IMPRESSION
    context['month_list'] = WorkoutLog.objects.filter(user=request.user).dates(
        'date', 'month')
    return render(request, 'workout/calendar.html', context)
Esempio n. 9
0
File: log.py Progetto: voszp/wger
def calendar(request, username=None, year=None, month=None):
    '''
    Show a calendar with all the workout logs
    '''
    is_owner, user = check_access(request.user, username)

    logger.info('aa bb cc')
    uid, token = make_token(user)
    year = int(year) if year else datetime.date.today().year
    month = int(month) if month else datetime.date.today().month

    context = {}
    logs = WorkoutLog.objects.filter(user=user,
                                     date__year=year,
                                     date__month=month).order_by('exercise')
    logs_filtered = cache.get(
        cache_mapper.get_workout_log(user.pk, year, month))
    if not logs_filtered:
        logs_filtered = {}

        # Process the logs. Group by date and check for impressions
        for log in logs:
            if log.date not in logs_filtered:
                session = log.get_workout_session()

                if session:
                    impression = session.impression
                else:
                    # Default is 'neutral'
                    impression = WorkoutSession.IMPRESSION_NEUTRAL

                logs_filtered[log.date.day] = {
                    'impression': impression,
                    'log': log
                }
        cache.set(cache_mapper.get_workout_log(user.pk, year, month),
                  logs_filtered)

    (current_workout, schedule) = Schedule.objects.get_current_workout(user)
    context['calendar'] = WorkoutCalendar(logs_filtered).formatmonth(
        year, month)
    context['logs'] = process_log_entries(logs)[0]
    context['current_year'] = year
    context['current_month'] = month
    context['current_workout'] = current_workout
    context['owner_user'] = user
    context['is_owner'] = is_owner
    context['impressions'] = WorkoutSession.IMPRESSION
    context['month_list'] = WorkoutLog.objects.filter(user=user).dates(
        'date', 'month')
    context['show_shariff'] = is_owner and user.userprofile.ro_access
    return render(request, 'workout/calendar.html', context)
Esempio n. 10
0
    def get_context_data(self, **kwargs):

        # Call the base implementation first to get a context
        context = super(WorkoutLogDetailView, self).get_context_data(**kwargs)
        is_owner = self.owner_user == self.request.user

        # Prepare the entries for rendering and the D3 chart
        workout_log = {}

        for day_list in self.object.canonical_representation["day_list"]:
            day_id = day_list["obj"].id
            workout_log[day_id] = {}
            for set_list in day_list["set_list"]:
                exercise_log = {}
                for exercise_list in set_list["exercise_list"]:
                    exercise_id = exercise_list["obj"].id
                    exercise_log[exercise_id] = []

                    # Filter the logs for user and exclude all units that are not weight
                    #
                    # TODO: add the repetition_unit to the filter. For some reason (bug
                    #       in django? DB problems?) when adding the filter there, the
                    #       execution time explodes. The weight unit filter works as
                    #       expected. Also, adding the unit IDs to the exclude list
                    #       also has the disadvantage that if new ones are added in a
                    #       local instance, they could "slip" through.
                    logs = (
                        exercise_list["obj"]
                        .workoutlog_set.filter(user=self.owner_user, weight_unit__in=(1, 2), workout=self.object)
                        .exclude(repetition_unit_id__in=(2, 3, 4, 5, 6, 7, 8))
                    )
                    entry_log, chart_data = process_log_entries(logs)
                    if entry_log:
                        exercise_log[exercise_list["obj"].id].append(entry_log)

                    if exercise_log:
                        workout_log[day_id][exercise_id] = {}
                        workout_log[day_id][exercise_id]["log_by_date"] = entry_log
                        workout_log[day_id][exercise_id]["div_uuid"] = "div-" + str(uuid.uuid4())
                        workout_log[day_id][exercise_id]["chart_data"] = chart_data

        context["workout_log"] = workout_log
        context["owner_user"] = self.owner_user
        context["is_owner"] = is_owner
        context["show_shariff"] = is_owner

        return context
Esempio n. 11
0
def calendar(request, year=None, month=None):
    '''
    Show a calendar with all the workout logs
    '''
    if not year:
        year = datetime.date.today().year
    else:
        year = int(year)
    if not month:
        month = datetime.date.today().month
    else:
        month = int(month)

    context = {}
    logs = WorkoutLog.objects.filter(user=request.user,
                                     date__year=year,
                                     date__month=month).order_by('exercise')
    logs_filtered = cache.get(cache_mapper.get_workout_log(request.user.pk, year, month))
    if not logs_filtered:
        logs_filtered = {}

        # Process the logs. Group by date and check for impressions
        for log in logs:
            if log.date not in logs_filtered:
                session = log.get_workout_session()

                if session:
                    impression = session.impression
                else:
                    # Default is 'neutral'
                    impression = WorkoutSession.IMPRESSION_NEUTRAL

                logs_filtered[log.date.day] = {'impression': impression,
                                               'log': log}
        cache.set(cache_mapper.get_workout_log(request.user.pk, year, month), logs_filtered)

    (current_workout, schedule) = Schedule.objects.get_current_workout(request.user)
    context['calendar'] = WorkoutCalendar(logs_filtered).formatmonth(year, month)
    context['logs'] = process_log_entries(logs)[0]
    context['current_year'] = year
    context['current_month'] = month
    context['current_workout'] = current_workout
    context['impressions'] = WorkoutSession.IMPRESSION
    context['month_list'] = WorkoutLog.objects.filter(user=request.user).dates('date', 'month')
    return render(request, 'workout/calendar.html', context)
Esempio n. 12
0
    def get_context_data(self, **kwargs):

        # Call the base implementation first to get a context
        context = super(WorkoutLogDetailView, self).get_context_data(**kwargs)
        is_owner = self.owner_user == self.request.user

        # Prepare the entries for rendering and the D3 chart
        workout_log = {}

        for day_list in self.object.canonical_representation['day_list']:
            day_id = day_list['obj'].id
            workout_log[day_id] = {}
            for set_list in day_list['set_list']:
                exercise_log = {}
                for exercise_list in set_list['exercise_list']:
                    exercise_id = exercise_list['obj'].id
                    exercise_log[exercise_id] = []

                    # Filter the logs for user and exclude all units that are not weight
                    logs = exercise_list['obj'].workoutlog_set.filter(
                        user=self.owner_user,
                        weight_unit__in=(1, 2),
                        repetition_unit=1,
                        workout=self.object)
                    entry_log, chart_data = process_log_entries(logs)
                    if entry_log:
                        exercise_log[exercise_list['obj'].id].append(entry_log)

                    if exercise_log:
                        workout_log[day_id][exercise_id] = {}
                        workout_log[day_id][exercise_id][
                            'log_by_date'] = entry_log
                        workout_log[day_id][exercise_id][
                            'div_uuid'] = 'div-' + str(uuid.uuid4())
                        workout_log[day_id][exercise_id][
                            'chart_data'] = chart_data

        context['workout_log'] = workout_log
        context['owner_user'] = self.owner_user
        context['is_owner'] = is_owner
        context['show_shariff'] = is_owner

        return context
Esempio n. 13
0
File: log.py Progetto: qbig/wger
def calendar(request, year=None, month=None):
    '''
    Show a calendar with all the workout logs
    '''
    if not year:
        year = datetime.date.today().year
    else:
        year = int(year)
    if not month:
        month = datetime.date.today().month
    else:
        month = int(month)

    context = {}
    logs_filtered = []
    temp_date_list = []
    logs = WorkoutLog.objects.filter(user=request.user,
                                     date__year=year,
                                     date__month=month).order_by('exercise')

    # Process the logs, 'overview' list for the calendar
    for log in logs:
        if log.date not in temp_date_list:
            temp_date_list.append(log.date)
            logs_filtered.append(log)

    (current_workout,
     schedule) = Schedule.objects.get_current_workout(request.user)
    context['calendar'] = WorkoutCalendar(logs_filtered).formatmonth(
        year, month)
    context['logs'] = process_log_entries(logs)[0]
    context['current_year'] = year
    context['current_month'] = month
    context['current_workout'] = current_workout
    context['month_list'] = WorkoutLog.objects.filter(user=request.user).dates(
        'date', 'month')
    return render(request, 'workout/calendar.html', context)
Esempio n. 14
0
File: log.py Progetto: qbig/wger
    def get_context_data(self, **kwargs):

        # Call the base implementation first to get a context
        context = super(WorkoutLogDetailView, self).get_context_data(**kwargs)

        # Prepare the entries for rendering and the D3 chart
        workout_log = {}

        for day_list in self.object.canonical_representation['day_list']:
            day_id = day_list['obj'].id
            workout_log[day_id] = {}
            for set_list in day_list['set_list']:
                exercise_log = {}
                for exercise_list in set_list['exercise_list']:
                    exercise_id = exercise_list['obj'].id
                    exercise_log[exercise_id] = []

                    logs = exercise_list['obj'].workoutlog_set.filter(
                        user=self.request.user, workout=self.object)
                    entry_log, chart_data = process_log_entries(logs)
                    if entry_log:
                        exercise_log[exercise_list['obj'].id].append(entry_log)

                    if exercise_log:
                        workout_log[day_id][exercise_id] = {}
                        workout_log[day_id][exercise_id][
                            'log_by_date'] = entry_log
                        workout_log[day_id][exercise_id][
                            'div_uuid'] = 'div-' + str(uuid.uuid4())
                        workout_log[day_id][exercise_id][
                            'chart_data'] = chart_data

        context['workout_log'] = workout_log
        context['reps'] = _("Reps")

        return context