From be807ce7093f41fa6345314fc4fb3fa7434bcee7 Mon Sep 17 00:00:00 2001 From: Lorian Schaeffer Date: Thu, 26 May 2011 10:32:37 -0700 Subject: [PATCH] Add feature to manage and print labels. --- htsworkflow/frontend/labels/__init__.py | 0 htsworkflow/frontend/labels/admin.py | 66 +++++++++++++++++++ htsworkflow/frontend/labels/models.py | 35 ++++++++++ htsworkflow/frontend/labels/tests.py | 23 +++++++ htsworkflow/frontend/labels/views.py | 1 + .../frontend/templates/admin/index.html | 13 ++++ htsworkflow/settings.py | 1 + 7 files changed, 139 insertions(+) create mode 100644 htsworkflow/frontend/labels/__init__.py create mode 100644 htsworkflow/frontend/labels/admin.py create mode 100644 htsworkflow/frontend/labels/models.py create mode 100644 htsworkflow/frontend/labels/tests.py create mode 100644 htsworkflow/frontend/labels/views.py diff --git a/htsworkflow/frontend/labels/__init__.py b/htsworkflow/frontend/labels/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/htsworkflow/frontend/labels/admin.py b/htsworkflow/frontend/labels/admin.py new file mode 100644 index 0000000..24445e5 --- /dev/null +++ b/htsworkflow/frontend/labels/admin.py @@ -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 index 0000000..8794492 --- /dev/null +++ b/htsworkflow/frontend/labels/models.py @@ -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 index 0000000..2247054 --- /dev/null +++ b/htsworkflow/frontend/labels/tests.py @@ -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 index 0000000..60f00ef --- /dev/null +++ b/htsworkflow/frontend/labels/views.py @@ -0,0 +1 @@ +# Create your views here. diff --git a/htsworkflow/frontend/templates/admin/index.html b/htsworkflow/frontend/templates/admin/index.html index 0b5e1b2..4a5677d 100644 --- a/htsworkflow/frontend/templates/admin/index.html +++ b/htsworkflow/frontend/templates/admin/index.html @@ -23,6 +23,19 @@ Flowcells {% trans 'Add' %} + + +
+ + + + + + + + + +
Label Printing
Label Contents{% trans 'Add' %}
Label Templates{% trans 'Add' %}

{% if app_list %} diff --git a/htsworkflow/settings.py b/htsworkflow/settings.py index 68dfcf9..250add4 100644 --- a/htsworkflow/settings.py +++ b/htsworkflow/settings.py @@ -204,6 +204,7 @@ INSTALLED_APPS = ( 'htsworkflow.frontend.reports', 'htsworkflow.frontend.inventory', 'htsworkflow.frontend.bcmagic', + 'htsworkflow.frontend.labels', 'django.contrib.databrowse', ) -- 2.30.2