From: stefan Date: Thu, 14 Apr 2011 12:47:48 +0000 (+0000) Subject: Allow default value declaration in DB schema. X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=7d78eb0b4d3694e4dd9041006d78149ed7b7c8e7;p=roundup.git Allow default value declaration in DB schema. git-svn-id: http://svn.roundup-tracker.org/svnroot/roundup/roundup/trunk@4590 57a73879-2fb5-44c3-a270-3262357dd7e2 --- diff --git a/roundup/cgi/templating.py b/roundup/cgi/templating.py index a541f50..11e1449 100644 --- a/roundup/cgi/templating.py +++ b/roundup/cgi/templating.py @@ -567,10 +567,7 @@ class HTMLClass(HTMLInputMixin, HTMLPermissions): for klass, htmlklass in propclasses: if not isinstance(prop, klass): continue - if isinstance(prop, hyperdb.Multilink): - value = [] - else: - value = None + value = prop.get_default_value() return htmlklass(self._client, self._classname, None, prop, item, value, self._anonymous) @@ -603,13 +600,10 @@ class HTMLClass(HTMLInputMixin, HTMLPermissions): l = [] for name, prop in self._props.items(): for klass, htmlklass in propclasses: - if isinstance(prop, hyperdb.Multilink): - value = [] - else: - value = None if isinstance(prop, klass): + value = prop.get_default_value() l.append(htmlklass(self._client, self._classname, '', - prop, name, value, self._anonymous)) + prop, name, value, self._anonymous)) if sort: l.sort(lambda a,b:cmp(a._name, b._name)) return l diff --git a/roundup/hyperdb.py b/roundup/hyperdb.py index 3659215..a9bb9e1 100644 --- a/roundup/hyperdb.py +++ b/roundup/hyperdb.py @@ -35,11 +35,15 @@ from roundup.i18n import _ # class _Type(object): """A roundup property type.""" - def __init__(self, required=False): + def __init__(self, required=False, default_value = None): self.required = required + self.__default_value = default_value def __repr__(self): ' more useful for dumps ' return '<%s.%s>'%(self.__class__.__module__, self.__class__.__name__) + def get_default_value(self): + """The default value when creating a new instance of this property.""" + return self.__default_value def sort_repr (self, cls, val, name): """Representation used for sorting. This should be a python built-in type, otherwise sorting will take ages. Note that @@ -50,8 +54,8 @@ class _Type(object): class String(_Type): """An object designating a String property.""" - def __init__(self, indexme='no', required=False): - super(String, self).__init__(required) + def __init__(self, indexme='no', required=False, default_value = ""): + super(String, self).__init__(required, default_value) self.indexme = indexme == 'yes' def from_raw(self, value, propname='', **kw): """fix the CRLF/CR -> LF stuff""" @@ -85,8 +89,9 @@ class Password(_Type): class Date(_Type): """An object designating a Date property.""" - def __init__(self, offset=None, required=False): - super(Date, self).__init__(required) + def __init__(self, offset=None, required=False, default_value = None): + super(Date, self).__init__(required = required, + default_value = default_value) self._offset = offset def offset(self, db): if self._offset is not None: @@ -124,10 +129,11 @@ class Interval(_Type): class _Pointer(_Type): """An object designating a Pointer property that links or multilinks to a node in a specified class.""" - def __init__(self, classname, do_journal='yes', required=False): + def __init__(self, classname, do_journal='yes', required=False, + default_value = None): """ Default is to journal link and unlink events """ - super(_Pointer, self).__init__(required) + super(_Pointer, self).__init__(required, default_value) self.classname = classname self.do_journal = do_journal == 'yes' def __repr__(self): @@ -163,6 +169,14 @@ class Multilink(_Pointer): "do_journal" indicates whether the linked-to nodes should have 'link' and 'unlink' events placed in their journal """ + + def __init__(self, classname, do_journal = 'yes', required = False): + + super(Multilink, self).__init__(classname, + do_journal, + required = required, + default_value = []) + def from_raw(self, value, db, klass, propname, itemid, **kw): if not value: return []