NOTE: settings.py update:
authorBrandon King <kingb@caltech.edu>
Thu, 2 Jul 2009 00:22:52 +0000 (00:22 +0000)
committerBrandon King <kingb@caltech.edu>
Thu, 2 Jul 2009 00:22:52 +0000 (00:22 +0000)
  Will need to include a section as follows to ini file:

[bcprinter]
printer1_host=<ip_address>
printer1_port=9100
printer2_host=<ip_address>
printer2_port=9100

Added a simple way to print out a label on inventory item summary
page. Needs major improvement on this page, but went for getting
labels printed to start with.

htsworkflow/frontend/bcprinter/__init__.py [new file with mode: 0644]
htsworkflow/frontend/bcprinter/models.py [new file with mode: 0644]
htsworkflow/frontend/bcprinter/util.py [new file with mode: 0644]
htsworkflow/frontend/bcprinter/views.py [new file with mode: 0644]
htsworkflow/frontend/inventory/urls.py
htsworkflow/frontend/inventory/views.py
htsworkflow/frontend/settings.py
htsworkflow/frontend/templates/inventory/default.zpl [new file with mode: 0644]
htsworkflow/frontend/templates/inventory/inventory_summary.html

diff --git a/htsworkflow/frontend/bcprinter/__init__.py b/htsworkflow/frontend/bcprinter/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/htsworkflow/frontend/bcprinter/models.py b/htsworkflow/frontend/bcprinter/models.py
new file mode 100644 (file)
index 0000000..71a8362
--- /dev/null
@@ -0,0 +1,3 @@
+from django.db import models
+
+# Create your models here.
diff --git a/htsworkflow/frontend/bcprinter/util.py b/htsworkflow/frontend/bcprinter/util.py
new file mode 100644 (file)
index 0000000..1880728
--- /dev/null
@@ -0,0 +1,27 @@
+from htsworkflow.frontend import settings
+
+import ftplib
+import socket
+import StringIO
+
+
+def print_zpl(zpl_text, host=settings.BCPRINTER_PRINTER1_HOST):
+    """
+    Sends zpl_text to printer
+    """
+    ftp = ftplib.FTP(host=host, user='blank', passwd='')
+    ftp.login()
+    ftp.storlines("STOR printme.txt", StringIO.StringIO(zpl_text))
+    ftp.quit()
+    
+
+def print_zpl_socket(zpl_text, host=settings.BCPRINTER_PRINTER1_HOST, port=settings.BCPRINTER_PRINTER1_PORT):
+    """
+    Sends zpl_text to printer via a socket
+    """
+    s = socket.socket()
+    # PORT 9100 is default for Zebra tabletop/desktop printers
+    # PORT 6101 is default for Zebra mobile printers
+    s.connect((host, port))
+    s.sendall(zpl_text)
+    s.close()
\ No newline at end of file
diff --git a/htsworkflow/frontend/bcprinter/views.py b/htsworkflow/frontend/bcprinter/views.py
new file mode 100644 (file)
index 0000000..60f00ef
--- /dev/null
@@ -0,0 +1 @@
+# Create your views here.
index 0d7da6c21699c20341f07b2a4967200bcb5ba5a4..4331cb770582130e218ad3451519359dc40f310c 100644 (file)
@@ -8,5 +8,6 @@ urlpatterns = patterns('',
      
     # INDEX
     (r'^(?P<uuid>[a-fA-F0-9]{32})/$', 'htsworkflow.frontend.inventory.views.item_summary'),
-     (r'^$', 'htsworkflow.frontend.inventory.views.index')
+    (r'^(?P<uuid>[a-fA-F0-9]{32})/print/$', 'htsworkflow.frontend.inventory.views.item_print'),
+    (r'^$', 'htsworkflow.frontend.inventory.views.index')
     )
index 9f9207b66739e7e2b80e366d27b0b659bb43c302..5dd3655aa583edc5d8985bbce2acdf2f20e372dc 100644 (file)
@@ -1,6 +1,8 @@
 from htsworkflow.frontend.inventory.models import Item, LongTermStorage
 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 django.core.exceptions import ObjectDoesNotExist
@@ -17,6 +19,20 @@ INVENTORY_CONTEXT_DEFAULTS = {
     'bcmagic': BarcodeMagicForm()
 }
 
