Add an 'active' state to the sequencers.
[htsworkflow.git] / htsworkflow / frontend / experiments / admin.py
1 from htsworkflow.frontend.experiments.models import \
2      FlowCell, DataRun, DataFile, FileType, ClusterStation, Sequencer, Lane
3 from django.contrib import admin
4 from django.contrib.admin.widgets import FilteredSelectMultiple
5 from django.forms import ModelForm
6 from django.forms.fields import Field, CharField
7 from django.forms.widgets import TextInput, Select
8 from django.utils.encoding import force_unicode
9 from django.utils.html import escape, conditional_escape
10 from django.utils.translation import ugettext_lazy as _
11
12 class DataFileForm(ModelForm):
13     class Meta:
14         model = DataFile
15
16 class DataFileInline(admin.TabularInline):
17     model = DataFile
18     form = DataFileForm
19     raw_id_fields = ('library',)
20     extra = 0
21
22 class DataRunOptions(admin.ModelAdmin):
23   search_fields = [
24       'flowcell_id',
25       'run_folder',
26       'run_note',
27       ]
28   list_display = [
29       'runfolder_name',
30       'result_dir',
31       'run_start_time',
32   ]
33   fieldsets = (
34       (None, {
35         'fields': (('flowcell', 'run_status'),
36                    ('runfolder_name', 'cycle_start', 'cycle_stop'),
37                    ('result_dir',),
38                    ('last_update_time'),
39                    ('image_software', 'image_version'),
40                    ('basecall_software', 'basecall_version'),
41                    ('alignment_software', 'alignment_version'),
42                    ('comment',))
43       }),
44     )
45   inlines = [ DataFileInline ]
46   #list_filter = ('run_status', 'run_start_time')
47 admin.site.register(DataRun, DataRunOptions)
48
49
50 class FileTypeAdmin(admin.ModelAdmin):
51     list_display = ('name', 'mimetype', 'regex')
52 admin.site.register(FileType, FileTypeAdmin)
53
54 # lane form setup needs to come before Flowcell form config
55 # as flowcell refers to the LaneInline class
56 class LaneForm(ModelForm):
57     comment = CharField(widget=TextInput(attrs={'size':'80'}), required=False)
58
59     class Meta:
60         model = Lane
61
62 class LaneInline(admin.StackedInline):
63     """
64     Controls display of Lanes on the Flowcell form.
65     """
66     model = Lane
67     extra = 8
68     form = LaneForm
69     raw_id_fields = ('library',)
70     fieldsets = (
71       (None, {
72         'fields': ('lane_number', 'flowcell',
73                    ('library',),
74                    ('pM', 'cluster_estimate', 'status'),
75                    'comment',)
76       }),
77     )
78
79 class LaneOptions(admin.ModelAdmin):
80     """
81     Controls display of Lane browser
82     """
83     search_fields = ('=flowcell__flowcell_id', 'library__id', 'library__library_name' )
84     list_display = ('flowcell', 'lane_number', 'library', 'comment')
85     fieldsets = (
86       (None, {
87         'fields': ('lane_number', 'flowcell',
88                    ('library'),
89                    ('pM', 'cluster_estimate'))
90       }),
91       ('Optional', {
92         'classes': ('collapse', ),
93         'fields': ('comment', )
94       }),
95     )
96 admin.site.register(Lane, LaneOptions)
97
98 class FlowCellOptions(admin.ModelAdmin):
99     class Media:
100         css = { 'all': ('css/admin_flowcell.css',) }
101     date_hierarchy = "run_date"
102     save_on_top = True
103     search_fields = ('flowcell_id',
104         'sequencer__name',
105         'cluster_station__name',
106         '=lane__library__id',
107         'lane__library__library_name')
108     list_display = ('flowcell_id','run_date','Lanes')
109     list_filter = ('sequencer','cluster_station')
110     fieldsets = (
111         (None, {
112           'fields': ('run_date', ('flowcell_id','cluster_station','sequencer'),
113                     ('read_length', 'control_lane', 'paired_end'),)
114         }),
115         ('Notes:', { 'fields': ('notes',),}),
116     )
117     inlines = [
118       LaneInline,
119     ]
120
121     def formfield_for_dbfield(self, db_field, **kwargs):
122         field = super(FlowCellOptions, self).formfield_for_dbfield(db_field,
123                                                                    **kwargs)
124
125         # Override field attributes
126         if db_field.name == 'sequencer':
127             # seems kind of clunky.
128             # the goal is to replace the default select/combo box with one
129             # that can strike out disabled options.
130             attrs = field.widget.widget.attrs
131             disabled_sequencers = field.queryset.filter(active=False)
132             attrs['disabled_sequencers'] = [ unicode(s.id) for s in disabled_sequencers ]
133             field.widget.widget = SequencerSelect(attrs=attrs)
134         elif db_field.name == "notes":
135             field.widget.attrs["rows"] = "3"
136         return field
137 admin.site.register(FlowCell, FlowCellOptions)
138
139 class ClusterStationOptions(admin.ModelAdmin):
140     list_display = ('name', )
141     fieldsets = ( ( None, { 'fields': ( 'name', ) } ), )
142 admin.site.register(ClusterStation, ClusterStationOptions)
143
144 class SequencerSelect(Select):
145     def render_option(self, selected_choices, option_value, option_label):
146         disabled_sequencers = self.attrs.get('disabled_sequencers', [])
147         option_value = unicode(option_value)
148         selected_html = (option_value in selected_choices) and u' selected="selected"' or ''
149         cssclass = "strikeout" if option_value in disabled_sequencers else ''
150         return u'<option class="%s" value="%s"%s>%s</option>' % (
151             cssclass, escape(option_value), selected_html,
152             conditional_escape(force_unicode(option_label)))
153
154 class SequencerOptions(admin.ModelAdmin):
155     list_display = ('name', 'active', 'instrument_name', 'model')
156     fieldsets = ( ( None,
157                     { 'fields': (
158                         'name', 'active', 'instrument_name', 'serial_number',
159                         'model', 'comment') } ), )
160
161 admin.site.register(Sequencer, SequencerOptions)