1 from __future__ import absolute_import, print_function
5 from django.db import models
6 from django.db.models.signals import pre_save
8 from samples.models import Library
9 from experiments.models import FlowCell
10 from bcmagic.models import Printer
12 LOGGER = logging.getLogger(__name__)
16 except ImportError, e:
17 # Some systems are using python 2.4, which doesn't have uuid
19 LOGGER.warning('Real uuid is not available, initializing fake uuid module')
25 def _assign_uuid(sender, instance, **kwargs):
27 Assigns a UUID to model on save
29 #print 'Entered _assign_uuid'
30 if instance.uuid is None or len(instance.uuid) != 32:
31 instance.uuid = uuid.uuid1().hex
33 def _switch_default(sender, instance, **kwargs):
35 When new instance has default == True, uncheck all other defaults
38 other_defaults = PrinterTemplate.objects.filter(default=True)
40 for other in other_defaults:
45 class Vendor(models.Model):
46 name = models.CharField(max_length=256)
47 url = models.URLField(blank=True, null=True)
49 def __unicode__(self):
50 return u"%s" % (self.name)
53 class Location(models.Model):
55 name = models.CharField(max_length=256, unique=True)
56 location_description = models.TextField()
58 uuid = models.CharField(max_length=32, blank=True, help_text="Leave blank for automatic UUID generation", editable=False)
60 notes = models.TextField(blank=True, null=True)
62 def __unicode__(self):
63 if len(self.location_description) > 16:
64 return u"%s: %s" % (self.name, self.location_description[0:16]+u"...")
66 return u"%s: %s" % (self.name, self.location_description)
68 pre_save.connect(_assign_uuid, sender=Location)
71 class ItemInfo(models.Model):
72 model_id = models.CharField(max_length=256, blank=True, null=True)
73 part_number = models.CharField(max_length=256, blank=True, null=True)
74 lot_number = models.CharField(max_length=256, blank=True, null=True)
76 url = models.URLField(blank=True, null=True)
78 qty_purchased = models.IntegerField(default=1)
80 vendor = models.ForeignKey(Vendor)
81 purchase_date = models.DateField(blank=True, null=True)
82 warranty_months = models.IntegerField(blank=True, null=True)
84 notes = models.TextField(blank=True, null=True)
86 def __unicode__(self):
89 name += u"model:%s " % (self.model_id)
91 name += u"part:%s " % (self.part_number)
93 name += u"lot:%s " % (self.lot_number)
95 return u"%s: %s" % (name, self.purchase_date)
98 verbose_name_plural = "Item Info"
101 class ItemType(models.Model):
103 name = models.CharField(max_length=64, unique=True)
104 description = models.TextField(blank=True, null=True)
106 def __unicode__(self):
107 return u"%s" % (self.name)
109 class ItemStatus(models.Model):
110 name = models.CharField(max_length=64, unique=True)
111 notes = models.TextField(blank=True, null=True)
113 def __unicode__(self):
117 verbose_name_plural = "Item Status"
120 class Item(models.Model):
122 item_type = models.ForeignKey(ItemType)
124 #Automatically assigned uuid; used for barcode if one is not provided in
126 uuid = models.CharField(max_length=32, blank=True, help_text="Leave blank for automatic UUID generation", unique=True, editable=False)
128 # field for existing barcodes; used instead of uuid if provided
129 barcode_id = models.CharField(max_length=256, blank=True, null=True)
130 force_use_uuid = models.BooleanField(default=False)
132 item_info = models.ForeignKey(ItemInfo)
134 location = models.ForeignKey(Location)
136 status = models.ForeignKey(ItemStatus, blank=True, null=True)
138 creation_date = models.DateTimeField(auto_now_add=True)
139 modified_date = models.DateTimeField(auto_now=True)
141 notes = models.TextField(blank=True, null=True)
143 def __unicode__(self):
144 if self.barcode_id is None or len(self.barcode_id) == 0:
145 return u"invu|%s" % (self.uuid)
147 return u"invb|%s" % (self.barcode_id)
149 def get_absolute_url(self):
150 return '/inventory/%s/' % (self.uuid)
152 pre_save.connect(_assign_uuid, sender=Item)
155 class PrinterTemplate(models.Model):
157 Maps templates to printer to use
159 item_type = models.ForeignKey(ItemType)
160 printer = models.ForeignKey(Printer)
162 default = models.BooleanField(default=False)
164 template = models.TextField()
166 def __unicode__(self):
168 return u'%s %s' % (self.item_type.name, self.printer.name)
170 return u'%s %s (default)' % (self.item_type.name, self.printer.name)
172 pre_save.connect(_switch_default, sender=PrinterTemplate)
175 class LongTermStorage(models.Model):
177 flowcell = models.ForeignKey(FlowCell)
178 libraries = models.ManyToManyField(Library)
180 storage_devices = models.ManyToManyField(Item)
182 creation_date = models.DateTimeField(auto_now_add=True)
183 modified_date = models.DateTimeField(auto_now=True)
185 def __unicode__(self):
186 return u"%s: %s" % (str(self.flowcell), ', '.join([ str(s) for s in self.storage_devices.iterator() ]))
189 verbose_name_plural = "Long Term Storage"
193 class ReagentBase(models.Model):
195 reagent = models.ManyToManyField(Item)
197 creation_date = models.DateTimeField(auto_now_add=True)
198 modified_date = models.DateTimeField(auto_now=True)
204 class ReagentFlowcell(ReagentBase):
206 Links reagents and flowcells
208 flowcell = models.ForeignKey(FlowCell)
210 def __unicode__(self):
211 return u"%s: %s" % (str(self.flowcell), ', '.join([ str(s) for s in self.reagent.iterator() ]))
214 class ReagentLibrary(ReagentBase):
216 Links libraries and flowcells
218 library = models.ForeignKey(Library)
220 def __unicode__(self):
221 return u"%s: %s" % (str(self.library), ', '.join([ str(s) for s in self.reagent.iterator() ]))