1 from htsworkflow.frontend.inventory.models import Item, LongTermStorage
2 from htsworkflow.frontend.experiments.models import FlowCell
3 from htsworkflow.frontend.bcmagic.forms import BarcodeMagicForm
4 from htsworkflow.util.jsonutil import encode_json
6 from django.core.exceptions import ObjectDoesNotExist
7 from django.http import HttpResponse, HttpResponseRedirect
8 from django.shortcuts import render_to_response
9 from django.template import RequestContext
10 from django.template.loader import get_template
11 from django.contrib.auth.decorators import login_required
15 INVENTORY_CONTEXT_DEFAULTS = {
16 'app_name': 'Inventory Tracker',
17 'bcmagic': BarcodeMagicForm()
20 def data_items(request):
22 Returns items in json format
24 item_list = Item.objects.all()
25 d = { 'results': len(item_list) }
28 for item in item_list:
30 item_d['uuid'] = item.uuid
31 item_d['barcode_id'] = item.barcode_id
32 item_d['model_id'] = item.item_info.model_id
33 item_d['part_number'] = item.item_info.part_number
34 item_d['lot_number'] = item.item_info.lot_number
35 item_d['vendor'] = item.item_info.vendor.name
36 item_d['creation_date'] = item.creation_date.strftime('%Y-%m-%d %H:%M:%S')
37 item_d['modified_date'] = item.modified_date.strftime('%Y-%m-%d %H:%M:%S')
38 item_d['location'] = item.location.name
40 # Item status if exists
41 if item.status is None:
44 item_d['status'] = item.status.name
46 # Stored flowcells on device
47 if item.longtermstorage_set.count() > 0:
48 item_d['flowcells'] = ','.join([ lts.flowcell.flowcell_id for lts in item.longtermstorage_set.all() ])
50 item_d['flowcells'] = ''
52 item_d['type'] = item.item_type.name
57 return HttpResponse(encode_json(d), content_type="application/javascript")
65 'page_name': 'Inventory Index'
67 context_dict.update(INVENTORY_CONTEXT_DEFAULTS)
69 return render_to_response('inventory_index.html',
71 context_instance=RequestContext(request))
74 def link_flowcell_and_device(request, flowcell, serial):
76 Updates database records of a flowcell being archived on a device with a particular serial #
78 assert flowcell is not None
79 assert serial is not None
83 LIBRARY_UPDATED = False
85 ###########################################
86 # Retrieve Storage Device
88 sd = Item.objects.get(barcode_id=serial)
89 except ObjectDoesNotExist, e:
90 msg = "Item with barcode_id of %s not found." % (serial)
91 raise ObjectDoesNotExist(msg)
93 ###########################################
96 fc = FlowCell.objects.get(flowcell_id=flowcell)
97 except ObjectDoesNotExist, e:
98 msg = "FlowCell with flowcell_id of %s not found." % (flowcell)
99 raise ObjectDoesNotExist(msg)
101 ###########################################
102 # Retrieve or create LongTermStorage Object
103 count = fc.longtermstorage_set.count()
106 msg = "There really should only be one longtermstorage object per flowcell"
107 raise ValueError, msg
109 # lts already attached to flowcell
110 lts = fc.longtermstorage_set.all()[0]
112 lts = LongTermStorage()
115 # Need a primary keey before linking to storage devices
120 ############################################
121 # Link Storage to Flowcell
123 # Add a link to this storage device if it is not already linked.
124 if sd not in lts.storage_devices.all():
125 lts.storage_devices.add(sd)
128 ###########################################
129 # Add Library Links to LTS
131 if fc.lane_1_library not in lts.libraries.all():
132 lts.libraries.add(fc.lane_1_library)
133 LIBRARY_UPDATED = True
136 if fc.lane_2_library not in lts.libraries.all():
137 lts.libraries.add(fc.lane_2_library)
138 LIBRARY_UPDATED = True
141 if fc.lane_3_library not in lts.libraries.all():
142 lts.libraries.add(fc.lane_3_library)
143 LIBRARY_UPDATED = True
146 if fc.lane_4_library not in lts.libraries.all():
147 lts.libraries.add(fc.lane_4_library)
148 LIBRARY_UPDATED = True
152 if fc.lane_5_library not in lts.libraries.all():
153 lts.libraries.add(fc.lane_5_library)
154 LIBRARY_UPDATED = True
157 if fc.lane_6_library not in lts.libraries.all():
158 lts.libraries.add(fc.lane_6_library)
159 LIBRARY_UPDATED = True
162 if fc.lane_7_library not in lts.libraries.all():
163 lts.libraries.add(fc.lane_7_library)
164 LIBRARY_UPDATED = True
167 if fc.lane_8_library not in lts.libraries.all():
168 lts.libraries.add(fc.lane_8_library)
169 LIBRARY_UPDATED = True
176 if LTS_UPDATED or SD_UPDATED or LIBRARY_UPDATED:
177 msg.append(' LongTermStorage (LTS) Created: %s' % (LTS_UPDATED))
178 msg.append(' Storage Device Linked to LTS: %s' % (SD_UPDATED))
179 msg.append(' Libraries updated in LTS: %s' % (LIBRARY_UPDATED))
181 msg.append(' No Updates Needed.')
183 return HttpResponse('\n'.join(msg))