def test_cache_delete_log_2(self):
        '''
        Test that the caches are only cleared for a the log's month
        '''
        log_hash = hash((1, 2012, 10))
        log_hash_day = hash((1, 2012, 10, 1))
        self.user_login('admin')
        self.client.get(
            reverse('manager:workout:calendar',
                    kwargs={
                        'year': 2012,
                        'month': 10
                    }))
        self.client.get(
            reverse('manager:workout:calendar-day',
                    kwargs={
                        'username': '******',
                        'year': 2012,
                        'month': 10,
                        'day': 1
                    }))

        log = WorkoutLog.objects.get(pk=3)
        log.delete()

        self.assertTrue(cache.get(cache_mapper.get_workout_log_list(log_hash)))
        self.assertTrue(
            cache.get(cache_mapper.get_workout_log_list(log_hash_day)))
    def test_cache_update_log(self):
        '''
        Test that the caches are cleared when saving a log
        '''
        log_hash = hash((1, 2012, 10))
        log_hash_day = hash((1, 2012, 10, 1))
        self.user_login('admin')
        self.client.get(
            reverse('manager:workout:calendar',
                    kwargs={
                        'year': 2012,
                        'month': 10
                    }))
        self.client.get(
            reverse('manager:workout:calendar-day',
                    kwargs={
                        'username': '******',
                        'year': 2012,
                        'month': 10,
                        'day': 1
                    }))

        log = WorkoutLog.objects.get(pk=1)
        log.weight = 35
        log.save()

        self.assertFalse(cache.get(
            cache_mapper.get_workout_log_list(log_hash)))
        self.assertFalse(
            cache.get(cache_mapper.get_workout_log_list(log_hash_day)))
Example #3
0
    def test_calendar(self):
        '''
        Test the log cache is correctly generated on visit
        '''
        log_hash = hash((1, 2012, 10))
        self.user_login('admin')
        self.assertFalse(cache.get(cache_mapper.get_workout_log_list(log_hash)))

        self.client.get(reverse('manager:workout:calendar', kwargs={'year': 2012, 'month': 10}))
        self.assertTrue(cache.get(cache_mapper.get_workout_log_list(log_hash)))
Example #4
0
    def test_calendar_anonymous(self):
        """
        Test the log cache is correctly generated on visit by anonymous users
        """
        log_hash = hash((1, 2012, 10))
        self.user_logout()
        self.assertFalse(cache.get(cache_mapper.get_workout_log_list(log_hash)))

        self.client.get(reverse('manager:workout:calendar', kwargs={'username': '******',
                                                                    'year': 2012,
                                                                    'month': 10}))
        self.assertTrue(cache.get(cache_mapper.get_workout_log_list(log_hash)))
Example #5
0
    def test_calendar_day_anonymous(self):
        '''
        Test the log cache is correctly generated on visit by anonymous users
        '''
        log_hash = hash((1, 2012, 10, 1))
        self.user_logout()
        self.assertFalse(cache.get(cache_mapper.get_workout_log_list(log_hash)))

        self.client.get(reverse('manager:workout:calendar-day', kwargs={'username': '******',
                                                                        'year': 2012,
                                                                        'month': 10,
                                                                        'day': 1}))
        self.assertTrue(cache.get(cache_mapper.get_workout_log_list(log_hash)))
Example #6
0
    def test_calendar_day(self):
        '''
        Test the log cache on the calendar day view is correctly generated on visit
        '''
        log_hash = hash((1, 2012, 10, 1))
        self.user_login('admin')
        self.assertFalse(cache.get(cache_mapper.get_workout_log_list(log_hash)))

        self.client.get(reverse('manager:workout:calendar-day', kwargs={'username': '******',
                                                                        'year': 2012,
                                                                        'month': 10,
                                                                        'day': 1}))
        self.assertTrue(cache.get(cache_mapper.get_workout_log_list(log_hash)))
Example #7
0
    def test_cache_delete_log_2(self):
        '''
        Test that the caches are only cleared for a the log's month
        '''
        log_hash = hash((1, 2012, 10))
        log_hash_day = hash((1, 2012, 10, 1))
        self.user_login('admin')
        self.client.get(reverse('manager:workout:calendar', kwargs={'year': 2012, 'month': 10}))
        self.client.get(reverse('manager:workout:calendar-day', kwargs={'username': '******',
                                                                        'year': 2012,
                                                                        'month': 10,
                                                                        'day': 1}))

        log = WorkoutLog.objects.get(pk=3)
        log.delete()

        self.assertTrue(cache.get(cache_mapper.get_workout_log_list(log_hash)))
        self.assertTrue(cache.get(cache_mapper.get_workout_log_list(log_hash_day)))
Example #8
0
    def test_cache_update_log(self):
        '''
        Test that the caches are cleared when saving a log
        '''
        log_hash = hash((1, 2012, 10))
        log_hash_day = hash((1, 2012, 10, 1))
        self.user_login('admin')
        self.client.get(reverse('manager:workout:calendar', kwargs={'year': 2012, 'month': 10}))
        self.client.get(reverse('manager:workout:calendar-day', kwargs={'username': '******',
                                                                        'year': 2012,
                                                                        'month': 10,
                                                                        'day': 1}))

        log = WorkoutLog.objects.get(pk=1)
        log.weight = 35
        log.save()

        self.assertFalse(cache.get(cache_mapper.get_workout_log_list(log_hash)))
        self.assertFalse(cache.get(cache_mapper.get_workout_log_list(log_hash_day)))
    def test_cache_delete_session_2(self):
        '''
        Test that the caches are only cleared for a the session's month
        '''
        log_hash = hash((1, 2012, 10))
        self.user_login('admin')
        self.client.get(reverse('manager:workout:calendar', kwargs={'year': 2012, 'month': 10}))

        session = WorkoutSession.objects.get(pk=2)
        session.delete()

        self.assertTrue(cache.get(cache_mapper.get_workout_log_list(log_hash)))
