Project

General

Profile

Download (4.17 KB) Statistics
| Branch: | Tag: | Revision:

root / extra / modules / categories_admin.ptl @ ca1830a1

1
# w.c.s. - web application for online forms
2
# Copyright (C) 2005-2010  Entr'ouvert
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, see <http://www.gnu.org/licenses/>.
16

    
17
from quixote import redirect
18
from quixote.directory import Directory
19

    
20
from qommon import misc
21
from wcs.categories import Category
22
from qommon.form import *
23
from qommon.admin.menu import html_top, command_icon, error_page
24
import wcs.admin.categories
25

    
26
class CategoryUI:
27
    def __init__(self, category):
28
        self.category = category
29
        if self.category is None:
30
            self.category = Category()
31

    
32
    def get_form(self):
33
        form = Form(enctype='multipart/form-data')
34
        form.add(StringWidget, 'name', title = _('Category Name'), required = True, size=30,
35
                value = self.category.name)
36
        form.add(TextWidget, 'description', title = _('Description'), cols = 80, rows = 10,
37
                value = self.category.description)
38
        form.add(SingleSelectWidget, 'homepage_position',
39
                        title=_('Position on homepage'),
40
                        value=self.category.get_homepage_position(),
41
                        options = [('1st', _('First Column')),
42
                                   ('2nd', _('Second Column')),
43
                                   ('side', _('Sidebar')),
44
                                   ('none', _('None'))])
45
        form.add(IntWidget, 'limit',
46
                        title=_('Limit number of items displayed on homepage'),
47
                        value=self.category.get_limit())
48

    
49
        form.add_submit('submit', _('Submit'))
50
        form.add_submit('cancel', _('Cancel'))
51
        return form
52

    
53
    def submit_form(self, form):
54
        if self.category.id:
55
            self.category.name = form.get_widget('name').parse()
56
            category = self.category
57
        else:
58
            category = Category(name = form.get_widget('name').parse())
59

    
60
        name = form.get_widget('name').parse()
61
        category_names = [x.name for x in Category.select() if x.id != category.id]
62
        if name in category_names:
63
            form.get_widget('name').set_error(_('This name is already used'))
64
            raise ValueError()
65

    
66
        category.description = form.get_widget('description').parse()
67
        category.homepage_position = form.get_widget('homepage_position').parse()
68
        category.limit = form.get_widget('limit').parse()
69

    
70
        category.store()
71

    
72

    
73

    
74
class CategoryPage(wcs.admin.categories.CategoryPage):
75
    def __init__(self, component):
76
        self.category = Category.get(component)
77
        self.category_ui = CategoryUI(self.category)
78
        get_response().breadcrumb.append((component + '/', self.category.name))
79

    
80

    
81
class CategoriesDirectory(wcs.admin.categories.CategoriesDirectory):
82
    label = N_('Categories')
83

    
84
    def new [html] (self):
85
        get_response().breadcrumb.append( ('categories/', _('Categories')) )
86
        get_response().breadcrumb.append( ('new', _('New')) )
87
        category_ui = CategoryUI(None)
88
        form = category_ui.get_form()
89
        if form.get_widget('cancel').parse():
90
            return redirect('.')
91

    
92
        if form.is_submitted() and not form.has_errors():
93
            try:
94
                category_ui.submit_form(form)
95
            except ValueError:
96
                pass
97
            else:
98
                return redirect('.')
99

    
100
        html_top('categories', title = _('New Category'))
101
        '<h2>%s</h2>' % _('New Category')
102
        form.render()
103

    
104
    def _q_lookup(self, component):
105
        get_response().breadcrumb.append( ('categories/', _('Categories')) )
106
        return CategoryPage(component)
107

    
108
from admin import AdminRootDirectory
109
AdminRootDirectory.register_page('categories', CategoriesDirectory())
(7-7/26)