1 from itertools import chain
2 from htsworkflow.frontend.experiments.models import \
3 FlowCell, DataRun, DataFile, FileType, ClusterStation, Sequencer, Lane
4 from django.contrib import admin
5 from django.contrib.admin.widgets import FilteredSelectMultiple
6 from django.forms import ModelForm
7 from django.forms.fields import Field, CharField
8 from django.forms.widgets import TextInput, Select
9 from django.utils.encoding import force_unicode
10 from django.utils.html import escape, conditional_escape
11 from django.utils.translation import ugettext_lazy as _
13 class DataFileForm(ModelForm):
17 class DataFileInline(admin.TabularInline):
20 raw_id_fields = ('library',)
23 class DataRunOptions(admin.ModelAdmin):
36 'fields': (('flowcell', 'run_status'),
37 ('runfolder_name', 'cycle_start', 'cycle_stop'),
40 ('image_software', 'image_version'),
41 ('basecall_software', 'basecall_version'),
42 ('alignment_software', 'alignment_version'),
46 inlines = [ DataFileInline ]
47 #list_filter = ('run_status', 'run_start_time')
48 admin.site.register(DataRun, DataRunOptions)
51 class FileTypeAdmin(admin.ModelAdmin):
52 list_display = ('name', 'mimetype', 'regex')
53 admin.site.register(FileType, FileTypeAdmin)
55 # lane form setup needs to come before Flowcell form config
56 # as flowcell refers to the LaneInline class
57 class LaneForm(ModelForm):
58 comment = CharField(widget=TextInput(attrs={'size':'80'}), required=False)
63 class LaneInline(admin.StackedInline):
65 Controls display of Lanes on the Flowcell form.
70 raw_id_fields = ('library',)
73 'fields': ('lane_number', 'flowcell',
75 ('pM', 'cluster_estimate', 'status'),
80 class LaneOptions(admin.ModelAdmin):
82 Controls display of Lane browser
84 search_fields = ('=flowcell__flowcell_id', 'library__id', 'library__library_name' )
85 list_display = ('flowcell', 'lane_number', 'library', 'comment')
88 'fields': ('lane_number', 'flowcell',
90 ('pM', 'cluster_estimate'))
93 'classes': ('collapse', ),
94 'fields': ('comment', )
97 admin.site.register(Lane, LaneOptions)
99 class FlowCellOptions(admin.ModelAdmin):
101 css = { 'all': ('css/admin_flowcell.css',) }
102 date_hierarchy = "run_date"
104 search_fields = ('flowcell_id',
106 'cluster_station__name',
107 '=lane__library__id',
108 'lane__library__library_name')
109 list_display = ('flowcell_id','run_date','Lanes')
110 list_filter = ('sequencer','cluster_station', 'paired_end')
113 'fields': ('run_date', ('flowcell_id','cluster_station','sequencer'),
114 ('read_length', 'control_lane', 'paired_end'),)
116 ('Notes:', { 'fields': ('notes',),}),
122 def formfield_for_dbfield(self, db_field, **kwargs):
123 field = super(FlowCellOptions, self).formfield_for_dbfield(db_field,
126 # Override field attributes
127 if db_field.name == 'sequencer':
128 # seems kind of clunky.
129 # the goal is to replace the default select/combo box with one
130 # that can strike out disabled options.
131 attrs = field.widget.widget.attrs
132 field.widget.widget = SequencerSelect(attrs=attrs, queryset=field.queryset)
133 elif db_field.name == "notes":
134 field.widget.attrs["rows"] = "3"
136 admin.site.register(FlowCell, FlowCellOptions)
138 class ClusterStationOptions(admin.ModelAdmin):
139 list_display = ('name', 'isdefault',)
140 fieldsets = ( ( None, { 'fields': ( 'name', 'isdefault') } ), )
141 admin.site.register(ClusterStation, ClusterStationOptions)
143 class SequencerSelect(Select):
144 def __init__(self, queryset=None, *args, **kwargs):
145 super(SequencerSelect, self).__init__(*args, **kwargs)
146 self.queryset = queryset
148 def render_options(self, choices, selected_choices):
149 # Normalize to strings.
150 selected_choices = set([force_unicode(v) for v in selected_choices])
152 for option_value, option_label in chain(self.choices, choices):
153 if isinstance(option_label, (list, tuple)):
154 output.append(u'<optgroup label="%s">' % escape(force_unicode(option_value)))
155 for option in option_label:
156 output.append(self.render_option(selected_choices, *option))
157 output.append(u'</optgroup>')
159 output.append(self.render_option(selected_choices, option_value, option_label))
160 return u'\n'.join(output)
162 # render_options blatently grabbed from 1.3.1 as the 1.2 version
163 # has render_option, which is what I needed to overload as a
164 # nested function in render_options
165 def render_options(self, choices, selected_choices):
166 # Normalize to strings.
167 selected_choices = set([force_unicode(v) for v in selected_choices])
169 for option_value, option_label in chain(self.choices, choices):
170 if isinstance(option_label, (list, tuple)):
171 output.append(u'<optgroup label="%s">' % escape(force_unicode(option_value)))
172 for option in option_label:
173 output.append(self.render_option(selected_choices, *option))
174 output.append(u'</optgroup>')
176 output.append(self.render_option(selected_choices, option_value, option_label))
177 return u'\n'.join(output)
180 def render_option(self, selected_choices, option_value, option_label):
181 disabled_sequencers = [ unicode(s.id) for s in self.queryset.filter(active=False) ]
182 option_value = unicode(option_value)
183 selected_html = (option_value in selected_choices) and u' selected="selected"' or ''
184 cssclass = "strikeout" if option_value in disabled_sequencers else ''
185 return u'<option class="%s" value="%s"%s>%s</option>' % (
186 cssclass, escape(option_value), selected_html,
187 conditional_escape(force_unicode(option_label)))
189 class SequencerOptions(admin.ModelAdmin):
190 list_display = ('name', 'active', 'isdefault', 'instrument_name', 'model')
191 fieldsets = ( ( None,
193 'name', ('active', 'isdefault'), 'instrument_name', 'serial_number',
194 'model', 'comment') } ), )
196 admin.site.register(Sequencer, SequencerOptions)