submitting jobs: remove stage out attribute remainings
[qcg-portal.git] / qcg / forms.py
1 # coding=utf-8
2 from django import forms
3 from django.core.validators import RegexValidator
4 from django.template.defaultfilters import capfirst
5 from pyqcg.utils import TaskStatus
6
7 from qcg.fields import TimeRangeField
8 from qcg.models import Task, Allocation
9
10
11 date_range_validator = RegexValidator(r'[0-9]{2}\.[0-9]{2}\.[0-9]{4} - [0-9]{2}\.[0-9]{2}\.[0-9]{4}')
12 nodes_validator = RegexValidator(r'^[0-9]{1,3}:[0-9]{1,2}(:[0-9]{1,2})?$')
13 env_name_validator = RegexValidator(r'^[a-zA-Z_][a-zA-Z0-9_]*$')
14
15 CHOICES_PLACEHOLDER = (None, '')
16
17
18 class FiltersForm(forms.Form):
19     ACTIVE, FINISHED, FAILED = range(3)
20     STATUS_CHOICES = (
21         (ACTIVE, u"Aktywne"),
22         (FINISHED, u"Zakończone"),
23         (FAILED, u"Niepowodzenia"),
24     )
25
26     STATUS_MAP = {
27         ACTIVE: (
28             Task.STATUS_CHOICES_REVERSED[TaskStatus.UNSUBMITTED],
29             Task.STATUS_CHOICES_REVERSED[TaskStatus.UNCOMMITTED],
30             Task.STATUS_CHOICES_REVERSED[TaskStatus.QUEUED],
31             Task.STATUS_CHOICES_REVERSED[TaskStatus.PREPROCESSING],
32             Task.STATUS_CHOICES_REVERSED[TaskStatus.PENDING],
33             Task.STATUS_CHOICES_REVERSED[TaskStatus.RUNNING],
34             Task.STATUS_CHOICES_REVERSED[TaskStatus.STOPPED],
35             Task.STATUS_CHOICES_REVERSED[TaskStatus.POSTPROCESSING],
36         ),
37         FINISHED: (
38             Task.STATUS_CHOICES_REVERSED[TaskStatus.FINISHED],
39         ),
40         FAILED: (
41             Task.STATUS_CHOICES_REVERSED[TaskStatus.FAILED],
42             Task.STATUS_CHOICES_REVERSED[TaskStatus.CANCELED],
43         ),
44     }
45     STATUS_CHOICES_DICT = dict(STATUS_CHOICES)
46
47     status = forms.MultipleChoiceField(choices=STATUS_CHOICES, label=u"Status", required=False,
48                                        widget=forms.CheckboxSelectMultiple)
49     host = forms.MultipleChoiceField(label=u"Host", required=False, widget=forms.CheckboxSelectMultiple)
50
51     # advanced
52     keywords = forms.CharField(max_length=100, label=u"Wyszukaj frazę", required=False)
53     submission = forms.CharField(label=u"Data zlecenia", validators=[date_range_validator], required=False)
54     finish = forms.CharField(label=u"Data zakończenia", validators=[date_range_validator], required=False)
55
56     def __init__(self, *args, **kwargs):
57         super(FiltersForm, self).__init__(*args, **kwargs)
58
59         self.fields['host'].choices = tuple(
60             (host, capfirst(host.split('.', 1)[0]))
61             for host in Allocation.objects.values_list('host_name', flat=True).order_by('host_name').distinct())
62
63
64 class JobDescriptionForm(forms.Form):
65     class Host(object):
66         GALERA = 'galera.task.gda.pl'
67         HYDRA = 'hydra.icm.edu.pl'
68         INULA = 'inula.man.poznan.pl'
69         MOSS = 'moss.man.poznan.pl'
70         NOVA = 'nova.wcss.wroc.pl'
71         REEF = 'reef.man.poznan.pl'
72         ZEUS = 'zeus.cyfronet.pl'
73
74         CHOICES = (
75             CHOICES_PLACEHOLDER,
76             (GALERA, u'Galera'),
77             (HYDRA, u'Hydra'),
78             (INULA, u'Inula'),
79             (MOSS, u'Moss'),
80             (NOVA, u'Supernova'),
81             (REEF, u'Reef'),
82             (ZEUS, u'Zeus'),
83         )
84
85     class Process(object):
86         NONE = ''
87         CMD = 'c'
88         SCRIPT = 's'
89
90         CHOICES = (
91             (NONE, u'Brak'),
92             (CMD, u'Polecenie'),
93             (SCRIPT, u'Skrypt'),
94         )
95
96     APPLICATION_CHOICES = (
97         CHOICES_PLACEHOLDER,
98         ('bash', 'BASH'),
99         ('gromacs/4.6.3', 'GROMACS 4.6.3'),
100         ('matlab', 'MATLAB'),
101         ('python', 'Python'),
102     )
103     QUEUE_CHOICES = (
104         CHOICES_PLACEHOLDER,
105         ('plgid', 'plgrid'),
106         ('plgid-long', 'plgrid-long'),
107         ('plgid-testing', 'plgrid-testing'),
108     )
109     MODULES_CHOICES = (
110         ('plgrid/apps/python', 'plgrid/apps/python'),
111         ('plgrid/apps/matlab', 'plgrid/apps/matlab'),
112     )
113     PROTOCOL_CHOICES = (
114         ('', u'Brak'),
115         ('mailto', u'E-mail'),
116         ('xmpp', u'XMPP'),
117     )
118
119     application = forms.ChoiceField(choices=APPLICATION_CHOICES, label=u"Aplikacja", required=False)  # TODO choices
120     master_file = forms.CharField(label=u"Plik główny", max_length=500, required=False)
121     executable = forms.CharField(label=u"Plik wykonywalny", max_length=500, required=False)
122     script = forms.CharField(label=u"Skrypt", widget=forms.Textarea(attrs={'rows': 2, 'cols': 40}), required=False)  # TODO saving to grid ftp
123     arguments = forms.MultipleChoiceField(label=u"Argumenty", required=False)
124     note = forms.CharField(label=u"Opis", widget=forms.Textarea(attrs={'rows': 2, 'cols': 40}), required=False)
125     grant = forms.CharField(label=u"Grant", max_length=100, required=False)
126
127     hosts = forms.MultipleChoiceField(label=u"Host", choices=Host.CHOICES, required=False)
128     properties = forms.CharField(label=u"Właściwości węzłów", required=False)
129     queue = forms.ChoiceField(choices=QUEUE_CHOICES, label=u"Kolejka", required=False)
130     procs = forms.IntegerField(label=u"Liczba procesów", min_value=0, required=False)
131     nodes = forms.CharField(label=u"Topologia węzłów", max_length=10, validators=[nodes_validator], required=False)
132     wall_time = TimeRangeField(label=u"Wall time", required=False)
133     memory = forms.IntegerField(label=u"Pamięć (MB)", min_value=0, required=False)
134     memory_per_slot = forms.IntegerField(label=u"Pamięci per proces (MB)", min_value=0, required=False)
135     modules = forms.MultipleChoiceField(label=u"Moduły", choices=MODULES_CHOICES, required=False)  # TODO choices
136     reservation = forms.CharField(label=u"Rezerwacja", max_length=100, required=False)
137
138     input = forms.CharField(label=u"Standardowe wejście", max_length=500, required=False)
139     stage_in = forms.MultipleChoiceField(label=u"Stage in", required=False)
140
141     monitoring = forms.BooleanField(label=u"Portal QCG-Monitoring", required=False)
142     notify_type = forms.ChoiceField(label=u"Monitorowanie stanu", choices=PROTOCOL_CHOICES, required=False, initial='',
143                                     widget=forms.RadioSelect)
144     notify_address = forms.EmailField(label=u"Adres", required=False)
145     watch_output_type = forms.ChoiceField(label=u"Monitorowanie wyjścia", choices=PROTOCOL_CHOICES, required=False,
146                                           initial='', widget=forms.RadioSelect)
147     watch_output_address = forms.EmailField(label=u"Adres", required=False)
148     watch_output_pattern = forms.CharField(label=u"Wzorzec", max_length=500, required=False)
149
150     preprocess_type = forms.ChoiceField(label=u"Preprocessing", choices=Process.CHOICES, required=False,
151                                         initial=Process.NONE, widget=forms.RadioSelect)
152     preprocess_cmd = forms.CharField(label=u"Polecenie", max_length=1000, required=False)
153     preprocess_script = forms.CharField(label=u"Skrypt", max_length=500, required=False)
154     postprocess_type = forms.ChoiceField(label=u"Postprocessing", choices=Process.CHOICES, required=False,
155                                          initial=Process.NONE, widget=forms.RadioSelect)
156     postprocess_cmd = forms.CharField(label=u"Polecenie", max_length=1000, required=False)
157     postprocess_script = forms.CharField(label=u"Skrypt", max_length=500, required=False)
158     native = forms.MultipleChoiceField(label=u"Opcje systemu kolejkowego", required=False)
159     persistent = forms.BooleanField(label=u"Trwałe", required=False)
160
161     def __init__(self, data=None, *args, **kwargs):
162         super(JobDescriptionForm, self).__init__(data, *args, **kwargs)
163
164         if data is not None:
165             # accept user defined choices
166             self.fields['queue'].choices += ((data.get('queue'), data.get('queue')), )
167             self.fields['arguments'].choices += ((v, v) for v in data.getlist('arguments'))
168             self.fields['native'].choices += ((v, v) for v in data.getlist('native'))
169             self.fields['stage_in'].choices += ((v, v) for v in data.getlist('stage_in'))
170
171     def clean(self):
172         data = super(JobDescriptionForm, self).clean()
173
174         if data['application'] and not data['master_file']:
175             self.add_error('master_file', u"W trybie uruchamiania aplikacji należy podać plik główny")
176
177         notify_type = data.get('notify_type')
178         data['notify'] = u'{}:{}'.format(notify_type, data['notify_address']) if notify_type else ''
179
180         wo_type = data.get('watch_output_type')
181         data['watch_output'] = u'{}:{}'.format(wo_type, data['watch_output_address']) if wo_type else ''
182
183         preprocess_type = data.get('preprocess_type')
184         if preprocess_type == self.Process.CMD:
185             data['preprocess'] = data['preprocess_cmd']
186         elif preprocess_type == self.Process.SCRIPT:
187             data['preprocess'] = data['preprocess_script']
188         else:
189             data['preprocess'] = ''
190
191         postprocess_type = data.get('postprocess_type')
192         if postprocess_type == self.Process.CMD:
193             data['postprocess'] = data['postprocess_cmd']
194         elif postprocess_type == self.Process.SCRIPT:
195             data['postprocess'] = data['postprocess_script']
196         else:
197             data['postprocess'] = ''
198
199         return data
200
201     def clean_application(self):
202         return self.cleaned_data['application'].split('/', 1) if self.cleaned_data['application'] else ''
203
204     def clean_executable(self):
205         return self._gsiftp_suffix(self.cleaned_data['executable'])
206
207     def clean_master_file(self):
208         return self._gsiftp_suffix(self.cleaned_data['master_file'])
209
210     def clean_nodes(self):
211         return map(int, self.cleaned_data['nodes'].split(':', 2)) if self.cleaned_data['nodes'] else ''
212
213     def clean_input(self):
214         return self._gsiftp_suffix(self.cleaned_data['input'])
215
216     def clean_stage_in(self):
217         return ['gsiftp://' + item for item in self.cleaned_data['stage_in']]
218
219     def clean_preprocess_script(self):
220         return self._gsiftp_suffix(self.cleaned_data['preprocess_script'])
221
222     def clean_postprocess_script(self):
223         return self._gsiftp_suffix(self.cleaned_data['postprocess_script'])
224
225     @staticmethod
226     def _gsiftp_suffix(url):
227         return 'gsiftp://' + url if url else ''
228
229
230 class EnvForm(forms.Form):
231     name = forms.CharField(label=u"Nazwa", max_length=100, validators=[env_name_validator],
232                            widget=forms.TextInput(attrs={'placeholder': u'Nazwa'}))
233     value = forms.CharField(label=u"Wartość", max_length=500,
234                             widget=forms.TextInput(attrs={'placeholder': u'Wartość'}))
235
236
237 EnvFormSet = forms.formset_factory(EnvForm, can_delete=True, extra=0)
238
239
240 class ColumnsForm(forms.Form):
241     JOB_ID, DESCRIPTION, SUBMISSION, START, END, STATUS, HOST = range(7)
242     COLUMNS_CHOICES = (
243         (JOB_ID, u"Identyfikator zadania"),
244         (DESCRIPTION, u"Opis"),
245         (SUBMISSION, u"Wysłane"),
246         (START, u"Start"),
247         (END, u"Koniec"),
248         (STATUS, u"Status"),
249         (HOST, u"Host"),
250     )
251
252     columns = forms.MultipleChoiceField(choices=COLUMNS_CHOICES, initial=[k for k, v in COLUMNS_CHOICES[1:]],
253                                         label=u"Kolumny", required=False, widget=forms.CheckboxSelectMultiple)