wcs/wcs/categories.py

126 lines
4.6 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/>.
from quixote import get_publisher, get_response
from quixote.html import TemplateIO, htmltext
from qommon.storage import StorableObject
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
redirect_url = None
# declarations for serialization
XML_NODES = [('name', 'str'), ('url_name', 'str'), ('description', 'str'),
('redirect_url', 'str'), ('position', 'int')]
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 get_substitution_variables(self, minimal=False):
d = {
'category_name': self.name,
'category_id': self.url_name,
'category_slug': 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):
if not self.description:
return None
text = self.description
if text[0] != '<':
text = '<p>%s</p>' % text
if editable and get_publisher().get_backoffice_root().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'))