1 from htsworkflow.frontend.inventory.models import Item, LongTermStorage
2 from htsworkflow.frontend.inventory.bcmagic import item_search
3 from htsworkflow.frontend.bcmagic.plugin import register_search_plugin
4 from htsworkflow.frontend.experiments.models import FlowCell
5 from htsworkflow.frontend.bcmagic.forms import BarcodeMagicForm
6 from htsworkflow.frontend.bcmagic.util import print_zpl_socket
7 from htsworkflow.frontend import settings
8 #from htsworkflow.util.jsonutil import encode_json
10 from django.core.exceptions import ObjectDoesNotExist
11 from django.http import HttpResponse, HttpResponseRedirect
12 from django.shortcuts import render_to_response
13 from django.template import RequestContext
14 from django.template.loader import get_template
15 from django.contrib.auth.decorators import login_required
17 register_search_plugin('Inventory Item', item_search)
21 except ImportError, e:
22 import simplejson as json
24 INVENTORY_CONTEXT_DEFAULTS = {
25 'app_name': 'Inventory Tracker',
26 'bcmagic': BarcodeMagicForm()
29 INVENTORY_ITEM_PRINT_DEFAULTS = {
30 'Hard Drive': 'inventory/hard_drive_shell.zpl',
31 'default': 'inventory/default.zpl',
32 'host': settings.BCPRINTER_PRINTER1_HOST
35 def getTemplateByType(item_type):
37 returns template to use given item_type
39 if item_type in INVENTORY_ITEM_PRINT_DEFAULTS:
40 return INVENTORY_ITEM_PRINT_DEFAULTS[item_type]
42 return INVENTORY_ITEM_PRINT_DEFAULTS['default']
45 def data_items(request):
47 Returns items in json format
49 item_list = Item.objects.all()
50 d = { 'results': len(item_list) }
53 for item in item_list:
55 item_d['uuid'] = item.uuid
56 item_d['barcode_id'] = item.barcode_id
57 item_d['model_id'] = item.item_info.model_id
58 item_d['part_number'] = item.item_info.part_number
59 item_d['lot_number'] = item.item_info.lot_number
60 item_d['vendor'] = item.item_info.vendor.name
61 item_d['creation_date'] = item.creation_date.strftime('%Y-%m-%d %H:%M:%S')
62 item_d['modified_date'] = item.modified_date.strftime('%Y-%m-%d %H:%M:%S')
63 item_d['location'] = item.location.name
65 # Item status if exists
66 if item.status is None:
69 item_d['status'] = item.status.name
71 # Stored flowcells on device
72 if item.longtermstorage_set.count() > 0:
73 item_d['flowcells'] = ','.join([ lts.flowcell.flowcell_id for lts in item.longtermstorage_set.all() ])
75 item_d['flowcells'] = ''
77 item_d['type'] = item.item_type.name
82 return HttpResponse(json.dumps(d), content_type="application/javascript")
90 'page_name': 'Inventory Index'
92 context_dict.update(INVENTORY_CONTEXT_DEFAULTS)
94 return render_to_response('inventory/inventory_index.html',
96 context_instance=RequestContext(request))
100 def item_summary_by_barcode(request, barcode_id, msg=''):
102 Display a summary for an item by barcode
105 item = Item.objects.get(barcode_id=barcode_id)
106 except ObjectDoesNotExist, e:
109 return item_summary_by_uuid(request, None, msg, item)
113 def item_summary_by_uuid(request, uuid, msg='', item=None):
115 Display a summary for an item
117 # Use item instead of looking it up if it is passed.
120 item = Item.objects.get(uuid=uuid)
121 except ObjectDoesNotExist, e:
125 'page_name': 'Item Summary',
130 context_dict.update(INVENTORY_CONTEXT_DEFAULTS)
132 return render_to_response('inventory/inventory_summary.html',
134 context_instance=RequestContext(request))
137 def _item_print(item, request):
139 Prints an item given a type of item label to print
141 #FIXME: Hard coding this for now... need to abstract later.
142 context = {'item': item}
144 # Print using barcode_id
145 if not item.force_use_uuid and (item.barcode_id is None or len(item.barcode_id.strip())):
146 context['use_uuid'] = False
147 msg = 'Printing item with barcode id: %s' % (item.barcode_id)
150 context['use_uuid'] = True
151 msg = 'Printing item with UUID: %s' % (item.uuid)
153 c = RequestContext(request, context)
154 t = get_template(getTemplateByType(item.item_type.name))
155 print_zpl_socket(t.render(c))
160 def item_print(request, uuid):
162 Print a label for a given item
165 item = Item.objects.get(uuid=uuid)
166 except ObjectDoesNotExist, e:
168 msg = "Item with UUID %s does not exist" % (uuid)
171 msg = _item_print(item, request)
173 return item_summary_by_uuid(request, uuid, msg)
176 def link_flowcell_and_device(request, flowcell, serial):
178 Updates database records of a flowcell being archived on a device with a particular serial #
180 assert flowcell is not None
181 assert serial is not None
185 LIBRARY_UPDATED = False
187 ###########################################
188 # Retrieve Storage Device
190 sd = Item.objects.get(barcode_id=serial)
191 except ObjectDoesNotExist, e:
192 msg = "Item with barcode_id of %s not found." % (serial)
193 raise ObjectDoesNotExist(msg)
195 ###########################################
198 fc = FlowCell.objects.get(flowcell_id=flowcell)
199 except ObjectDoesNotExist, e:
200 msg = "FlowCell with flowcell_id of %s not found." % (flowcell)
201 raise ObjectDoesNotExist(msg)
203 ###########################################
204 # Retrieve or create LongTermStorage Object
205 count = fc.longtermstorage_set.count()
208 msg = "There really should only be one longtermstorage object per flowcell"
209 raise ValueError, msg
211 # lts already attached to flowcell
212 lts = fc.longtermstorage_set.all()[0]
214 lts = LongTermStorage()
217 # Need a primary keey before linking to storage devices
222 ############################################
223 # Link Storage to Flowcell
225 # Add a link to this storage device if it is not already linked.
226 if sd not in lts.storage_devices.all():
227 lts.storage_devices.add(sd)
230 ###########################################
231 # Add Library Links to LTS
233 if fc.lane_1_library not in lts.libraries.all():
234 lts.libraries.add(fc.lane_1_library)
235 LIBRARY_UPDATED = True
238 if fc.lane_2_library not in lts.libraries.all():
239 lts.libraries.add(fc.lane_2_library)
240 LIBRARY_UPDATED = True
243 if fc.lane_3_library not in lts.libraries.all():
244 lts.libraries.add(fc.lane_3_library)
245 LIBRARY_UPDATED = True
248 if fc.lane_4_library not in lts.libraries.all():
249 lts.libraries.add(fc.lane_4_library)
250 LIBRARY_UPDATED = True
254 if fc.lane_5_library not in lts.libraries.all():
255 lts.libraries.add(fc.lane_5_library)
256 LIBRARY_UPDATED = True
259 if fc.lane_6_library not in lts.libraries.all():
260 lts.libraries.add(fc.lane_6_library)
261 LIBRARY_UPDATED = True
264 if fc.lane_7_library not in lts.libraries.all():
265 lts.libraries.add(fc.lane_7_library)
266 LIBRARY_UPDATED = True
269 if fc.lane_8_library not in lts.libraries.all():
270 lts.libraries.add(fc.lane_8_library)
271 LIBRARY_UPDATED = True
278 if LTS_UPDATED or SD_UPDATED or LIBRARY_UPDATED:
279 msg.append(' LongTermStorage (LTS) Created: %s' % (LTS_UPDATED))
280 msg.append(' Storage Device Linked to LTS: %s' % (SD_UPDATED))
281 msg.append(' Libraries updated in LTS: %s' % (LIBRARY_UPDATED))
283 msg.append(' No Updates Needed.')
285 return HttpResponse('\n'.join(msg))