-from htsworkflow.frontend.inventory.models import Item, LongTermStorage
+from htsworkflow.frontend.samples.changelist import HTSChangeList
+from htsworkflow.frontend.inventory.models import Item, LongTermStorage, ItemType
+from htsworkflow.frontend.inventory.admin import ItemAdmin, ItemTypeAdmin
from htsworkflow.frontend.inventory.bcmagic import item_search
from htsworkflow.frontend.bcmagic.plugin import register_search_plugin
from htsworkflow.frontend.experiments.models import FlowCell
from htsworkflow.frontend.bcmagic.forms import BarcodeMagicForm
-from htsworkflow.frontend.bcprinter.util import print_zpl_socket
-from htsworkflow.frontend import settings
-#from htsworkflow.util.jsonutil import encode_json
+from htsworkflow.frontend.bcmagic.utils import print_zpl_socket
+from django.conf import settings
+from django.contrib.auth.decorators import login_required
from django.core.exceptions import ObjectDoesNotExist
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
-from django.template import RequestContext
+from django.template import RequestContext, Template
from django.template.loader import get_template
-from django.contrib.auth.decorators import login_required
register_search_plugin('Inventory Item', item_search)
try:
import json
-except ImportError, e:
+except ImportError as e:
import simplejson as json
INVENTORY_CONTEXT_DEFAULTS = {
'bcmagic': BarcodeMagicForm()
}
-INVENTORY_ITEM_PRINT_DEFAULTS = {
- 'Hard Drive': 'inventory/hard_drive_shell.zpl',
- 'default': 'inventory/default.zpl',
- 'host': settings.BCPRINTER_PRINTER1_HOST
+def __flowcell_rundate_sort(x, y):
+ """
+ Sort by rundate
+ """
+ if x.run_date > y.run_date:
+ return 1
+ elif x.run_date == y.run_date:
+ return 0
+ else:
+ return -1
+
+def __expand_longtermstorage_context(context, item):
+ """
+ Expand information for LongTermStorage
+ """
+ flowcell_list = []
+ flowcell_id_list = []
+ library_id_list = []
+
+ for lts in item.longtermstorage_set.all():
+ flowcell_list.append(lts.flowcell)
+ flowcell_id_list.append(lts.flowcell.flowcell_id)
+ library_id_list.extend([ lib.id for lib in lts.libraries.all() ])
+
+ flowcell_list.sort(__flowcell_rundate_sort)
+ context['oldest_rundate'] = flowcell_list[0].run_date
+ context['latest_rundate'] = flowcell_list[-1].run_date
+
+ context['flowcell_id_list'] = flowcell_id_list
+ context['library_id_list_1_to_20'] = library_id_list[0:20]
+ context['library_id_list_21_to_40'] = library_id_list[20:40]
+ context['library_id_list_41_to_60'] = library_id_list[40:60]
+ context['library_id_list_61_to_80'] = library_id_list[60:80]
+
+
+EXPAND_CONTEXT = {
+ 'Hard Drive': __expand_longtermstorage_context
}
-def getTemplateByType(item_type):
+#INVENTORY_ITEM_PRINT_DEFAULTS = {
+# 'Hard Drive': 'inventory/hard_drive_shell.zpl',
+# 'default': 'inventory/default.zpl',
+# 'host': settings.BCPRINTER_PRINTER1_HOST
+#}
+
+def getPrinterTemplateByType(item_type):
"""
returns template to use given item_type
"""
- if item_type in INVENTORY_ITEM_PRINT_DEFAULTS:
- return INVENTORY_ITEM_PRINT_DEFAULTS[item_type]
+ assert item_type.printertemplate_set.count() < 2
+
+ # Get the template for item_type
+ if item_type.printertemplate_set.count() > 0:
+ printer_template = item_type.printertemplate_set.all()[0]
+ return printer_template
+ # Get default
else:
- return INVENTORY_ITEM_PRINT_DEFAULTS['default']
+ try:
+ printer_template = PrinterTemplate.objects.get(default=True)
+ except ObjectDoesNotExist:
+ msg = "No template for item type (%s) and no default template found" % (item_type.name)
+ raise ValueError(msg)
+
+ return printer_template
+
@login_required
def data_items(request):
item_list = Item.objects.all()
d = { 'results': len(item_list) }
rows = []
-
+
for item in item_list:
item_d = {}
item_d['uuid'] = item.uuid
item_d['creation_date'] = item.creation_date.strftime('%Y-%m-%d %H:%M:%S')
item_d['modified_date'] = item.modified_date.strftime('%Y-%m-%d %H:%M:%S')
item_d['location'] = item.location.name
-
+
# Item status if exists
if item.status is None:
item_d['status'] = ''
else:
item_d['status'] = item.status.name
-
+
# Stored flowcells on device
if item.longtermstorage_set.count() > 0:
item_d['flowcells'] = ','.join([ lts.flowcell.flowcell_id for lts in item.longtermstorage_set.all() ])
else:
item_d['flowcells'] = ''
-
+
item_d['type'] = item.item_type.name
rows.append(item_d)
-
+
d['rows'] = rows
-
+
return HttpResponse(json.dumps(d), content_type="application/javascript")
+@login_required
+def all_index(request):
+ """
+ Inventory Index View
+ """
+ # build changelist
+ item_changelist = HTSChangeList(request, Item,
+ list_filter=[],
+ search_fields=[],
+ list_per_page=200,
+ model_admin=ItemAdmin(Item, None)
+ )
+
+ context_dict = {
+ 'item_changelist': item_changelist,
+ 'page_name': 'Inventory Index'
+ }
+ context_dict.update(INVENTORY_CONTEXT_DEFAULTS)
+
+ return render_to_response('inventory/inventory_all_index.html',
+ context_dict,
+ context_instance=RequestContext(request))
+
@login_required
def index(request):
"""
Inventory Index View
"""
+ # build changelist
+ item_changelist = HTSChangeList(request, ItemType,
+ list_filter=[],
+ search_fields=['name', 'description'],
+ list_per_page=50,
+ model_admin=ItemTypeAdmin(ItemType, None)
+ )
+
context_dict = {
+ 'item_changelist': item_changelist,
'page_name': 'Inventory Index'
}
context_dict.update(INVENTORY_CONTEXT_DEFAULTS)
-
return render_to_response('inventory/inventory_index.html',
context_dict,
context_instance=RequestContext(request))
-
+
+@login_required
+def itemtype_index(request, name):
+ """
+ Inventory Index View
+ """
+
+ name = name.replace('%20', ' ')
+
+ itemtype = ItemType.objects.get(name=name)
+
+ # build changelist
+ item_changelist = HTSChangeList(request, Item,
+ list_filter=[],
+ search_fields=[],
+ list_per_page=200,
+ model_admin=ItemAdmin(Item, None)
+ )
+
+ context_dict = {
+ 'item_changelist': item_changelist,
+ 'page_name': 'Inventory Index'
+ }
+ context_dict.update(INVENTORY_CONTEXT_DEFAULTS)
+
+ return render_to_response('inventory/inventory_itemtype_index.html',
+ context_dict,
+ context_instance=RequestContext(request))
+
@login_required
def item_summary_by_barcode(request, barcode_id, msg=''):
"""
try:
item = Item.objects.get(barcode_id=barcode_id)
- except ObjectDoesNotExist, e:
+ except ObjectDoesNotExist as e:
item = None
-
+
return item_summary_by_uuid(request, None, msg, item)
-
+
@login_required
def item_summary_by_uuid(request, uuid, msg='', item=None):
if item is None:
try:
item = Item.objects.get(uuid=uuid)
- except ObjectDoesNotExist, e:
+ except ObjectDoesNotExist as e:
item = None
-
+
context_dict = {
'page_name': 'Item Summary',
'item': item,
'msg': msg
}
context_dict.update(INVENTORY_CONTEXT_DEFAULTS)
-
+
return render_to_response('inventory/inventory_summary.html',
context_dict,
context_instance=RequestContext(request))
+
+
+
+
+def __expand_context(context, item):
+ """
+ If EXPAND_CONTEXT dictionary has item.item_type.name function registered, use it to expand context
+ """
+ if item.item_type.name in EXPAND_CONTEXT:
+ expand_func = EXPAND_CONTEXT[item.item_type.name]
+ expand_func(context, item)
+
def _item_print(item, request):
"""
Prints an item given a type of item label to print
"""
#FIXME: Hard coding this for now... need to abstract later.
context = {'item': item}
-
+ __expand_context(context, item)
+
# Print using barcode_id
if not item.force_use_uuid and (item.barcode_id is None or len(item.barcode_id.strip())):
context['use_uuid'] = False
else:
context['use_uuid'] = True
msg = 'Printing item with UUID: %s' % (item.uuid)
-
+
+ printer_template = getPrinterTemplateByType(item.item_type)
+
c = RequestContext(request, context)
- t = get_template(getTemplateByType(item.item_type.name))
- print_zpl_socket(t.render(c))
-
+ t = Template(printer_template.template)
+ print_zpl_socket(t.render(c), host=printer_template.printer.ip_address)
+
return msg
@login_required
"""
try:
item = Item.objects.get(uuid=uuid)
- except ObjectDoesNotExist, e:
+ except ObjectDoesNotExist as e:
item = None
msg = "Item with UUID %s does not exist" % (uuid)
-
+
if item is not None:
msg = _item_print(item, request)
-
+
return item_summary_by_uuid(request, uuid, msg)
"""
assert flowcell is not None
assert serial is not None
-
+
LTS_UPDATED = False
SD_UPDATED = False
LIBRARY_UPDATED = False
-
+
###########################################
# Retrieve Storage Device
try:
sd = Item.objects.get(barcode_id=serial)
- except ObjectDoesNotExist, e:
+ except ObjectDoesNotExist as e:
msg = "Item with barcode_id of %s not found." % (serial)
raise ObjectDoesNotExist(msg)
-
+
###########################################
# Retrieve FlowCell
- try:
- fc = FlowCell.objects.get(flowcell_id=flowcell)
- except ObjectDoesNotExist, e:
+ try:
+ fc = FlowCell.objects.get(flowcell_id__startswith=flowcell)
+ except ObjectDoesNotExist as e:
msg = "FlowCell with flowcell_id of %s not found." % (flowcell)
raise ObjectDoesNotExist(msg)
-
+
###########################################
# Retrieve or create LongTermStorage Object
count = fc.longtermstorage_set.count()
lts = None
if count > 1:
msg = "There really should only be one longtermstorage object per flowcell"
- raise ValueError, msg
+ raise ValueError(msg)
elif count == 1:
# lts already attached to flowcell
lts = fc.longtermstorage_set.all()[0]
# Need a primary keey before linking to storage devices
lts.save()
LTS_UPDATED = True
-
-
+
+
############################################
# Link Storage to Flowcell
-
+
# Add a link to this storage device if it is not already linked.
if sd not in lts.storage_devices.all():
lts.storage_devices.add(sd)
SD_UPDATED = True
-
+
###########################################
# Add Library Links to LTS
-
- if fc.lane_1_library not in lts.libraries.all():
- lts.libraries.add(fc.lane_1_library)
- LIBRARY_UPDATED = True
- print 1
-
- if fc.lane_2_library not in lts.libraries.all():
- lts.libraries.add(fc.lane_2_library)
- LIBRARY_UPDATED = True
- print 2
-
- if fc.lane_3_library not in lts.libraries.all():
- lts.libraries.add(fc.lane_3_library)
- LIBRARY_UPDATED = True
- print 3
-
- if fc.lane_4_library not in lts.libraries.all():
- lts.libraries.add(fc.lane_4_library)
- LIBRARY_UPDATED = True
- print 4
-
-
- if fc.lane_5_library not in lts.libraries.all():
- lts.libraries.add(fc.lane_5_library)
- LIBRARY_UPDATED = True
- print 5
-
- if fc.lane_6_library not in lts.libraries.all():
- lts.libraries.add(fc.lane_6_library)
- LIBRARY_UPDATED = True
- print 6
-
- if fc.lane_7_library not in lts.libraries.all():
- lts.libraries.add(fc.lane_7_library)
- LIBRARY_UPDATED = True
- print 7
-
- if fc.lane_8_library not in lts.libraries.all():
- lts.libraries.add(fc.lane_8_library)
- LIBRARY_UPDATED = True
- print 8
-
+
+ for lane in fc.lane_set.all():
+ if lane.library not in lts.libraries.all():
+ lts.libraries.add(lane.library)
+ LIBRARY_UPDATED = True
+
# Save Changes
lts.save()
-
+
msg = ['Success:']
if LTS_UPDATED or SD_UPDATED or LIBRARY_UPDATED:
msg.append(' LongTermStorage (LTS) Created: %s' % (LTS_UPDATED))
msg.append(' Libraries updated in LTS: %s' % (LIBRARY_UPDATED))
else:
msg.append(' No Updates Needed.')
-
- return HttpResponse('\n'.join(msg))
\ No newline at end of file
+
+ return HttpResponse('\n'.join(msg))