Added a 'Print Labels' action to the Library Admin Page.
authorBrandon King <kingb@caltech.edu>
Thu, 29 Apr 2010 00:25:58 +0000 (00:25 +0000)
committerBrandon King <kingb@caltech.edu>
Thu, 29 Apr 2010 00:25:58 +0000 (00:25 +0000)
 * Django 1.1 feature.
 * FIXME: Requires a Printer Template (Inventory) with type Library to work...
   * returns a useful error if template does not exist.

htsworkflow/frontend/samples/admin.py
htsworkflow/frontend/samples/models.py

index deb61593d49dfbb13508d77c634b3096bc02a564..594cf35d4aa94d19b22a60904b513cc144e7f234 100644 (file)
@@ -3,11 +3,14 @@ from django.contrib.admin import widgets
 from django.contrib.admin.models import User
 from django.contrib.auth.admin import UserAdmin
 from django.contrib.auth.forms import UserCreationForm, UserChangeForm
+from django.template import Context, Template
 from django.db import models
 from django.utils.translation import ugettext_lazy as _
 
 from htsworkflow.frontend.samples.models import Antibody, Cellline, Condition, ExperimentType, HTSUser, LibraryType, Species, Affiliation, Library, Tag
 from htsworkflow.frontend.experiments.models import Lane
+from htsworkflow.frontend.inventory.models import PrinterTemplate
+from htsworkflow.frontend.bcmagic.utils import print_zpl_socket
 
 class AffiliationOptions(admin.ModelAdmin):
     list_display = ('name','contact','email')
@@ -151,6 +154,47 @@ class LibraryOptions(admin.ModelAdmin):
     inlines = [
       LaneLibraryInline,
     ]
+    actions = ['action_print_library_labels']
+    
+    
+    def action_print_library_labels(self, request, queryset):
+        """
+        Django action which prints labels for the selected set of labels from the
+        Django Admin interface.
+        """
+        
+        #Probably should ask if the user really meant to print all selected
+        # libraries if the count is above X. X=10 maybe?
+        
+        # Grab the library template
+        #FIXME: Hardcoding library template name. Not a good idea... *sigh*.
+        EVIL_HARDCODED_LIBRARY_TEMPLATE_NAME = "Library"
+        
+        try:
+            template = PrinterTemplate.objects.get(item_type__name=EVIL_HARDCODED_LIBRARY_TEMPLATE_NAME)
+        except PrinterTemplate.DoesNotExist:
+            self.message_user(request, "Could not find a library template with ItemType.name of '%s'" % \
+                              (EVIL_HARDCODED_LIBRARY_TEMPLATE_NAME))
+            return
+        
+        # ZPL Template
+        t = Template(template.template)
+        
+        #Iterate over selected labels to print
+        for library in queryset.all():
+            
+            # Django Template Context
+            c = Context({'library': library})
+            
+            # Send rendered template to the printer that the template
+            #  object has been attached to in the database.
+            print_zpl_socket(t.render(c), host=template.printer.ip_address)
+    
+        self.message_user(request, "%s labels printed." % (len(queryset)))
+                          
+    action_print_library_labels.short_description = "Print Labels"
+
+
 
     # some post 1.0.2 version of django has formfield_overrides 
     # which would replace this code with:
index 32d63a35d545fafc9f2f78ca295364db266d11a3..cce961918138a1e7f57a1f300be3f3078d594159 100644 (file)
@@ -127,6 +127,7 @@ class LibraryType(models.Model):
   def __unicode__(self):
     return unicode(self.name)
 
+
 class Library(models.Model):
   id = models.CharField(max_length=10, primary_key=True)
   library_name = models.CharField(max_length=100, unique=True)
@@ -253,16 +254,20 @@ class Library(models.Model):
     else: tstr = 'not processed yet' 
     return tstr
   aligned_reads.allow_tags = True
-
+  
   def public(self):
     SITE_ROOT = '/'
     summary_url = self.get_absolute_url()
     return '<a href="%s">S</a>' % (summary_url,)
   public.allow_tags = True
-
+    
   @models.permalink
   def get_absolute_url(self):
     return ('htsworkflow.frontend.samples.views.library_to_flowcells', [str(self.id)])
+    
+  
+    
+
 
 class HTSUser(User):
     """