Add flowcell model type to the flowcell model
[htsworkflow.git] / htsworkflow / frontend / experiments / models.py
index 6c952db1bb431b9f136600e3ec5849f25ebcf78a..2232d964386944269dda19b7da1c12f0bc15342b 100644 (file)
@@ -128,6 +128,30 @@ class Sequencer(models.Model):
 
 pre_save.connect(Sequencer.update_isdefault, sender=Sequencer)
 
+class FlowCellModel(models.Model):
+    name = models.TextField()
+    fixed_time = models.IntegerField(default=0, help_text='(seconds)')
+    per_cycle_time = models.IntegerField(default=0, help_text='(seconds)')
+    isdefault = models.BooleanField(default=False)
+
+    @classmethod
+    def default(cls):
+        d = cls.objects.filter(isdefault=True).all()
+        if len(d) > 0:
+            return d[-1]
+        return None
+
+    @staticmethod
+    def update_isdefault(sender, instance, **kwargs):
+        """Clear default if needed
+        """
+        if instance.isdefault:
+            for s in FlowCellType.objects.filter(isdefault=True).all():
+                if s.id != instance.id:
+                    s.isdefault = False
+                    s.save()
+
+pre_save.connect(FlowCellModel.update_isdefault, sender=FlowCellModel)
 
 class FlowCell(models.Model):
     flowcell_id = models.CharField(max_length=20, unique=True, db_index=True)
@@ -149,6 +173,7 @@ class FlowCell(models.Model):
 
     cluster_station = models.ForeignKey(ClusterStation, default=ClusterStation.default)
     sequencer = models.ForeignKey(Sequencer, default=Sequencer.default)
+    flowcell_model = models.ForeignKey(FlowCellModel, default=FlowCellModel.default)
 
     notes = models.TextField(blank=True)