Get the cms up to the point of rendering a template
This commit is contained in:
0
common/djangoapps/track/__init__.py
Normal file
0
common/djangoapps/track/__init__.py
Normal file
24
common/djangoapps/track/middleware.py
Normal file
24
common/djangoapps/track/middleware.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import json
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
import views
|
||||
|
||||
class TrackMiddleware:
|
||||
def process_request(self, request):
|
||||
try:
|
||||
# We're already logging events, and we don't want to capture user
|
||||
# names/passwords.
|
||||
if request.META['PATH_INFO'] in ['/event', '/login']:
|
||||
return
|
||||
|
||||
event = { 'GET' : dict(request.GET),
|
||||
'POST' : dict(request.POST)}
|
||||
|
||||
# TODO: Confirm no large file uploads
|
||||
event = json.dumps(event)
|
||||
event = event[:512]
|
||||
|
||||
views.server_track(request, request.META['PATH_INFO'], event)
|
||||
except:
|
||||
pass
|
||||
3
common/djangoapps/track/models.py
Normal file
3
common/djangoapps/track/models.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
16
common/djangoapps/track/tests.py
Normal file
16
common/djangoapps/track/tests.py
Normal file
@@ -0,0 +1,16 @@
|
||||
"""
|
||||
This file demonstrates writing tests using the unittest module. These will pass
|
||||
when you run "manage.py test".
|
||||
|
||||
Replace this with more appropriate tests for your application.
|
||||
"""
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
|
||||
class SimpleTest(TestCase):
|
||||
def test_basic_addition(self):
|
||||
"""
|
||||
Tests that 1 + 1 always equals 2.
|
||||
"""
|
||||
self.assertEqual(1 + 1, 2)
|
||||
70
common/djangoapps/track/views.py
Normal file
70
common/djangoapps/track/views.py
Normal file
@@ -0,0 +1,70 @@
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import datetime
|
||||
|
||||
# Create your views here.
|
||||
from django.http import HttpResponse
|
||||
from django.http import Http404
|
||||
from django.conf import settings
|
||||
|
||||
log = logging.getLogger("tracking")
|
||||
|
||||
def log_event(event):
|
||||
event_str = json.dumps(event)
|
||||
log.info(event_str[:settings.TRACK_MAX_EVENT])
|
||||
|
||||
def user_track(request):
|
||||
try: # TODO: Do the same for many of the optional META parameters
|
||||
username = request.user.username
|
||||
except:
|
||||
username = "anonymous"
|
||||
|
||||
try:
|
||||
scookie = request.META['HTTP_COOKIE'] # Get cookies
|
||||
scookie = ";".join([c.split('=')[1] for c in scookie.split(";") if "sessionid" in c]).strip() # Extract session ID
|
||||
except:
|
||||
scookie = ""
|
||||
|
||||
try:
|
||||
agent = request.META['HTTP_USER_AGENT']
|
||||
except:
|
||||
agent = ''
|
||||
|
||||
# TODO: Move a bunch of this into log_event
|
||||
event = {
|
||||
"username" : username,
|
||||
"session" : scookie,
|
||||
"ip" : request.META['REMOTE_ADDR'],
|
||||
"event_source" : "browser",
|
||||
"event_type" : request.GET['event_type'],
|
||||
"event" : request.GET['event'],
|
||||
"agent" : agent,
|
||||
"page" : request.GET['page'],
|
||||
"time": datetime.datetime.utcnow().isoformat(),
|
||||
}
|
||||
log_event(event)
|
||||
return HttpResponse('success')
|
||||
|
||||
def server_track(request, event_type, event, page=None):
|
||||
try:
|
||||
username = request.user.username
|
||||
except:
|
||||
username = "anonymous"
|
||||
|
||||
try:
|
||||
agent = request.META['HTTP_USER_AGENT']
|
||||
except:
|
||||
agent = ''
|
||||
|
||||
event = {
|
||||
"username" : username,
|
||||
"ip" : request.META['REMOTE_ADDR'],
|
||||
"event_source" : "server",
|
||||
"event_type" : event_type,
|
||||
"event" : event,
|
||||
"agent" : agent,
|
||||
"page" : page,
|
||||
"time": datetime.datetime.utcnow().isoformat(),
|
||||
}
|
||||
log_event(event)
|
||||
Reference in New Issue
Block a user