Improved debugging output.
[htsworkflow.git] / htsworkflow / frontend / inventory / views.py
1 from htsworkflow.frontend.inventory.models import Item, LongTermStorage
2 from htsworkflow.frontend.experiments.models import FlowCell
3
4 from django.core.exceptions import ObjectDoesNotExist
5 from django.http import HttpResponse
6
7
8 def link_flowcell_and_device(request, flowcell, serial):
9     """
10     Updates database records of a flowcell being archived on a device with a particular serial #
11     """
12     assert flowcell is not None
13     assert serial is not None
14     
15     LTS_UPDATED = False
16     SD_UPDATED = False
17     LIBRARY_UPDATED = False
18         
19     ###########################################
20     # Retrieve Storage Device
21     try:
22         sd = Item.objects.get(barcode_id=serial)
23     except ObjectDoesNotExist, e:
24         msg = "Item with barcode_id of %s not found." % (serial)
25         raise ObjectDoesNotExist(msg)
26     
27     ###########################################
28     # Retrieve FlowCell
29     try:    
30         fc = FlowCell.objects.get(flowcell_id=flowcell)
31     except ObjectDoesNotExist, e:
32         msg = "FlowCell with flowcell_id of %s not found." % (flowcell)
33         raise ObjectDoesNotExist(msg)
34     
35     ###########################################
36     # Retrieve or create LongTermStorage Object
37     count = fc.longtermstorage_set.count()
38     lts = None
39     if count > 1:
40         msg = "There really should only be one longtermstorage object per flowcell"
41         raise ValueError, msg
42     elif count == 1:
43         # lts already attached to flowcell
44         lts = fc.longtermstorage_set.all()[0]
45     else:
46         lts = LongTermStorage()
47         # Attach flowcell
48         lts.flowcell = fc
49         # Need a primary keey before linking to storage devices
50         lts.save()
51         LTS_UPDATED = True
52         
53         
54     ############################################
55     # Link Storage to Flowcell
56     
57     # Add a link to this storage device if it is not already linked.
58     if sd not in lts.storage_devices.all():
59         lts.storage_devices.add(sd)
60         SD_UPDATED = True
61     
62     ###########################################
63     # Add Library Links to LTS
64     
65     if fc.lane_1_library not in lts.libraries.all():
66         lts.libraries.add(fc.lane_1_library)
67         LIBRARY_UPDATED = True
68         print 1
69     
70     if fc.lane_2_library not in lts.libraries.all():
71         lts.libraries.add(fc.lane_2_library)
72         LIBRARY_UPDATED = True
73         print 2
74     
75     if fc.lane_3_library not in lts.libraries.all():
76         lts.libraries.add(fc.lane_3_library)
77         LIBRARY_UPDATED = True
78         print 3
79     
80     if fc.lane_4_library not in lts.libraries.all():
81         lts.libraries.add(fc.lane_4_library)
82         LIBRARY_UPDATED = True
83         print 4
84     
85     
86     if fc.lane_5_library not in lts.libraries.all():
87         lts.libraries.add(fc.lane_5_library)
88         LIBRARY_UPDATED = True
89         print 5
90     
91     if fc.lane_6_library not in lts.libraries.all():
92         lts.libraries.add(fc.lane_6_library)
93         LIBRARY_UPDATED = True
94         print 6
95     
96     if fc.lane_7_library not in lts.libraries.all():
97         lts.libraries.add(fc.lane_7_library)
98         LIBRARY_UPDATED = True
99         print 7
100     
101     if fc.lane_8_library not in lts.libraries.all():
102         lts.libraries.add(fc.lane_8_library)
103         LIBRARY_UPDATED = True
104         print 8
105         
106     # Save Changes
107     lts.save()
108     
109     msg = ['Success:']
110     if LTS_UPDATED or SD_UPDATED or LIBRARY_UPDATED:
111         msg.append('  LongTermStorage (LTS) Created: %s' % (LTS_UPDATED))
112         msg.append('   Storage Device Linked to LTS: %s' % (SD_UPDATED))
113         msg.append('       Libraries updated in LTS: %s' % (LIBRARY_UPDATED))
114     else:
115         msg.append('  No Updates Needed.')
116     
117     return HttpResponse('\n'.join(msg))