Add feature to manage and print labels.
authorLorian Schaeffer <lorian@caltech.edu>
Thu, 26 May 2011 17:32:37 +0000 (10:32 -0700)
committerDiane Trout <diane@caltech.edu>
Thu, 26 May 2011 17:48:13 +0000 (10:48 -0700)
htsworkflow/frontend/labels/__init__.py [new file with mode: 0644]
htsworkflow/frontend/labels/admin.py [new file with mode: 0644]
htsworkflow/frontend/labels/models.py [new file with mode: 0644]
htsworkflow/frontend/labels/tests.py [new file with mode: 0644]
htsworkflow/frontend/labels/views.py [new file with mode: 0644]
htsworkflow/frontend/templates/admin/index.html
htsworkflow/settings.py

diff --git a/htsworkflow/frontend/labels/__init__.py b/htsworkflow/frontend/labels/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/htsworkflow/frontend/labels/admin.py b/htsworkflow/frontend/labels/admin.py
new file mode 100644 (file)
index 0000000..24445e5
--- /dev/null
@@ -0,0 +1,66 @@
+from htsworkflow.frontend.labels.models import LabelContent, LabelTemplate, LabelPrinter
+from htsworkflow.frontend.inventory.models import PrinterTemplate
+from htsworkflow.frontend.bcmagic.utils import print_zpl_socket
+from django.template import Context, Template
+from django.contrib import admin
+
+class LabelContentOptions(admin.ModelAdmin):
+    save_as = True
+    save_on_top = True
+    search_fields = (
+        'title',
+        'subtitle',
+        'text',
+        'barcode',
+        'creator',
+    )
+    list_display = ('title','subtitle','text','barcode','template','creator')
+    list_filter = ('template','creator',)
+    fieldsets = (
+      (None, {
+          'fields': (('title','subtitle','text','barcode'),
+                     ('template','creator'))
+
+      }),
+    )
+    actions = ['action_print_labels']
+    
+    def action_print_labels(self, request, queryset):
+        """
+        Django action which prints labels for the selected set of labels from the
+        Django Admin interface.
+        """
+       
+        zpl_list = []
+        #Iterate over selected labels to print
+        for label in queryset.all():
+
+          template_used = LabelTemplate.objects.get(name=label.template.name)
+          # ZPL Template
+          t = Template(template_used.ZPL_code)
+
+          # Django Template Context
+          c = Context({'label': label})
+            
+          # Send rendered template to the printer that the template
+          #  object has been attached to in the database.
+          zpl_list.append(t.render(c))
+        
+        print_zpl_socket(zpl_list, host=template_used.printer.ip_address)
+    
+        self.message_user(request, "%s labels printed." % (len(queryset)))
+                          
+    action_print_labels.short_description = "Print Selected Labels"
+
+class LabelTemplateOptions(admin.ModelAdmin):
+    save_as = True
+    save_on_top = True
+    list_display = ('name', 'printer', 'ZPL_code')
+
+class LabelPrinterOptions(admin.ModelAdmin):
+    list_display = ('name', 'ip_address', 'labels')
+
+admin.site.register(LabelContent, LabelContentOptions)
+admin.site.register(LabelTemplate, LabelTemplateOptions)
+admin.site.register(LabelPrinter, LabelPrinterOptions)
+
diff --git a/htsworkflow/frontend/labels/models.py b/htsworkflow/frontend/labels/models.py
new file mode 100644 (file)
index 0000000..8794492
--- /dev/null
@@ -0,0 +1,35 @@
+from django.db import models
+
+class LabelPrinter(models.Model):
+    """
+    Barcode Printer Information
+    """
+    name = models.CharField(max_length=256)
+    model = models.CharField(max_length=64, default='ZM400')
+    ip_address = models.IPAddressField()
+    labels = models.CharField(max_length=200)
+    notes = models.TextField(null=True, blank=True)
+
+    def __unicode__(self):
+        return u'%s: %s' % (self.name, self.labels)
+
+class LabelTemplate(models.Model):
+    """
+    Maps templates to printer to use
+    """
+    name = models.CharField(max_length=200)
+    description = models.TextField(null=True, blank=True)
+    printer = models.ForeignKey(LabelPrinter)
+    
+    ZPL_code = models.TextField('template')
+    
+    def __unicode__(self):
+            return '%s %s' % (self.name, self.printer.name)
+
+class LabelContent(models.Model):
+    title = models.CharField(max_length=200, null=True, blank=True)
+    subtitle = models.CharField(max_length=200, null=True, blank=True)
+    text = models.CharField(max_length=200, null=True, blank=True)
+    barcode = models.CharField(max_length=200, null=True, blank=True)
+    template = models.ForeignKey(LabelTemplate)
+    creator = models.CharField(max_length=200)
diff --git a/htsworkflow/frontend/labels/tests.py b/htsworkflow/frontend/labels/tests.py
new file mode 100644 (file)
index 0000000..2247054
--- /dev/null
@@ -0,0 +1,23 @@
+"""
+This file demonstrates two different styles of tests (one doctest and one
+unittest). These will both pass when you run "manage.py test".
+
+Replace these with more appropriate tests for your application.
+"""
+
+from django.test import TestCase
+
+class SimpleTest(TestCase):
+    def test_basic_addition(self):
+        """
+        Tests that 1 + 1 always equals 2.
+        """
+        self.failUnlessEqual(1 + 1, 2)
+
+__test__ = {"doctest": """
+Another way to test that 1 + 1 is equal to 2.
+
+>>> 1 + 1 == 2
+True
+"""}
+
diff --git a/htsworkflow/frontend/labels/views.py b/htsworkflow/frontend/labels/views.py
new file mode 100644 (file)
index 0000000..60f00ef
--- /dev/null
@@ -0,0 +1 @@
+# Create your views here.
index 0b5e1b20bc31b4da7d77280aec06795e77c9aa62..4a5677d2da199df85f36ca47f552bc3b9889d2fa 100644 (file)
 <th scope="row"><a href="/admin/experiments/flowcell/">Flowcells</a></th>
 <td><a href="/admin/experiments/flowcell/add/" class="addlink">{% trans 'Add' %}</a></td>
 </tr>
+</table></div>
+
+<div class='module'>
+<table>
+<caption>Label Printing</caption>
+<tr>
+<th scope="row"><a href="/admin/labels/labelcontent/">Label Contents</a></th>
+<td><a href="/admin/labels/labelcontent/add/" class="addlink">{% trans 'Add' %}</a></td>
+</tr>
+<tr>
+<th scope="row"><a href="/admin/labels/labeltemplate/">Label Templates</a></th>
+<td><a href="/admin/labels/labeltemplate/add/" class="addlink">{% trans 'Add' %}</a></td>
+</tr>
 </table></div><BR>
 
 {% if app_list %}
index 68dfcf90916f10a6578a7fd467618896854ae63a..250add4183f20e128bd5a9e1cd20fb49ad601296 100644 (file)
@@ -204,6 +204,7 @@ INSTALLED_APPS = (
     'htsworkflow.frontend.reports',
     'htsworkflow.frontend.inventory',
     'htsworkflow.frontend.bcmagic',
+    'htsworkflow.frontend.labels',
     'django.contrib.databrowse',
 )