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 INVENTORY_CONTEXT_DEFAULTS = {
18 'app_name': 'Inventory Tracker',
19 'bcmagic': BarcodeMagicForm()
22 INVENTORY_ITEM_PRINT_DEFAULTS = {
23 'default': 'inventory/default.zpl',
24 'host': settings.BCPRINTER_PRINTER1_HOST
27 def getTemplateByType(item_type):
29 returns template to use given item_type
31 if item_type in INVENTORY_ITEM_PRINT_DEFAULTS:
32 return INVENTORY_ITEM_PRINT_DEFAULTS[item_type]
34 return INVENTORY_ITEM_PRINT_DEFAULTS['default']
37 def data_items(request):
39 Returns items in json format
41 item_list = Item.objects.all()
42 d = { 'results': len(item_list) }
45 for item in item_list:
47 item_d['uuid'] = item.uuid
48 item_d['barcode_id'] = item.barcode_id
49 item_d['model_id'] = item.item_info.model_id
50 item_d['part_number'] = item.item_info.part_number
51 item_d['lot_number'] = item.item_info.lot_number
52 item_d['vendor'] = item.item_info.vendor.name
53 item_d['creation_date'] = item.creation_date.strftime('%Y-%m-%d %H:%M:%S')
54 item_d['modified_date'] = item.modified_date.strftime('%Y-%m-%d %H:%M:%S')
55 item_d['location'] = item.location.name
57 # Item status if exists
58 if item.status is None:
61 item_d['status'] = item.status.name
63 # Stored flowcells on device
64 if item.longtermstorage_set.count() > 0:
65 item_d['flowcells'] = ','.join([ lts.flowcell.flowcell_id for lts in item.longtermstorage_set.all() ])
67 item_d['flowcells'] = ''
69 item_d['type'] = item.item_type.name
74 return HttpResponse(encode_json(d), content_type="application/javascript")
82 'page_name': 'Inventory Index'
84 context_dict.update(INVENTORY_CONTEXT_DEFAULTS)
86 return render_to_response('inventory/inventory_index.html',
88 context_instance=RequestContext(request))
91 def item_summary(request, uuid, msg=''):
93 Display a summary for an item
96 item = Item.objects.get(uuid=uuid)
97 except ObjectDoesNotExist, e:
101 'page_name': 'Item Summary',
106 context_dict.update(INVENTORY_CONTEXT_DEFAULTS)
108 return render_to_response('inventory/inventory_summary.html',
110 context_instance=RequestContext(request))
113 def _item_print(item, request):
115 Prints an item given a type of item label to print
117 #FIXME: Hard coding this for now... need to abstract later.
118 context = {'item': item}
120 # Print using barcode_id
121 if not item.force_use_uuid and (item.barcode_id is None or len(item.barcode_id.strip())):
122 context['use_uuid'] = False
123 msg = 'Printing item with barcode id: %s' % (item.barcode_id)
126 context['use_uuid'] = True
127 msg = 'Printing item with UUID: %s' % (item.uuid)
129 c = RequestContext(request, context)
130 t = get_template(getTemplateByType(item.item_type.name))
131 print_zpl_socket(t.render(c))
136 def item_print(request, uuid):
138 Print a label for a given item
141 item = Item.objects.get(uuid=uuid)
142 except ObjectDoesNotExist, e:
144 msg = "Item with UUID %s does not exist" % (uuid)
147 msg = _item_print(item, request)
149 return item_summary(request, uuid, msg)
152 def link_flowcell_and_device(request, flowcell, serial):
154 Updates database records of a flowcell being archived on a device with a particular serial #
156 assert flowcell is not None
157 assert serial is not None
161 LIBRARY_UPDATED = False
163 ###########################################
164 # Retrieve Storage Device
166 sd = Item.objects.get(barcode_id=serial)
167 except ObjectDoesNotExist, e:
168 msg = "Item with barcode_id of %s not found." % (serial)
169 raise ObjectDoesNotExist(msg)
171 ###########################################
174 fc = FlowCell.objects.get(flowcell_id=flowcell)
175 except ObjectDoesNotExist, e:
176 msg = "FlowCell with flowcell_id of %s not found." % (flowcell)
177 raise ObjectDoesNotExist(msg)
179 ###########################################
180 # Retrieve or create LongTermStorage Object
181 count = fc.longtermstorage_set.count()
184 msg = "There really should only be one longtermstorage object per flowcell"
185 raise ValueError, msg
187 # lts already attached to flowcell
188 lts = fc.longtermstorage_set.all()[0]
190 lts = LongTermStorage()
193 # Need a primary keey before linking to storage devices
198 ############################################
199 # Link Storage to Flowcell
201 # Add a link to this storage device if it is not already linked.
202 if sd not in lts.storage_devices.all():
203 lts.storage_devices.add(sd)
206 ###########################################
207 # Add Library Links to LTS
209 if fc.lane_1_library not in lts.libraries.all():
210 lts.libraries.add(fc.lane_1_library)
211 LIBRARY_UPDATED = True
214 if fc.lane_2_library not in lts.libraries.all():
215 lts.libraries.add(fc.lane_2_library)
216 LIBRARY_UPDATED = True
219 if fc.lane_3_library not in lts.libraries.all():
220 lts.libraries.add(fc.lane_3_library)
221 LIBRARY_UPDATED = True
224 if fc.lane_4_library not in lts.libraries.all():
225 lts.libraries.add(fc.lane_4_library)
226 LIBRARY_UPDATED = True
230 if fc.lane_5_library not in lts.libraries.all():
231 lts.libraries.add(fc.lane_5_library)
232 LIBRARY_UPDATED = True
235 if fc.lane_6_library not in lts.libraries.all():
236 lts.libraries.add(fc.lane_6_library)
237 LIBRARY_UPDATED = True
240 if fc.lane_7_library not in lts.libraries.all():
241 lts.libraries.add(fc.lane_7_library)
242 LIBRARY_UPDATED = True
245 if fc.lane_8_library not in lts.libraries.all():
246 lts.libraries.add(fc.lane_8_library)
247 LIBRARY_UPDATED = True
254 if LTS_UPDATED or SD_UPDATED or LIBRARY_UPDATED:
255 msg.append(' LongTermStorage (LTS) Created: %s' % (LTS_UPDATED))
256 msg.append(' Storage Device Linked to LTS: %s' % (SD_UPDATED))
257 msg.append(' Libraries updated in LTS: %s' % (LIBRARY_UPDATED))
259 msg.append(' No Updates Needed.')
261 return HttpResponse('\n'.join(msg))