From d33fbaaca284582fae920d9cc22a4755afeb169d Mon Sep 17 00:00:00 2001 From: Maciej Tronowski Date: Wed, 4 Mar 2015 11:35:33 +0100 Subject: [PATCH] tweaks to TimeRangeField --- qcg/fields.py | 35 ++++++++++++++++++----------------- qcg/forms.py | 2 +- qcg/static/qcg/main.css | 13 +++++++++++++ qcg/templates/qcg/job_new.html | 2 +- 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/qcg/fields.py b/qcg/fields.py index e067f46..73b46f4 100644 --- a/qcg/fields.py +++ b/qcg/fields.py @@ -7,9 +7,9 @@ from django.core.exceptions import ValidationError class TimeRangeWidget(forms.MultiWidget): def decompress(self, value): if not value: - return [0, TimeRangeField.SECONDS] + return [None, None] - seconds = value.seconds + 86400 * value.days + seconds = value.total_seconds() if seconds % 60 != 0: return [seconds, TimeRangeField.SECONDS] @@ -31,7 +31,7 @@ class TimeRangeField(forms.MultiValueField): ) value = forms.IntegerField(min_value=0) - unit = forms.ChoiceField(choices=UNIT_CHOICES) + unit = forms.TypedChoiceField(choices=UNIT_CHOICES, coerce=int) widget = TimeRangeWidget(widgets=(value.widget, unit.widget)) @@ -39,17 +39,18 @@ class TimeRangeField(forms.MultiValueField): super(TimeRangeField, self).__init__((self.value, self.unit), *args, **kwargs) def compress(self, data_list): - try: - unit = int(data_list[1]) - if unit == self.SECONDS: - return timedelta(seconds=data_list[0]) - elif unit == self.MINUTES: - return timedelta(minutes=data_list[0]) - elif unit == self.HOURS: - return timedelta(hours=data_list[0]) - elif unit == self.DAYS: - return timedelta(days=data_list[0]) - else: - raise ValidationError(u"Nieprawidłowa jednostka") - except ValueError: - raise ValidationError(u"Nieprawidłowa jednostka") + value, unit = data_list + + if value is None: + return None + + if unit == self.SECONDS: + return timedelta(seconds=value) + elif unit == self.MINUTES: + return timedelta(minutes=value) + elif unit == self.HOURS: + return timedelta(hours=value) + elif unit == self.DAYS: + return timedelta(days=value) + + raise ValidationError(u"Nieprawidłowa jednostka") diff --git a/qcg/forms.py b/qcg/forms.py index b2ad790..7d53c7c 100644 --- a/qcg/forms.py +++ b/qcg/forms.py @@ -121,7 +121,7 @@ class JobDescriptionForm(forms.Form): 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 diff --git a/qcg/static/qcg/main.css b/qcg/static/qcg/main.css index b3bdc3b..f3ee656 100644 --- a/qcg/static/qcg/main.css +++ b/qcg/static/qcg/main.css @@ -53,3 +53,16 @@ textarea { #add-env-form { padding-top: 7px; } + + +.timerange input { + display: inline-block; + width: 67%; + margin-right: 5px; +} + +.timerange select { + display: inline-block; + width: 30%; + float: right; +} diff --git a/qcg/templates/qcg/job_new.html b/qcg/templates/qcg/job_new.html index d5b7344..f2f55c2 100644 --- a/qcg/templates/qcg/job_new.html +++ b/qcg/templates/qcg/job_new.html @@ -124,7 +124,7 @@ {% bootstrap_field form.modules layout="horizontal" form_group_class="form-group collapse" %} {% bootstrap_field form.procs layout="horizontal" %} {% bootstrap_field form.nodes layout="horizontal" form_group_class="form-group collapse" %} - {% include 'qcg/time_range_field.html' with field_name='wall_time' field=form.wall_time %} + {% bootstrap_field form.wall_time layout="horizontal" form_group_class="form-group timerange" %} {% bootstrap_field form.memory layout="horizontal" form_group_class="form-group collapse" %} {% bootstrap_field form.memory_per_slot layout="horizontal" form_group_class="form-group collapse" %} {% bootstrap_field form.reservation layout="horizontal" form_group_class="form-group collapse" %} -- 1.7.9.5