wcs/wcs/categories.py

181 lines
6.7 KiB
Python

# w.c.s. - web application for online forms
# Copyright (C) 2005-2010 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/>.
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.substitution import Substitutions
from qommon.xml_storage import XmlStorableObject
class Category(XmlStorableObject):
_names = 'categories'
name = None
url_name = None
description = None
position = None
# declarations for serialization
TEXT_ATTRIBUTES = ['name', 'url_name', 'description']
INT_ATTRIBUTES = ['position']
def __init__(self, name = None):
StorableObject.__init__(self)
self.name = name
def migrate(self):
changed = False
if not self.url_name:
self.url_name = simplify(self.name)
changed = True
if changed:
self.store()
def get_by_urlname(cls, url_name):
objects = [x for x in cls.select() if x.url_name == url_name]
if objects:
return objects[0]
raise KeyError()
get_by_urlname = classmethod(get_by_urlname)
def has_urlname(cls, url_name):
objects = [x for x in cls.select() if x.url_name == url_name]
if objects:
return True
return False
has_urlname = classmethod(has_urlname)
def sort_by_position(cls, categories):
def cmp_position(x, y):
if x.position == y.position:
return 0
if x.position is None:
return 1
if y.position is None:
return -1
return cmp(x.position, y.position)
categories.sort(cmp_position)
sort_by_position = classmethod(sort_by_position)
def remove_self(self):
from formdef import FormDef
for form in FormDef.select(lambda x: x.category_id == self.id):
form.category_id = None
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,
'category_id': self.url_name,
}
if not minimal:
d.update({
'category_description': self.description,
})
return d
def get_url(self):
base_url = get_publisher().get_frontoffice_url()
return '%s/%s/' % (base_url, self.url_name)
def get_description_html_text(self, editable=True):
from qommon.admin.menu import is_accessible
if not self.description:
return None
text = self.description
if text[0] != '<':
text = '<p>%s</p>' % text
if editable and is_accessible('categories'):
root_url = get_publisher().get_root_url()
get_response().add_javascript(['jquery.js', 'jquery-ui.js',
'ckeditor/ckeditor.js', 'qommon.wysiwyg.js',
'ckeditor/adapters/jquery.js'])
get_response().add_javascript_code('var LIVE_EDIT_TOOLTIP = "%s";\n' % \
_('Double click to edit the text'))
get_response().add_css_include('../js/smoothness/jquery-ui-1.10.0.custom.min.css')
return htmltext('<div class="block-edit-custom-text">'\
'<a class="edit-custom-text" href="%sadmin/categories/%s/description">%s</a>'\
'<div class="block-custom-text">'\
'%s'\
'</div></div>' % (root_url, self.id, _('Edit This Text'), text))
return htmltext(text)
Substitutions.register('category_name', category=N_('General'), comment=N_('Category Name'))
Substitutions.register('category_description', category=N_('General'), comment=N_('Category Description'))
Substitutions.register('category_id', category=N_('General'), comment=N_('Category Identifier'))