categories: change on-disk storage to be XML (#4739)

This commit is contained in:
Frédéric Péters 2014-11-25 13:33:58 +01:00
parent 9cbdb2da56
commit dfe463ae48
3 changed files with 55 additions and 1 deletions

View File

@ -1,3 +1,5 @@
import os
import pickle
import shutil
import tempfile
from cStringIO import StringIO
@ -87,3 +89,22 @@ def test_xml_import():
assert test.id == test2.id
assert test.name == test2.name
assert test.description == test2.description
def test_load_old_pickle():
Category.wipe()
test = Category()
test.id = 1
test.name = 'Test'
test.description = 'Hello world'
os.mkdir(os.path.join(pub.app_dir, 'categories'))
fd = file(os.path.join(pub.app_dir, 'categories', '1'), 'w')
pickle.dump(test, fd)
fd.close()
test2 = Category.get(1)
assert test.id == test2.id
assert test.name == test2.name
assert test.description == test2.description

View File

@ -22,8 +22,9 @@ from quixote.html import TemplateIO, htmltext
from qommon.storage import StorableObject
from qommon.misc import simplify, is_user_admin, indent_xml
from qommon.substitution import Substitutions
from qommon.xml_storage import XmlStorableObject
class Category(StorableObject):
class Category(XmlStorableObject):
_names = 'categories'
name = None
url_name = None

32
wcs/qommon/xml_storage.py Normal file
View File

@ -0,0 +1,32 @@
# w.c.s. - web application for online forms
# Copyright (C) 2005-2014 Entr'ouvert
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
from .storage import StorableObject
class XmlStorableObject(StorableObject):
@classmethod
def storage_load(cls, fd):
first_byte = fd.read(1)
fd.seek(0)
if first_byte == '<':
return cls.import_from_xml(fd, include_id=True)
else:
return StorableObject.storage_load(fd)
@classmethod
def storage_dumps(cls, object):
return object.export_to_xml_string(include_id=True)