root/extra/modules/categories_admin.py @ 8c9a8c4e
| aeda4490 | Frédéric Péters | # 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
|
|||
| ca1830a1 | Frédéric Péters | # along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
| aeda4490 | Frédéric Péters | ||
from quixote import redirect
|
|||
from quixote.directory import Directory
|
|||
| 2bac98e9 | Frédéric Péters | from quixote.html import TemplateIO, htmltext
|
|
| aeda4490 | Frédéric Péters | ||
from qommon import misc
|
|||
from wcs.categories import Category
|
|||
from qommon.form import *
|
|||
from qommon.admin.menu import html_top, command_icon, error_page
|
|||
| c5a8974a | Frédéric Péters | import wcs.admin.categories
|
|
| aeda4490 | Frédéric Péters | ||
class CategoryUI:
|
|||
def __init__(self, category):
|
|||
self.category = category
|
|||
if self.category is None:
|
|||
self.category = Category()
|
|||
def get_form(self):
|
|||
form = Form(enctype='multipart/form-data')
|
|||
form.add(StringWidget, 'name', title = _('Category Name'), required = True, size=30,
|
|||
value = self.category.name)
|
|||
form.add(TextWidget, 'description', title = _('Description'), cols = 80, rows = 10,
|
|||
value = self.category.description)
|
|||
| 818bc7c2 | Frédéric Péters | form.add(SingleSelectWidget, 'homepage_position',
|
|
| aeda4490 | Frédéric Péters | title=_('Position on homepage'),
|
|
| 818bc7c2 | Frédéric Péters | value=self.category.get_homepage_position(),
|
|
| aeda4490 | Frédéric Péters | options = [('1st', _('First Column')),
|
|
('2nd', _('Second Column')),
|
|||
('side', _('Sidebar')),
|
|||
('none', _('None'))])
|
|||
| fc4cb69d | Frédéric Péters | form.add(IntWidget, 'limit',
|
|
title=_('Limit number of items displayed on homepage'),
|
|||
value=self.category.get_limit())
|
|||
| aeda4490 | Frédéric Péters | form.add_submit('submit', _('Submit'))
|
|
form.add_submit('cancel', _('Cancel'))
|
|||
return form
|
|||
def submit_form(self, form):
|
|||
if self.category.id:
|
|||
self.category.name = form.get_widget('name').parse()
|
|||
category = self.category
|
|||
else:
|
|||
category = Category(name = form.get_widget('name').parse())
|
|||
name = form.get_widget('name').parse()
|
|||
category_names = [x.name for x in Category.select() if x.id != category.id]
|
|||
if name in category_names:
|
|||
form.get_widget('name').set_error(_('This name is already used'))
|
|||
raise ValueError()
|
|||
category.description = form.get_widget('description').parse()
|
|||
| 818bc7c2 | Frédéric Péters | category.homepage_position = form.get_widget('homepage_position').parse()
|
|
| fc4cb69d | Frédéric Péters | category.limit = form.get_widget('limit').parse()
|
|
| aeda4490 | Frédéric Péters | ||
category.store()
|
|||
| c5a8974a | Frédéric Péters | class CategoryPage(wcs.admin.categories.CategoryPage):
|
|
| aeda4490 | Frédéric Péters | def __init__(self, component):
|
|
self.category = Category.get(component)
|
|||
self.category_ui = CategoryUI(self.category)
|
|||
get_response().breadcrumb.append((component + '/', self.category.name))
|
|||
| c5a8974a | Frédéric Péters | class CategoriesDirectory(wcs.admin.categories.CategoriesDirectory):
|
|
| aeda4490 | Frédéric Péters | label = N_('Categories')
|
|
| 2bac98e9 | Frédéric Péters | def new(self):
|
|
| aeda4490 | Frédéric Péters | get_response().breadcrumb.append( ('categories/', _('Categories')) )
|
|
get_response().breadcrumb.append( ('new', _('New')) )
|
|||
category_ui = CategoryUI(None)
|
|||
form = category_ui.get_form()
|
|||
if form.get_widget('cancel').parse():
|
|||
return redirect('.')
|
|||
if form.is_submitted() and not form.has_errors():
|
|||
try:
|
|||
category_ui.submit_form(form)
|
|||
except ValueError:
|
|||
pass
|
|||
else:
|
|||
return redirect('.')
|
|||
html_top('categories', title = _('New Category'))
|
|||
| 2bac98e9 | Frédéric Péters | r = TemplateIO(html=True)
|
|
r += htmltext('<h2>%s</h2>') % _('New Category')
|
|||
r += form.render()
|
|||
return r.getvalue()
|
|||
| aeda4490 | Frédéric Péters | ||
def _q_lookup(self, component):
|
|||
get_response().breadcrumb.append( ('categories/', _('Categories')) )
|
|||
return CategoryPage(component)
|
|||
from admin import AdminRootDirectory
|
|||
AdminRootDirectory.register_page('categories', CategoriesDirectory())
|