Refine user handling.
[htsworkflow.git] / htsworkflow / frontend / samples / models.py
index 5b8f6a7da74d9e4631e064ee9f0f093aebb5b2f7..32d63a35d545fafc9f2f78ca295364db266d11a3 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, blank=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"),)
@@ -120,12 +128,13 @@ class LibraryType(models.Model):
     return unicode(self.name)
 
 class Library(models.Model):
-  id = models.AutoField(primary_key=True)
-  library_id = models.CharField(max_length=30, db_index=True, unique=True)
+  id = models.CharField(max_length=10, primary_key=True)
   library_name = models.CharField(max_length=100, unique=True)
   library_species = models.ForeignKey(Species)
   # new field 2008 Mar 5, alter table samples_library add column "hidden" NOT NULL default 0;
   hidden = models.BooleanField()
+  # new field 2009 Oct 6, alter table samples_library add column "account_number" varchar(100) NULL
+  account_number = models.CharField(max_length=100, null=True, blank=True)
   cell_line = models.ForeignKey(Cellline, blank=True, null=True, verbose_name="Background")
   condition = models.ForeignKey(Condition, blank=True, null=True)
   antibody = models.ForeignKey(Antibody,blank=True,null=True)
@@ -138,7 +147,7 @@ class Library(models.Model):
   REPLICATE_NUM = ((1,1),(2,2),(3,3),(4,4))
   replicate =  models.PositiveSmallIntegerField(choices=REPLICATE_NUM,default=1) 
   experiment_type = models.ForeignKey(ExperimentType)
-  library_type = models.ForeignKey(LibraryType, null=True)
+  library_type = models.ForeignKey(LibraryType, blank=True, null=True)
   creation_date = models.DateField(blank=True, null=True)
   made_for = models.CharField(max_length=50, blank=True, 
       verbose_name='ChIP/DNA/RNA Made By')
@@ -168,12 +177,12 @@ class Library(models.Model):
   notes = models.TextField(blank=True)
   
   def __unicode__(self):
-    return u'#%s: %s' % (self.library_id, self.library_name)
+    return u'#%s: %s' % (self.id, self.library_name)
   
   class Meta:
     verbose_name_plural = "libraries"
     #ordering = ["-creation_date"] 
-    ordering = ["-library_id"]
+    ordering = ["-id"]
   
   def antibody_name(self):
     str ='<a target=_self href="/admin/samples/antibody/'+self.antibody.id.__str__()+'/" title="'+self.antibody.__str__()+'">'+self.antibody.nickname+'</a>' 
@@ -208,15 +217,15 @@ class Library(models.Model):
     return u'%s' % ( ", ".join(ar))
 
   def DataRun(self):
-    str ='<a target=_self href="/admin/experiments/datarun/?q='+self.library_id+'" title="Check All Data Runs for This Specific Library ..." ">Data Run</a>' 
+    str ='<a target=_self href="/admin/experiments/datarun/?q='+self.id+'" title="Check All Data Runs for This Specific Library ..." ">Data Run</a>' 
     return str
   DataRun.allow_tags = True
 
   def aligned_m_reads(self):
-    return getLibReads(self.library_id)
+    return getLibReads(self.id)
 
   def aligned_reads(self):
-    res = getLibReads(self.library_id)
+    res = getLibReads(self.id)
 
     # Check data sanity
     if res[2] != "OK":
@@ -253,4 +262,32 @@ class Library(models.Model):
 
   @models.permalink
   def get_absolute_url(self):
-    return ('htsworkflow.frontend.samples.views.library_to_flowcells', [str(self.library_id)])
+    return ('htsworkflow.frontend.samples.views.library_to_flowcells', [str(self.id)])
+
+class HTSUser(User):
+    """
+    Provide some site-specific customization for the django user class
+    """
+    #objects = UserManager()
+
+    class Meta:
+        ordering = ['first_name', 'last_name', 'username']
+
+    def admin_url(self):
+        return '/admin/%s/%s/%d' % (self._meta.app_label, self._meta.module_name, self.id)
+
+    def __unicode__(self):
+        #return unicode(self.username) + u" (" + unicode(self.get_full_name()) + u")"
+        return unicode(self.get_full_name()) + u' (' + unicode(self.username) + ')'
+    
+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)