Mercurial > p > roundup > code
comparison roundup/hyperdb.py @ 4481:9bbf3758c16a
Allow default value declaration in DB schema.
| author | Stefan Seefeld <stefan@seefeld.name> |
|---|---|
| date | Thu, 14 Apr 2011 12:47:48 +0000 |
| parents | 1613754d2646 |
| children | 22bc0426e348 |
comparison
equal
deleted
inserted
replaced
| 4480:1613754d2646 | 4481:9bbf3758c16a |
|---|---|
| 33 # | 33 # |
| 34 # Types | 34 # Types |
| 35 # | 35 # |
| 36 class _Type(object): | 36 class _Type(object): |
| 37 """A roundup property type.""" | 37 """A roundup property type.""" |
| 38 def __init__(self, required=False): | 38 def __init__(self, required=False, default_value = None): |
| 39 self.required = required | 39 self.required = required |
| 40 self.__default_value = default_value | |
| 40 def __repr__(self): | 41 def __repr__(self): |
| 41 ' more useful for dumps ' | 42 ' more useful for dumps ' |
| 42 return '<%s.%s>'%(self.__class__.__module__, self.__class__.__name__) | 43 return '<%s.%s>'%(self.__class__.__module__, self.__class__.__name__) |
| 44 def get_default_value(self): | |
| 45 """The default value when creating a new instance of this property.""" | |
| 46 return self.__default_value | |
| 43 def sort_repr (self, cls, val, name): | 47 def sort_repr (self, cls, val, name): |
| 44 """Representation used for sorting. This should be a python | 48 """Representation used for sorting. This should be a python |
| 45 built-in type, otherwise sorting will take ages. Note that | 49 built-in type, otherwise sorting will take ages. Note that |
| 46 individual backends may chose to use something different for | 50 individual backends may chose to use something different for |
| 47 sorting as long as the outcome is the same. | 51 sorting as long as the outcome is the same. |
| 48 """ | 52 """ |
| 49 return val | 53 return val |
| 50 | 54 |
| 51 class String(_Type): | 55 class String(_Type): |
| 52 """An object designating a String property.""" | 56 """An object designating a String property.""" |
| 53 def __init__(self, indexme='no', required=False): | 57 def __init__(self, indexme='no', required=False, default_value = ""): |
| 54 super(String, self).__init__(required) | 58 super(String, self).__init__(required, default_value) |
| 55 self.indexme = indexme == 'yes' | 59 self.indexme = indexme == 'yes' |
| 56 def from_raw(self, value, propname='', **kw): | 60 def from_raw(self, value, propname='', **kw): |
| 57 """fix the CRLF/CR -> LF stuff""" | 61 """fix the CRLF/CR -> LF stuff""" |
| 58 if propname == 'content': | 62 if propname == 'content': |
| 59 # Why oh why wasn't the FileClass content property a File | 63 # Why oh why wasn't the FileClass content property a File |
| 83 return val | 87 return val |
| 84 return str(val) | 88 return str(val) |
| 85 | 89 |
| 86 class Date(_Type): | 90 class Date(_Type): |
| 87 """An object designating a Date property.""" | 91 """An object designating a Date property.""" |
| 88 def __init__(self, offset=None, required=False): | 92 def __init__(self, offset=None, required=False, default_value = None): |
| 89 super(Date, self).__init__(required) | 93 super(Date, self).__init__(required = required, |
| 94 default_value = default_value) | |
| 90 self._offset = offset | 95 self._offset = offset |
| 91 def offset(self, db): | 96 def offset(self, db): |
| 92 if self._offset is not None: | 97 if self._offset is not None: |
| 93 return self._offset | 98 return self._offset |
| 94 return db.getUserTimezone() | 99 return db.getUserTimezone() |
| 122 return val.as_seconds() | 127 return val.as_seconds() |
| 123 | 128 |
| 124 class _Pointer(_Type): | 129 class _Pointer(_Type): |
| 125 """An object designating a Pointer property that links or multilinks | 130 """An object designating a Pointer property that links or multilinks |
| 126 to a node in a specified class.""" | 131 to a node in a specified class.""" |
| 127 def __init__(self, classname, do_journal='yes', required=False): | 132 def __init__(self, classname, do_journal='yes', required=False, |
| 133 default_value = None): | |
| 128 """ Default is to journal link and unlink events | 134 """ Default is to journal link and unlink events |
| 129 """ | 135 """ |
| 130 super(_Pointer, self).__init__(required) | 136 super(_Pointer, self).__init__(required, default_value) |
| 131 self.classname = classname | 137 self.classname = classname |
| 132 self.do_journal = do_journal == 'yes' | 138 self.do_journal = do_journal == 'yes' |
| 133 def __repr__(self): | 139 def __repr__(self): |
| 134 """more useful for dumps. But beware: This is also used in schema | 140 """more useful for dumps. But beware: This is also used in schema |
| 135 storage in SQL backends! | 141 storage in SQL backends! |
| 161 "classname" indicates the class to link to | 167 "classname" indicates the class to link to |
| 162 | 168 |
| 163 "do_journal" indicates whether the linked-to nodes should have | 169 "do_journal" indicates whether the linked-to nodes should have |
| 164 'link' and 'unlink' events placed in their journal | 170 'link' and 'unlink' events placed in their journal |
| 165 """ | 171 """ |
| 172 | |
| 173 def __init__(self, classname, do_journal = 'yes', required = False): | |
| 174 | |
| 175 super(Multilink, self).__init__(classname, | |
| 176 do_journal, | |
| 177 required = required, | |
| 178 default_value = []) | |
| 179 | |
| 166 def from_raw(self, value, db, klass, propname, itemid, **kw): | 180 def from_raw(self, value, db, klass, propname, itemid, **kw): |
| 167 if not value: | 181 if not value: |
| 168 return [] | 182 return [] |
| 169 | 183 |
| 170 # get the current item value if it's not a new item | 184 # get the current item value if it's not a new item |
