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()
21 def data_items(request):
23 Returns items in json format
25 item_list = Item.objects.all()
26 d = { 'results': len(item_list) }
29 for item in item_list:
31 item_d['uuid'] = item.uuid
32 item_d['barcode_id'] = item.barcode_id
33 item_d['model_id'] = item.item_info.model_id
34 item_d['part_number'] = item.item_info.part_number
35 item_d['lot_number'] = item.item_info.lot_number
36 item_d['vendor'] = item.item_info.vendor.name
37 item_d['creation_date'] = item.creation_date.strftime('%Y-%m-%d %H:%M:%S')
38 item_d['modified_date'] = item.modified_date.strftime('%Y-%m-%d %H:%M:%S')
39 item_d['location'] = item.location.name
41 # Item status if exists
42 if item.status is None:
45 item_d['status'] = item.status.name
47 # Stored flowcells on device
48 if item.longtermstorage_set.count() > 0:
49 item_d['flowcells'] = ','.join([ lts.flowcell.flowcell_id for lts in item.longtermstorage_set.all() ])
51 item_d['flowcells'] = ''
53 item_d['type'] = item.item_type.name
58 return HttpResponse(encode_json(d), content_type="application/javascript")
66 'page_name': 'Inventory Index'
68 context_dict.update(INVENTORY_CONTEXT_DEFAULTS)
70 return render_to_response('inventory/inventory_index.html',
72 context_instance=RequestContext(request))
75 def item_summary(request, uuid):
77 Display a summary for an item
80 item = Item.objects.get(uuid=uuid)
81 except ObjectDoesNotExist, e:
85 'page_name': 'Item Summary',
89 context_dict.update(INVENTORY_CONTEXT_DEFAULTS)
91 return render_to_response('inventory/inventory_summary.html',
93 context_instance=RequestContext(request))
95 def link_flowcell_and_device(request, flowcell, serial):
97 Updates database records of a flowcell being archived on a device with a particular serial #
99 assert flowcell is not None
100 assert serial is not None
104 LIBRARY_UPDATED = False
106 ###########################################
107 # Retrieve Storage Device
109 sd = Item.objects.get(barcode_id=serial)
110 except ObjectDoesNotExist, e:
111 msg = "Item with barcode_id of %s not found." % (serial)
112 raise ObjectDoesNotExist(msg)
114 ###########################################
117 fc = FlowCell.objects.get(flowcell_id=flowcell)
118 except ObjectDoesNotExist, e:
119 msg = "FlowCell with flowcell_id of %s not found." % (flowcell)
120 raise ObjectDoesNotExist(msg)
122 ###########################################
123 # Retrieve or create LongTermStorage Object
124 count = fc.longtermstorage_set.count()
127 msg = "There really should only be one longtermstorage object per flowcell"
128 raise ValueError, msg
130 # lts already attached to flowcell
131 lts = fc.longtermstorage_set.all()[0]
133 lts = LongTermStorage()
136 # Need a primary keey before linking to storage devices
141 ############################################
142 # Link Storage to Flowcell
144 # Add a link to this storage device if it is not already linked.
145 if sd not in lts.storage_devices.all():
146 lts.storage_devices.add(sd)
149 ###########################################
150 # Add Library Links to LTS
152 if fc.lane_1_library not in lts.libraries.all():
153 lts.libraries.add(fc.lane_1_library)
154 LIBRARY_UPDATED = True
157 if fc.lane_2_library not in lts.libraries.all():
158 lts.libraries.add(fc.lane_2_library)
159 LIBRARY_UPDATED = True
162 if fc.lane_3_library not in lts.libraries.all():
163 lts.libraries.add(fc.lane_3_library)
164 LIBRARY_UPDATED = True
167 if fc.lane_4_library not in lts.libraries.all():
168 lts.libraries.add(fc.lane_4_library)
169 LIBRARY_UPDATED = True
173 if fc.lane_5_library not in lts.libraries.all():
174 lts.libraries.add(fc.lane_5_library)
175 LIBRARY_UPDATED = True
178 if fc.lane_6_library not in lts.libraries.all():
179 lts.libraries.add(fc.lane_6_library)
180 LIBRARY_UPDATED = True
183 if fc.lane_7_library not in lts.libraries.all():
184 lts.libraries.add(fc.lane_7_library)
185 LIBRARY_UPDATED = True
188 if fc.lane_8_library not in lts.libraries.all():
189 lts.libraries.add(fc.lane_8_library)
190 LIBRARY_UPDATED = True
197 if LTS_UPDATED or SD_UPDATED or LIBRARY_UPDATED:
198 msg.append(' LongTermStorage (LTS) Created: %s' % (LTS_UPDATED))
199 msg.append(' Storage Device Linked to LTS: %s' % (SD_UPDATED))
200 msg.append(' Libraries updated in LTS: %s' % (LIBRARY_UPDATED))
202 msg.append(' No Updates Needed.')
204 return HttpResponse('\n'.join(msg))