def log_login(sender, **kwargs): """A user has logged in. Create a Session object in the database and log an event.""" django_user = kwargs['user'] try: clusive_user = ClusiveUser.objects.get(user=django_user) user_agent = kwargs['request'].META.get('HTTP_USER_AGENT', '') login_session = LoginSession(user=clusive_user, userAgent=user_agent) login_session.save() # Put the ID of the database object into the HTTP session so that we know which one to close later. django_session = kwargs['request'].session django_session[SESSION_ID_KEY] = login_session.id.__str__() # Store the user's "Current Period" - in the case of teachers who may be associated with more than one Period, # this will be the first-listed one. Teachers should have some mechanism of changing their current period # in the user interface, so they can interact with any of their classes; this should update the current period # as stored in the session so that event logging will use the correct one. periods = clusive_user.periods.all() current_period = periods.first() if periods else None if (current_period): django_session[PERIOD_KEY] = current_period.id # Create an event event = Event.build(type='SESSION_EVENT', action='LOGGED_IN', login_session=login_session, group=current_period) event.save() logger.debug("Login by user %s at %s" % (clusive_user, event.eventTime)) except ClusiveUser.DoesNotExist: logger.warning("Login by a non-Clusive user: %s", django_user)
def log_annotation_action(sender, **kwargs): """User adds, deletes, or undeletes an annotation""" common_event_args = get_common_event_args(kwargs) action = kwargs.get('action') # Should be HIGHLIGHTED or REMOVED annotation = kwargs.get('annotation') logger.debug("Annotation %s: %s" % (action, annotation)) event = Event.build(type='ANNOTATION_EVENT', action=action, value=annotation.clean_text(), **common_event_args) event.save()
def get(self, request, *args, **kwargs): # Get event ID, it is needed during page construction event = Event.build(type='VIEW_EVENT', action='VIEWED', session=request.session) self.event_id = event.id # Super will create the page as normal result = super().get(request, *args, **kwargs) # Configure_event run last so it can use any info generated during page construction. self.configure_event(event) event.save() return result
def create_event(kwargs, control=None, value=None, action='USED', event_type='TOOL_USE_EVENT'): common_event_args = get_common_event_args(kwargs) event = Event.build(type=event_type, action=action, control=control, value=value, **common_event_args) if event: # TODO: this doesn't validate the object by the rules like # limited choices for ACTION and TYPE based on Caliper's spec # See https://docs.djangoproject.com/en/3.1/ref/models/instances/#validating-objects event.save()
def log_logout(sender, **kwargs): """A user has logged out. Find the Session object in the database and set the end time.""" django_session = kwargs['request'].session login_session_id = django_session.get(SESSION_ID_KEY, None) if (login_session_id): login_session = LoginSession.objects.get(id=login_session_id) # Create an event event = Event.build(type='SESSION_EVENT', action='LOGGED_OUT', session=django_session) if event: event.save() # Close out session login_session.endedAtTime = timezone.now() login_session.save() clusive_user = login_session.user logger.debug("Logout user %s at %s" % (clusive_user, event.eventTime))
def log_timeout(sender, **kwargs): """A user's session has been timed out after some period of inactivity""" if(kwargs['user'] and kwargs['user'].is_authenticated): django_session = kwargs['session'] login_session_id = django_session.get(SESSION_ID_KEY, None) period_id = django_session.get(PERIOD_KEY, None) try: clusive_user = ClusiveUser.objects.get(user=kwargs['user']) # should match what's in loginsession login_session = LoginSession.objects.get(id=login_session_id) period = Period.objects.filter(id=period_id).first() # May be null # Create an event event = Event.build(type='SESSION_EVENT', action='TIMED_OUT', login_session=login_session, group=period) event.save() # Close out session login_session.endedAtTime = timezone.now() login_session.save() logger.debug("Timeout user %s", clusive_user) except ClusiveUser.DoesNotExist: logger.debug("Timeout of non-Clusive user session: %s", kwargs['user'])
def configure_event(self, event: Event): event.page = 'Wordbank'
def configure_event(self, event: Event): event.page = 'Reading' event.book_version_id = self.book_version.id event.tip_type = self.tip_shown
def configure_event(self, event: Event): event.page = 'ManageStudent'
def configure_event(self, event: Event): event.page = 'ManageClass'
def configure_event(self, event: Event): event.page = 'EditMetadataReplace'
def configure_event(self, event: Event): event.page = 'UploadReplacement'
def configure_event(self, event: Event): event.page = 'UploadNew'