print killing wsgi; minor fix.
[htsworkflow.git] / htsworkflow / frontend / inventory / models.py
index 8f54fc3abcc87a43739f34c0d1c40a487fd1a69a..dd0844330b7a0ad7f179b515ee0c6e96da6b0a53 100644 (file)
@@ -1,3 +1,5 @@
+import logging
+
 from django.db import models
 from django.db.models.signals import pre_save
 
@@ -5,13 +7,22 @@ from htsworkflow.frontend.samples.models import Library
 from htsworkflow.frontend.experiments.models import FlowCell
 
 
-import uuid
+try:
+    import uuid
+except ImportError, e:
+    # Some systems are using python 2.4, which doesn't have uuid
+    # this is a stub
+    logging.warning('Real uuid is not available, initializing fake uuid module')
+    class uuid:
+        def uuid1(self):
+            self.hex = None
+            return self
 
 def _assign_uuid(sender, instance, **kwargs):
     """
     Assigns a UUID to model on save
     """
-    print 'Entered _assign_uuid'
+    #print 'Entered _assign_uuid'
     if instance.uuid is None or len(instance.uuid) != 32:
         instance.uuid = uuid.uuid1().hex
 
@@ -89,7 +100,7 @@ class Item(models.Model):
     
     #Automatically assigned uuid; used for barcode if one is not provided in
     # barcode_id
-    uuid = models.CharField(max_length=32, blank=True, help_text="Leave blank for automatic UUID generation")
+    uuid = models.CharField(max_length=32, blank=True, help_text="Leave blank for automatic UUID generation", unique=True)
     
     # field for existing barcodes; used instead of uuid if provided
     barcode_id = models.CharField(max_length=256, blank=True, null=True)
@@ -112,6 +123,9 @@ class Item(models.Model):
         else:
             return u"invb|%s" % (self.barcode_id)
             
+    def get_absolute_url(self):
+        return '/inventory/%s/' % (self.uuid)
+            
 pre_save.connect(_assign_uuid, sender=Item)
 
 
@@ -122,5 +136,40 @@ class LongTermStorage(models.Model):
 
     storage_devices = models.ManyToManyField(Item)
     
+    creation_date = models.DateTimeField(auto_now_add=True)
+    modified_date = models.DateTimeField(auto_now=True)
+    
+    def __unicode__(self):
+        return u"%s: %s" % (str(self.flowcell), ', '.join([ str(s) for s in self.storage_devices.iterator() ]))
+        
+
+
+class ReagentBase(models.Model):
+    
+    reagent = models.ManyToManyField(Item)
+    
+    creation_date = models.DateTimeField(auto_now_add=True)
+    modified_date = models.DateTimeField(auto_now=True)
+    
+    class Meta:
+        abstract = True
+
+
+class ReagentFlowcell(ReagentBase):
+    """
+    Links reagents and flowcells
+    """
+    flowcell = models.ForeignKey(FlowCell)
+    
+    def __unicode__(self):
+        return u"%s: %s" % (str(self.flowcell), ', '.join([ str(s) for s in self.reagent.iterator() ]))
+   
+
+class ReagentLibrary(ReagentBase):
+    """
+    Links libraries and flowcells
+    """
+    library = models.ForeignKey(Library)
+    
     def __unicode__(self):
-        return u"%s: %s" % (str(self.flowcell), ', '.join([ str(s) for s in self.storage_devices.iterator() ]))
\ No newline at end of file
+        return u"%s: %s" % (str(self.library), ', '.join([ str(s) for s in self.reagent.iterator() ]))