commit e06572672cc356170812f7d26e46a4653bd5916d
Author: Tom Mullins <tmullins@andrew.cmu.edu>
Date:   Fri Mar 22 21:45:44 2013 -0400

    Added add_card_event view and made some minor changes to models

diff --git a/crm/crm/settings.py b/crm/crm/settings.py
index a198e1a..ddfdb75 100644
--- a/crm/crm/settings.py
+++ b/crm/crm/settings.py
@@ -92,7 +92,7 @@ TEMPLATE_LOADERS = (
 MIDDLEWARE_CLASSES = (
     'django.middleware.common.CommonMiddleware',
     'django.contrib.sessions.middleware.SessionMiddleware',
-    'django.middleware.csrf.CsrfViewMiddleware',
+    #'django.middleware.csrf.CsrfViewMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
     'django.contrib.messages.middleware.MessageMiddleware',
     # Uncomment the next line for simple clickjacking protection:
diff --git a/crm/crm/urls.py b/crm/crm/urls.py
index 1bdc33a..a11955f 100644
--- a/crm/crm/urls.py
+++ b/crm/crm/urls.py
@@ -10,6 +10,7 @@ urlpatterns = patterns('',
     # url(r'^$', 'crm.views.home', name='home'),
     # url(r'^crm/', include('crm.foo.urls')),
     url(r'^roboauth/(?P<rfid_tag>[0-9A-Fa-f]+)/(?P<mach_num>\d+)/', 'robocrm.views.roboauth'),
+    url(r'^add_card_event/', 'robocrm.views.add_card_event'),
 
     # Uncomment the admin/doc line below to enable admin documentation:
     # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
diff --git a/crm/robocrm/models.py b/crm/robocrm/models.py
index 1ff1cf4..257c2b7 100644
--- a/crm/robocrm/models.py
+++ b/crm/robocrm/models.py
@@ -88,7 +88,7 @@ class RoboUser(models.Model):
                                   choices=STATUS_CHOICES,
                                   default=GOOD)
   def __unicode__(self):
-    return self.user.username;
+    return self.user.username
 
 # needed for Django Auth model
 def create_roboclub_user(sender, instance, created, **kwargs):
@@ -106,11 +106,11 @@ class Event(models.Model):
   succ = models.BooleanField(default=False)
   imgurl = models.URLField()
   machine = models.ForeignKey('Machine')
-  project = models.ForeignKey('Project')
+  project = models.ForeignKey('Project', null=True)
   matuse = models.TextField()
   
   def __unicode__(self):
-    return u'%s %s %s' (self.type, self.user.username, self.succ)
+    return u'%s %s %s'%(self.type, self.user.user.username, self.succ)
 
 # Project Model
 class Project(models.Model):
diff --git a/crm/robocrm/views.py b/crm/robocrm/views.py
index 5d55614..006560a 100644
--- a/crm/robocrm/views.py
+++ b/crm/robocrm/views.py
@@ -1,12 +1,12 @@
 # Create your views here.
-from django.http import HttpResponse
+from django.http import HttpResponse, Http404
+from django.contrib.auth import authenticate, login
 from robocrm.models import *
 
 def index(request):
   return HttpResponse("Hello again, world!")
 
 def roboauth(request, rfid_tag, mach_num):
-#  return HttpResponse("Hello, world!")
   r = RoboUser.objects.filter(rfid=rfid_tag)
   if r.count() > 0:
     us = r[0]
@@ -18,3 +18,39 @@ def roboauth(request, rfid_tag, mach_num):
   else :
     return HttpResponse("0")
 
+def add_card_event(request):
+  if request.method != 'POST':
+    raise Http404
+  if 'username' in request.POST and 'password' in request.POST:
+    user = authenticate(username=request.POST['username'],
+        password=request.POST['password'])
+    if user is not None and user.is_active:
+      login(request, user)
+
+  if not request.user.is_authenticated() \
+      or not request.user.has_perm('robocrm.add_event'):
+    raise PermissionDenied
+
+  tstart = request.POST['tstart'] # TODO convert to date
+  tend = request.POST['tend']
+  user_id = request.POST['user_id']
+  succ = request.POST['succ'] == '1'
+  imgurl = '' # TODO find url based on tstart
+  machine_id = int(request.POST['machine_id'])
+
+  robouser = RoboUser.objects.get(rfid__iexact=user_id)
+  machine = Machine.objects.get(id__exact=machine_id)
+
+  ev = Event(type='card',
+      tstart=tstart,
+      tend=tend,
+      user=robouser,
+      succ=succ,
+      imgurl=imgurl,
+      machine=machine,
+      project=None,
+      matuse='')
+
+  ev.save()
+
+  return HttpResponse()
