1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
1 # copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
3 # |
3 # |
4 # This file is part of CubicWeb. |
4 # This file is part of CubicWeb. |
5 # |
5 # |
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
41 .. autoclass:: cubicweb.web.formfields.FloatField() |
41 .. autoclass:: cubicweb.web.formfields.FloatField() |
42 .. autoclass:: cubicweb.web.formfields.BooleanField() |
42 .. autoclass:: cubicweb.web.formfields.BooleanField() |
43 .. autoclass:: cubicweb.web.formfields.DateField() |
43 .. autoclass:: cubicweb.web.formfields.DateField() |
44 .. autoclass:: cubicweb.web.formfields.DateTimeField() |
44 .. autoclass:: cubicweb.web.formfields.DateTimeField() |
45 .. autoclass:: cubicweb.web.formfields.TimeField() |
45 .. autoclass:: cubicweb.web.formfields.TimeField() |
|
46 .. autoclass:: cubicweb.web.formfields.TimeIntervalField() |
46 |
47 |
47 Compound fields |
48 Compound fields |
48 '''''''''''''''' |
49 '''''''''''''''' |
49 |
50 |
50 .. autoclass:: cubicweb.web.formfields.RichTextField() |
51 .. autoclass:: cubicweb.web.formfields.RichTextField() |
61 |
62 |
62 """ |
63 """ |
63 __docformat__ = "restructuredtext en" |
64 __docformat__ = "restructuredtext en" |
64 |
65 |
65 from warnings import warn |
66 from warnings import warn |
66 from datetime import datetime |
67 from datetime import datetime, timedelta |
67 |
68 |
68 from logilab.mtconverter import xml_escape |
69 from logilab.mtconverter import xml_escape |
69 from logilab.common import nullobject |
70 from logilab.common import nullobject |
70 from logilab.common.date import ustrftime |
71 from logilab.common.date import ustrftime |
|
72 from logilab.common.configuration import format_time |
|
73 from logilab.common.textutils import apply_units, TIME_UNITS |
71 |
74 |
72 from yams.schema import KNOWN_METAATTRIBUTES, role_name |
75 from yams.schema import KNOWN_METAATTRIBUTES, role_name |
73 from yams.constraints import (SizeConstraint, StaticVocabularyConstraint, |
76 from yams.constraints import (SizeConstraint, StaticVocabularyConstraint, |
74 FormatConstraint) |
77 FormatConstraint) |
75 |
78 |
925 try: |
928 try: |
926 return float(value) |
929 return float(value) |
927 except ValueError: |
930 except ValueError: |
928 raise ProcessFormError(form._cw._('a float is expected')) |
931 raise ProcessFormError(form._cw._('a float is expected')) |
929 return None |
932 return None |
|
933 |
|
934 |
|
935 class TimeIntervalField(StringField): |
|
936 """Use this field to edit time interval (`Interval` yams type). |
|
937 |
|
938 Unless explicitly specified, the widget for this field will be a |
|
939 :class:`~cubicweb.web.formwidgets.TextInput`. |
|
940 """ |
|
941 widget = fw.TextInput |
|
942 |
|
943 def format_single_value(self, req, value): |
|
944 if value: |
|
945 value = format_time(value.days * 24 * 3600 + value.seconds) |
|
946 return unicode(value) |
|
947 return u'' |
|
948 |
|
949 def example_format(self, req): |
|
950 """return a sample string describing what can be given as input for this |
|
951 field |
|
952 """ |
|
953 return u'20s, 10min, 24h, 4d' |
|
954 |
|
955 def _ensure_correctly_typed(self, form, value): |
|
956 if isinstance(value, basestring): |
|
957 value = value.strip() |
|
958 if not value: |
|
959 return None |
|
960 try: |
|
961 value = apply_units(value, TIME_UNITS) |
|
962 except ValueError: |
|
963 raise ProcessFormError(form._cw._('a number (in seconds) or 20s, 10min, 24h or 4d are expected')) |
|
964 return timedelta(0, value) |
930 |
965 |
931 |
966 |
932 class DateField(StringField): |
967 class DateField(StringField): |
933 """Use this field to edit date (`Date` yams type). |
968 """Use this field to edit date (`Date` yams type). |
934 |
969 |
1199 'Date': DateField, |
1234 'Date': DateField, |
1200 'Datetime': DateTimeField, |
1235 'Datetime': DateTimeField, |
1201 'TZDatetime': DateTimeField, |
1236 'TZDatetime': DateTimeField, |
1202 'Time': TimeField, |
1237 'Time': TimeField, |
1203 'TZTime': TimeField, |
1238 'TZTime': TimeField, |
1204 # XXX implement 'Interval': TimeIntervalField, |
1239 'Interval': TimeIntervalField, |
1205 } |
1240 } |