Minor updates to inventory schema based on input from Lorian.
[htsworkflow.git] / htsworkflow / frontend / inventory / models.py
1 from django.db import models
2 from django.db.models.signals import pre_save
3
4 from htsworkflow.frontend.samples.models import Library
5 from htsworkflow.frontend.experiments.models import FlowCell
6
7
8 import uuid
9
10 def _assign_uuid(sender, instance, **kwargs):
11     """
12     Assigns a UUID to model on save
13     """
14     print 'Entered _assign_uuid'
15     if instance.uuid is None or len(instance.uuid) != 32:
16         instance.uuid = uuid.uuid1().hex
17
18
19 class Vendor(models.Model):
20     name = models.CharField(max_length=256)
21     url = models.URLField(blank=True, null=True)
22
23     def __unicode__(self):
24         return u"%s" % (self.name)
25
26
27 class Location(models.Model):
28     
29     name = models.CharField(max_length=256, unique=True)
30     location_description = models.TextField()
31     
32     uuid = models.CharField(max_length=32, blank=True, help_text="Leave blank for automatic UUID generation")
33     
34     notes = models.TextField(blank=True, null=True)
35     
36     def __unicode__(self):
37         if len(self.location_description) > 16:
38             return u"%s: %s" % (self.name, self.location_description[0:16]+u"...")
39         else:
40             return u"%s: %s" % (self.name, self.location_description)
41
42 pre_save.connect(_assign_uuid, sender=Location)
43
44 class ItemInfo(models.Model):
45     model_id = models.CharField(max_length=256, blank=True, null=True)
46     part_number = models.CharField(max_length=256, blank=True, null=True)
47     lot_number = models.CharField(max_length=256, blank=True, null=True)
48     
49     url = models.URLField(blank=True, null=True)
50     
51     qty_purchased = models.IntegerField(default=1)
52     
53     vendor = models.ForeignKey(Vendor)
54     purchase_date = models.DateField(blank=True, null=True)
55     warranty_months = models.IntegerField(blank=True, null=True)
56     
57     notes = models.TextField(blank=True, null=True)
58     
59     def __unicode__(self):
60         name = u''
61         if self.model_id:
62             name += u"model:%s " % (self.model_id)
63         if self.part_number:
64             name += u"part:%s " % (self.part_number)
65         if self.lot_number:
66             name += u"lot:%s " % (self.lot_number)
67             
68         return u"%s: %s" % (name, self.purchase_date)
69
70
71 class ItemType(models.Model):
72     
73     name = models.CharField(max_length=64, unique=True)
74     description = models.TextField(blank=True, null=True)
75     
76     def __unicode__(self):
77         return u"%s" % (self.name)
78
79 class ItemStatus(models.Model):
80     name = models.CharField(max_length=64, unique=True)
81     notes = models.TextField(blank=True, null=True)
82     
83     def __unicode__(self):
84         return self.name
85
86 class Item(models.Model):
87     
88     item_type = models.ForeignKey(ItemType)
89     
90     #Automatically assigned uuid; used for barcode if one is not provided in
91     # barcode_id
92     uuid = models.CharField(max_length=32, blank=True, help_text="Leave blank for automatic UUID generation")
93     
94     # field for existing barcodes; used instead of uuid if provided
95     barcode_id = models.CharField(max_length=256, blank=True, null=True)
96     force_use_uuid = models.BooleanField(default=False)
97     
98     item_info = models.ForeignKey(ItemInfo)
99     
100     location = models.ForeignKey(Location)
101     
102     status = models.ForeignKey(ItemStatus, blank=True, null=True)
103     
104     creation_date = models.DateTimeField(auto_now_add=True)
105     modified_date = models.DateTimeField(auto_now=True)
106     
107     notes = models.TextField(blank=True, null=True)
108     
109     def __unicode__(self):
110         if self.barcode_id is None or len(self.barcode_id) == 0:
111             return u"invu|%s" % (self.uuid)
112         else:
113             return u"invb|%s" % (self.barcode_id)
114             
115 pre_save.connect(_assign_uuid, sender=Item)
116
117
118 class LongTermStorage(models.Model):
119     
120     flowcell = models.ForeignKey(FlowCell)
121     libraries = models.ManyToManyField(Library)
122
123     storage_devices = models.ManyToManyField(Item)
124     
125     def __unicode__(self):
126         return u"%s: %s" % (str(self.flowcell), ', '.join([ str(s) for s in self.storage_devices.iterator() ]))