X-Git-Url: http://mmka.chem.univ.gda.pl/gitweb/?a=blobdiff_plain;f=qcg%2Fforms.py;h=c67f40ea1739bc38a0be677bb1e665d72135533b;hb=efdba7ecadc632e65fb58aa819964335dffd56a1;hp=3d90145fedcb89cfe4f1cc421611f29953b495ba;hpb=1e4d91a02e314709d4ec1425f814996ff042a2d8;p=qcg-portal.git diff --git a/qcg/forms.py b/qcg/forms.py index 3d90145..c67f40e 100644 --- a/qcg/forms.py +++ b/qcg/forms.py @@ -2,18 +2,17 @@ from django import forms from django.core.validators import RegexValidator from django.template.defaultfilters import capfirst -from django.utils.functional import lazy from pyqcg.utils import TaskStatus -from qcg.models import Task, Allocation - - -def host_choices(): - return tuple((host, capfirst(host.split('.')[0])) for host in - Allocation.objects.values_list('host_name', flat=True).order_by('host_name').distinct()) +from qcg.fields import TimeRangeField +from qcg.models import Task, Allocation, JobTemplate 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-9]{1,2})?$') +env_name_validator = RegexValidator(r'^[a-zA-Z_][a-zA-Z0-9_]*$') + +CHOICES_PLACEHOLDER = (None, '') class FiltersForm(forms.Form): @@ -26,6 +25,8 @@ class FiltersForm(forms.Form): STATUS_MAP = { ACTIVE: ( + Task.STATUS_CHOICES_REVERSED[TaskStatus.UNSUBMITTED], + Task.STATUS_CHOICES_REVERSED[TaskStatus.UNCOMMITTED], Task.STATUS_CHOICES_REVERSED[TaskStatus.QUEUED], Task.STATUS_CHOICES_REVERSED[TaskStatus.PREPROCESSING], Task.STATUS_CHOICES_REVERSED[TaskStatus.PENDING], @@ -35,22 +36,240 @@ class FiltersForm(forms.Form): ), FINISHED: ( Task.STATUS_CHOICES_REVERSED[TaskStatus.FINISHED], - Task.STATUS_CHOICES_REVERSED[TaskStatus.FAILED], - Task.STATUS_CHOICES_REVERSED[TaskStatus.CANCELED], ), FAILED: ( Task.STATUS_CHOICES_REVERSED[TaskStatus.FAILED], Task.STATUS_CHOICES_REVERSED[TaskStatus.CANCELED], ), } + STATUS_CHOICES_DICT = dict(STATUS_CHOICES) + keywords = forms.CharField(max_length=100, label=u"Wyszukaj frazę", required=False) status = forms.MultipleChoiceField(choices=STATUS_CHOICES, label=u"Status", required=False, widget=forms.CheckboxSelectMultiple) - host = forms.MultipleChoiceField(choices=lazy(host_choices, tuple)(), label=u"Host", required=False, - widget=forms.CheckboxSelectMultiple) # advanced - keywords = forms.CharField(max_length=100, label=u"Wyszukaj frazę", required=False) - status_exact = forms.ChoiceField(choices=[(None, u"----------")] + Task.STATUS_CHOICES, label=u"Status", required=False) + host = forms.MultipleChoiceField(label=u"Host", required=False, widget=forms.CheckboxSelectMultiple) + purged = forms.TypedChoiceField(label=u"Istniejący katalog roboczy?", required=False, coerce=lambda x: bool(int(x)), + choices=((0, 'Tak'), (1, 'Nie')), widget=forms.RadioSelect) submission = forms.CharField(label=u"Data zlecenia", validators=[date_range_validator], required=False) finish = forms.CharField(label=u"Data zakończenia", validators=[date_range_validator], required=False) + + def __init__(self, *args, **kwargs): + super(FiltersForm, self).__init__(*args, **kwargs) + + self.fields['host'].choices = tuple( + (host, capfirst(host.split('.', 1)[0])) + for host in Allocation.objects.values_list('host_name', flat=True).order_by('host_name').distinct()) + + +class JobDescriptionForm(forms.Form): + class Host(object): + GALERA = 'galera.task.gda.pl' + HYDRA = 'hydra.icm.edu.pl' + INULA = 'inula.man.poznan.pl' + MOSS = 'moss.man.poznan.pl' + NOVA = 'nova.wcss.wroc.pl' + REEF = 'reef.man.poznan.pl' + ZEUS = 'zeus.cyfronet.pl' + + CHOICES = ( + CHOICES_PLACEHOLDER, + (GALERA, u'Galera'), + (HYDRA, u'Hydra'), + (INULA, u'Inula'), + (MOSS, u'Moss'), + (NOVA, u'Supernova'), + (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'), + ('gromacs/4.6.3', 'GROMACS 4.6.3'), + ('matlab', 'MATLAB'), + ('python', 'Python'), + ) + QUEUE_CHOICES = ( + CHOICES_PLACEHOLDER, + ('plgid', 'plgrid'), + ('plgid-long', 'plgrid-long'), + ('plgid-testing', 'plgrid-testing'), + ) + MODULES_CHOICES = ( + ('plgrid/apps/python', 'plgrid/apps/python'), + ('plgrid/apps/matlab', 'plgrid/apps/matlab'), + ) + PROTOCOL_CHOICES = ( + ('', u'Brak'), + ('mailto', u'E-mail'), + ('xmpp', u'XMPP'), + ) + + application = forms.ChoiceField(choices=APPLICATION_CHOICES, label=u"Aplikacja", required=False) # TODO choices + master_file = forms.CharField(label=u"Plik główny", max_length=500, required=False) + executable = forms.CharField(label=u"Plik wykonywalny", max_length=500, required=False) + script = forms.CharField(label=u"Skrypt", widget=forms.Textarea(attrs={'rows': 2, 'cols': 40}), required=False) + 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) + + 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 = 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) + + input = forms.CharField(label=u"Standardowe wejście", max_length=500, required=False) + stage_in = forms.MultipleChoiceField(label=u"Stage in", required=False) + + monitoring = forms.BooleanField(label=u"Portal QCG-Monitoring", required=False) + 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=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=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) + 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) + native = forms.MultipleChoiceField(label=u"Opcje systemu kolejkowego", required=False) + persistent = forms.BooleanField(label=u"Trwałe", required=False) + + def __init__(self, data=None, initial=None, *args, **kwargs): + super(JobDescriptionForm, self).__init__(data, initial=initial, *args, **kwargs) + + if data or initial: + self._init_user_choices('queue', data, initial) + self._init_user_choices('arguments', data, initial) + self._init_user_choices('native', data, initial) + self._init_user_choices('stage_in', data, initial) + + def clean(self): + data = super(JobDescriptionForm, self).clean() + + if data['application'] and not data['master_file']: + self.add_error('master_file', u"W trybie uruchamiania aplikacji należy podać plik główny") + + if data['procs'] and data['nodes']: + self.add_error(None, u"Zdefiniuj tylko jedno z pól: liczbę procesów lub topologię węzłów") + + 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'] = '' + + return data + + def clean_application(self): + return self.cleaned_data['application'].split('/', 1) if self.cleaned_data['application'] else '' + + def clean_nodes(self): + return map(int, self.cleaned_data['nodes'].split(':', 2)) if self.cleaned_data['nodes'] else '' + + def clean_executable(self): + return self._gsiftp_suffix(self.cleaned_data['executable']) + + def clean_master_file(self): + return self._gsiftp_suffix(self.cleaned_data['master_file']) + + def clean_input(self): + return self._gsiftp_suffix(self.cleaned_data['input']) + + def clean_stage_in(self): + return [self._gsiftp_suffix(item) for item in self.cleaned_data['stage_in']] + + def clean_preprocess_script(self): + return self._gsiftp_suffix(self.cleaned_data['preprocess_script']) + + def clean_postprocess_script(self): + return self._gsiftp_suffix(self.cleaned_data['postprocess_script']) + + @staticmethod + def _gsiftp_suffix(url): + if url: + return url if url.startswith('gsiftp://') else 'gsiftp://' + url + + def _init_user_choices(self, name, data, initial): + initial = initial.get(name) if initial is not None else None + choices = data.getlist(name) if data is not None else [] + + if initial: + choices += initial if isinstance(initial, list) else [initial] + self.fields[name].initial = initial + + if choices: + self.fields[name].choices += ((v, v) for v in choices) + + +class EnvForm(forms.Form): + name = forms.CharField(label=u"Nazwa", max_length=100, validators=[env_name_validator], + widget=forms.TextInput(attrs={'placeholder': u'Nazwa'})) + value = forms.CharField(label=u"Wartość", max_length=500, + widget=forms.TextInput(attrs={'placeholder': u'Wartość'})) + + +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) + + +class JobTemplateForm(forms.ModelForm): + class Meta: + model = JobTemplate + fields = ('name',)