3 from django.db import models
4 from django.db.models.signals import pre_save
6 from htsworkflow.frontend.samples.models import Library
7 from htsworkflow.frontend.experiments.models import FlowCell
8 from htsworkflow.frontend.bcmagic.models import Printer
10 LOGGER = logging.getLogger(__name__)
14 except ImportError, e:
15 # Some systems are using python 2.4, which doesn't have uuid
17 LOGGER.warning('Real uuid is not available, initializing fake uuid module')
23 def _assign_uuid(sender, instance, **kwargs):
25 Assigns a UUID to model on save
27 #print 'Entered _assign_uuid'
28 if instance.uuid is None or len(instance.uuid) != 32:
29 instance.uuid = uuid.uuid1().hex
31 def _switch_default(sender, instance, **kwargs):
33 When new instance has default == True, uncheck all other defaults
36 other_defaults = PrinterTemplate.objects.filter(default=True)
38 for other in other_defaults:
43 class Vendor(models.Model):
44 name = models.CharField(max_length=256)
45 url = models.URLField(blank=True, null=True)
47 def __unicode__(self):
48 return u"%s" % (self.name)
51 class Location(models.Model):
53 name = models.CharField(max_length=256, unique=True)
54 location_description = models.TextField()
56 uuid = models.CharField(max_length=32, blank=True, help_text="Leave blank for automatic UUID generation", editable=False)
58 notes = models.TextField(blank=True, null=True)
60 def __unicode__(self):
61 if len(self.location_description) > 16:
62 return u"%s: %s" % (self.name, self.location_description[0:16]+u"...")
64 return u"%s: %s" % (self.name, self.location_description)
66 pre_save.connect(_assign_uuid, sender=Location)
69 class ItemInfo(models.Model):
70 model_id = models.CharField(max_length=256, blank=True, null=True)
71 part_number = models.CharField(max_length=256, blank=True, null=True)
72 lot_number = models.CharField(max_length=256, blank=True, null=True)
74 url = models.URLField(blank=True, null=True)
76 qty_purchased = models.IntegerField(default=1)
78 vendor = models.ForeignKey(Vendor)
79 purchase_date = models.DateField(blank=True, null=True)
80 warranty_months = models.IntegerField(blank=True, null=True)
82 notes = models.TextField(blank=True, null=True)
84 def __unicode__(self):
87 name += u"model:%s " % (self.model_id)
89 name += u"part:%s " % (self.part_number)
91 name += u"lot:%s " % (self.lot_number)
93 return u"%s: %s" % (name, self.purchase_date)
96 verbose_name_plural = "Item Info"
99 class ItemType(models.Model):
101 name = models.CharField(max_length=64, unique=True)
102 description = models.TextField(blank=True, null=True)
104 def __unicode__(self):
105 return u"%s" % (self.name)
107 class ItemStatus(models.Model):
108 name = models.CharField(max_length=64, unique=True)
109 notes = models.TextField(blank=True, null=True)
111 def __unicode__(self):
115 verbose_name_plural = "Item Status"
118 class Item(models.Model):
120 item_type = models.ForeignKey(ItemType)
122 #Automatically assigned uuid; used for barcode if one is not provided in
124 uuid = models.CharField(max_length=32, blank=True, help_text="Leave blank for automatic UUID generation", unique=True, editable=False)
126 # field for existing barcodes; used instead of uuid if provided
127 barcode_id = models.CharField(max_length=256, blank=True, null=True)
128 force_use_uuid = models.BooleanField(default=False)
130 item_info = models.ForeignKey(ItemInfo)
132 location = models.ForeignKey(Location)
134 status = models.ForeignKey(ItemStatus, blank=True, null=True)
136 creation_date = models.DateTimeField(auto_now_add=True)
137 modified_date = models.DateTimeField(auto_now=True)
139 notes = models.TextField(blank=True, null=True)
141 def __unicode__(self):
142 if self.barcode_id is None or len(self.barcode_id) == 0:
143 return u"invu|%s" % (self.uuid)
145 return u"invb|%s" % (self.barcode_id)
147 def get_absolute_url(self):
148 return '/inventory/%s/' % (self.uuid)
150 pre_save.connect(_assign_uuid, sender=Item)
153 class PrinterTemplate(models.Model):
155 Maps templates to printer to use
157 item_type = models.ForeignKey(ItemType)
158 printer = models.ForeignKey(Printer)
160 default = models.BooleanField(default=False)
162 template = models.TextField()
164 def __unicode__(self):
166 return u'%s %s' % (self.item_type.name, self.printer.name)
168 return u'%s %s (default)' % (self.item_type.name, self.printer.name)
170 pre_save.connect(_switch_default, sender=PrinterTemplate)
173 class LongTermStorage(models.Model):
175 flowcell = models.ForeignKey(FlowCell)
176 libraries = models.ManyToManyField(Library)
178 storage_devices = models.ManyToManyField(Item)
180 creation_date = models.DateTimeField(auto_now_add=True)
181 modified_date = models.DateTimeField(auto_now=True)
183 def __unicode__(self):
184 return u"%s: %s" % (str(self.flowcell), ', '.join([ str(s) for s in self.storage_devices.iterator() ]))
187 verbose_name_plural = "Long Term Storage"
191 class ReagentBase(models.Model):
193 reagent = models.ManyToManyField(Item)
195 creation_date = models.DateTimeField(auto_now_add=True)
196 modified_date = models.DateTimeField(auto_now=True)
202 class ReagentFlowcell(ReagentBase):
204 Links reagents and flowcells
206 flowcell = models.ForeignKey(FlowCell)
208 def __unicode__(self):
209 return u"%s: %s" % (str(self.flowcell), ', '.join([ str(s) for s in self.reagent.iterator() ]))
212 class ReagentLibrary(ReagentBase):
214 Links libraries and flowcells
216 library = models.ForeignKey(Library)
218 def __unicode__(self):
219 return u"%s: %s" % (str(self.library), ', '.join([ str(s) for s in self.reagent.iterator() ]))