# Path: pinax/lms/activities/hooks.py
# class ActivitiesDefaultHookSet(object):
# class HookProxy(object):
#     def activity_class_path(self, *args, **kwargs):
#     def all_activities(self):
#     def success_message(self, request, activity):
#     def already_completed_message(self, request, activity):
#     def __getattr__(self, attr):
# 
# Path: pinax/lms/activities/proxies.py
# class ActivityState(BaseProxy):
# 
#     model = models.ActivityState
# 
#     def __init__(self, request, activity_key, activity_class_path):
#         self.request = request
#         self.user = request.user
#         self.activity_key = activity_key
#         self.activity_class_path = activity_class_path
#         super(ActivityState, self).__init__()
# 
#     def get_session_key(self):
#         extra = [self.activity_key]
#         return super(ActivityState, self).get_session_key(extra)
# 
#     def lookup(self, identifier):
#         if identifier is not None:
#             return next(iter(self.model.objects.filter(pk=identifier)), None)
#         if self.request.user.is_authenticated():
#             return next(iter(self.model.objects.filter(user=self.user, activity_key=self.activity_key)), None)
# 
#     def create(self):
#         kwargs = {
#             "activity_key": self.activity_key,
#             "activity_class_path": self.activity_class_path,
#         }
#         if self.user.is_authenticated():
#             kwargs["user"] = self.user
#         obj = self.model.objects.create(**kwargs)
#         return obj.pk, obj
# 
#     @property
#     def completed_count(self):
#         if self.exists:
#             return self.obj.completed_count
#         return 0
# 
#     @property
#     def data(self):
#         if self.exists:
#             return self.obj.data
#         return {}
# 
#     @property
#     def in_progress(self):
#         if self.exists:
#             return self.obj.in_progress
#         return None
# 
#     @property
#     def latest(self):
#         if self.exists:
#             return self.obj.latest
#         return None
# 
#     @property
#     def last_completed(self):
#         if self.exists:
#             return self.obj.last_completed
#         return None
# 
#     @property
#     def all_sessions(self):
#         if self.exists:
#             return self.obj.all_sessions
#         return models.ActivitySessionState.objects.none()
# 
#     @property
#     def progression(self):
#         if self.exists:
#             return self.obj.progression
#         return "start"
# 
# Path: pinax/lms/activities/models.py
# class ActivitySessionState(models.Model):
#     """
#     this stores the state of a particular session of a particular user
#     doing a particular activity.
#     """
# 
#     activity_state = models.ForeignKey(ActivityState, related_name="sessions", on_delete=models.CASCADE)
# 
#     started = models.DateTimeField(default=timezone.now)
#     completed = models.DateTimeField(null=True)  # NULL means in progress
# 
#     data = jsonfield.JSONField(default=dict, blank=True)
# 
#     class Meta:
#         unique_together = [("activity_state", "started")]
# 
#     def mark_completed(self):
#         self.completed = timezone.now()
#         self.save()
#         self.activity_state.completed_count = models.F("completed_count") + 1
#         self.activity_state.save()


from django.contrib.auth.models import User
from django.http import Http404
from django.shortcuts import redirect, render, get_object_or_404
from django.urls import reverse
from django.views.generic import View
from account.decorators import login_required
from .hooks import hookset
from .proxies import ActivityState
from .models import ActivitySessionState
from .utils import load_path_attr

    for key, activity_class_path in hookset.all_activities():
        activity = load_path_attr(activity_class_path)
        activity_states = ActivityState.objects.filter(activity_key=key)
        completed_activity_states = activity_states.exclude(completed_count=0)
        activity_session_states = ActivitySessionState.objects.filter(activity_key=key)
        completed_activity_session_states = activity_session_states.filter(completed__isnull=False)

        activities.append({
            "activity_key": key,
            "title": activity.title,
            "activity_states": activity_states,
            "completed_activity_states": completed_activity_states,
            "activity_session_states": activity_session_states,
            "completed_activity_session_states": completed_activity_session_states,
        })
    return render(request, "staff_dashboard.html", {
        "users": User.objects.all(),
        "activity_states": ActivityState.objects.all(),
        "activity_session_states": ActivitySessionState.objects.all(),
        "activities": activities,
    })


@login_required
def staff_activity_detail(request, activity_key):

    if not request.user.is_staff:
        raise Http404


