ignore . and .. in directory listing
[qcg-portal.git] / qcg / forms.py
index 1771327..c1fda14 100644 (file)
@@ -4,12 +4,12 @@ from django.core.validators import RegexValidator
 from django.template.defaultfilters import capfirst
 from pyqcg.utils import TaskStatus
 
-from qcg.fields import PredefinedChoiceField, MultiplePredefinedChoiceField
+from qcg.fields import TimeRangeField
 from qcg.models import Task, Allocation
 
 
 date_range_validator = RegexValidator(r'[0-9]{2}\.[0-9]{2}\.[0-9]{4} - [0-9]{2}\.[0-9]{2}\.[0-9]{4}')
-nodes_validator = RegexValidator(r'^[0-9]{1,3}(:[0-9]{1,2}){0,2}$')
+nodes_validator = RegexValidator(r'^[0-9]{1,3}:[0-9]{1,2}(:[0-9]{1,2})?$')
 env_name_validator = RegexValidator(r'^[a-zA-Z_][a-zA-Z0-9_]*$')
 
 CHOICES_PLACEHOLDER = (None, '')
@@ -81,11 +81,24 @@ class JobDescriptionForm(forms.Form):
             (REEF, u'Reef'),
             (ZEUS, u'Zeus'),
         )
+
+    class Process(object):
+        NONE = ''
+        CMD = 'c'
+        SCRIPT = 's'
+
+        CHOICES = (
+            (NONE, u'Brak'),
+            (CMD, u'Polecenie'),
+            (SCRIPT, u'Skrypt'),
+        )
+
     APPLICATION_CHOICES = (
         CHOICES_PLACEHOLDER,
         ('bash', 'BASH'),
-        ('python', 'Python'),
+        ('gromacs/4.6.3', 'GROMACS 4.6.3'),
         ('matlab', 'MATLAB'),
+        ('python', 'Python'),
     )
     QUEUE_CHOICES = (
         CHOICES_PLACEHOLDER,
@@ -97,69 +110,106 @@ class JobDescriptionForm(forms.Form):
         ('plgrid/apps/python', 'plgrid/apps/python'),
         ('plgrid/apps/matlab', 'plgrid/apps/matlab'),
     )
