print killing wsgi; minor fix.
[htsworkflow.git] / htsworkflow / frontend / inventory / models.py
1 import logging
2
3 from django.db import models
4 from django.db.models.signals import pre_save
5
6 from htsworkflow.frontend.samples.models import Library
7 from htsworkflow.frontend.experiments.models import FlowCell
8
9
10 try:
11     import uuid
12 except ImportError, e:
13     # Some systems are using python 2.4, which doesn't have uuid
14     # this is a stub
15     logging.warning('Real uuid is not available, initializing fake uuid module')
16     class uuid:
17         def uuid1(self):
18             self.hex = None
19             return self
20
21 def _assign_uuid(sender, instance, **kwargs):
22     """
23     Assigns a UUID to model on save
24     """
25     #print 'Entered _assign_uuid'
26     if instance.uuid is None or len(instance.uuid) != 32:
27         instance.uuid = uuid.uuid1().hex
28
29
30 class Vendor(models.Model):
31     name = models.CharField(max_length=256)
32     url = models.URLField(blank=True, null=True)
33
34     def __unicode__(self):
35         return u"%s" % (self.name)
36
37
38 class Location(models.Model):
39     
40     name = models.CharField(max_length=256, unique=True)
41     location_description = models.TextField()
42     
43     uuid = models.CharField(max_length=32, blank=True, help_text="Leave blank for automatic UUID generation")
44     
45     notes = models.TextField(blank=True, null=True)
46     
47     def __unicode__(self):
48         if len(self.location_description) > 16:
49             return u"%s: %s" % (self.name, self.location_description[0:16]+u"...")
50         else:
51             return u"%s: %s" % (self.name, self.location_description)
52
53 pre_save.connect(_assign_uuid, sender=Location)
54
55 class ItemInfo(models.Model):
56     model_id = models.CharField(max_length=256, blank=True, null=True)
57     part_number = models.CharField(max_length=256, blank=True, null=True)
58     lot_number = models.CharField(max_length=256, blank=True, null=True)
59     
60     url = models.URLField(blank=True, null=True)
61     
62     qty_purchased = models.IntegerField(default=1)
63     
64     vendor = models.ForeignKey(Vendor)
65     purchase_date = models.DateField(blank=True, null=True)
66     warranty_months = models.IntegerField(blank=True, null=True)
67     
68     notes = models.TextField(blank=True, null=True)
69     
70     def __unicode__(self):
71         name = u''
72         if self.model_id:
73             name += u"model:%s " % (self.model_id)
74         if self.part_number:
75             name += u"part:%s " % (self.part_number)
76         if self.lot_number:
77             name += u"lot:%s " % (self.lot_number)
78             
79         return u"%s: %s" % (name, self.purchase_date)
80
81
82 class ItemType(models.Model):
83     
84     name = models.CharField(max_length=64, unique=True)
85     description = models.TextField(blank=True, null=True)
86     
87     def __unicode__(self):
88         return u"%s" % (self.name)
89
90 class ItemStatus(models.Model):
91     name = models.CharField(max_length=64, unique=True)
92     notes = models.TextField(blank=True, null=True)
93     
94     def __unicode__(self):
95         return self.name
96
97 class Item(models.Model):
98     
99     item_type = models.ForeignKey(ItemType)
100     
101     #Automatically assigned uuid; used for barcode if one is not provided in
102     # barcode_id
103     uuid = models.CharField(max_length=32, blank=True, help_text="Leave blank for automatic UUID generation", unique=True)
104     
105     # field for existing barcodes; used instead of uuid if provided
106     barcode_id = models.CharField(max_length=256, blank=True, null=True)
107     force_use_uuid = models.BooleanField(default=False)
108     
109     item_info = models.ForeignKey(ItemInfo)
110     
111     location = models.ForeignKey(Location)
112     
113     status = models.ForeignKey(ItemStatus, blank=True, null=True)
114     
115     creation_date = models.DateTimeField(auto_now_add=True)
116     modified_date = models.DateTimeField(auto_now=True)
117     
118     notes = models.TextField(blank=True, null=True)
119     
120     def __unicode__(self):
121         if self.barcode_id is None or len(self.barcode_id) == 0:
122             return u"invu|%s" % (self.uuid)
123         else:
124             return u"invb|%s" % (self.barcode_id)
125             
126     def get_absolute_url(self):
127         return '/inventory/%s/' % (self.uuid)
128             
129 pre_save.connect(_assign_uuid, sender=Item)
130
131
132 class LongTermStorage(models.Model):
133     
134     flowcell = models.ForeignKey(FlowCell)
135     libraries = models.ManyToManyField(Library)
136
137     storage_devices = models.ManyToManyField(Item)
138     
139     creation_date = models.DateTimeField(auto_now_add=True)
140     modified_date = models.DateTimeField(auto_now=True)
141     
142     def __unicode__(self):
143         return u"%s: %s" % (str(self.flowcell), ', '.join([ str(s) for s in self.storage_devices.iterator() ]))
144         
145
146
147 class ReagentBase(models.Model):
148     
149     reagent = models.ManyToManyField(Item)
150     
151     creation_date = models.DateTimeField(auto_now_add=True)
152     modified_date = models.DateTimeField(auto_now=True)
153     
154     class Meta:
155         abstract = True
156
157
158 class ReagentFlowcell(ReagentBase):
159     """
160     Links reagents and flowcells
161     """
162     flowcell = models.ForeignKey(FlowCell)
163     
164     def __unicode__(self):
165         return u"%s: %s" % (str(self.flowcell), ', '.join([ str(s) for s in self.reagent.iterator() ]))
166    
167
168 class ReagentLibrary(ReagentBase):
169     """
170     Links libraries and flowcells
171     """
172     library = models.ForeignKey(Library)
173     
174     def __unicode__(self):
175         return u"%s: %s" % (str(self.library), ', '.join([ str(s) for s in self.reagent.iterator() ]))