from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save

import uuid

class Survey(models.Model):
    created = models.DateTimeField()
    survey_id = models.CharField(max_length=200, unique=True)
    description = models.TextField()
    definition = models.TextField()

class History(models.Model):
    user = models.ForeignKey(User)
    date = models.DateTimeField(auto_now_add=True)
    survey = models.ForeignKey(Survey)
    epidb_id = models.CharField(max_length=36, null=True)

class Info(models.Model):
    user = models.ForeignKey(User, unique=True)
    last_survey = models.ForeignKey(History)
    last_survey_date = models.DateTimeField()

class Unsaved(models.Model):
    history = models.ForeignKey(History)
    date = models.DateTimeField()
    data = models.TextField()

class EpiDBUser(models.Model):
    user = models.ForeignKey(User, unique=True)
    global_id = models.CharField(max_length=36)

def add_global_id(sender, **kwargs):
    instance = kwargs.get('instance', None)
    print repr(instance)
    try:
        user = EpiDBUser.objects.get(user=instance)
    except EpiDBUser.DoesNotExist:
        user = EpiDBUser()
        user.user = instance
        user.global_id = str(uuid.uuid4())
        print "Global id:", user.global_id
        user.save()
        
post_save.connect(add_global_id, sender=User)

class Profile(models.Model):
    user = models.ForeignKey(User, unique=True)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    complete = models.BooleanField(default=False)
    data = models.TextField()

@@ -163,54 +163,178 @@
     
-created = models.DateTimeField()%0A    survey_id
+survey_id = models.CharField(max_length=50, unique=True)%0A    created = models.DateTimeField(auto_now_add=True)%0A    updated = models.DateTimeField(auto_now=True)%0A    title
  = m
@@ -363,29 +363,16 @@
 ngth=200
-, unique=True
 )%0A    de
@@ -396,24 +396,45 @@
 s.TextField(
+null=True, blank=True
 )%0A    defini
@@ -463,22 +463,63 @@
 d()%0A
-%0Aclass History
+    active = models.BooleanField()%0A%0Aclass Participation
 (mod
@@ -565,16 +565,55 @@
 y(User)%0A
+    survey = models.ForeignKey(Survey)%0A
     date
@@ -659,47 +659,8 @@
 ue)%0A
-    survey = models.ForeignKey(Survey)%0A
     
@@ -717,43 +717,34 @@
 ue)%0A
-%0Aclass Info(models.Model):%0A    user
+    previous_participation
  = m
@@ -764,87 +764,42 @@
 Key(
-User, unique=True)%0A    last_survey = models.ForeignKey(History)%0A    last_survey
+'self')%0A    previous_participation
 _dat
@@ -863,15 +863,21 @@
     
-history
+participation
  = m
@@ -897,15 +897,21 @@
 Key(
-History
+Participation
 )%0A  
@@ -983,127 +983,453 @@
 ass 
-EpiDBUser(models.Model):%0A    user = models.ForeignKey(User, unique=True)%0A    global_id = models.CharField(max_length=36
+SurveyUser(models.Model):%0A    global_id = models.CharField(max_length=36)%0A    last_participation = models.ForeignKey(Participation)%0A    last_participation_date = models.DateTimeField()%0A%0Aclass Profile(models.Model):%0A    user = models.ForeignKey(User, unique=True)%0A    created = models.DateTimeField(auto_now_add=True)%0A    updated = models.DateTimeField(auto_now=True)%0A    complete = models.BooleanField(default=False)%0A    data = models.TextField(
 )%0A%0Ad
@@ -1512,33 +1512,8 @@
 ne)%0A
-    print repr(instance)%0A
     
@@ -1528,29 +1528,30 @@
      user = 
-EpiDB
+Survey
 User.objects
@@ -1581,21 +1581,22 @@
  except 
-EpiDB
+Survey
 User.Doe
@@ -1625,13 +1625,14 @@
 r = 
-EpiDB
+Survey
 User
@@ -1710,51 +1710,8 @@
 ())%0A
-        print %22Global id:%22, user.global_id%0A
     
@@ -1726,24 +1726,16 @@
 .save()%0A
-        
 %0Apost_sa
@@ -1778,265 +1778,4 @@
 r)%0A%0A
-class Profile(models.Model):%0A    user = models.ForeignKey(User, unique=True)%0A    created = models.DateTimeField(auto_now_add=True)%0A    updated = models.DateTimeField(auto_now=True)%0A    complete = models.BooleanField(default=False)%0A    data = models.TextField()%0A

Update survey models.