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))
94 def item_summary(request, uuid, msg=''):
96 Display a summary for an item
99 item = Item.objects.get(uuid=uuid)
100 except ObjectDoesNotExist, e:
104 'page_name': 'Item Summary',
109 context_dict.update(INVENTORY_CONTEXT_DEFAULTS)
111 return render_to_response('inventory/inventory_summary.html',
113 context_instance=RequestContext(request))
116 def _item_print(item, request):
118 Prints an item given a type of item label to print
120 #FIXME: Hard coding this for now... need to abstract later.
121 context = {'item': item}
123 # Print using barcode_id
124 if not item.force_use_uuid and (item.barcode_id is None or len(item.barcode_id.strip())):
125 context['use_uuid'] = False
126 msg = 'Printing item with barcode id: %s' % (item.barcode_id)
129 context['use_uuid'] = True
130 msg = 'Printing item with UUID: %s' % (item.uuid)
132 c = RequestContext(request, context)
133 t = get_template(getTemplateByType(item.item_type.name))
134 print_zpl_socket(t.render(c))
139 def item_print(request, uuid):
141 Print a label for a given item
144 item = Item.objects.get(uuid=uuid)
145 except ObjectDoesNotExist, e:
147 msg = "Item with UUID %s does not exist" % (uuid)
150 msg = _item_print(item, request)
152 return item_summary(request, uuid, msg)
155 def link_flowcell_and_device(request, flowcell, serial):
157 Updates database records of a flowcell being archived on a device with a particular serial #
159 assert flowcell is not None
160 assert serial is not None
164 LIBRARY_UPDATED = False
166 ###########################################
167 # Retrieve Storage Device
169 sd = Item.objects.get(barcode_id=serial)
170 except ObjectDoesNotExist, e:
171 msg = "Item with barcode_id of %s not found." % (serial)
172 raise ObjectDoesNotExist(msg)
174 ###########################################
177 fc = FlowCell.objects.get(flowcell_id=flowcell)
178 except ObjectDoesNotExist, e:
179 msg = "FlowCell with flowcell_id of %s not found." % (flowcell)
180 raise ObjectDoesNotExist(msg)
182 ###########################################
183 # Retrieve or create LongTermStorage Object
184 count = fc.longtermstorage_set.count()
187 msg = "There really should only be one longtermstorage object per flowcell"
188 raise ValueError, msg
190 # lts already attached to flowcell
191 lts = fc.longtermstorage_set.all()[0]
193 lts = LongTermStorage()
196 # Need a primary keey before linking to storage devices
201 ############################################
202 # Link Storage to Flowcell
204 # Add a link to this storage device if it is not already linked.
205 if sd not in lts.storage_devices.all():
206 lts.storage_devices.add(sd)
209 ###########################################
210 # Add Library Links to LTS
212 if fc.lane_1_library not in lts.libraries.all():
213 lts.libraries.add(fc.lane_1_library)
214 LIBRARY_UPDATED = True
217 if fc.lane_2_library not in lts.libraries.all():
218 lts.libraries.add(fc.lane_2_library)
219 LIBRARY_UPDATED = True
222 if fc.lane_3_library not in lts.libraries.all():
223 lts.libraries.add(fc.lane_3_library)
224 LIBRARY_UPDATED = True
227 if fc.lane_4_library not in lts.libraries.all():
228 lts.libraries.add(fc.lane_4_library)
229 LIBRARY_UPDATED = True
233 if fc.lane_5_library not in lts.libraries.all():
234 lts.libraries.add(fc.lane_5_library)
235 LIBRARY_UPDATED = True
238 if fc.lane_6_library not in lts.libraries.all():
239 lts.libraries.add(fc.lane_6_library)
240 LIBRARY_UPDATED = True
243 if fc.lane_7_library not in lts.libraries.all():
244 lts.libraries.add(fc.lane_7_library)
245 LIBRARY_UPDATED = True
248 if fc.lane_8_library not in lts.libraries.all():
249 lts.libraries.add(fc.lane_8_library)
250 LIBRARY_UPDATED = True
257 if LTS_UPDATED or SD_UPDATED or LIBRARY_UPDATED:
258 msg.append(' LongTermStorage (LTS) Created: %s' % (LTS_UPDATED))
259 msg.append(' Storage Device Linked to LTS: %s' % (SD_UPDATED))
260 msg.append(' Libraries updated in LTS: %s' % (LIBRARY_UPDATED))
262 msg.append(' No Updates Needed.')
264 return HttpResponse('\n'.join(msg))