Force addition of HTSUser object if someone is creating an auth_users object
[htsworkflow.git] / htsworkflow / frontend / samples / models.py
index 8650061e9550cc82ed80a09ecd63602c63d83475..749c82eee4cb9e99aceafb783fa6241cf942091e 100644 (file)
@@ -1,6 +1,8 @@
 import urlparse
 from django.db import models
-from django.contrib.auth.models import User
+from django.contrib.auth.models import User, UserManager
+from django.db.models.signals import pre_save, post_save
+from django.db import connection
 from htsworkflow.frontend import settings
 from htsworkflow.frontend.reports.libinfopar import *
 
@@ -102,6 +104,8 @@ class Affiliation(models.Model):
   name = models.CharField(max_length=256, db_index=True, verbose_name='Name')
   contact = models.CharField(max_length=256, null=True, blank=True,verbose_name='Lab Name')  
   email = models.EmailField(null=True,blank=True)
+  users = models.ManyToManyField('HTSUser', null=True)
+  users.admin_order_field = "username"
   
   def __unicode__(self):
     str = unicode(self.name)
@@ -109,6 +113,10 @@ class Affiliation(models.Model):
       str += u' ('+self.contact+u')' 
     return str
 
+  def Users(self):
+      users = self.users.all().order_by('username')
+      return ", ".join([unicode(a) for a in users ])
+
   class Meta:
     ordering = ["name","contact"]
     unique_together = (("name", "contact"),)
@@ -254,3 +262,27 @@ class Library(models.Model):
   @models.permalink
   def get_absolute_url(self):
     return ('htsworkflow.frontend.samples.views.library_to_flowcells', [str(self.library_id)])
+
+class HTSUser(User):
+    """
+    Provide some site-specific customization for the django user class
+    """
+    #objects = UserManager()
+
+    class Meta:
+        ordering = ['username']
+
+    def admin_url(self):
+        return '/admin/%s/%s/%d' % (self._meta.app_label, self._meta.module_name, self.id)
+    
+def HTSUserInsertID(sender, instance, **kwargs):
+    """
+    Force addition of HTSUsers when someone just modifies the auth_user object
+    """
+    u = HTSUser.objects.filter(pk=instance.id)
+    if len(u) == 0:
+        cursor = connection.cursor()
+        cursor.execute('INSERT INTO samples_htsuser (user_ptr_id) VALUES (%s);' % (instance.id,))
+        cursor.close()
+    
+post_save.connect(HTSUserInsertID, sender=User)