| author | rougeronj |
| Mon, 14 Jan 2013 14:58:03 +0100 | |
| changeset 1062 | 14c98d369c5a |
| parent 1061 | 92e3a410e192 |
| child 1064 | d8b2af693c69 |
| permissions | -rw-r--r-- |
|
818
1762d0711008
Add a custom duration field for the create content form
rougeronj
parents:
diff
changeset
|
1 |
""" |
|
1762d0711008
Add a custom duration field for the create content form
rougeronj
parents:
diff
changeset
|
2 |
Field classes. |
|
1762d0711008
Add a custom duration field for the create content form
rougeronj
parents:
diff
changeset
|
3 |
""" |
|
1762d0711008
Add a custom duration field for the create content form
rougeronj
parents:
diff
changeset
|
4 |
|
|
1762d0711008
Add a custom duration field for the create content form
rougeronj
parents:
diff
changeset
|
5 |
from django import forms |
|
1762d0711008
Add a custom duration field for the create content form
rougeronj
parents:
diff
changeset
|
6 |
from django.utils.translation import ugettext_lazy as _ |
|
1061
92e3a410e192
update LdtDurationFied to send correct validation messages
rougeronj
parents:
818
diff
changeset
|
7 |
from django.core.exceptions import ValidationError |
|
818
1762d0711008
Add a custom duration field for the create content form
rougeronj
parents:
diff
changeset
|
8 |
import logging |
|
1762d0711008
Add a custom duration field for the create content form
rougeronj
parents:
diff
changeset
|
9 |
import time |
|
1762d0711008
Add a custom duration field for the create content form
rougeronj
parents:
diff
changeset
|
10 |
import math |
|
1762d0711008
Add a custom duration field for the create content form
rougeronj
parents:
diff
changeset
|
11 |
|
|
1762d0711008
Add a custom duration field for the create content form
rougeronj
parents:
diff
changeset
|
12 |
__all__ = ( |
|
1762d0711008
Add a custom duration field for the create content form
rougeronj
parents:
diff
changeset
|
13 |
'LdtDurationField' |
|
1762d0711008
Add a custom duration field for the create content form
rougeronj
parents:
diff
changeset
|
14 |
) |
|
1762d0711008
Add a custom duration field for the create content form
rougeronj
parents:
diff
changeset
|
15 |
|
|
1762d0711008
Add a custom duration field for the create content form
rougeronj
parents:
diff
changeset
|
16 |
class LdtDurationField (forms.TimeField): |
|
1061
92e3a410e192
update LdtDurationFied to send correct validation messages
rougeronj
parents:
818
diff
changeset
|
17 |
""" |
|
92e3a410e192
update LdtDurationFied to send correct validation messages
rougeronj
parents:
818
diff
changeset
|
18 |
A field allowing to enter the duration format (eg: XhMM, XXhMM, HH:MM:SS) |
|
92e3a410e192
update LdtDurationFied to send correct validation messages
rougeronj
parents:
818
diff
changeset
|
19 |
""" |
|
92e3a410e192
update LdtDurationFied to send correct validation messages
rougeronj
parents:
818
diff
changeset
|
20 |
|
|
818
1762d0711008
Add a custom duration field for the create content form
rougeronj
parents:
diff
changeset
|
21 |
default_error_messages = { |
|
1061
92e3a410e192
update LdtDurationFied to send correct validation messages
rougeronj
parents:
818
diff
changeset
|
22 |
'required': _(u'The duration field can not be empty.'), |
|
92e3a410e192
update LdtDurationFied to send correct validation messages
rougeronj
parents:
818
diff
changeset
|
23 |
'invalid': _(u'Enter a valid duration format;'), |
|
818
1762d0711008
Add a custom duration field for the create content form
rougeronj
parents:
diff
changeset
|
24 |
} |
|
1762d0711008
Add a custom duration field for the create content form
rougeronj
parents:
diff
changeset
|
25 |
|
|
1762d0711008
Add a custom duration field for the create content form
rougeronj
parents:
diff
changeset
|
26 |
def __init__(self, formats, *args, **kwargs): |
|
1762d0711008
Add a custom duration field for the create content form
rougeronj
parents:
diff
changeset
|
27 |
self.formats = formats |
|
1762d0711008
Add a custom duration field for the create content form
rougeronj
parents:
diff
changeset
|
28 |
super(LdtDurationField, self).__init__(*args, **kwargs) |
|
1762d0711008
Add a custom duration field for the create content form
rougeronj
parents:
diff
changeset
|
29 |
|
|
1061
92e3a410e192
update LdtDurationFied to send correct validation messages
rougeronj
parents:
818
diff
changeset
|
30 |
def to_python(self, value): |
|
92e3a410e192
update LdtDurationFied to send correct validation messages
rougeronj
parents:
818
diff
changeset
|
31 |
logging.debug(value) |
|
92e3a410e192
update LdtDurationFied to send correct validation messages
rougeronj
parents:
818
diff
changeset
|
32 |
dur = value |
|
818
1762d0711008
Add a custom duration field for the create content form
rougeronj
parents:
diff
changeset
|
33 |
for format in self.formats: |
|
1762d0711008
Add a custom duration field for the create content form
rougeronj
parents:
diff
changeset
|
34 |
try: |
|
1762d0711008
Add a custom duration field for the create content form
rougeronj
parents:
diff
changeset
|
35 |
dur = time.strptime(dur, format) |
|
1762d0711008
Add a custom duration field for the create content form
rougeronj
parents:
diff
changeset
|
36 |
dur = dur.tm_hour*3600 + dur.tm_min*60 + dur.tm_sec |
|
1762d0711008
Add a custom duration field for the create content form
rougeronj
parents:
diff
changeset
|
37 |
dur = dur*1000 |
|
1762d0711008
Add a custom duration field for the create content form
rougeronj
parents:
diff
changeset
|
38 |
break |
|
1762d0711008
Add a custom duration field for the create content form
rougeronj
parents:
diff
changeset
|
39 |
except: |
|
1062
14c98d369c5a
update LdtDurationField to correctly test if the value is an integer, and send custom message error if it's not
rougeronj
parents:
1061
diff
changeset
|
40 |
logging.debug("trying next format") |
|
14c98d369c5a
update LdtDurationField to correctly test if the value is an integer, and send custom message error if it's not
rougeronj
parents:
1061
diff
changeset
|
41 |
logging.debug(str(dur)) |
|
1061
92e3a410e192
update LdtDurationFied to send correct validation messages
rougeronj
parents:
818
diff
changeset
|
42 |
return dur |
|
92e3a410e192
update LdtDurationFied to send correct validation messages
rougeronj
parents:
818
diff
changeset
|
43 |
|
|
92e3a410e192
update LdtDurationFied to send correct validation messages
rougeronj
parents:
818
diff
changeset
|
44 |
def validate(self, value): |
|
92e3a410e192
update LdtDurationFied to send correct validation messages
rougeronj
parents:
818
diff
changeset
|
45 |
if value==None or value=="": |
|
92e3a410e192
update LdtDurationFied to send correct validation messages
rougeronj
parents:
818
diff
changeset
|
46 |
raise ValidationError(self.default_error_messages['required']) |
|
92e3a410e192
update LdtDurationFied to send correct validation messages
rougeronj
parents:
818
diff
changeset
|
47 |
else: |
|
1062
14c98d369c5a
update LdtDurationField to correctly test if the value is an integer, and send custom message error if it's not
rougeronj
parents:
1061
diff
changeset
|
48 |
try: |
|
14c98d369c5a
update LdtDurationField to correctly test if the value is an integer, and send custom message error if it's not
rougeronj
parents:
1061
diff
changeset
|
49 |
int(value) |
|
14c98d369c5a
update LdtDurationField to correctly test if the value is an integer, and send custom message error if it's not
rougeronj
parents:
1061
diff
changeset
|
50 |
except (ValueError, TypeError): |
|
14c98d369c5a
update LdtDurationField to correctly test if the value is an integer, and send custom message error if it's not
rougeronj
parents:
1061
diff
changeset
|
51 |
raise ValidationError(self.default_error_messages['invalid']) |
|
1061
92e3a410e192
update LdtDurationFied to send correct validation messages
rougeronj
parents:
818
diff
changeset
|
52 |