Sample ID now generated partially base on primary key of sample.
[htsworkflow.git] / samplebc / samples / models.py
1 from django.db import models
2 from django.db.models import signals
3 from django.contrib.auth.models import User, Group
4
5 from samplebc import settings
6
7
8 #AUDIT_TYPES = [('Create', 'Create'),
9 #               ('Update', 'Update'),
10 #               ('Delete', 'Delete')]
11
12 EXP_TYPES = [('RNASeq', 'RNASeq'),
13              ('ChIPSeq','ChIPSeq'),
14              ('DeNovoSeq','DeNovoSeq'),
15              ('MethylSeq','MethylSeq')]
16
17 FREEZER_BUILDINGS = [('Kerckhoff', 'Kerckhoff')]
18
19
20
21 class Owner(models.Model):
22     name = models.CharField(max_length=32)
23     
24     def __unicode__(self):
25         return u'%s' % (self.name)
26
27
28 class Freezer(models.Model):
29     name = models.CharField(max_length=256)
30     building = models.CharField(max_length=256, choices=FREEZER_BUILDINGS,
31                                 default=FREEZER_BUILDINGS[0][0])
32     loc_desc = models.CharField(max_length=256, blank=True, null=True)
33     temperature = models.FloatField(help_text='Celsius')
34     
35     notes = models.TextField(blank=True, null=True)
36     
37     uuid = models.CharField(max_length=32, unique=True)
38     
39     def __unicode__(self):
40         return u'%s (%s; %sC)' % (self.name, self.building, self.temperature)
41         
42     @models.permalink
43     def get_absolute_url(self):
44         return ('samplebc.samples.views.freezer_summary', [str(self.uuid)])
45
46     
47 class SampleType(models.Model):
48     name = models.CharField(max_length=256)
49     
50     def __unicode__(self):
51         return u'%s' % (self.name)
52
53
54 class Container(models.Model):
55     name = models.CharField(max_length=256, unique=True)
56     sample_type = models.ManyToManyField(SampleType)
57     max_items = models.IntegerField()
58     freezer = models.ForeignKey(Freezer)
59     
60     notes = models.TextField(blank=True, null=True)
61     
62     uuid = models.CharField(max_length=32, unique=True)
63     
64     def __unicode__(self):
65         return u'%s (spc: %s)' % (self.name, self.space_available())
66         
67     @models.permalink
68     def get_absolute_url(self):
69         return ('samplebc.samples.views.container_summary', [str(self.uuid)])
70         
71     def space_available(self):
72         """
73         returns the space available for the container
74         """
75         return (self.max_items - self.sample_set.count())
76
77
78 class Sample(models.Model):
79     name = models.CharField(max_length=256, unique=True)
80     #sampleid = models.CharField(max_length=64, unique=True)
81     sampleid = models.CharField(max_length=64, unique=True)
82     sample_type = models.ForeignKey(SampleType)
83     owner = models.ForeignKey(Owner)
84     
85     description = models.TextField()
86     
87     # Concentration in ng/ul
88     concentration = models.FloatField(help_text='ng/ul', blank=True, null=True)
89     volume = models.FloatField(help_text='ul', blank=True, null=True)
90     container = models.ForeignKey(Container, blank=True, null=True)
91     
92     def __unicode__(self):
93         return u's|%s|%s' % (self.sampleid, self.owner.name)
94         
95     @models.permalink
96     def get_absolute_url(self):
97         return ('samplebc.samples.views.sample_summary', [str(self.sampleid)])
98
99 def assign_sample_id(sender, instance, **kwargs):
100
101     print 'Instance ID: %s' % (instance.id)    
102     if instance.sampleid is None or len(instance.sampleid) == 0:
103         instance.sampleid = settings.HTSW_SAMPLE_ID_TEMPLATE % (instance.id)
104         instance.save()
105         
106 signals.post_save.connect(assign_sample_id, sender=Sample)
107