File: interval.py

package info (click to toggle)
wtforms-components 0.11.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 352 kB
  • sloc: python: 1,582; makefile: 135; sh: 11
file content (64 lines) | stat: -rw-r--r-- 1,607 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from intervals import (
    DateInterval,
    DateTimeInterval,
    DecimalInterval,
    FloatInterval,
    IntervalException,
    IntInterval,
)

from .html5 import StringField


class IntervalField(StringField):
    """
    A string field representing an interval object from
    `intervals`_.

    .. _intervals:
       https://github.com/kvesteri/intervals
    """

    def _value(self):
        if self.raw_data:
            return self.raw_data[0]
        if self.data:
            return self.data.hyphenized
        else:
            return ""

    def process_formdata(self, valuelist):
        if valuelist:
            if valuelist[0] == "" or valuelist[0] == "":
                self.data = None
            else:
                try:
                    self.data = self.interval_class.from_string(valuelist[0])
                except IntervalException:
                    self.data = None
                    raise ValueError(self.gettext(self.error_msg))


class DecimalIntervalField(IntervalField):
    error_msg = "Not a valid decimal range value"
    interval_class = DecimalInterval


class FloatIntervalField(IntervalField):
    error_msg = "Not a valid float range value"
    interval_class = FloatInterval


class IntIntervalField(IntervalField):
    error_msg = "Not a valid int range value"
    interval_class = IntInterval


class DateIntervalField(IntervalField):
    error_msg = "Not a valid date range value"
    interval_class = DateInterval


class DateTimeIntervalField(IntervalField):
    error_msg = "Not a valid datetime range value"
    interval_class = DateTimeInterval