diff --git a/wcs/categories.py b/wcs/categories.py
index 43e5578b8..b0c50c0d4 100644
--- a/wcs/categories.py
+++ b/wcs/categories.py
@@ -14,26 +14,25 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, see .
-import xml.etree.ElementTree as ET
-
from quixote import get_publisher, get_response
from quixote.html import TemplateIO, htmltext
from qommon.storage import StorableObject
-from qommon.misc import simplify, indent_xml
+from qommon.misc import simplify
from qommon.substitution import Substitutions
from qommon.xml_storage import XmlStorableObject
class Category(XmlStorableObject):
_names = 'categories'
+ _xml_tagname = 'category'
name = None
url_name = None
description = None
position = None
# declarations for serialization
- TEXT_ATTRIBUTES = ['name', 'url_name', 'description']
- INT_ATTRIBUTES = ['position']
+ XML_NODES = [('name', 'str'), ('url_name', 'str'), ('description', 'str'),
+ ('position', 'int')]
def __init__(self, name = None):
StorableObject.__init__(self)
@@ -82,61 +81,6 @@ class Category(XmlStorableObject):
form.store()
StorableObject.remove_self(self)
- def export_to_xml(self, include_id=False):
- charset = get_publisher().site_charset
- root = ET.Element('category')
- if include_id and self.id:
- root.attrib['id'] = str(self.id)
- for text_attribute in list(self.TEXT_ATTRIBUTES):
- if not hasattr(self, text_attribute) or not getattr(self, text_attribute):
- continue
- ET.SubElement(root, text_attribute).text = unicode(
- getattr(self, text_attribute), charset)
- for int_attribute in list(self.INT_ATTRIBUTES):
- if not hasattr(self, int_attribute) or not getattr(self, int_attribute):
- continue
- ET.SubElement(root, int_attribute).text = str(getattr(self, int_attribute))
- return root
-
- def export_to_xml_string(self, include_id=False):
- x = self.export_to_xml(include_id=include_id)
- indent_xml(x)
- return ET.tostring(x)
-
- def import_from_xml(cls, fd, charset=None, include_id=False):
- try:
- tree = ET.parse(fd)
- except:
- raise ValueError()
- return cls.import_from_xml_tree(tree, charset=charset,
- include_id=include_id)
- import_from_xml = classmethod(import_from_xml)
-
- def import_from_xml_tree(cls, tree, include_id=False, charset=None):
- if charset is None:
- charset = get_publisher().site_charset
- category = cls()
-
- # if the tree we get is actually a ElementTree for real, we get its
- # root element and go on happily.
- if not ET.iselement(tree):
- tree = tree.getroot()
-
- if include_id and tree.attrib.get('id'):
- category.id = int(tree.attrib.get('id'))
- for text_attribute in list(cls.TEXT_ATTRIBUTES):
- value = tree.find(text_attribute)
- if value is None:
- continue
- setattr(category, text_attribute, value.text.encode(charset))
- for int_attribute in list(cls.INT_ATTRIBUTES):
- value = tree.find(int_attribute)
- if value is None:
- continue
- setattr(category, int_attribute, int(value.text))
- return category
- import_from_xml_tree = classmethod(import_from_xml_tree)
-
def get_substitution_variables(self, minimal=False):
d = {
'category_name': self.name,
diff --git a/wcs/qommon/xml_storage.py b/wcs/qommon/xml_storage.py
index 260f04d3e..12d5a0c3b 100644
--- a/wcs/qommon/xml_storage.py
+++ b/wcs/qommon/xml_storage.py
@@ -14,10 +14,16 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, see .
+import xml.etree.ElementTree as ET
+
+from quixote import get_publisher
+
+from .misc import indent_xml
from .storage import StorableObject
-
class XmlStorableObject(StorableObject):
+ XML_NODES = []
+
@classmethod
def storage_load(cls, fd):
first_byte = fd.read(1)
@@ -30,3 +36,67 @@ class XmlStorableObject(StorableObject):
@classmethod
def storage_dumps(cls, object):
return object.export_to_xml_string(include_id=True)
+
+ def export_to_xml(self, include_id=False):
+ charset = get_publisher().site_charset
+ root = ET.Element(self._xml_tagname)
+ if include_id and self.id:
+ root.attrib['id'] = str(self.id)
+ for attribute in self.XML_NODES:
+ attribute_name, attribute_type = attribute
+ if not getattr(self, attribute_name, None):
+ continue
+ element = ET.SubElement(root, attribute_name)
+ getattr(self, 'export_%s_to_xml' % attribute_type)(element,
+ attribute_name, charset=charset)
+ return root
+
+ def export_str_to_xml(self, element, attribute_name, charset):
+ element.text = unicode(getattr(self, attribute_name), charset)
+
+ def export_int_to_xml(self, element, attribute_name, charset):
+ element.text = str(getattr(self, attribute_name))
+
+ def export_to_xml_string(self, include_id=False):
+ x = self.export_to_xml(include_id=include_id)
+ indent_xml(x)
+ return ET.tostring(x)
+
+ def import_from_xml(cls, fd, charset=None, include_id=False):
+ try:
+ tree = ET.parse(fd)
+ except:
+ raise ValueError()
+ return cls.import_from_xml_tree(tree, charset=charset,
+ include_id=include_id)
+ import_from_xml = classmethod(import_from_xml)
+
+ def import_from_xml_tree(cls, tree, include_id=False, charset=None):
+ if charset is None:
+ charset = get_publisher().site_charset
+ obj = cls()
+
+ # if the tree we get is actually a ElementTree for real, we get its
+ # root element and go on happily.
+ if not ET.iselement(tree):
+ tree = tree.getroot()
+
+ if include_id and tree.attrib.get('id'):
+ obj.id = int(tree.attrib.get('id'))
+
+ for attribute in cls.XML_NODES:
+ attribute_name, attribute_type = attribute
+ element = tree.find(attribute_name)
+ if element is None:
+ continue
+ setattr(obj, attribute_name,
+ getattr(obj, 'import_%s_from_xml' %
+ attribute_type)(element, charset=charset))
+ return obj
+ import_from_xml_tree = classmethod(import_from_xml_tree)
+
+ def import_str_from_xml(self, element, charset):
+ return element.text.encode(charset)
+
+ def import_int_from_xml(self, element, charset):
+ return int(element.text)