Stop assuming that library_id == pk.
[htsworkflow.git] / htsworkflow / frontend / samples / models.py
index 2845e81d93317031d2e263765c82200a231e5d63..7199fbf8ea10eb547d56dd27a1955a127cc864b0 100644 (file)
@@ -59,6 +59,12 @@ class Condition(models.Model):
     class Meta:
         ordering = ["condition_name"]
 
+class ExperimentType(models.Model):
+  name = models.CharField(max_length=50, unique=True)
+
+  def __unicode__(self):
+    return unicode(self.name)
+
 class Tag(models.Model): 
   tag_name = models.CharField(max_length=100, db_index=True,blank=False,null=False) 
   TAG_CONTEXT = ( 
@@ -93,8 +99,8 @@ class Species(models.Model):
     ordering = ["scientific_name"]
   
 class Affiliation(models.Model):
-  name = models.CharField(max_length=256, db_index=True, verbose_name='Group Name')
-  contact = models.CharField(max_length=256, null=True, blank=True,verbose_name='Contact Name')  
+  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)
   
   def __unicode__(self):
@@ -109,11 +115,13 @@ class Affiliation(models.Model):
 
 class Library(models.Model):
   id = models.AutoField(primary_key=True)
-  library_id = models.CharField(max_length=30, db_index=True)
+  library_id = models.CharField(max_length=30, db_index=True, unique=True)
   library_name = models.CharField(max_length=100, unique=True)
   library_species = models.ForeignKey(Species)
-  cell_line = models.ForeignKey(Cellline)
-  condition = models.ForeignKey(Condition)
+  # new field 2008 Mar 5, alter table samples_library add column "hidden" NOT NULL default 0;
+  hidden = models.BooleanField()
+  cell_line = models.ForeignKey(Cellline, null=True)
+  condition = models.ForeignKey(Condition, null=True)
   antibody = models.ForeignKey(Antibody,blank=True,null=True)
   # New field Aug/25/08. SQL: alter table fctracker_library add column "lib_affiliation" varchar(256)  NULL;
   affiliations = models.ManyToManyField(Affiliation,related_name='library_affiliations',null=True)
@@ -123,18 +131,7 @@ class Library(models.Model):
   # SQL to add column: alter table fctracker_library add column "replicate" smallint unsigned NULL;
   REPLICATE_NUM = ((1,1),(2,2),(3,3),(4,4))
   replicate =  models.PositiveSmallIntegerField(choices=REPLICATE_NUM,default=1) 
-
-  EXPERIMENT_TYPES = (
-      ('INPUT_RXLCh','INPUT_RXLCh'),
-      ('ChIP-seq', 'ChIP-seq'),
-      ('Sheared', 'Sheared'),
-      ('RNA-seq', 'RNA-seq'),
-      ('Methyl-seq', 'Methyl-seq'),
-      ('DIP-seq', 'DIP-seq'),
-    ) 
-  experiment_type = models.CharField(max_length=50, choices=EXPERIMENT_TYPES,
-                                     default='RNA-seq')
-  
+  experiment_type = models.ForeignKey(ExperimentType)
   creation_date = models.DateField(blank=True, null=True)
   made_for = models.CharField(max_length=50, blank=True, 
       verbose_name='ChIP/DNA/RNA Made By')
@@ -152,10 +149,10 @@ class Library(models.Model):
       ('Done', 'Completed'),
     )
   stopping_point = models.CharField(max_length=25, choices=PROTOCOL_END_POINTS, default='Done')
-  amplified_from_sample = models.ForeignKey('self', blank=True, null=True)  
+  amplified_from_sample = models.ForeignKey('self', blank=True, null=True, related_name='amplified_into_sample')  
   
   undiluted_concentration = models.DecimalField("Concentration", 
-      max_digits=5, decimal_places=2, default=0, blank=True, null=True,
+      max_digits=5, decimal_places=2, blank=True, null=True,
       help_text=u"Undiluted concentration (ng/\u00b5l)") 
       # note \u00b5 is the micro symbol in unicode
   successful_pM = models.DecimalField(max_digits=9, decimal_places=1, blank=True, null=True)
@@ -168,7 +165,8 @@ class Library(models.Model):
   
   class Meta:
     verbose_name_plural = "libraries"
-    ordering = ["-creation_date"] #["-library_id"]
+    #ordering = ["-creation_date"] 
+    ordering = ["-library_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>' 
@@ -233,6 +231,10 @@ class Library(models.Model):
 
   def public(self):
     SITE_ROOT = '/'
-    summary_url = urlparse.urljoin(SITE_ROOT, 'library/%s' % (self.library_id))
+    summary_url = self.get_absolute_url()
     return '<a href="%s">S</a>' % (summary_url,)
   public.allow_tags = True
+
+  @models.permalink
+  def get_absolute_url(self):
+    return ('htsworkflow.frontend.samples.views.library_to_flowcells', [str(self.library_id)])