26781ee75e8ec1fc64931226c917c1a56c8a3145
[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)
46     model_url = models.URLField(blank=True, null=True)
47     
48     qty_purchased = models.IntegerField(default=1)
49     
50     vendor = models.ForeignKey(Vendor)
51     purchase_date = models.DateField(blank=True, null=True)
52     warranty_months = models.IntegerField(blank=True, null=True)
53     
54     def __unicode__(self):
55         return u"%s: %s" % (self.model_id, self.purchase_date)
56
57
58 class ItemType(models.Model):
59     
60     name = models.CharField(max_length=64, unique=True)
61     description = models.TextField(blank=True, null=True)
62     
63     def __unicode__(self):
64         return u"%s" % (self.name)
65     
66
67 class Item(models.Model):
68     
69     item_type = models.ForeignKey(ItemType)
70     
71     #Automatically assigned uuid; used for barcode if one is not provided in
72     # barcode_id
73     uuid = models.CharField(max_length=32, blank=True, help_text="Leave blank for automatic UUID generation")
74     
75     # field for existing barcodes; used instead of uuid if provided
76     barcode_id = models.CharField(max_length=256, blank=True, null=True)
77     force_use_uuid = models.BooleanField(default=False)
78     
79     item_info = models.ForeignKey(ItemInfo)
80     
81     location = models.ForeignKey(Location)
82     
83     creation_date = models.DateTimeField(auto_now_add=True)
84     modified_date = models.DateTimeField(auto_now=True)
85     
86     notes = models.TextField(blank=True, null=True)
87     
88     def __unicode__(self):
89         if self.barcode_id is None or len(self.barcode_id) == 0:
90             return u"invu|%s" % (self.uuid)
91         else:
92             return u"invb|%s" % (self.barcode_id)
93             
94 pre_save.connect(_assign_uuid, sender=Item)
95
96
97 class LongTermStorage(models.Model):
98     
99     flowcell = models.ForeignKey(FlowCell)
100     libraries = models.ManyToManyField(Library)
101
102     storage_devices = models.ManyToManyField(Item)
103     
104     def __unicode__(self):
105         return u"%s: %s" % (str(self.flowcell), ', '.join([ str(s) for s in self.storage_devices.iterator() ]))