Project

General

Profile

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

root / extra / modules / links_ui.ptl @ 2750dbae

1
from quixote import get_request, get_response, get_session, redirect
2
from quixote.directory import Directory, AccessControlled
3

    
4
import wcs
5
import wcs.admin.root
6
from wcs.backoffice.menu import *
7

    
8
from qommon import errors
9
from qommon.form import *
10

    
11
from links import Link
12

    
13

    
14
class LinkDirectory(Directory):
15
    _q_exports = ['', 'edit', 'delete']
16

    
17
    def __init__(self, link):
18
        self.link = link
19

    
20
    def _q_index [html] (self):
21
        form = Form(enctype='multipart/form-data')
22
        form.add_submit('edit', _('Edit'))
23
        form.add_submit('delete', _('Delete'))
24
        form.add_submit('back', _('Back'))
25

    
26
        if form.get_submit() == 'edit':
27
            return redirect('edit')
28
        if form.get_submit() == 'delete':
29
            return redirect('delete')
30
        if form.get_submit() == 'back':
31
            return redirect('..')
32

    
33
        html_top('links', title = _('Link: %s') % self.link.title)
34
        '<h2>%s</h2>' % _('Link: %s') % self.link.title
35
        '<p>'
36
        self.link.url
37
        '</p>'
38

    
39
        form.render()
40

    
41
    def edit [html] (self):
42
        form = self.form()
43
        if form.get_submit() == 'cancel':
44
            return redirect('.')
45

    
46
        if form.is_submitted() and not form.has_errors():
47
            self.submit(form)
48
            return redirect('..')
49

    
50
        html_top('links', title = _('Edit Link: %s') % self.link.title)
51
        '<h2>%s</h2>' % _('Edit Link: %s') % self.link.title
52
        form.render()
53

    
54

    
55
    def form(self):
56
        form = Form(enctype='multipart/form-data')
57
        form.add(StringWidget, 'title', title = _('Title'), required = True,
58
                value = self.link.title)
59
        form.add(StringWidget, 'url', title = _('URL'), required=False,
60
                value = self.link.url,
61
                hint=_('Leave empty to create a title'))
62
        form.add_submit('submit', _('Submit'))
63
        form.add_submit('cancel', _('Cancel'))
64
        return form
65

    
66
    def submit(self, form):
67
        for k in ('title', 'url'):
68
            widget = form.get_widget(k)
69
            if widget:
70
                setattr(self.link, k, widget.parse())
71
        self.link.store()
72

    
73
    def delete [html] (self):
74
        form = Form(enctype='multipart/form-data')
75
        form.widgets.append(HtmlWidget('<p>%s</p>' % _(
76
                        'You are about to irrevocably delete this link.')))
77
        form.add_submit('submit', _('Submit'))
78
        form.add_submit('cancel', _('Cancel'))
79
        if form.get_submit() == 'cancel':
80
            return redirect('..')
81
        if not form.is_submitted() or form.has_errors():
82
            get_response().breadcrumb.append(('delete', _('Delete')))
83
            html_top('links', title = _('Delete Link'))
84
            '<h2>%s</h2>' % _('Deleting Link: %s') % self.link.title
85
            form.render()
86
        else:
87
            self.link.remove_self()
88
            return redirect('..')
89

    
90

    
91
class LinksDirectory(AccessControlled, Directory):
92
    _q_exports = ['', 'new', 'listing', 'update_order']
93
    label = N_('Links')
94

    
95
    def _q_access(self):
96
        user = get_request().user
97
        if not user:
98
            raise errors.AccessUnauthorizedError()
99
        admin_role = get_cfg('aq-permissions', {}).get('links', None)
100
        if not (user.is_admin or admin_role in (user.roles or [])):
101
            raise errors.AccessForbiddenError(
102
                    public_msg = _('You are not allowed to access Links Management'),
103
                    location_hint = 'backoffice')
104

    
105
        get_response().breadcrumb.append(('links/', _('Links')))
106

    
107

    
108
    def _q_index [html] (self):
109
        html_top('links', _('Links'))
110
        get_response().add_javascript(['jquery.js', 'interface.js', 'biglist.js'])
111

    
112
        '<ul id="main-actions">'
113
        '  <li><a class="new-item" href="new">%s</a></li>' % _('New Link')
114
        '</ul>'
115

    
116
        links = Link.select()
117
        Link.sort_by_position(links)
118

    
119
        '<ul class="biglist sortable" id="links-list">'
120
        for l in links:
121
            link_id = l.id
122
            '<li class="biglistitem" id="itemId_%s">' % link_id
123
            '<strong class="label"><a href="%s/">%s</a></strong>' % (link_id, l.title)
124
            '<p class="details">'
125
            l.url
126
            '</p>'
127
            '<p class="commands">'
128
            command_icon('%s/edit' % link_id, 'edit')
129
            command_icon('%s/delete' % link_id, 'remove')
130
            '</p></li>'
131
        '</ul>'
132

    
133
    def update_order(self):
134
        request = get_request()
135
        new_order = request.form['order'].strip(';').split(';')
136
        links = Link.select()
137
        dict = {}
138
        for l in links:
139
            dict[str(l.id)] = l
140
        for i, o in enumerate(new_order):
141
            dict[o].position = i + 1
142
            dict[o].store()
143
        return 'ok'
144

    
145

    
146
    def new [html] (self):
147
        link_ui = LinkDirectory(Link())
148

    
149
        form = link_ui.form()
150
        if form.get_submit() == 'cancel':
151
            return redirect('.')
152

    
153
        if form.is_submitted() and not form.has_errors():
154
            link_ui.submit(form)
155
            return redirect('%s/' % link_ui.link.id)
156

    
157
        get_response().breadcrumb.append(('new', _('New Link')))
158
        html_top('links', title = _('New Link'))
159
        '<h2>%s</h2>' % _('New Link')
160
        form.render()
161

    
162
    def _q_lookup(self, component):
163
        try:
164
            link = Link.get(component)
165
        except KeyError:
166
            raise errors.TraversalError()
167
        get_response().breadcrumb.append((str(link.id), link.title))
168
        return LinkDirectory(link)
169

    
170
    def listing(self):
171
        return redirect('.')
172

    
(18-18/26)