~barry/mailman/events-and-web

« back to all changes in this revision

Viewing changes to src/mailman/database/types.py

  • Committer: Barry Warsaw
  • Date: 2011-09-23 23:47:19 UTC
  • Revision ID: barry@list.org-20110923234719-fp31k6ch59oh3cvf
Improved the way flufl.enum.Enums are stored in the database.  Now, they
should always be stored as INTEGER columns, with the enum class explicitly
described in the code.  This should be more efficient, and besides EIBTI.

Also, filled in a few additional IMailingList attributes which were not
documented in the interface.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
from storm.properties import SimpleProperty
30
30
from storm.variables import Variable
31
31
 
32
 
from mailman.utilities.modules import find_name
33
 
 
34
32
 
35
33
 
36
34
class _EnumVariable(Variable):
37
 
    """Storm variable.
 
35
    """Storm variable for supporting flufl.enum.Enum types.
38
36
 
39
 
    To use this, make the database column a TEXT.
 
37
    To use this, make the database column a INTEGER.
40
38
    """
41
39
 
 
40
    def __init__(self, *args, **kws):
 
41
        self._enum = kws.pop('enum')
 
42
        super(_EnumVariable, self).__init__(*args, **kws)
 
43
 
42
44
    def parse_set(self, value, from_db):
43
45
        if value is None:
44
46
            return None
45
47
        if not from_db:
46
48
            return value
47
 
        path, colon, intvalue = value.rpartition(':')
48
 
        class_ = find_name(path)
49
 
        return class_[int(intvalue)]
 
49
        return self._enum[value]
50
50
 
51
51
    def parse_get(self, value, to_db):
52
52
        if value is None:
53
53
            return None
54
54
        if not to_db:
55
55
            return value
56
 
        return '{0}.{1}:{2}'.format(
57
 
            value.enumclass.__module__,
58
 
            value.enumclass.__name__,
59
 
            int(value))
 
56
        return int(value)
60
57
 
61
58
 
62
59
class Enum(SimpleProperty):
63
 
    """Custom Enum type for Storm."""
 
60
    """Custom Enum type for Storm supporting flufl.enum.Enums."""
64
61
 
65
62
    variable_class = _EnumVariable
 
63
 
 
64
    def __init__(self, enum=None):
 
65
        super(Enum, self).__init__(enum=enum)