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.frontend.bcprinter.util import print_zpl_socket
5 from htsworkflow.frontend import settings
6 #from htsworkflow.util.jsonutil import encode_json
8 from django.core.exceptions import ObjectDoesNotExist
9 from django.http import HttpResponse, HttpResponseRedirect
10 from django.shortcuts import render_to_response
11 from django.template import RequestContext
12 from django.template.loader import get_template
13 from django.contrib.auth.decorators import login_required
17 except ImportError, e:
18 import simplejson as json
20 INVENTORY_CONTEXT_DEFAULTS = {
21 'app_name': 'Inventory Tracker',
22 'bcmagic': BarcodeMagicForm()
25 INVENTORY_ITEM_PRINT_DEFAULTS = {
26 'default': 'inventory/default.zpl',
27 'host': settings.BCPRINTER_PRINTER1_HOST
30 def getTemplateByType(item_type):
32 returns template to use given item_type
34 if item_type in INVENTORY_ITEM_PRINT_DEFAULTS:
35 return INVENTORY_ITEM_PRINT_DEFAULTS[item_type]
37 return INVENTORY_ITEM_PRINT_DEFAULTS['default']
40 def data_items(request):
42 Returns items in json format
44 item_list = Item.objects.all()
45 d = { 'results': len(item_list) }
48 for item in item_list:
50 item_d['uuid'] = item.uuid
51 item_d['barcode_id'] = item.barcode_id
52 item_d['model_id'] = item.item_info.model_id
53 item_d['part_number'] = item.item_info.part_number
54 item_d['lot_number'] = item.item_info.lot_number
55 item_d['vendor'] = item.item_info.vendor.name
56 item_d['creation_date'] = item.creation_date.strftime('%Y-%m-%d %H:%M:%S')
57 item_d['modified_date'] = item.modified_date.strftime('%Y-%m-%d %H:%M:%S')
58 item_d['location'] = item.location.name
60 # Item status if exists
61 if item.status is None:
64 item_d['status'] = item.status.name
66 # Stored flowcells on device
67 if item.longtermstorage_set.count() > 0:
68 item_d['flowcells'] = ','.join([ lts.flowcell.flowcell_id for lts in item.longtermstorage_set.all() ])
70 item_d['flowcells'] = ''
72 item_d['type'] = item.item_type.name
77 return HttpResponse(json.dumps(d), content_type="application/javascript")
85 'page_name': 'Inventory Index'
87 context_dict.update(INVENTORY_CONTEXT_DEFAULTS)
89 return render_to_response('inventory/inventory_index.html',
91 context_instance=RequestContext(request))
95 def item_summary_by_barcode(request, barcode_id, msg=''):
97 Display a summary for an item by barcode
100 item = Item.objects.get(barcode_id=barcode_id)
101 except ObjectDoesNotExist, e:
104 return item_summary_by_uuid(request, None, msg, item)
108 def item_summary_by_uuid(request, uuid, msg='', item=None):
110 Display a summary for an item
112 # Use item instead of looking it up if it is passed.
115 item = Item.objects.get(uuid=uuid)
116 except ObjectDoesNotExist, e:
120 'page_name': 'Item Summary',
125 context_dict.update(INVENTORY_CONTEXT_DEFAULTS)
127 return render_to_response('inventory/inventory_summary.html',
129 context_instance=RequestContext(request))
132 def _item_print(item, request):
134 Prints an item given a type of item label to print
136 #FIXME: Hard coding this for now... need to abstract later.
137 context = {'item': item}
139 # Print using barcode_id
140 if not item.force_use_uuid and (item.barcode_id is None or len(item.barcode_id.strip())):
141 context['use_uuid'] = False
142 msg = 'Printing item with barcode id: %s' % (item.barcode_id)
145 context['use_uuid'] = True
146 msg = 'Printing item with UUID: %s' % (item.uuid)
148 c = RequestContext(request, context)
149 t = get_template(getTemplateByType(item.item_type.name))
150 print_zpl_socket(t.render(c))
155 def item_print(request, uuid):
157 Print a label for a given item
160 item = Item.objects.get(uuid=uuid)
161 except ObjectDoesNotExist, e:
163 msg = "Item with UUID %s does not exist" % (uuid)
166 msg = _item_print(item, request)
168 return item_summary_by_uuid(request, uuid, msg)
171 def link_flowcell_and_device(request, flowcell, serial):
173 Updates database records of a flowcell being archived on a device with a particular serial #
175 assert flowcell is not None
176 assert serial is not None
180 LIBRARY_UPDATED = False
182 ###########################################
183 # Retrieve Storage Device
185 sd = Item.objects.get(barcode_id=serial)
186 except ObjectDoesNotExist, e:
187 msg = "Item with barcode_id of %s not found." % (serial)
188 raise ObjectDoesNotExist(msg)
190 ###########################################
193 fc = FlowCell.objects.get(flowcell_id=flowcell)
194 except ObjectDoesNotExist, e:
195 msg = "FlowCell with flowcell_id of %s not found." % (flowcell)
196 raise ObjectDoesNotExist(msg)
198 ###########################################
199 # Retrieve or create LongTermStorage Object
200 count = fc.longtermstorage_set.count()
203 msg = "There really should only be one longtermstorage object per flowcell"
204 raise ValueError, msg
206 # lts already attached to flowcell
207 lts = fc.longtermstorage_set.all()[0]
209 lts = LongTermStorage()
212 # Need a primary keey before linking to storage devices
217 ############################################
218 # Link Storage to Flowcell
220 # Add a link to this storage device if it is not already linked.
221 if sd not in lts.storage_devices.all():
222 lts.storage_devices.add(sd)
225 ###########################################
226 # Add Library Links to LTS
228 if fc.lane_1_library not in lts.libraries.all():
229 lts.libraries.add(fc.lane_1_library)
230 LIBRARY_UPDATED = True
233 if fc.lane_2_library not in lts.libraries.all():
234 lts.libraries.add(fc.lane_2_library)
235 LIBRARY_UPDATED = True
238 if fc.lane_3_library not in lts.libraries.all():
239 lts.libraries.add(fc.lane_3_library)
240 LIBRARY_UPDATED = True
243 if fc.lane_4_library not in lts.libraries.all():
244 lts.libraries.add(fc.lane_4_library)
245 LIBRARY_UPDATED = True
249 if fc.lane_5_library not in lts.libraries.all():
250 lts.libraries.add(fc.lane_5_library)
251 LIBRARY_UPDATED = True
254 if fc.lane_6_library not in lts.libraries.all():
255 lts.libraries.add(fc.lane_6_library)
256 LIBRARY_UPDATED = True
259 if fc.lane_7_library not in lts.libraries.all():
260 lts.libraries.add(fc.lane_7_library)
261 LIBRARY_UPDATED = True
264 if fc.lane_8_library not in lts.libraries.all():
265 lts.libraries.add(fc.lane_8_library)
266 LIBRARY_UPDATED = True
273 if LTS_UPDATED or SD_UPDATED or LIBRARY_UPDATED:
274 msg.append(' LongTermStorage (LTS) Created: %s' % (LTS_UPDATED))
275 msg.append(' Storage Device Linked to LTS: %s' % (SD_UPDATED))
276 msg.append(' Libraries updated in LTS: %s' % (LIBRARY_UPDATED))
278 msg.append(' No Updates Needed.')
280 return HttpResponse('\n'.join(msg))