Tracking is partially in place

This commit is contained in:
Piotr Mitros
2011-12-31 11:38:28 -05:00
parent 55f8c58784
commit 4355de794b
6 changed files with 83 additions and 0 deletions

View File

@@ -103,6 +103,7 @@ INSTALLED_APPS = (
'textbook',
'staticbook',
'simplewiki',
'track',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:

0
track/__init__.py Normal file
View File

3
track/models.py Normal file
View File

@@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

16
track/tests.py Normal file
View 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)

62
track/views.py Normal file
View File

@@ -0,0 +1,62 @@
# Create your views here.
from django.http import HttpResponse
from django.http import Http404
from django.conf import settings
import json
import tempfile
directory = tempfile.mkdtemp(prefix = settings.TRACK_DIR)
logfile = None
file_index = 0
log_index = 0
MAXLOG = 5
def make_file():
global logfile, log_index, file_index
if logfile != None:
logfile.close()
logfile = open(directory+"/%05i"%(file_index), "w")
file_index = file_index + 1
log_index = 0
def log_event(event):
global logfile, log_index
if settings.TRACK_DIR == None:
print event
return
if logfile == None or log_index >= MAXLOG:
make_file()
event_str = json.dumps(event)
logfile.write(event_str+'\n')
log_index = log_index + 1
def user_track(request):
event = {
"username" : request.user.username,
"session" : request.META['HTTP_COOKIE'],
"ip" : request.META['REMOTE_ADDR'],
"event_source" : "browser",
"event_type" : request.GET['event_type'],
"event" : request.GET['event'],
"agent" : request.META['HTTP_USER_AGENT'],
"page" : request.GET['page'],
}
log_event(event)
return HttpResponse('success')
def server_track(request, event_type, event, page=None):
event = {
"username" : request.user.username,
"ip" : request.META['REMOTE_ADDR'],
"ip" : request.META['REMOTE_ADDR'],
"event_source" : "server",
"event_type" : event_type,
"event" : event,
"agent" : request.META['HTTP_USER_AGENT'],
"page" : page,
}
log_event(event)

View File

@@ -6,6 +6,7 @@ import django.contrib.auth.views
# admin.autodiscover()
urlpatterns = patterns('',
url(r'^event$', 'track.views.user_track'),
(r'^wiki/', include('simplewiki.urls')),
url(r'^courseware/(?P<course>[^/]*)/(?P<chapter>[^/]*)/(?P<section>[^/]*)/$', 'courseware.views.index'),
url(r'^courseware/(?P<course>[^/]*)/(?P<chapter>[^/]*)/$', 'courseware.views.index'),