1
|
from quixote import get_response, redirect
|
2
|
from quixote.directory import Directory
|
3
|
|
4
|
from qommon.form import *
|
5
|
from qommon.admin.menu import html_top, command_icon
|
6
|
|
7
|
from field_prefill import FieldPrefill
|
8
|
|
9
|
class FieldUI:
|
10
|
def __init__(self, field_prefill):
|
11
|
self.field_prefill = field_prefill
|
12
|
|
13
|
def form_edit(self):
|
14
|
form = Form(enctype='multipart/form-data')
|
15
|
form.add(StringWidget, 'name', title = _('Field name'), required = True,
|
16
|
size = 50, value = self.field_prefill.name)
|
17
|
form.add(StringWidget, 'xpath', title = _('Xpath of the attribute'), required = True,
|
18
|
size = 50, value = self.field_prefill.xpath, hint=_('Example: /pp:PP/pp:InformalName'))
|
19
|
form.add(IntWidget, 'number', title = _('Number of the field in the data'), required = True,
|
20
|
size = 3, value = self.field_prefill.number,
|
21
|
hint=_('Change it if there are multiple fields corresponding to the same Xpath and you want to get another than the first one'))
|
22
|
form.add(CheckboxWidget, 'raw_xml', title=_('Get raw XML value'),
|
23
|
value = self.field_prefill.raw_xml)
|
24
|
form.add(StringWidget, 'regexp_match', title = _('Python regexp of a string to match'),
|
25
|
size = 50, value = self.field_prefill.regexp_match)
|
26
|
form.add(StringWidget, 'regexp_replacing', title = _('Python regexp of the replacing string'),
|
27
|
size = 50, value = self.field_prefill.regexp_replacing)
|
28
|
form.add(WidgetDict, 'select_options', title = _('Options mapping for a select field'),
|
29
|
value = self.field_prefill.select_options, add_element_label = _('Add item'))
|
30
|
form.add_submit('submit', _('Submit'))
|
31
|
form.add_submit('cancel', _('Cancel'))
|
32
|
return form
|
33
|
|
34
|
def submit_edit(self, form):
|
35
|
for f in ('name', 'xpath', 'number', 'raw_xml', 'regexp_match', 'regexp_replacing', 'select_options'):
|
36
|
widget = form.get_widget(f)
|
37
|
setattr(self.field_prefill, f, widget.parse())
|
38
|
self.field_prefill.store()
|
39
|
|
40
|
class FieldPage(Directory):
|
41
|
_q_exports = ['', 'delete']
|
42
|
|
43
|
def __init__(self, field_id):
|
44
|
self.field_prefill = FieldPrefill.get(field_id)
|
45
|
self.field_ui = FieldUI(self.field_prefill)
|
46
|
get_response().breadcrumb.append((field_id + '/', field_id))
|
47
|
|
48
|
def _q_index [html] (self):
|
49
|
form = self.field_ui.form_edit()
|
50
|
redo = False
|
51
|
|
52
|
if form.get_widget('cancel').parse():
|
53
|
return redirect('..')
|
54
|
|
55
|
if form.get_widget('select_options') and form.get_widget('select_options').get_widget('add_element').parse():
|
56
|
form.clear_errors()
|
57
|
redo = True
|
58
|
|
59
|
if redo is False and form.is_submitted() and not form.has_errors():
|
60
|
self.field_ui.submit_edit(form)
|
61
|
return redirect('..')
|
62
|
|
63
|
get_response().breadcrumb.append( ('edit', _('Edit')) )
|
64
|
html_top('edit', title = _('Edit'))
|
65
|
'<h2>%s</h2>' % _('Edit')
|
66
|
|
67
|
form.render()
|
68
|
|
69
|
def delete [html] (self):
|
70
|
form = Form(enctype='multipart/form-data')
|
71
|
form.widgets.append(HtmlWidget('<p>%s</p>' % _(
|
72
|
'You are about to irrevocably delete this field.')))
|
73
|
form.add_submit('submit', _('Submit'))
|
74
|
form.add_submit('cancel', _('Cancel'))
|
75
|
if form.get_widget('cancel').parse():
|
76
|
return redirect('..')
|
77
|
if not form.is_submitted() or form.has_errors():
|
78
|
get_response().breadcrumb.append(('delete', _('Delete')))
|
79
|
html_top('delete_form', title = _('Delete Field'))
|
80
|
'<h2>%s : %s</h2>' % (_('Delete Field'), self.field_prefill.id)
|
81
|
form.render()
|
82
|
else:
|
83
|
self.field_prefill.remove_self()
|
84
|
return redirect('..')
|
85
|
|
86
|
|
87
|
class FieldsDirectory(Directory):
|
88
|
_q_exports = ['', 'new']
|
89
|
|
90
|
def __init__(self, form_prefill):
|
91
|
get_response().breadcrumb.append(('fields/', _('Fields')))
|
92
|
self.form_prefill = form_prefill
|
93
|
|
94
|
def _q_lookup(self, component):
|
95
|
return FieldPage(component)
|
96
|
|
97
|
def _q_index [html] (self):
|
98
|
html_top('fields', title = _('Fields'))
|
99
|
"""<ul id="nav-fields-admin">
|
100
|
<li><a href="new">%s</a></li>
|
101
|
</ul>""" % _('New Field')
|
102
|
|
103
|
'<ul class="biglist">'
|
104
|
|
105
|
for field_prefill in FieldPrefill.select(lambda x: x.form_id == self.form_prefill.id):
|
106
|
if not field_prefill.name:
|
107
|
continue
|
108
|
|
109
|
# Split too long xpath
|
110
|
xpath = field_prefill.xpath
|
111
|
xpath_tokens = xpath.split(str('/'))
|
112
|
if len(xpath_tokens) > 3:
|
113
|
xpath = str('.../') + str('/').join(xpath_tokens[-3:])
|
114
|
|
115
|
'<li>'
|
116
|
'<strong class="label">%s</strong>' % field_prefill.name
|
117
|
'<br />%s' % xpath
|
118
|
'<p class="commands">'
|
119
|
command_icon('%s/' % field_prefill.id, 'edit')
|
120
|
command_icon('%s/delete' % field_prefill.id, 'remove')
|
121
|
'</p></li>'
|
122
|
'</ul>'
|
123
|
|
124
|
def new [html] (self):
|
125
|
get_response().breadcrumb.append(('new', _('New')) )
|
126
|
field_prefill = FieldPrefill()
|
127
|
field_prefill.form_id = self.form_prefill.id
|
128
|
field_prefill.store()
|
129
|
return redirect('%s/' % field_prefill.id)
|
130
|
|