Record new minor release.
[htsworkflow.git] / htsworkflow / frontend / samples / admin.py
index d686e21e1bf14875e0d7e0c0ada807d623e64958..23b99f8d7421cf073d835a9418354472a703e082 100644 (file)
@@ -2,11 +2,18 @@ from django.contrib import admin
 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
+
+# Let's disable those pesky delete everything by accident features.
+admin.site.disable_action('delete_selected')
 
 class AffiliationOptions(admin.ModelAdmin):
     list_display = ('name','contact','email')
@@ -60,7 +67,18 @@ class ExperimentTypeOptions(admin.ModelAdmin):
   #list_display = ('name',)
   #fieldsets = ( (None, { 'fields': ('name',) }), )
 
-class HTSUserOptions(UserAdmin): pass
+class HTSUserCreationForm(UserCreationForm):
+    class Meta:
+        model = HTSUser
+        fields = ("username",'first_name','last_name')
+
+class HTSUserChangeForm(UserChangeForm):
+    class Meta:
+        model = HTSUser
+        
+class HTSUserOptions(UserAdmin):
+    form = HTSUserChangeForm
+    add_form = HTSUserCreationForm
 
 class LaneLibraryInline(admin.StackedInline):
   model = Lane
@@ -73,18 +91,23 @@ class LibraryTypeOptions(admin.ModelAdmin):
     model = LibraryType
 
 class LibraryOptions(admin.ModelAdmin):
+    class Media:
+        css = {
+            "all": ("css/wide_account_number.css",)
+            }
+        
     date_hierarchy = "creation_date"
     save_as = True
     save_on_top = True
     search_fields = (
-        'library_id',
+        'id',
         'library_name',
         'cell_line__cellline_name',
         'library_species__scientific_name',
         'library_species__common_name',
     )
     list_display = (
-        'library_id',
+        'id',
         #'aligned_reads',
         #'DataRun',
         'library_name',
@@ -115,17 +138,24 @@ class LibraryOptions(admin.ModelAdmin):
         'condition',
         'stopping_point',
         'hidden')
-    list_display_links = ('library_id', 'library_name',)
+    list_display_links = ('id', 'library_name',)
     fieldsets = (
       (None, {
         'fields': (
-          ('library_id','library_name','hidden'),
+          ('id','library_name','hidden'),
           ('library_species'),
           ('library_type', 'experiment_type', 'replicate'),
           ('cell_line','condition','antibody'),)
          }),
          ('Creation Information:', {
-             'fields' : (('made_for', 'made_by', 'creation_date'), ('stopping_point', 'amplified_from_sample'), ('avg_lib_size','undiluted_concentration', 'ten_nM_dilution', 'successful_pM'), 'notes',)
+             'fields' : (('made_for', 'made_by', 'creation_date'), 
+                         ('stopping_point', 'amplified_from_sample'), 
+                         ('gel_cut_size', 'insert_size', 
+                          'undiluted_concentration', 'ten_nM_dilution', 
+                          'successful_pM'), 
+                         ('bioanalyzer_concentration','bioanalyzer_image_url'),
+                         ('bioanalyzer_summary'), 
+                         ('account_number', 'notes',))
          }),
          ('Library/Project Affiliation:', {
              'fields' : (('affiliations'), ('tags'),)
@@ -134,18 +164,61 @@ 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)
+        
+        zpl_list = []
+        #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.
+            zpl_list.append(t.render(c))
+        
+        print_zpl_socket(zpl_list, 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:
-    # formfield_overrids = {
-    #   models.ManyToMany: { 'widget': widgets.FilteredSelectMultiple }
-    # }
     def formfield_for_dbfield(self, db_field, **kwargs):
-      if db_field.name == 'affiliations':
-        kwargs['widget'] = widgets.FilteredSelectMultiple(db_field.verbose_name, (db_field.name in self.filter_vertical))
-      rv = super(LibraryOptions, self).formfield_for_dbfield(db_field, **kwargs)
-      print db_field.name, kwargs
-      return rv
+        # Override Field type
+        if db_field.name in ('affiliations', 'tags'):
+            kwargs['widget'] = widgets.FilteredSelectMultiple(
+                db_field.verbose_name,
+                (db_field.name in self.filter_vertical)
+            )
+        field = super(LibraryOptions, self).formfield_for_dbfield(db_field,
+                                                                  **kwargs)
+        # Override field attributes
+        if db_field.name == "bioanalyzer_summary":
+            field.widget.attrs["rows"] = "3"
+        return field
 
 class SpeciesOptions(admin.ModelAdmin):
     fieldsets = (