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
7 from qcg.fields import TimeRangeField
8 from qcg.models import Task, Allocation, JobTemplate
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_]*$')
15 CHOICES_PLACEHOLDER = (None, '')
18 class FiltersForm(forms.Form):
19 ACTIVE, FINISHED, FAILED = range(3)
22 (FINISHED, u"Zakończone"),
23 (FAILED, u"Niepowodzenia"),
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],
38 Task.STATUS_CHOICES_REVERSED[TaskStatus.FINISHED],
41 Task.STATUS_CHOICES_REVERSED[TaskStatus.FAILED],
42 Task.STATUS_CHOICES_REVERSED[TaskStatus.CANCELED],
45 STATUS_CHOICES_DICT = dict(STATUS_CHOICES)
47 keywords = forms.CharField(max_length=100, label=u"Wyszukaj frazę", required=False)
48 status = forms.MultipleChoiceField(choices=STATUS_CHOICES, label=u"Status", required=False,
49 widget=forms.CheckboxSelectMultiple)
52 host = forms.MultipleChoiceField(label=u"Host", required=False, widget=forms.CheckboxSelectMultiple)
53 purged = forms.TypedChoiceField(label=u"Istniejący katalog roboczy?", required=False, coerce=lambda x: bool(int(x)),
54 choices=((0, 'Tak'), (1, 'Nie')), widget=forms.RadioSelect)
55 submission = forms.CharField(label=u"Data zlecenia", validators=[date_range_validator], required=False)
56 finish = forms.CharField(label=u"Data zakończenia", validators=[date_range_validator], required=False)
58 def __init__(self, *args, **kwargs):
59 super(FiltersForm, self).__init__(*args, **kwargs)
61 self.fields['host'].choices = tuple(
62 (host, capfirst(host.split('.', 1)[0]))
63 for host in Allocation.objects.values_list('host_name', flat=True).order_by('host_name').distinct())
66 class JobDescriptionForm(forms.Form):
68 GALERA = 'galera.task.gda.pl'
69 HYDRA = 'hydra.icm.edu.pl'
70 INULA = 'inula.man.poznan.pl'
71 MOSS = 'moss.man.poznan.pl'
72 NOVA = 'nova.wcss.wroc.pl'
73 REEF = 'reef.man.poznan.pl'
74 ZEUS = 'zeus.cyfronet.pl'
87 class Process(object):
98 APPLICATION_CHOICES = (
101 ('gromacs/4.6.3', 'GROMACS 4.6.3'),
102 ('matlab', 'MATLAB'),
103 ('python', 'Python'),
108 ('plgid-long', 'plgrid-long'),
109 ('plgid-testing', 'plgrid-testing'),
112 ('plgrid/apps/python', 'plgrid/apps/python'),
113 ('plgrid/apps/matlab', 'plgrid/apps/matlab'),
117 ('mailto', u'E-mail'),
121 application = forms.ChoiceField(choices=APPLICATION_CHOICES, label=u"Aplikacja", required=False) # TODO choices
122 master_file = forms.CharField(label=u"Plik główny", max_length=500, required=False)
123 executable = forms.CharField(label=u"Plik wykonywalny", max_length=500, required=False)
124 script = forms.CharField(label=u"Skrypt", widget=forms.Textarea(attrs={'rows': 2, 'cols': 40}), required=False)
125 arguments = forms.MultipleChoiceField(label=u"Argumenty", required=False)
126 note = forms.CharField(label=u"Opis", widget=forms.Textarea(attrs={'rows': 2, 'cols': 40}), required=False)
127 grant = forms.CharField(label=u"Grant", max_length=100, required=False)
129 hosts = forms.MultipleChoiceField(label=u"Host", choices=Host.CHOICES, required=False)
130 properties = forms.CharField(label=u"Właściwości węzłów", required=False)
131 queue = forms.ChoiceField(choices=QUEUE_CHOICES, label=u"Kolejka", required=False)
132 procs = forms.IntegerField(label=u"Liczba procesów", min_value=0, required=False)
133 nodes = forms.CharField(label=u"Topologia węzłów", max_length=10, validators=[nodes_validator], required=False)
134 wall_time = TimeRangeField(label=u"Wall time", required=False)
135 memory = forms.IntegerField(label=u"Pamięć (MB)", min_value=0, required=False)
136 memory_per_slot = forms.IntegerField(label=u"Pamięci per proces (MB)", min_value=0, required=False)
137 modules = forms.MultipleChoiceField(label=u"Moduły", choices=MODULES_CHOICES, required=False) # TODO choices
138 reservation = forms.CharField(label=u"Rezerwacja", max_length=100, required=False)
140 input = forms.CharField(label=u"Standardowe wejście", max_length=500, required=False)
141 stage_in = forms.MultipleChoiceField(label=u"Stage in", required=False)
143 monitoring = forms.BooleanField(label=u"Portal QCG-Monitoring", required=False)
144 notify_type = forms.ChoiceField(label=u"Monitorowanie stanu", choices=PROTOCOL_CHOICES, required=False, initial='',
145 widget=forms.RadioSelect)
146 notify_address = forms.EmailField(label=u"Adres", required=False)
147 watch_output_type = forms.ChoiceField(label=u"Monitorowanie wyjścia", choices=PROTOCOL_CHOICES, required=False,
148 initial='', widget=forms.RadioSelect)
149 watch_output_address = forms.EmailField(label=u"Adres", required=False)
150 watch_output_pattern = forms.CharField(label=u"Wzorzec", max_length=500, required=False)
152 preprocess_type = forms.ChoiceField(label=u"Preprocessing", choices=Process.CHOICES, required=False,
153 initial=Process.NONE, widget=forms.RadioSelect)
154 preprocess_cmd = forms.CharField(label=u"Polecenie", max_length=1000, required=False)
155 preprocess_script = forms.CharField(label=u"Skrypt", max_length=500, required=False)
156 postprocess_type = forms.ChoiceField(label=u"Postprocessing", choices=Process.CHOICES, required=False,
157 initial=Process.NONE, widget=forms.RadioSelect)
158 postprocess_cmd = forms.CharField(label=u"Polecenie", max_length=1000, required=False)
159 postprocess_script = forms.CharField(label=u"Skrypt", max_length=500, required=False)
160 native = forms.MultipleChoiceField(label=u"Opcje systemu kolejkowego", required=False)
161 persistent = forms.BooleanField(label=u"Trwałe", required=False)
163 def __init__(self, data=None, initial=None, *args, **kwargs):
164 super(JobDescriptionForm, self).__init__(data, initial=initial, *args, **kwargs)
167 self._init_user_choices('queue', data, initial)
168 self._init_user_choices('arguments', data, initial)
169 self._init_user_choices('native', data, initial)
170 self._init_user_choices('stage_in', data, initial)
173 data = super(JobDescriptionForm, self).clean()
175 if data['application'] and not data['master_file']:
176 self.add_error('master_file', u"W trybie uruchamiania aplikacji należy podać plik główny")
178 if not data['application'] and not data['executable'] and not data['script']:
179 self.add_error(None, u"Jedno z następujących pól jest niezbędne do uruchomienia zadania: "
180 u"Aplikacja, Plik wykonywalny lub Skrypt")
182 if data['procs'] and data['nodes']:
183 self.add_error(None, u"Zdefiniuj tylko jedno z pól: liczbę procesów lub topologię węzłów")
185 notify_type = data.get('notify_type')
186 data['notify'] = u'{}:{}'.format(notify_type, data['notify_address']) if notify_type else ''
188 wo_type = data.get('watch_output_type')
189 data['watch_output'] = u'{}:{}'.format(wo_type, data['watch_output_address']) if wo_type else ''
191 preprocess_type = data.get('preprocess_type')
192 if preprocess_type == self.Process.CMD:
193 data['preprocess'] = data['preprocess_cmd']
194 elif preprocess_type == self.Process.SCRIPT:
195 data['preprocess'] = data['preprocess_script']
197 data['preprocess'] = ''
199 postprocess_type = data.get('postprocess_type')
200 if postprocess_type == self.Process.CMD:
201 data['postprocess'] = data['postprocess_cmd']
202 elif postprocess_type == self.Process.SCRIPT:
203 data['postprocess'] = data['postprocess_script']
205 data['postprocess'] = ''
209 def clean_application(self):
210 return self.cleaned_data['application'].split('/', 1) if self.cleaned_data['application'] else ''
212 def clean_nodes(self):
213 return map(int, self.cleaned_data['nodes'].split(':', 2)) if self.cleaned_data['nodes'] else ''
215 def clean_executable(self):
216 return self._gsiftp_suffix(self.cleaned_data['executable'])
218 def clean_master_file(self):
219 return self._gsiftp_suffix(self.cleaned_data['master_file'])
221 def clean_input(self):
222 return self._gsiftp_suffix(self.cleaned_data['input'])
224 def clean_stage_in(self):
225 return [self._gsiftp_suffix(item) for item in self.cleaned_data['stage_in']]
227 def clean_preprocess_script(self):
228 return self._gsiftp_suffix(self.cleaned_data['preprocess_script'])
230 def clean_postprocess_script(self):
231 return self._gsiftp_suffix(self.cleaned_data['postprocess_script'])
234 def _gsiftp_suffix(url):
236 return url if url.startswith('gsiftp://') else 'gsiftp://' + url
238 def _init_user_choices(self, name, data, initial):
239 initial = initial.get(name) if initial is not None else None
240 choices = data.getlist(name) if data is not None else []
243 choices += initial if isinstance(initial, list) else [initial]
244 self.fields[name].initial = initial
247 self.fields[name].choices += ((v, v) for v in choices)
250 class EnvForm(forms.Form):
251 name = forms.CharField(label=u"Nazwa", max_length=100, validators=[env_name_validator],
252 widget=forms.TextInput(attrs={'placeholder': u'Nazwa'}))
253 value = forms.CharField(label=u"Wartość", max_length=500,
254 widget=forms.TextInput(attrs={'placeholder': u'Wartość'}))
257 EnvFormSet = forms.formset_factory(EnvForm, can_delete=True, extra=0)
260 class ColumnsForm(forms.Form):
261 JOB_ID, DESCRIPTION, SUBMISSION, START, END, STATUS, HOST = range(7)
263 (JOB_ID, u"Identyfikator zadania"),
264 (DESCRIPTION, u"Opis"),
265 (SUBMISSION, u"Wysłane"),
272 columns = forms.MultipleChoiceField(choices=COLUMNS_CHOICES, initial=[k for k, v in COLUMNS_CHOICES[1:]],
273 label=u"Kolumny", required=False, widget=forms.CheckboxSelectMultiple)
276 class JobTemplateForm(forms.ModelForm):