1 from __future__ import absolute_import, print_function
5 from django.db import models
6 from django.db.models.signals import pre_save, post_init
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 as 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')
27 def _assign_uuid(sender, instance, **kwargs):
29 Assigns a UUID to model on save
31 #print 'Entered _assign_uuid'
32 if instance.uuid is None or len(instance.uuid) != 32:
33 instance.uuid = uuid.uuid1().hex
36 def _switch_default(sender, instance, **kwargs):
38 When new instance has default == True, uncheck all other defaults
41 other_defaults = PrinterTemplate.objects.filter(default=True)
43 for other in other_defaults:
48 class Vendor(models.Model):
49 name = models.CharField(max_length=256)
50 url = models.URLField(blank=True, null=True)
52 def __unicode__(self):
53 return u"%s" % (self.name)
56 class Location(models.Model):
58 name = models.CharField(max_length=256, unique=True)
59 location_description = models.TextField()
61 uuid = models.CharField(max_length=32,
63 help_text="Leave blank for automatic UUID generation",
66 notes = models.TextField(blank=True, null=True)
68 def __unicode__(self):
69 if len(self.location_description) > 16:
70 return u"%s: %s" % (self.name, self.location_description[0:16]+u"...")
72 return u"%s: %s" % (self.name, self.location_description)
74 post_init.connect(_assign_uuid, sender=Location)
77 class ItemInfo(models.Model):
78 model_id = models.CharField(max_length=256, blank=True, null=True)
79 part_number = models.CharField(max_length=256, blank=True, null=True)
80 lot_number = models.CharField(max_length=256, blank=True, null=True)
82 url = models.URLField(blank=True, null=True)
84 qty_purchased = models.IntegerField(default=1)
86 vendor = models.ForeignKey(Vendor)
87 purchase_date = models.DateField(blank=True, null=True)
88 warranty_months = models.IntegerField(blank=True, null=True)
90 notes = models.TextField(blank=True, null=True)
92 def __unicode__(self):
95 name += u"model:%s " % (self.model_id)
97 name += u"part:%s " % (self.part_number)
99 name += u"lot:%s " % (self.lot_number)
101 return u"%s: %s" % (name, self.purchase_date)
104 verbose_name_plural = "Item Info"
107 class ItemType(models.Model):
109 name = models.CharField(max_length=64, unique=True)
110 description = models.TextField(blank=True, null=True)
112 def __unicode__(self):
113 return u"%s" % (self.name)
116 class ItemStatus(models.Model):
117 name = models.CharField(max_length=64, unique=True)
118 notes = models.TextField(blank=True, null=True)
120 def __unicode__(self):
124 verbose_name_plural = "Item Status"
127 class Item(models.Model):
128 item_type = models.ForeignKey(ItemType)
130 #Automatically assigned uuid; used for barcode if one is not provided in
132 uuid = models.CharField(max_length=32,
134 help_text="Leave blank for automatic UUID generation",
138 # field for existing barcodes; used instead of uuid if provided
139 barcode_id = models.CharField(max_length=256, blank=True, null=True)
140 force_use_uuid = models.BooleanField(default=False)
142 item_info = models.ForeignKey(ItemInfo)
144 location = models.ForeignKey(Location)
146 status = models.ForeignKey(ItemStatus, blank=True, null=True)
148 creation_date = models.DateTimeField(auto_now_add=True)
149 modified_date = models.DateTimeField(auto_now=True)
151 notes = models.TextField(blank=True, null=True)
153 def __unicode__(self):
154 if self.barcode_id is None or len(self.barcode_id) == 0:
155 return u"invu|%s" % (self.uuid)
157 return u"invb|%s" % (self.barcode_id)
159 def get_absolute_url(self):
160 return '/inventory/%s/' % (self.uuid)
162 post_init.connect(_assign_uuid, sender=Item)
165 class PrinterTemplate(models.Model):
167 Maps templates to printer to use
169 item_type = models.ForeignKey(ItemType)
170 printer = models.ForeignKey(Printer)
172 default = models.BooleanField(default=False)
174 template = models.TextField()
176 def __unicode__(self):
178 return u'%s %s' % (self.item_type.name, self.printer.name)
180 return u'%s %s (default)' % (self.item_type.name, self.printer.name)
182 pre_save.connect(_switch_default, sender=PrinterTemplate)
185 class LongTermStorage(models.Model):
186 flowcell = models.ForeignKey(FlowCell)
187 libraries = models.ManyToManyField(Library)
189 storage_devices = models.ManyToManyField(Item)
191 creation_date = models.DateTimeField(auto_now_add=True)
192 modified_date = models.DateTimeField(auto_now=True)
194 def __unicode__(self):
195 return u"%s: %s" % (str(self.flowcell), ', '.join([str(s) for s in self.storage_devices.iterator()]))
198 verbose_name_plural = "Long Term Storage"
201 class ReagentBase(models.Model):
202 reagent = models.ManyToManyField(Item)
204 creation_date = models.DateTimeField(auto_now_add=True)
205 modified_date = models.DateTimeField(auto_now=True)
211 class ReagentFlowcell(ReagentBase):
213 Links reagents and flowcells
215 flowcell = models.ForeignKey(FlowCell)
217 def __unicode__(self):
218 return u"%s: %s" % (str(self.flowcell), ', '.join([str(s) for s in self.reagent.iterator()]))
221 class ReagentLibrary(ReagentBase):
223 Links libraries and flowcells
225 library = models.ForeignKey(Library)
227 def __unicode__(self):
228 return u"%s: %s" % (str(self.library), ', '.join([str(s) for s in self.reagent.iterator()]))