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 if instance.uuid is None or len(instance.uuid) != 32:
32 instance.uuid = uuid.uuid1().hex
35 def _switch_default(sender, instance, **kwargs):
37 When new instance has default == True, uncheck all other defaults
40 other_defaults = PrinterTemplate.objects.filter(default=True)
42 for other in other_defaults:
47 class Vendor(models.Model):
48 name = models.CharField(max_length=256)
49 url = models.URLField(blank=True, null=True)
51 def __unicode__(self):
52 return u"%s" % (self.name)
55 class Location(models.Model):
57 name = models.CharField(max_length=256, unique=True)
58 location_description = models.TextField()
60 uuid = models.CharField(max_length=32,
62 help_text="Leave blank for automatic UUID generation",
65 notes = models.TextField(blank=True, null=True)
67 def __unicode__(self):
68 if len(self.location_description) > 16:
69 return u"%s: %s" % (self.name, self.location_description[0:16]+u"...")
71 return u"%s: %s" % (self.name, self.location_description)
73 post_init.connect(_assign_uuid, sender=Location)
76 class ItemInfo(models.Model):
77 model_id = models.CharField(max_length=256, blank=True, null=True)
78 part_number = models.CharField(max_length=256, blank=True, null=True)
79 lot_number = models.CharField(max_length=256, blank=True, null=True)
81 url = models.URLField(blank=True, null=True)
83 qty_purchased = models.IntegerField(default=1)
85 vendor = models.ForeignKey(Vendor)
86 purchase_date = models.DateField(blank=True, null=True)
87 warranty_months = models.IntegerField(blank=True, null=True)
89 notes = models.TextField(blank=True, null=True)
91 def __unicode__(self):
94 name += u"model:%s " % (self.model_id)
96 name += u"part:%s " % (self.part_number)
98 name += u"lot:%s " % (self.lot_number)
100 return u"%s: %s" % (name, self.purchase_date)
103 verbose_name_plural = "Item Info"
106 class ItemType(models.Model):
108 name = models.CharField(max_length=64, unique=True)
109 description = models.TextField(blank=True, null=True)
111 def __unicode__(self):
112 return u"%s" % (self.name)
115 class ItemStatus(models.Model):
116 name = models.CharField(max_length=64, unique=True)
117 notes = models.TextField(blank=True, null=True)
119 def __unicode__(self):
123 verbose_name_plural = "Item Status"
126 class Item(models.Model):
127 item_type = models.ForeignKey(ItemType)
129 #Automatically assigned uuid; used for barcode if one is not provided in
131 uuid = models.CharField(max_length=32,
133 help_text="Leave blank for automatic UUID generation",
137 # field for existing barcodes; used instead of uuid if provided
138 barcode_id = models.CharField(max_length=256, blank=True, null=True)
139 force_use_uuid = models.BooleanField(default=False)
141 item_info = models.ForeignKey(ItemInfo)
143 location = models.ForeignKey(Location)
145 status = models.ForeignKey(ItemStatus, blank=True, null=True)
147 creation_date = models.DateTimeField(auto_now_add=True)
148 modified_date = models.DateTimeField(auto_now=True)
150 notes = models.TextField(blank=True, null=True)
152 def __unicode__(self):
153 if self.barcode_id is None or len(self.barcode_id) == 0:
154 return u"invu|%s" % (self.uuid)
156 return u"invb|%s" % (self.barcode_id)
158 def get_absolute_url(self):
159 return '/inventory/%s/' % (self.uuid)
161 post_init.connect(_assign_uuid, sender=Item)
164 class PrinterTemplate(models.Model):
166 Maps templates to printer to use
168 item_type = models.ForeignKey(ItemType)
169 printer = models.ForeignKey(Printer)
171 default = models.BooleanField(default=False)
173 template = models.TextField()
175 def __unicode__(self):
177 return u'%s %s' % (self.item_type.name, self.printer.name)
179 return u'%s %s (default)' % (self.item_type.name, self.printer.name)
181 pre_save.connect(_switch_default, sender=PrinterTemplate)
184 class LongTermStorage(models.Model):
185 flowcell = models.ForeignKey(FlowCell)
186 libraries = models.ManyToManyField(Library)
188 storage_devices = models.ManyToManyField(Item)
190 creation_date = models.DateTimeField(auto_now_add=True)
191 modified_date = models.DateTimeField(auto_now=True)
193 def __unicode__(self):
194 return u"%s: %s" % (str(self.flowcell), ', '.join([str(s) for s in self.storage_devices.iterator()]))
197 verbose_name_plural = "Long Term Storage"
200 class ReagentBase(models.Model):
201 reagent = models.ManyToManyField(Item)
203 creation_date = models.DateTimeField(auto_now_add=True)
204 modified_date = models.DateTimeField(auto_now=True)
210 class ReagentFlowcell(ReagentBase):
212 Links reagents and flowcells
214 flowcell = models.ForeignKey(FlowCell)
216 def __unicode__(self):
217 return u"%s: %s" % (str(self.flowcell), ', '.join([str(s) for s in self.reagent.iterator()]))
220 class ReagentLibrary(ReagentBase):
222 Links libraries and flowcells
224 library = models.ForeignKey(Library)
226 def __unicode__(self):
227 return u"%s: %s" % (str(self.library), ', '.join([str(s) for s in self.reagent.iterator()]))