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