def create_usercache(apps, schema_editor):
    """
    Creates a usercache table for all users
    """
    User = apps.get_model('auth', 'User')
    Usercache = apps.get_model('core', 'Usercache')
    WorkoutLog = apps.get_model('manager', 'WorkoutLog')
    WorkoutSession = apps.get_model('manager', 'WorkoutSession')
    for user in User.objects.all():
        last_activity = None
        last_log = WorkoutLog.objects.filter(user=user).order_by('date').last()
        if last_log:
            last_activity = last_log.date
        last_session = WorkoutSession.objects.filter(user=user).order_by('date'
            ).last()
        if last_session:
            last_session = last_session.date
        if last_session:
            if not last_activity:
                last_activity = last_session
            if last_activity < last_session:
                last_activity = last_session
        Usercache.objects.create(user=user, last_activity=last_activity)
