Project

General

Profile

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

root / extra / modules / forms_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
from qommon import misc, get_logger
11

    
12
from wcs.formdef import FormDef
13
from wcs.categories import Category
14

    
15
class FormDirectory(Directory):
16
    _q_exports = ['options', 'disable', 'enable']
17

    
18
    def __init__(self, formdef):
19
        self.formdef = formdef
20

    
21
    def enable(self):
22
        self.formdef.disabled = False
23
        self.formdef.store()
24
        return redirect('..')
25

    
26
    def disable(self):
27
        self.formdef.disabled = True
28
        self.formdef.store()
29
        return redirect('..')
30

    
31
    def options [html] (self):
32
        form = Form(enctype='multipart/form-data')
33
        form.add_submit('submit', _('Submit'))
34
        form.add_submit('cancel', _('Cancel'))
35
        form.add_submit('disable', _('Disable Form'))
36

    
37
        if form.get_submit() == 'disable':
38
            return self.disable()
39

    
40
        if form.get_submit() == 'cancel':
41
            return redirect('..')
42

    
43
        if form.is_submitted() and not form.has_errors():
44
            return redirect('..')
45

    
46
        '<p>'
47
        _('No available options')
48
        '</p>'
49
        form.render()
50

    
51

    
52
def is_forms_admin():
53
    user = get_request().user
54
    if not user:
55
        return False
56
    admin_role = get_cfg('aq-permissions', {}).get('forms', None)
57
    if not (user.is_admin or admin_role in (user.roles or [])):
58
        return False
59
    return True
60

    
61

    
62
class FormsDirectory(AccessControlled, Directory):
63
    _q_exports = ['', 'new', 'listing', 'update_order']
64
    label = N_('Forms')
65

    
66
    def _q_access(self):
67
        get_response().breadcrumb.append(('forms/', _('Forms')))
68

    
69
    def _q_index [html] (self):
70
        html_top('forms', _('Forms'))
71

    
72
        session = get_session()
73
        user = get_request().user
74

    
75
        is_admin = is_forms_admin()
76

    
77
        l = []
78
        l2 = []
79
        if user:
80
            for formdef in FormDef.select(order_by='name', ignore_errors=True):
81
                if not formdef.disabled:
82
                    if is_admin or formdef.receiver_id in (user.roles or []):
83
                        l.append(formdef)
84
                elif is_admin:
85
                    l2.append(formdef)
86

    
87
        cats = Category.select(order_by = 'name')
88
        lists = []
89
        for c in cats:
90
            t = [x for x in l if x.category_id == c.id]
91
            if t:
92
                lists.append((c.name, t))
93
        t = [x for x in l if x.category_id is None]
94
        if t:
95
            lists.append((None, t))
96

    
97
        lists_disabled = []
98
        for c in cats:
99
            t = [x for x in l2 if x.category_id == c.id]
100
            if t:
101
                lists_disabled.append((c.name, t))
102
        t = [x for x in l2 if x.category_id is None]
103
        if t:
104
            lists_disabled.append((None, t))
105

    
106
        mid_term = ((len(lists) * 2) + sum([len(x[-1]) for x in lists])) / 2
107
        count = 0
108
        in_second_column = False
109
        '<div class="splitcontent-left">'
110
        for cat_name, formdefs in lists:
111
            if not in_second_column and count > mid_term:
112
                '</div>'
113
                '<div class="splitcontent-right">'
114
            count += 2 + len(formdefs)
115
            if len([x for x in formdefs if not x.disabled]) == 0:
116
                # all forms are disabled, dim the block
117
                '<div class="bo-block dimmed-block">'
118
            else:
119
                '<div class="bo-block">'
120
            if cat_name:
121
                '<h2>%s</h2>' % cat_name
122
            elif len(lists) > 1:
123
                '<h2>%s</h2>' % _('Misc')
124
            '<ul>'
125
            for formdef in formdefs:
126
                '<li>'
127
                if formdef.disabled:
128
                    '<span class="disabled">'
129
                '<a href="../%s/">%s</a>' % (formdef.url_name, formdef.name)
130
                if formdef.disabled:
131
                    '</span>'
132
                if is_admin:
133
                    ' (<a href="%s/options" rel="popup">%s</a>)' % (formdef.id, _('options'))
134
                '</li>'
135
            '</ul>'
136
            '</div>'
137
        '</div>'
138

    
139
        if is_admin and lists_disabled:
140
            get_response().filter['sidebar'] = self.get_sidebar(lists_disabled)
141
        get_logger().info('forms backoffice - home')
142

    
143
    def get_sidebar [html] (self, lists_disabled):
144
        '<h3>%s</h3>' % _('Available Forms')
145
        '<p>'
146
        _('Click on forms to enable them.')
147
        '</p>'
148
        for cat_name, formdefs in lists_disabled:
149
            if cat_name:
150
                '<h4>%s</h4>' % cat_name
151
            else:
152
                '<h4>%s</h4>' % _('Misc')
153
            '<ul>'
154
            for formdef in formdefs:
155
                '<li>'
156
                '<a href="%s/enable">%s</a>' % (formdef.id, formdef.name)
157
                '</li>'
158
            '</ul>'
159

    
160

    
161
    def _q_lookup(self, component):
162
        try:
163
            formdef = FormDef.get(component)
164
        except KeyError:
165
            raise errors.TraversalError()
166
        get_response().breadcrumb.append((str(formdef.id), formdef.name))
167
        return FormDirectory(formdef)
168

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

    
(16-16/26)