393bfa253a19d5831f69780eb1c06a226e0b0007
[htsworkflow.git] / htsworkflow / frontend / samples / admin.py
1 from django.contrib import admin
2 from django.contrib.admin import widgets
3 from django.contrib.admin.models import User
4 from django.contrib.auth.admin import UserAdmin
5 from django.contrib.auth.forms import UserCreationForm, UserChangeForm
6 from django.template import Context, Template
7 from django.db import models
8 from django.utils.translation import ugettext_lazy as _
9
10 from htsworkflow.frontend.samples.models import Antibody, Cellline, Condition, ExperimentType, HTSUser, LibraryType, Species, Affiliation, Library, Tag
11 from htsworkflow.frontend.experiments.models import Lane
12 from htsworkflow.frontend.inventory.models import PrinterTemplate
13 from htsworkflow.frontend.bcmagic.utils import print_zpl_socket
14
15 # Let's disable those pesky delete everything by accident features.
16 admin.site.disable_action('delete_selected')
17
18 class AffiliationOptions(admin.ModelAdmin):
19     list_display = ('name','contact','email')
20     fieldsets = (
21       (None, {
22           'fields': (('name','contact','email','users'))
23       }),
24     )
25
26     # some post 1.0.2 version of django has formfield_overrides 
27     # which would replace this code with:
28     # formfield_overrids = {
29     #   models.ManyToMany: { 'widget': widgets.FilteredSelectMultiple }
30     # }
31     def formfield_for_dbfield(self, db_field, **kwargs):
32       if db_field.name == 'users':
33         kwargs['widget'] = widgets.FilteredSelectMultiple(db_field.verbose_name, (db_field.name in self.filter_vertical))
34       rv = super(AffiliationOptions, self).formfield_for_dbfield(db_field, **kwargs)
35     #  print db_field.name, kwargs
36       return rv
37
38 class AntibodyOptions(admin.ModelAdmin):
39     search_fields = ('antigene','nickname','catalog','antibodies','source','biology','notes')
40     list_display = ('antigene','nickname','antibodies','catalog','source','biology','notes')
41     list_filter = ('antibodies','source')
42     fieldsets = (
43       (None, {
44           'fields': (('antigene','nickname','antibodies'),('catalog','source'),('biology'),('notes'))
45       }),
46      )
47
48 class CelllineOptions(admin.ModelAdmin):
49     list_display = ('cellline_name', 'nickname', 'notes')
50     search_fields = ('cellline_name', 'nickname', 'notes')
51     fieldsets = (
52       (None, {
53           'fields': (('cellline_name'),('notes'),)
54       }),
55      )
56
57 class ConditionOptions(admin.ModelAdmin):
58     list_display = (('condition_name'), ('notes'),)
59     fieldsets = (
60       (None, {
61           'fields': (('condition_name'),('nickname'),('notes'),)
62       }),
63      )
64
65 class ExperimentTypeOptions(admin.ModelAdmin):
66   model = ExperimentType
67   #list_display = ('name',)
68   #fieldsets = ( (None, { 'fields': ('name',) }), )
69
70 class HTSUserCreationForm(UserCreationForm):
71     class Meta:
72         model = HTSUser
73         fields = ("username",'first_name','last_name')
74
75 class HTSUserChangeForm(UserChangeForm):
76     class Meta:
77         model = HTSUser
78         
79 class HTSUserOptions(UserAdmin):
80     form = HTSUserChangeForm
81     add_form = HTSUserCreationForm
82
83 class LaneLibraryInline(admin.StackedInline):
84   model = Lane
85   extra = 0
86
87 class Library_Inline(admin.TabularInline):
88   model = Library
89
90 class LibraryTypeOptions(admin.ModelAdmin):
91     model = LibraryType
92
93 class LibraryOptions(admin.ModelAdmin):
94     class Media:
95         css = {
96             "all": ("css/wide_account_number.css",)
97             }
98         
99     date_hierarchy = "creation_date"
100     save_as = True
101     save_on_top = True
102     search_fields = (
103         'id',
104         'library_name',
105         'cell_line__cellline_name',
106         'library_species__scientific_name',
107         'library_species__common_name',
108     )
109     list_display = (
110         'id',
111         #'aligned_reads',
112         #'DataRun',
113         'library_name',
114         'public',
115         #'experiment_type',
116         #'organism',
117         #'antibody_name',
118         #'cell_line',
119         #'libtags',
120         #'made_for',
121         'affiliation',
122         #'made_by',
123         'undiluted_concentration',
124         'creation_date',
125         'stopping_point',
126         #'condition',
127
128     )
129     list_filter = (
130         'experiment_type', 
131         'library_species', 
132         'tags',
133         #'made_for',
134         'affiliations',
135         'made_by', 
136         'antibody',
137         'cell_line',
138         'condition',
139         'stopping_point',
140         'hidden')
141     list_display_links = ('id', 'library_name',)
142     fieldsets = (
143       (None, {
144         'fields': (
145           ('id','library_name','hidden'),
146           ('library_species'),
147           ('library_type', 'experiment_type', 'replicate'),
148           ('cell_line','condition','antibody'),)
149          }),
150          ('Creation Information:', {
151              'fields' : (('made_for', 'made_by', 'creation_date'), ('stopping_point', 'amplified_from_sample'), ('gel_cut_size', 'insert_size', 'undiluted_concentration', 'ten_nM_dilution', 'successful_pM'), 'account_number', 'notes',)
152          }),
153          ('Library/Project Affiliation:', {
154              'fields' : (('affiliations'), ('tags'),)
155          }),
156          )
157     inlines = [
158       LaneLibraryInline,
159     ]
160     actions = ['action_print_library_labels']
161     
162     
163     def action_print_library_labels(self, request, queryset):
164         """
165         Django action which prints labels for the selected set of labels from the
166         Django Admin interface.
167         """
168         
169         #Probably should ask if the user really meant to print all selected
170         # libraries if the count is above X. X=10 maybe?
171         
172         # Grab the library template
173         #FIXME: Hardcoding library template name. Not a good idea... *sigh*.
174         EVIL_HARDCODED_LIBRARY_TEMPLATE_NAME = "Library"
175         
176         try:
177             template = PrinterTemplate.objects.get(item_type__name=EVIL_HARDCODED_LIBRARY_TEMPLATE_NAME)
178         except PrinterTemplate.DoesNotExist:
179             self.message_user(request, "Could not find a library template with ItemType.name of '%s'" % \
180                               (EVIL_HARDCODED_LIBRARY_TEMPLATE_NAME))
181             return
182         
183         # ZPL Template
184         t = Template(template.template)
185         
186         zpl_list = []
187         #Iterate over selected labels to print
188         for library in queryset.all():
189             
190             # Django Template Context
191             c = Context({'library': library})
192             
193             # Send rendered template to the printer that the template
194             #  object has been attached to in the database.
195             zpl_list.append(t.render(c))
196         
197         print_zpl_socket(zpl_list, host=template.printer.ip_address)
198     
199         self.message_user(request, "%s labels printed." % (len(queryset)))
200                           
201     action_print_library_labels.short_description = "Print Labels"
202
203
204
205     # some post 1.0.2 version of django has formfield_overrides 
206     # which would replace this code with:
207     # formfield_overrids = {
208     #    models.ManyToMany: { 'widget': widgets.FilteredSelectMultiple }
209     #}
210     def formfield_for_dbfield(self, db_field, **kwargs):
211       if db_field.name in ('affiliations', 'tags'):
212         kwargs['widget'] = widgets.FilteredSelectMultiple(db_field.verbose_name,
213                                                           (db_field.name in self.filter_vertical))
214       rv = super(LibraryOptions, self).formfield_for_dbfield(db_field, **kwargs)
215       return rv
216
217 class SpeciesOptions(admin.ModelAdmin):
218     fieldsets = (
219       (None, {
220           'fields': (('scientific_name', 'common_name'),)
221       }),
222     )
223
224 class TagOptions(admin.ModelAdmin):
225     list_display = ('tag_name', 'context')
226     fieldsets = ( 
227         (None, {
228           'fields': ('tag_name', 'context')
229           }),
230         )
231
232 admin.site.register(Affiliation, AffiliationOptions)
233 admin.site.register(Antibody, AntibodyOptions)
234 admin.site.register(Cellline, CelllineOptions)
235 admin.site.register(Condition, ConditionOptions)
236 admin.site.register(ExperimentType, ExperimentTypeOptions)
237 admin.site.register(HTSUser, HTSUserOptions)
238 admin.site.register(LibraryType, LibraryTypeOptions)
239 admin.site.register(Library, LibraryOptions)
240 admin.site.register(Species, SpeciesOptions)
241 admin.site.register(Tag, TagOptions)