1 from htsworkflow.frontend.inventory.models import Item, LongTermStorage
2 from htsworkflow.frontend.experiments.models import FlowCell
4 from django.core.exceptions import ObjectDoesNotExist
5 from django.http import HttpResponse
8 def link_flowcell_and_device(request, flowcell, serial):
10 Updates database records of a flowcell being archived on a device with a particular serial #
12 assert flowcell is not None
13 assert serial is not None
17 LIBRARY_UPDATED = False
19 ###########################################
20 # Retrieve Storage Device
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)
27 ###########################################
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)
35 ###########################################
36 # Retrieve or create LongTermStorage Object
37 count = fc.longtermstorage_set.count()
40 msg = "There really should only be one longtermstorage object per flowcell"
43 # lts already attached to flowcell
44 lts = fc.longtermstorage_set.all()[0]
46 lts = LongTermStorage()
49 # Need a primary keey before linking to storage devices
54 ############################################
55 # Link Storage to Flowcell
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)
62 ###########################################
63 # Add Library Links to LTS
65 if fc.lane_1_library not in lts.libraries.all():
66 lts.libraries.add(fc.lane_1_library)
67 LIBRARY_UPDATED = True
70 if fc.lane_2_library not in lts.libraries.all():
71 lts.libraries.add(fc.lane_2_library)
72 LIBRARY_UPDATED = True
75 if fc.lane_3_library not in lts.libraries.all():
76 lts.libraries.add(fc.lane_3_library)
77 LIBRARY_UPDATED = True
80 if fc.lane_4_library not in lts.libraries.all():
81 lts.libraries.add(fc.lane_4_library)
82 LIBRARY_UPDATED = True
86 if fc.lane_5_library not in lts.libraries.all():
87 lts.libraries.add(fc.lane_5_library)
88 LIBRARY_UPDATED = True
91 if fc.lane_6_library not in lts.libraries.all():
92 lts.libraries.add(fc.lane_6_library)
93 LIBRARY_UPDATED = True
96 if fc.lane_7_library not in lts.libraries.all():
97 lts.libraries.add(fc.lane_7_library)
98 LIBRARY_UPDATED = True
101 if fc.lane_8_library not in lts.libraries.all():
102 lts.libraries.add(fc.lane_8_library)
103 LIBRARY_UPDATED = True
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))
115 msg.append(' No Updates Needed.')
117 return HttpResponse('\n'.join(msg))