Example #10
0
    def test_cache_delete_session(self):
        """
        Test that the caches are cleared when deleting a workout session
        """
        log_hash = hash((1, 2012, 10))
        self.user_login('admin')
        self.client.get(reverse('manager:workout:calendar', kwargs={'year': 2012, 'month': 10}))

        session = WorkoutSession.objects.get(pk=1)
        session.delete()

        self.assertFalse(cache.get(cache_mapper.get_workout_log_list(log_hash)))
    def test_cache_update_session(self):
        '''
        Test that the caches are cleared when updating a workout session
        '''
        log_hash = hash((1, 2012, 10))
        self.user_login('admin')
        self.client.get(reverse('manager:workout:calendar', kwargs={'year': 2012, 'month': 10}))

        session = WorkoutSession.objects.get(pk=1)
        session.notes = 'Lorem ipsum'
        session.save()

        self.assertFalse(cache.get(cache_mapper.get_workout_log_list(log_hash)))
Example #12
0
    def test_cache_update_session_2(self):
        """
        Test that the caches are only cleared for a the session's month
        """
        log_hash = hash((1, 2012, 10))
        self.user_login('admin')
        self.client.get(reverse('manager:workout:calendar', kwargs={'year': 2012, 'month': 10}))

        # Session is from 2014
        session = WorkoutSession.objects.get(pk=2)
        session.notes = 'Lorem ipsum'
        session.save()

        self.assertTrue(cache.get(cache_mapper.get_workout_log_list(log_hash)))
Example #13
0
def group_log_entries(user, year, month, day=None):
    '''
    Processes and regroups a list of log entries so they can be more easily
    used in the different calendar pages

    :param user: the user to filter the logs for
    :param year: year
    :param month: month
    :param day: optional, day

    :return: a dictionary with grouped logs by date and exercise
    '''
    if day:
        log_hash = hash((user.pk, year, month, day))
    else:
        log_hash = hash((user.pk, year, month))

    # There can be workout sessions without any associated log entries, so it is
    # not enough so simply iterate through the logs
    if day:
        filter_date = datetime.date(year, month, day)
        logs = WorkoutLog.objects.filter(user=user, date=filter_date)
        sessions = WorkoutSession.objects.filter(user=user, date=filter_date)

    else:
        logs = WorkoutLog.objects.filter(user=user,
                                         date__year=year,
                                         date__month=month)

        sessions = WorkoutSession.objects.filter(user=user,
                                                 date__year=year,
                                                 date__month=month)

    logs = logs.order_by('date', 'id')
    out = cache.get(cache_mapper.get_workout_log_list(log_hash))
    # out = OrderedDict()

    if not out:
        out = OrderedDict()

        # Logs
        for entry in logs:
            if not out.get(entry.date):
                out[entry.date] = {
                    'date': entry.date,
                    'workout': entry.workout,
                    'session': entry.get_workout_session(),
                    'logs': OrderedDict()
                }

            if not out[entry.date]['logs'].get(entry.exercise):
                out[entry.date]['logs'][entry.exercise] = []

            out[entry.date]['logs'][entry.exercise].append(entry)

        # Sessions
        for entry in sessions:
            if not out.get(entry.date):
                out[entry.date] = {
                    'date': entry.date,
                    'workout': entry.workout,
                    'session': entry,
                    'logs': {}
                }

        cache.set(cache_mapper.get_workout_log_list(log_hash), out)
    return out
Example #14
0
def group_log_entries(user, year, month, day=None):
    '''
    Processes and regroups a list of log entries so they can be more easily
    used in the different calendar pages

    :param user: the user to filter the logs for
    :param year: year
    :param month: month
    :param day: optional, day

    :return: a dictionary with grouped logs by date and exercise
    '''
    if day:
        log_hash = hash((user.pk, year, month, day))
    else:
        log_hash = hash((user.pk, year, month))

    # There can be workout sessions without any associated log entries, so it is
    # not enough so simply iterate through the logs
    if day:
        filter_date = datetime.date(year, month, day)
        logs = WorkoutLog.objects.filter(user=user, date=filter_date)
        sessions = WorkoutSession.objects.filter(user=user, date=filter_date)

    else:
        logs = WorkoutLog.objects.filter(user=user,
                                         date__year=year,
                                         date__month=month)

        sessions = WorkoutSession.objects.filter(user=user,
                                                 date__year=year,
                                                 date__month=month)

    logs = logs.order_by('date', 'id')
    out = cache.get(cache_mapper.get_workout_log_list(log_hash))
    # out = OrderedDict()

    if not out:
        out = OrderedDict()

        # Logs
        for entry in logs:
            if not out.get(entry.date):
                out[entry.date] = {'date': entry.date,
                                   'workout': entry.workout,
                                   'session': entry.get_workout_session(),
                                   'logs': OrderedDict()}

            if not out[entry.date]['logs'].get(entry.exercise):
                out[entry.date]['logs'][entry.exercise] = []

            out[entry.date]['logs'][entry.exercise].append(entry)

        # Sessions
        for entry in sessions:
            if not out.get(entry.date):
                out[entry.date] = {'date': entry.date,
                                   'workout': entry.workout,
                                   'session': entry,
                                   'logs': {}}

        cache.set(cache_mapper.get_workout_log_list(log_hash), out)
    return out