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 _
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
15 class AffiliationOptions(admin.ModelAdmin):
16 list_display = ('name','contact','email')
19 'fields': (('name','contact','email','users'))
23 # some post 1.0.2 version of django has formfield_overrides
24 # which would replace this code with:
25 # formfield_overrids = {
26 # models.ManyToMany: { 'widget': widgets.FilteredSelectMultiple }
28 def formfield_for_dbfield(self, db_field, **kwargs):
29 if db_field.name == 'users':
30 kwargs['widget'] = widgets.FilteredSelectMultiple(db_field.verbose_name, (db_field.name in self.filter_vertical))
31 rv = super(AffiliationOptions, self).formfield_for_dbfield(db_field, **kwargs)
32 # print db_field.name, kwargs
35 class AntibodyOptions(admin.ModelAdmin):
36 search_fields = ('antigene','nickname','catalog','antibodies','source','biology','notes')
37 list_display = ('antigene','nickname','antibodies','catalog','source','biology','notes')
38 list_filter = ('antibodies','source')
41 'fields': (('antigene','nickname','antibodies'),('catalog','source'),('biology'),('notes'))
45 class CelllineOptions(admin.ModelAdmin):
46 list_display = ('cellline_name', 'nickname', 'notes')
47 search_fields = ('cellline_name', 'nickname', 'notes')
50 'fields': (('cellline_name'),('notes'),)
54 class ConditionOptions(admin.ModelAdmin):
55 list_display = (('condition_name'), ('notes'),)
58 'fields': (('condition_name'),('nickname'),('notes'),)
62 class ExperimentTypeOptions(admin.ModelAdmin):
63 model = ExperimentType
64 #list_display = ('name',)
65 #fieldsets = ( (None, { 'fields': ('name',) }), )
67 class HTSUserCreationForm(UserCreationForm):
70 fields = ("username",'first_name','last_name')
72 class HTSUserChangeForm(UserChangeForm):
76 class HTSUserOptions(UserAdmin):
77 form = HTSUserChangeForm
78 add_form = HTSUserCreationForm
80 class LaneLibraryInline(admin.StackedInline):
84 class Library_Inline(admin.TabularInline):
87 class LibraryTypeOptions(admin.ModelAdmin):
90 class LibraryOptions(admin.ModelAdmin):
93 "all": ("css/wide_account_number.css",)
96 date_hierarchy = "creation_date"
102 'cell_line__cellline_name',
103 'library_species__scientific_name',
104 'library_species__common_name',
120 'undiluted_concentration',
138 list_display_links = ('id', 'library_name',)
142 ('id','library_name','hidden'),
144 ('library_type', 'experiment_type', 'replicate'),
145 ('cell_line','condition','antibody'),)
147 ('Creation Information:', {
148 'fields' : (('made_for', 'made_by', 'creation_date'), ('stopping_point', 'amplified_from_sample'), ('avg_lib_size','undiluted_concentration', 'ten_nM_dilution', 'successful_pM'), 'account_number', 'notes',)
150 ('Library/Project Affiliation:', {
151 'fields' : (('affiliations'), ('tags'),)
157 actions = ['action_print_library_labels']
160 def action_print_library_labels(self, request, queryset):
162 Django action which prints labels for the selected set of labels from the
163 Django Admin interface.
166 #Probably should ask if the user really meant to print all selected
167 # libraries if the count is above X. X=10 maybe?
169 # Grab the library template
170 #FIXME: Hardcoding library template name. Not a good idea... *sigh*.
171 EVIL_HARDCODED_LIBRARY_TEMPLATE_NAME = "Library"
174 template = PrinterTemplate.objects.get(item_type__name=EVIL_HARDCODED_LIBRARY_TEMPLATE_NAME)
175 except PrinterTemplate.DoesNotExist:
176 self.message_user(request, "Could not find a library template with ItemType.name of '%s'" % \
177 (EVIL_HARDCODED_LIBRARY_TEMPLATE_NAME))
181 t = Template(template.template)
183 #Iterate over selected labels to print
184 for library in queryset.all():
186 # Django Template Context
187 c = Context({'library': library})
189 # Send rendered template to the printer that the template
190 # object has been attached to in the database.
191 print_zpl_socket(t.render(c), host=template.printer.ip_address)
193 self.message_user(request, "%s labels printed." % (len(queryset)))
195 action_print_library_labels.short_description = "Print Labels"
199 # some post 1.0.2 version of django has formfield_overrides
200 # which would replace this code with:
201 # formfield_overrids = {
202 # models.ManyToMany: { 'widget': widgets.FilteredSelectMultiple }
204 def formfield_for_dbfield(self, db_field, **kwargs):
205 if db_field.name in ('affiliations', 'tags'):
206 kwargs['widget'] = widgets.FilteredSelectMultiple(db_field.verbose_name,
207 (db_field.name in self.filter_vertical))
208 rv = super(LibraryOptions, self).formfield_for_dbfield(db_field, **kwargs)
211 class SpeciesOptions(admin.ModelAdmin):
214 'fields': (('scientific_name', 'common_name'),)
218 class TagOptions(admin.ModelAdmin):
219 list_display = ('tag_name', 'context')
222 'fields': ('tag_name', 'context')
226 admin.site.register(Affiliation, AffiliationOptions)
227 admin.site.register(Antibody, AntibodyOptions)
228 admin.site.register(Cellline, CelllineOptions)
229 admin.site.register(Condition, ConditionOptions)
230 admin.site.register(ExperimentType, ExperimentTypeOptions)
231 admin.site.register(HTSUser, HTSUserOptions)
232 admin.site.register(LibraryType, LibraryTypeOptions)
233 admin.site.register(Library, LibraryOptions)
234 admin.site.register(Species, SpeciesOptions)
235 admin.site.register(Tag, TagOptions)