-    NOTIFY_CHOICES = (
-        (0, u'Brak'),
-        (1, u'E-mail'),
-        (2, u'XMPP'),
-    )
-    PROCESS_CHOICES = (
-        (0, u'Brak'),
-        (1, u'Polecenie'),
-        (2, u'Skrypt'),
+    PROTOCOL_CHOICES = (
+        ('', u'Brak'),
+        ('mailto', u'E-mail'),
+        ('xmpp', u'XMPP'),
     )
 
     application = forms.ChoiceField(choices=APPLICATION_CHOICES, label=u"Aplikacja", required=False)  # TODO choices
-    executable = forms.CharField(label=u"Polecenie", max_length=500, required=False)  # TODO grid ftp
-    arguments = forms.CharField(label=u"Argumenty", max_length=1000, required=False)
-
-    name = forms.CharField(label=u"Nazwa", max_length=100, required=False)
-    note = forms.CharField(label=u"Notatka", widget=forms.Textarea(attrs={'rows': 2, 'cols': 40}), required=False)
+    master_file = forms.CharField(label=u"Plik główny", max_length=500, required=False)  # TODO grid ftp
+    executable = forms.CharField(label=u"Plik wykonywalny", max_length=500, required=False)  # TODO grid ftp
+    script = forms.CharField(label=u"Skrypt", widget=forms.Textarea(attrs={'rows': 2, 'cols': 40}), required=False)  # TODO saving to grid ftp
+    arguments = forms.MultipleChoiceField(label=u"Argumenty", required=False)
+    note = forms.CharField(label=u"Opis", widget=forms.Textarea(attrs={'rows': 2, 'cols': 40}), required=False)
     grant = forms.CharField(label=u"Grant", max_length=100, required=False)
 
-    host = forms.ChoiceField(label=u"Host", choices=Host.CHOICES, required=False)
-    queue = PredefinedChoiceField(choices=QUEUE_CHOICES, label=u"Kolejka", required=False)
+    hosts = forms.MultipleChoiceField(label=u"Host", choices=Host.CHOICES, required=False)
+    properties = forms.CharField(label=u"Właściwości węzłów", required=False)
+    queue = forms.ChoiceField(choices=QUEUE_CHOICES, label=u"Kolejka", required=False)
     procs = forms.IntegerField(label=u"Liczba procesów", min_value=0, required=False)
     nodes = forms.CharField(label=u"Topologia węzłów", max_length=10, validators=[nodes_validator], required=False)
-    wall_time = forms.IntegerField(label=u"Wall time (s)", min_value=0, required=False)  # TODO duration field
+    wall_time = TimeRangeField(label=u"Wall time", required=False)
     memory = forms.IntegerField(label=u"Pamięć (MB)", min_value=0, required=False)
     memory_per_slot = forms.IntegerField(label=u"Pamięci per proces (MB)", min_value=0, required=False)
+    modules = forms.MultipleChoiceField(label=u"Moduły", choices=MODULES_CHOICES, required=False)  # TODO choices
+    reservation = forms.CharField(label=u"Rezerwacja", max_length=100, required=False)
 
     # TODO grid ftp
     input = forms.CharField(label=u"Standardowe wejście", max_length=500, required=False)
-    output = forms.CharField(label=u"Standardowe wyjście", max_length=500, required=False)
-    error = forms.CharField(label=u"Standardowe wyjście błędów", max_length=500, required=False)
-    stage_in = forms.CharField(label=u"Stage in", max_length=500, required=False)
-    stage_out = forms.CharField(label=u"Stage out", max_length=500, required=False)
-
-    properties = MultiplePredefinedChoiceField(label=u"Właściwości węzłów", required=False)
-    modules = forms.MultipleChoiceField(label=u"Moduły", choices=MODULES_CHOICES, required=False)  # TODO choices
-
-    not_after = forms.DateTimeField(label=u"Nie później niż", required=False)
-    not_before = forms.DateTimeField(label=u"Nie wcześniej niż", required=False)
-    deadline = forms.IntegerField(label=u"Deadline (s)", min_value=0, required=False)  # TODO duration field
-    reservation = forms.CharField(label=u"Rezerwacja", max_length=100, required=False)
+    stage_in = forms.MultipleChoiceField(label=u"Stage in", required=False)
+    # stage_out = forms.MultipleChoiceField(label=u"Stage out", required=False)
 
     monitoring = forms.BooleanField(label=u"Portal QCG-Monitoring", required=False)
-    notify_type = forms.ChoiceField(label=u"Monitorowanie stanu", choices=NOTIFY_CHOICES, required=False, initial=0,
+    notify_type = forms.ChoiceField(label=u"Monitorowanie stanu", choices=PROTOCOL_CHOICES, required=False, initial='',
                                     widget=forms.RadioSelect)
     notify_address = forms.EmailField(label=u"Adres", required=False)
-    watch_output_type = forms.ChoiceField(label=u"Monitorowanie wyjścia", choices=NOTIFY_CHOICES, required=False,
-                                          initial=0, widget=forms.RadioSelect)
+    watch_output_type = forms.ChoiceField(label=u"Monitorowanie wyjścia", choices=PROTOCOL_CHOICES, required=False,
+                                          initial='', widget=forms.RadioSelect)
     watch_output_address = forms.EmailField(label=u"Adres", required=False)
     watch_output_pattern = forms.CharField(label=u"Wzorzec", max_length=500, required=False)
 
-    preprocess_type = forms.ChoiceField(label=u"Preprocessing", choices=PROCESS_CHOICES, required=False, initial=0,
-                                        widget=forms.RadioSelect)
+    preprocess_type = forms.ChoiceField(label=u"Preprocessing", choices=Process.CHOICES, required=False,
+                                        initial=Process.NONE, widget=forms.RadioSelect)
     preprocess_cmd = forms.CharField(label=u"Polecenie", max_length=1000, required=False)
     preprocess_script = forms.CharField(label=u"Skrypt", max_length=500, required=False)  # TODO grid ftp
-    postprocess_type = forms.ChoiceField(label=u"Postprocessing", choices=PROCESS_CHOICES, required=False, initial=0,
-                                         widget=forms.RadioSelect)
+    postprocess_type = forms.ChoiceField(label=u"Postprocessing", choices=Process.CHOICES, required=False,
+                                         initial=Process.NONE, widget=forms.RadioSelect)
     postprocess_cmd = forms.CharField(label=u"Polecenie", max_length=1000, required=False)
     postprocess_script = forms.CharField(label=u"Skrypt", max_length=500, required=False)  # TODO grid ftp
-    native = MultiplePredefinedChoiceField(label=u"Parametry natywne", required=False)
+    native = forms.MultipleChoiceField(label=u"Opcje systemu kolejkowego", required=False)
     persistent = forms.BooleanField(label=u"Trwałe", required=False)
-    use_scratch = forms.BooleanField(label=u"Scratch", required=False)
 
+    def __init__(self, data=None, *args, **kwargs):
+        super(JobDescriptionForm, self).__init__(data, *args, **kwargs)
+
+        if data is not None:
+            # accept user defined choices
+            self.fields['queue'].choices += ((data.get('queue'), data.get('queue')), )
+            self.fields['arguments'].choices += ((v, v) for v in data.getlist('arguments'))
+            self.fields['native'].choices += ((v, v) for v in data.getlist('native'))
+            self.fields['stage_in'].choices += ((v, v) for v in data.getlist('stage_in'))
+            # self.fields['stage_out'].choices += ((v, v) for v in data.getlist('stage_out'))
+
+    def clean(self):
+        data = super(JobDescriptionForm, self).clean()
+
+        notify_type = data.get('notify_type')
+        data['notify'] = u'{}:{}'.format(notify_type, data['notify_address']) if notify_type else ''
+
+        wo_type = data.get('watch_output_type')
+        data['watch_output'] = u'{}:{}'.format(wo_type, data['watch_output_address']) if wo_type else ''
+
+        preprocess_type = data.get('preprocess_type')
+        if preprocess_type == self.Process.CMD:
+            data['preprocess'] = data['preprocess_cmd']
+        elif preprocess_type == self.Process.SCRIPT:
+            data['preprocess'] = data['preprocess_script']
+        else:
+            data['preprocess'] = ''
+
+        postprocess_type = data.get('postprocess_type')
+        if postprocess_type == self.Process.CMD:
+            data['postprocess'] = data['postprocess_cmd']
+        elif postprocess_type == self.Process.SCRIPT:
+            data['postprocess'] = data['postprocess_script']
+        else:
+            data['postprocess'] = ''
+
+    def clean_application(self):
+        return self.cleaned_data['application'].split('/', 1) if self.cleaned_data['application'] else ''
+
+    def clean_executable(self):
+        return 'gsiftp://' + self.cleaned_data['executable'] if self.cleaned_data['executable'] else ''
+
+    def clean_nodes(self):
+        return map(int, self.cleaned_data['nodes'].split(':', 2)) if self.cleaned_data['nodes'] else ''
+
+    def clean_input(self):
+        return 'gsiftp://' + self.cleaned_data['input'] if self.cleaned_data['input'] else ''
+
+    def clean_stage_in(self):
+        return ['gsiftp://' + item for item in self.cleaned_data['stage_in']]
 
 class EnvForm(forms.Form):
     name = forms.CharField(label=u"Nazwa", max_length=100, validators=[env_name_validator],
@@ -169,3 +219,19 @@ class EnvForm(forms.Form):
 
 
 EnvFormSet = forms.formset_factory(EnvForm, can_delete=True, extra=0)
+
+
+class ColumnsForm(forms.Form):
+    JOB_ID, DESCRIPTION, SUBMISSION, START, END, STATUS, HOST = range(7)
+    COLUMNS_CHOICES = (
+        (JOB_ID, u"Identyfikator zadania"),
+        (DESCRIPTION, u"Opis"),
+        (SUBMISSION, u"Wysłane"),
+        (START, u"Start"),
+        (END, u"Koniec"),
+        (STATUS, u"Status"),
+        (HOST, u"Host"),
+    )
+
+    columns = forms.MultipleChoiceField(choices=COLUMNS_CHOICES, initial=[k for k, v in COLUMNS_CHOICES[1:]],
+                                        label=u"Kolumny", required=False, widget=forms.CheckboxSelectMultiple)