+INVENTORY_ITEM_PRINT_DEFAULTS = {
+    'default': 'inventory/default.zpl',
+    'host': settings.BCPRINTER_PRINTER1_HOST
+}
+
+def getTemplateByType(item_type):
+    """
+    returns template to use given item_type
+    """
+    if item_type in INVENTORY_ITEM_PRINT_DEFAULTS:
+        return INVENTORY_ITEM_PRINT_DEFAULTS[item_type]
+    else:
+        return INVENTORY_ITEM_PRINT_DEFAULTS['default']
+
 @login_required
 def data_items(request):
     """
@@ -72,7 +88,7 @@ def index(request):
                               context_instance=RequestContext(request))
     
 @login_required
-def item_summary(request, uuid):
+def item_summary(request, uuid, msg=''):
     """
     Display a summary for an item
     """
@@ -84,7 +100,8 @@ def item_summary(request, uuid):
     context_dict = {
         'page_name': 'Item Summary',
         'item': item,
-        'uuid': uuid
+        'uuid': uuid,
+        'msg': msg
     }
     context_dict.update(INVENTORY_CONTEXT_DEFAULTS)
     
@@ -92,6 +109,46 @@ def item_summary(request, uuid):
                               context_dict,
                               context_instance=RequestContext(request))
 
+
+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}
+    
+    # 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
+        msg = 'Printing item with barcode id: %s' % (item.barcode_id)
+    # Print using uuid
+    else:
+        context['use_uuid'] = True
+        msg = 'Printing item with UUID: %s' % (item.uuid)
+    
+    c = RequestContext(request, context)
+    t = get_template(getTemplateByType(item.item_type.name))
+    print_zpl_socket(t.render(c))
+    
+    return msg
+
+@login_required
+def item_print(request, uuid):
+    """
+    Print a label for a given item
+    """
+    try:
+        item = Item.objects.get(uuid=uuid)
+    except ObjectDoesNotExist, 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(request, uuid, msg)
+
+
 def link_flowcell_and_device(request, flowcell, serial):
     """
     Updates database records of a flowcell being archived on a device with a particular serial #
index 17cdf23c6d26c1507d6217efc6535b58d0b84503..852f33cb9aa0a7472ffc3cc354e562a6523d4b16 100644 (file)
@@ -162,6 +162,7 @@ INSTALLED_APPS = (
     'htsworkflow.frontend.reports',
     'htsworkflow.frontend.inventory',
     'htsworkflow.frontend.bcmagic',
+    'htsworkflow.frontend.bcprinter',
     'django.contrib.databrowse',
 )
 
@@ -179,4 +180,9 @@ options_to_dict(ALLOWED_ANALYS_IPS, 'allowed_analysis_hosts')
 RESULT_HOME_DIR='/Users/diane/proj/solexa/results/flowcells'
 
 LINK_FLOWCELL_STORAGE_DEVICE_URL = options.get('frontend', 'link_flowcell_storage_device_url')
-
+# PORT 9100 is default for Zebra tabletop/desktop printers
+# PORT 6101 is default for Zebra mobile printers
+BCPRINTER_PRINTER1_HOST = options.get('bcprinter', 'printer1_host')
+BCPRINTER_PRINTER1_PORT = int(options.get('bcprinter', 'printer1_port'))
+BCPRINTER_PRINTER2_HOST = options.get('bcprinter', 'printer2_host')
+BCPRINTER_PRINTER2_PORT = int(options.get('bcprinter', 'printer2_port'))
diff --git a/htsworkflow/frontend/templates/inventory/default.zpl b/htsworkflow/frontend/templates/inventory/default.zpl
new file mode 100644 (file)
index 0000000..abd70f3
--- /dev/null
@@ -0,0 +1,25 @@
+^FX=========================
+^FX Harddrive Location Tracking Label
+^FX 300x375 dots
+^FX=========================
+
+^XA
+^LH 0,50
+
+^FO0,0
+^CF0,35
+^FB375,1,,C
+^FD{{ item.item_type.name }}:^FS
+
+^FX -------Text contains HD serial #-------------
+^FO35,75
+^CF0,42
+^FB305,3,,C
+^FD{% if use_uuid %}{{ item.uuid }}{% else %}{{ item.barcode_id }}{% endif %}^FS
+
+^FX -------Barcode contains HD serial #-----------
+^FO150,150
+^BXN,3,200
+^FD{% if use_uuid %}{{ item.uuid }}{% else %}{{ item.barcode_id }}{% endif %}^FS
+
+^XZ
index 0f184881d0e07802564c82d6ae2d09c8143d8e25..5112c215201fb59be46939173d5e79f4c08d1f32 100644 (file)
@@ -1,9 +1,13 @@
 {% extends "inventory/inventory_app.html" %}
 
 {% block content %}
-        {% if item %}
-                <h3>Item: {{item.uuid}}</h3>
-        {% else %}
-                <h3>Item with UUID of {{ uuid }} not found.</h3>
-        {% endif %}
+{% if item %}
+        <h3>Item: {{item.uuid}}</h3>
+        <a href="{% url htsworkflow.frontend.inventory.views.index %}{{item.uuid}}/print/">Print</a>
+        
+        
+        
+{% else %}
+        <h3>Item with UUID of {{ uuid }} not found.</h3>
+{% endif %}
 {% endblock %}