categories: change on-disk storage to be XML (#4739)
This commit is contained in:
parent
9cbdb2da56
commit
dfe463ae48
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
Loading…
Reference in New Issue