Projet

Général

Profil

Télécharger (4,79 ko) Statistiques
| Branche: | Tag: | Révision:

root / auquotidien / modules / categories_admin.py @ 8b02623d

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
from quixote.html import TemplateIO, htmltext
20

    
21
from qommon import _
22
from qommon import misc
23
from wcs.categories import Category
24
from qommon.form import *
25
from qommon.backoffice.menu import html_top
26
from qommon.admin.menu import command_icon, error_page
27
import wcs.admin.categories
28

    
29
class CategoryUI:
30
    def __init__(self, category):
31
        self.category = category
32
        if self.category is None:
33
            self.category = Category()
34

    
35
    def get_form(self):
36
        form = Form(enctype='multipart/form-data')
37
        form.add(StringWidget, 'name', title = _('Category Name'), required = True, size=30,
38
                value = self.category.name)
39
        form.add(TextWidget, 'description', title = _('Description'), cols = 80, rows = 10,
40
                value = self.category.description)
41

    
42
        homepage_redirect_url = get_cfg('misc', {}).get('homepage-redirect-url')
43
        if not homepage_redirect_url:
44
            form.add(SingleSelectWidget, 'homepage_position',
45
                        title=_('Position on homepage'),
46
                        value=self.category.get_homepage_position(),
47
                        options = [('1st', _('First Column')),
48
                                   ('2nd', _('Second Column')),
49
                                   ('side', _('Sidebar')),
50
                                   ('none', _('None'))])
51
            form.add(IntWidget, 'limit',
52
                        title=_('Limit number of items displayed on homepage'),
53
                        value=self.category.get_limit())
54

    
55
        form.add(StringWidget, 'redirect_url', size=32,
56
                title=_('URL Redirection'),
57
                hint=_('If set, redirect the site category page to the given URL.'),
58
                value=self.category.redirect_url)
59

    
60
        form.add_submit('submit', _('Submit'))
61
        form.add_submit('cancel', _('Cancel'))
62
        return form
63

    
64
    def submit_form(self, form):
65
        if self.category.id:
66
            self.category.name = form.get_widget('name').parse()
67
            category = self.category
68
        else:
69
            category = Category(name = form.get_widget('name').parse())
70

    
71
        name = form.get_widget('name').parse()
72
        category_names = [x.name for x in Category.select() if x.id != category.id]
73
        if name in category_names:
74
            form.get_widget('name').set_error(_('This name is already used'))
75
            raise ValueError()
76

    
77
        category.description = form.get_widget('description').parse()
78
        category.redirect_url = form.get_widget('redirect_url').parse()
79

    
80
        homepage_redirect_url = get_cfg('misc', {}).get('homepage-redirect-url')
81
        if not homepage_redirect_url:
82
            category.homepage_position = form.get_widget('homepage_position').parse()
83
            category.limit = form.get_widget('limit').parse()
84

    
85
        category.store()
86

    
87

    
88

    
89
class CategoryPage(wcs.admin.categories.CategoryPage):
90
    def __init__(self, component):
91
        self.category = Category.get(component)
92
        self.category_ui = CategoryUI(self.category)
93
        get_response().breadcrumb.append((component + '/', self.category.name))
94

    
95

    
96
class CategoriesDirectory(wcs.admin.categories.CategoriesDirectory):
97
    label = N_('Categories')
98

    
99
    def new(self):
100
        get_response().breadcrumb.append( ('categories/', _('Categories')) )
101
        get_response().breadcrumb.append( ('new', _('New')) )
102
        category_ui = CategoryUI(None)
103
        form = category_ui.get_form()
104
        if form.get_widget('cancel').parse():
105
            return redirect('.')
106

    
107
        if form.is_submitted() and not form.has_errors():
108
            try:
109
                category_ui.submit_form(form)
110
            except ValueError:
111
                pass
112
            else:
113
                return redirect('.')
114

    
115
        html_top('categories', title = _('New Category'))
116
        r = TemplateIO(html=True)
117
        r += htmltext('<h2>%s</h2>') % _('New Category')
118
        r += form.render()
119
        return r.getvalue()
120

    
121
    def _q_lookup(self, component):
122
        get_response().breadcrumb.append( ('categories/', _('Categories')) )
123
        return CategoryPage(component)
(12-12/27)