# Path: pinax/lms/activities/hooks.py
# class ActivitiesDefaultHookSet(object):
# class HookProxy(object):
#     def activity_class_path(self, *args, **kwargs):
#     def all_activities(self):
#     def success_message(self, request, activity):
#     def already_completed_message(self, request, activity):
#     def __getattr__(self, attr):
# 
# Path: pinax/lms/activities/proxies.py
# class ActivityState(BaseProxy):
# 
#     model = models.ActivityState
# 
#     def __init__(self, request, activity_key, activity_class_path):
#         self.request = request
#         self.user = request.user
#         self.activity_key = activity_key
#         self.activity_class_path = activity_class_path
#         super(ActivityState, self).__init__()
# 
#     def get_session_key(self):
#         extra = [self.activity_key]
#         return super(ActivityState, self).get_session_key(extra)
# 
#     def lookup(self, identifier):
#         if identifier is not None:
#             return next(iter(self.model.objects.filter(pk=identifier)), None)
#         if self.request.user.is_authenticated():
#             return next(iter(self.model.objects.filter(user=self.user, activity_key=self.activity_key)), None)
# 
#     def create(self):
#         kwargs = {
#             "activity_key": self.activity_key,
#             "activity_class_path": self.activity_class_path,
#         }
#         if self.user.is_authenticated():
#             kwargs["user"] = self.user
#         obj = self.model.objects.create(**kwargs)
#         return obj.pk, obj
# 
#     @property
#     def completed_count(self):
#         if self.exists:
#             return self.obj.completed_count
#         return 0
# 
#     @property
#     def data(self):
#         if self.exists:
#             return self.obj.data
#         return {}
# 
#     @property
#     def in_progress(self):
#         if self.exists:
#             return self.obj.in_progress
#         return None
# 
#     @property
#     def latest(self):
#         if self.exists:
#             return self.obj.latest
#         return None
# 
#     @property
#     def last_completed(self):
#         if self.exists:
#             return self.obj.last_completed
#         return None
# 
#     @property
#     def all_sessions(self):
#         if self.exists:
#             return self.obj.all_sessions
#         return models.ActivitySessionState.objects.none()
# 
#     @property
#     def progression(self):
#         if self.exists:
#             return self.obj.progression
#         return "start"
# 
# Path: pinax/lms/activities/models.py
# class ActivitySessionState(models.Model):
#     """
#     this stores the state of a particular session of a particular user
#     doing a particular activity.
#     """
# 
#     activity_state = models.ForeignKey(ActivityState, related_name="sessions", on_delete=models.CASCADE)
# 
#     started = models.DateTimeField(default=timezone.now)
#     completed = models.DateTimeField(null=True)  # NULL means in progress
# 
#     data = jsonfield.JSONField(default=dict, blank=True)
# 
#     class Meta:
#         unique_together = [("activity_state", "started")]
# 
#     def mark_completed(self):
#         self.completed = timezone.now()
#         self.save()
#         self.activity_state.completed_count = models.F("completed_count") + 1
#         self.activity_state.save()


# Path: pinax/lms/activities/views.py
from django.contrib.auth.models import User
from django.http import Http404
from django.shortcuts import redirect, render, get_object_or_404
from django.urls import reverse
from django.views.generic import View
from account.decorators import login_required
from .hooks import hookset
from .proxies import ActivityState
from .models import ActivitySessionState
from .utils import load_path_attr


    for key, activity_class_path in hookset.all_activities():
        activity = load_path_attr(activity_class_path)
        activity_states = ActivityState.objects.filter(activity_key=key)
        completed_activity_states = activity_states.exclude(completed_count=0)
        activity_session_states = ActivitySessionState.objects.filter(activity_key=key)
        completed_activity_session_states = activity_session_states.filter(completed__isnull=False)

        activities.append({
            "activity_key": key,
            "title": activity.title,
            "activity_states": activity_states,
            "completed_activity_states": completed_activity_states,
            "activity_session_states": activity_session_states,
            "completed_activity_session_states": completed_activity_session_states,
        })
    return render(request, "staff_dashboard.html", {
        "users": User.objects.all(),
        "activity_states": ActivityState.objects.all(),
        "activity_session_states": ActivitySessionState.objects.all(),
        "activities": activities,
    })


@login_required
def staff_activity_detail(request, activity_key):

    if not request.user.is_staff:
        raise Http404


    activity_states = ActivityState.objects.filter(activity_key=activity_key)