|
1 | 1 | import os |
2 | 2 | import codecs |
| 3 | +import datetime |
3 | 4 |
|
4 | 5 | from django import forms |
5 | 6 | from django.db.models import Q |
@@ -33,6 +34,32 @@ class GroupModelChoiceField(forms.ModelChoiceField): |
33 | 34 | def label_from_instance(self, obj): |
34 | 35 | return obj.acronym |
35 | 36 |
|
| 37 | +class CustomDurationField(DurationField): |
| 38 | + '''Custom DurationField to display as HH:MM (no seconds)''' |
| 39 | + def prepare_value(self, value): |
| 40 | + if isinstance(value, datetime.timedelta): |
| 41 | + return duration_string(value) |
| 42 | + return value |
| 43 | + |
| 44 | +def duration_string(duration): |
| 45 | + '''Custom duration_string to return HH:MM (no seconds)''' |
| 46 | + days = duration.days |
| 47 | + seconds = duration.seconds |
| 48 | + microseconds = duration.microseconds |
| 49 | + |
| 50 | + minutes = seconds // 60 |
| 51 | + seconds = seconds % 60 |
| 52 | + |
| 53 | + hours = minutes // 60 |
| 54 | + minutes = minutes % 60 |
| 55 | + |
| 56 | + string = '{:02d}:{:02d}'.format(hours, minutes) |
| 57 | + if days: |
| 58 | + string = '{} '.format(days) + string |
| 59 | + if microseconds: |
| 60 | + string += '.{:06d}'.format(microseconds) |
| 61 | + |
| 62 | + return string |
36 | 63 | # ------------------------------------------------- |
37 | 64 | # Forms |
38 | 65 | # ------------------------------------------------- |
@@ -160,7 +187,7 @@ def save(self, *args, **kwargs): |
160 | 187 | class InterimSessionModelForm(forms.ModelForm): |
161 | 188 | date = DatepickerDateField(date_format="yyyy-mm-dd", picker_settings={"autoclose": "1"}, label='Date', required=False) |
162 | 189 | time = forms.TimeField(widget=forms.TimeInput(format='%H:%M'), required=True) |
163 | | - requested_duration = DurationField(required=True) |
| 190 | + requested_duration = CustomDurationField(required=True) |
164 | 191 | end_time = forms.TimeField(required=False) |
165 | 192 | remote_instructions = forms.CharField(max_length=1024, required=True) |
166 | 193 | agenda = forms.CharField(required=False, widget=forms.Textarea, strip=False) |
|
0 commit comments