e9419dc4a480771c09ea0987a3b2034f5ffeb55a
[htsworkflow.git] / htsworkflow / frontend / experiments / admin.py
1 from itertools import chain
2 from htsworkflow.frontend.experiments.models import \
3      FlowCell, FlowCellModel, 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 _
12
13 class DataFileForm(ModelForm):
14     class Meta:
15         model = DataFile
16
17 class DataFileInline(admin.TabularInline):
18     model = DataFile
19     form = DataFileForm
20     raw_id_fields = ('library',)
21     extra = 0
22
23 class DataRunOptions(admin.ModelAdmin):
24   search_fields = [
25       'flowcell_id',
26       'run_folder',
27       'run_note',
28       ]
29   list_display = [
30       'runfolder_name',
31       'result_dir',
32       'run_start_time',
33   ]
34   fieldsets = (
35       (None, {
36         'fields': (('flowcell', 'run_status'),
37                    ('runfolder_name', 'cycle_start', 'cycle_stop'),
38                    ('result_dir',),
39                    ('last_update_time'),
40                    ('image_software', 'image_version'),
41                    ('basecall_software', 'basecall_version'),
42                    ('alignment_software', 'alignment_version'),
43                    ('comment',))
44       }),
45     )
46   inlines = [ DataFileInline ]
47   #list_filter = ('run_status', 'run_start_time')
48 admin.site.register(DataRun, DataRunOptions)
49
50
51 class FileTypeAdmin(admin.ModelAdmin):
52     list_display = ('name', 'mimetype', 'regex')
53 admin.site.register(FileType, FileTypeAdmin)
54
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)
59
60     class Meta:
61         model = Lane
62
63 class LaneInline(admin.StackedInline):
64     """
65     Controls display of Lanes on the Flowcell form.
66     """
67     model = Lane
68     extra = 8
69     form = LaneForm
70     raw_id_fields = ('library',)
71     fieldsets = (
72       (None, {
73         'fields': ('lane_number', 'flowcell',
74                    ('library',),
75                    ('pM', 'cluster_estimate', 'status'),
76                    'comment',)
77       }),
78     )
79
80 class LaneOptions(admin.ModelAdmin):
81     """
82     Controls display of Lane browser
83     """
84     search_fields = ('=flowcell__flowcell_id', 'library__id', 'library__library_name' )
85     list_display = ('flowcell', 'lane_number', 'library', 'comment')
86     fieldsets = (
87       (None, {
88         'fields': ('lane_number', 'flowcell',
89                    ('library'),
90                    ('pM', 'cluster_estimate'))
91       }),
92       ('Optional', {
93         'classes': ('collapse', ),
94         'fields': ('comment', )
95       }),
96     )
97 admin.site.register(Lane, LaneOptions)
98
99 class FlowCellModelOptions(admin.ModelAdmin):
100     search_fields = ('name',)
101     list_display = ('name', 'fixed_time', 'per_cycle_time', 'isdefault')
102     fieldsets = (
103         (None, { 'fields': ('name', 'fixed_time', 'per_cycle_time', 'isdefault') }),
104         )
105 admin.site.register(FlowCellModel, FlowCellModelOptions)
106
107 class FlowCellOptions(admin.ModelAdmin):
108     class Media:
109         css = { 'all': ('css/admin_flowcell.css',) }
110     date_hierarchy = "run_date"
111     save_on_top = True
112     search_fields = ('flowcell_id',
113         'sequencer__name',
114         'cluster_station__name',
115         '=lane__library__id',
116         'lane__library__library_name')
117     list_display = ('flowcell_id','run_date','Lanes')
118     list_filter = ('sequencer','cluster_station', 'paired_end')
119     fieldsets = (
120         (None, {
121           'fields': ('run_date', ('flowcell_id','cluster_station','sequencer'),
122                     ('flowcell_model', 'read_length', 'paired_end', 'control_lane', ),)
123         }),
124         ('Notes:', { 'fields': ('notes',),}),
125     )
126     inlines = [
127       LaneInline,
128     ]
129
130     def formfield_for_dbfield(self, db_field, **kwargs):
131         field = super(FlowCellOptions, self).formfield_for_dbfield(db_field,
132                                                                    **kwargs)
133
134         # Override field attributes
135         if db_field.name == 'sequencer':
136             # seems kind of clunky.
137             # the goal is to replace the default select/combo box with one
138             # that can strike out disabled options.
139             attrs = field.widget.widget.attrs
140             field.widget.widget = SequencerSelect(attrs=attrs, queryset=field.queryset)
141         elif db_field.name == "notes":
142             field.widget.attrs["rows"] = "3"
143         return field
144 admin.site.register(FlowCell, FlowCellOptions)
145
146 class ClusterStationOptions(admin.ModelAdmin):
147     list_display = ('name', 'isdefault',)
148     fieldsets = ( ( None, { 'fields': ( 'name', 'isdefault') } ), )
149 admin.site.register(ClusterStation, ClusterStationOptions)
150
151 class SequencerSelect(Select):
152     def __init__(self, queryset=None, *args, **kwargs):
153         super(SequencerSelect, self).__init__(*args, **kwargs)
154         self.queryset = queryset
155
156     def render_options(self, choices, selected_choices):
157         # Normalize to strings.
158         selected_choices = set([force_unicode(v) for v in selected_choices])
159         output = []
160         for option_value, option_label in chain(self.choices, choices):
161             if isinstance(option_label, (list, tuple)):
162                 output.append(u'<optgroup label="%s">' % escape(force_unicode(option_value)))
163                 for option in option_label:
164                     output.append(self.render_option(selected_choices, *option))
165                 output.append(u'</optgroup>')
166             else:
167                 output.append(self.render_option(selected_choices, option_value, option_label))
168         return u'\n'.join(output)
169
170     # render_options blatently grabbed from 1.3.1 as the 1.2 version
171     # has render_option, which is what I needed to overload as a
172     # nested function in render_options
173     def render_options(self, choices, selected_choices):
174         # Normalize to strings.
175         selected_choices = set([force_unicode(v) for v in selected_choices])
176         output = []
177         for option_value, option_label in chain(self.choices, choices):
178             if isinstance(option_label, (list, tuple)):
179                 output.append(u'<optgroup label="%s">' % escape(force_unicode(option_value)))
180                 for option in option_label:
181                     output.append(self.render_option(selected_choices, *option))
182                 output.append(u'</optgroup>')
183             else:
184                 output.append(self.render_option(selected_choices, option_value, option_label))
185         return u'\n'.join(output)
186
187
188     def render_option(self, selected_choices, option_value, option_label):
189         disabled_sequencers = [ unicode(s.id) for s in self.queryset.filter(active=False) ]
190         option_value = unicode(option_value)
191         selected_html = (option_value in selected_choices) and u' selected="selected"' or ''
192         cssclass = "strikeout" if option_value in disabled_sequencers else ''
193         return u'<option class="%s" value="%s"%s>%s</option>' % (
194             cssclass, escape(option_value), selected_html,
195             conditional_escape(force_unicode(option_label)))
196
197 class SequencerOptions(admin.ModelAdmin):
198     list_display = ('name', 'active', 'isdefault', 'instrument_name', 'model')
199     fieldsets = ( ( None,
200                     { 'fields': (
201                         'name', ('active', 'isdefault'), 'instrument_name', 'serial_number',
202                         'model', 'comment') } ), )
203
204 admin.site.register(Sequencer, SequencerOptions)