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
13 except ImportError, e:
14 # Some systems are using python 2.4, which doesn't have uuid
16 logging.warning('Real uuid is not available, initializing fake uuid module')
22 def _assign_uuid(sender, instance, **kwargs):
24 Assigns a UUID to model on save
26 print 'Entered _assign_uuid'
27 if instance.uuid is None or len(instance.uuid) != 32:
28 instance.uuid = uuid.uuid1().hex
30 def _switch_default(sender, instance, **kwargs):
32 When new instance has default == True, uncheck all other defaults
35 other_defaults = PrinterTemplate.objects.filter(default=True)
37 for other in other_defaults:
42 class Vendor(models.Model):
43 name = models.CharField(max_length=256)
44 url = models.URLField(blank=True, null=True)
46 def __unicode__(self):
47 return u"%s" % (self.name)
50 class Location(models.Model):
52 name = models.CharField(max_length=256, unique=True)
53 location_description = models.TextField()
55 uuid = models.CharField(max_length=32, blank=True, help_text="Leave blank for automatic UUID generation", editable=False)
57 notes = models.TextField(blank=True, null=True)
59 def __unicode__(self):
60 if len(self.location_description) > 16:
61 return u"%s: %s" % (self.name, self.location_description[0:16]+u"...")
63 return u"%s: %s" % (self.name, self.location_description)
65 pre_save.connect(_assign_uuid, sender=Location)
68 class ItemInfo(models.Model):
69 model_id = models.CharField(max_length=256, blank=True, null=True)
70 part_number = models.CharField(max_length=256, blank=True, null=True)
71 lot_number = models.CharField(max_length=256, blank=True, null=True)
73 url = models.URLField(blank=True, null=True)
75 qty_purchased = models.IntegerField(default=1)
77 vendor = models.ForeignKey(Vendor)
78 purchase_date = models.DateField(blank=True, null=True)
79 warranty_months = models.IntegerField(blank=True, null=True)
81 notes = models.TextField(blank=True, null=True)
83 def __unicode__(self):
86 name += u"model:%s " % (self.model_id)
88 name += u"part:%s " % (self.part_number)
90 name += u"lot:%s " % (self.lot_number)
92 return u"%s: %s" % (name, self.purchase_date)
95 verbose_name_plural = "Item Info"
98 class ItemType(models.Model):
100 name = models.CharField(max_length=64, unique=True)
101 description = models.TextField(blank=True, null=True)
103 def __unicode__(self):
104 return u"%s" % (self.name)
106 class ItemStatus(models.Model):
107 name = models.CharField(max_length=64, unique=True)
108 notes = models.TextField(blank=True, null=True)
110 def __unicode__(self):
114 verbose_name_plural = "Item Status"
117 class Item(models.Model):
119 item_type = models.ForeignKey(ItemType)
121 #Automatically assigned uuid; used for barcode if one is not provided in
123 uuid = models.CharField(max_length=32, blank=True, help_text="Leave blank for automatic UUID generation", unique=True, editable=False)
125 # field for existing barcodes; used instead of uuid if provided
126 barcode_id = models.CharField(max_length=256, blank=True, null=True)
127 force_use_uuid = models.BooleanField(default=False)
129 item_info = models.ForeignKey(ItemInfo)
131 location = models.ForeignKey(Location)
133 status = models.ForeignKey(ItemStatus, blank=True, null=True)
135 creation_date = models.DateTimeField(auto_now_add=True)
136 modified_date = models.DateTimeField(auto_now=True)
138 notes = models.TextField(blank=True, null=True)
140 def __unicode__(self):
141 if self.barcode_id is None or len(self.barcode_id) == 0:
142 return u"invu|%s" % (self.uuid)
144 return u"invb|%s" % (self.barcode_id)
146 def get_absolute_url(self):
147 return '/inventory/%s/' % (self.uuid)
149 pre_save.connect(_assign_uuid, sender=Item)
152 class PrinterTemplate(models.Model):
154 Maps templates to printer to use
156 item_type = models.ForeignKey(ItemType)
157 printer = models.ForeignKey(Printer)
159 default = models.BooleanField()
161 template = models.TextField()
163 def __unicode__(self):
165 return u'%s %s' % (self.item_type.name, self.printer.name)
167 return u'%s %s (default)' % (self.item_type.name, self.printer.name)
169 pre_save.connect(_switch_default, sender=PrinterTemplate)
172 class LongTermStorage(models.Model):
174 flowcell = models.ForeignKey(FlowCell)
175 libraries = models.ManyToManyField(Library)
177 storage_devices = models.ManyToManyField(Item)
179 creation_date = models.DateTimeField(auto_now_add=True)
180 modified_date = models.DateTimeField(auto_now=True)
182 def __unicode__(self):
183 return u"%s: %s" % (str(self.flowcell), ', '.join([ str(s) for s in self.storage_devices.iterator() ]))
186 verbose_name_plural = "Long Term Storage"
190 class ReagentBase(models.Model):
192 reagent = models.ManyToManyField(Item)
194 creation_date = models.DateTimeField(auto_now_add=True)
195 modified_date = models.DateTimeField(auto_now=True)
201 class ReagentFlowcell(ReagentBase):
203 Links reagents and flowcells
205 flowcell = models.ForeignKey(FlowCell)
207 def __unicode__(self):
208 return u"%s: %s" % (str(self.flowcell), ', '.join([ str(s) for s in self.reagent.iterator() ]))
211 class ReagentLibrary(ReagentBase):
213 Links libraries and flowcells
215 library = models.ForeignKey(Library)
217 def __unicode__(self):
218 return u"%s: %s" % (str(self.library), ', '.join([ str(s) for s in self.reagent.iterator() ]))