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