Track which flowcells are archived on which long term storage devices
[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     ###########################################
16     # Retrieve Storage Device
17     try:
18         sd = Item.objects.get(barcode_id=serial)
19     except ObjectDoesNotExist, e:
20         msg = "Item with barcode_id of %s not found." % (serial)
21         raise ObjectDoesNotExist(msg)
22     
23     ###########################################
24     # Retrieve FlowCell
25     try:    
26         fc = FlowCell.objects.get(flowcell_id=flowcell)
27     except ObjectDoesNotExist, e:
28         msg = "FlowCell with flowcell_id of %s not found." % (flowcell)
29         raise ObjectDoesNotExist(msg)
30     
31     ###########################################
32     # Retrieve or create LongTermStorage Object
33     count = fc.longtermstorage_set.count()
34     lts = None
35     if count > 1:
36         msg = "There really should only be one longtermstorage object per flowcell"
37         raise ValueError, msg
38     elif count == 1:
39         # lts already attached to flowcell
40         lts = fc.longtermstorage_set.all()[0]
41     else:
42         lts = LongTermStorage()
43         # Attach flowcell
44         lts.flowcell = fc
45         # Need a primary keey before linking to storage devices
46         lts.save()
47         
48         
49     ############################################
50     # Link Storage to Flowcell
51     
52     # Add a link to this storage device if it is not already linked.
53     if sd not in lts.storage_devices.values():
54         lts.storage_devices.add(sd)
55     
56     ###########################################
57     # Add Library Links to LTS
58     
59     if fc.lane_1_library not in lts.storage_devices.values():
60         lts.libraries.add(fc.lane_1_library)
61     
62     if fc.lane_2_library not in lts.storage_devices.values():
63         lts.libraries.add(fc.lane_2_library)
64     
65     if fc.lane_3_library not in lts.storage_devices.values():
66         lts.libraries.add(fc.lane_3_library)
67     
68     if fc.lane_4_library not in lts.storage_devices.values():
69         lts.libraries.add(fc.lane_4_library)
70     
71     
72     if fc.lane_5_library not in lts.storage_devices.values():
73         lts.libraries.add(fc.lane_5_library)
74     
75     if fc.lane_6_library not in lts.storage_devices.values():
76         lts.libraries.add(fc.lane_6_library)
77     
78     if fc.lane_7_library not in lts.storage_devices.values():
79         lts.libraries.add(fc.lane_7_library)
80     
81     if fc.lane_8_library not in lts.storage_devices.values():
82         lts.libraries.add(fc.lane_8_library)
83         
84     # Save Changes
85     lts.save()
86     
87     return HttpResponse("Success")