Ticket #6071: form_system.py

File form_system.py, 7.5 KB (added by hans.rauch@…, 17 years ago)
Line 
1#-*-coding: utf-8 -*-
2"""
3/dms/form_system.py
4
5.. enthaelt Modifikationen des Django-Formular-Systems
6 Django content Management System
7
8Hans Rauch
9hans.rauch@gmx.net
10
11Die Programme des dms-Systems koennen frei genutzt und den spezifischen
12Beduerfnissen entsprechend angepasst werden.
13
140.01 31.01.2007 Beginn der Dokumentation
150.02 13.09.2007 Adresse der Hilfeseiten
160.03 14.09.2007 Fehlerkorrektur fuer Hilfefenster
17"""
18
19import string, re
20from settings import HELP_URL
21
22from django.utils.encoding import smart_unicode
23
24from django.utils.translation import ugettext as _
25
26from dms.encode_decode import encode_html
27
28# -----------------------------------------------------
29class form_system :
30
31 def encode(self, text):
32 text = string.replace(text, '\n', ' ')
33 text = string.replace(text, "'", '"')
34 text = string.replace(text, '"', '"')
35 text = encode_html(text)
36 return text
37
38 def getJsHelp(self, rHelp, text, title, rAnchor):
39 """ zeigt Hilfe in einem PopUp-Fenster an """
40 if string.strip(text) == '':
41 return ''
42 text = self.encode(text)
43 title = _('Kurzhilfe: ') + self.encode(title)
44 #url = 'JavaScript:showHelp(\'' + rHelp + '#' + rAnchor + '\');" '
45 url = "JavaScript:showHelp('%s#%s');\" " % (rHelp, rAnchor)
46 #url += 'class="overlay" \n'
47 url += 'onMouseOver="return overlib('
48 url += "'" + text + "'"
49 url += ",CAPTION,'" + title + "',TEXTSIZE,2,CAPTIONSIZE,2,SNAPX,16,SNAPY,4,"
50 url += 'ol_vpos=ABOVE,ol_fgcolor=\'#f0f0ff\',ol_width=400)" onMouseOut="nd();"'
51
52 return '&nbsp;<a href="' + url + '>...?</a>\n'
53
54 def modify_form ( self, rId, rForm, valign, max_cols, rError='' ) :
55 """ """
56 def convert_date(s):
57 cStart = 'value="'
58 nLen = len(cStart)
59 nStart = string.find(s, cStart)
60 nEnd = min(string.find(s, '"', nStart+nLen+1), string.find(s, ' ', nStart+nLen+1))
61 n_end_2 = string.find(s, '"', nStart+nLen+1)
62 d = s[nStart+nLen:nEnd]
63 arr = string.splitfields(d, '-')
64 if len(arr) == 3 :
65 dGerman = u'%s.%s.%s' % (arr[2],arr[1],arr[0])
66 else :
67 dGerman = d
68 return s[:nStart+nLen] + dGerman + s[n_end_2:] + """ <a href="javascript: void(0);"
69 onclick="return getCalendar(document.form_input.id_""" + rId + """);"><img
70 src="/dms_media/javascript/calendar/calendar.png" border="0" /></a>"""
71
72 def convert_date_time(s):
73 cStart = 'value="'
74 nLen = len(cStart)
75 nStart = string.find(s, cStart)
76 nEnd = string.find(s, '"', nStart+nLen+1)
77 d = s[nStart+nLen:nEnd]
78 a = string.splitfields(d, ' ')
79 arr = string.splitfields(a[0], '-')
80 if len(arr) == 3 :
81 dGerman = u'%s.%s.%s' % (arr[2],arr[1],arr[0])
82 else :
83 dGerman = d
84 dGerman += ' ' + a[1][:a[1].rfind(':')] # Sekunden abschneiden
85 return s[:nStart+nLen] + dGerman + s[nEnd:]
86
87 myType = smart_unicode(type(rForm.field))
88 nPos = string.rfind ( myType, '.' )
89 myType = myType[nPos+1:-2]
90 if rError:
91 error = ''
92 for err in rError:
93 error += encode_html(err)
94 form = smart_unicode(rForm) + '<br /><span class="red"><b>' + error + '</b></span>'
95 else :
96 form = smart_unicode(rForm)
97 # --- soll das Eingabeobjekt horizontal angezeigt werden?
98 if not valign and string.find(form, '<ul>') >= 0:
99 form = string.replace(form, '<ul>', '')
100 form = string.replace(form, '</ul>', '')
101 form = string.replace(form, '<li>', '')
102 form = string.replace(form, '</li>', '')
103 if string.find(form, 'type="hidden"') >= 0:
104 v = 'value="'
105 obj = re.search(v+'.*?"', form, re.S)
106 form += ' ' + form[obj.start()+len(v):obj.end()-1]
107 form = form.replace('&lt;br /&gt;', '<br />')
108 form = form.replace('&amp;', '&')
109 #if not myType in ['DateField', 'DateTimeField']:
110 if not myType in ['DateField']:
111 return form
112 if myType == 'DateTimeField':
113 return convert_date_time(form)
114 return convert_date(form)
115
116 def get_form ( self, rFormList, rFormDict, app_name, rForm,
117 tab_cluster, valign, max_cols, show_errors):
118 """ """
119 from django.template.loader import get_template
120 from django.template import Context
121 h = get_template('app/form_header.html')
122 hm = get_template('app/form_header_multiple.html')
123 i = get_template('app/form_input.html')
124 im = get_template('app/form_input_multiple.html')
125 inf = get_template('app/form_info.html')
126 t = get_template('app/form_table_start.html')
127 my_help_url = HELP_URL + app_name + '/'
128 if show_errors:
129 my_errors = rForm.errors
130 else:
131 my_errors = {}
132 c = Context ( {} )
133 ret = t.render(c)
134 for my_id in rFormList:
135 if tab_cluster.has_key(my_id):
136 my_data = tab_cluster[my_id]
137 my_info = my_data[0]['title']
138 my_cols = my_data[0]['cols']
139 my_rows = my_data[0]['rows']
140 my_show_cols = my_data[0]['show_cols']
141 if my_data[0].has_key('valign'):
142 my_valign = my_data[0]['valign']
143 else:
144 my_valign = valign
145 my_cells = my_data[1]
146 c = Context ( {} )
147 ret += t.render(c)
148 if my_info != '':
149 c = Context ( { 'cols': 1+len(my_cols), 'info': my_info} )
150 ret += inf.render(c)
151 if my_show_cols:
152 c = Context ( { 'headers': my_cols, } )
153 ret += hm.render(c)
154 n_row = 0
155 for row in my_rows:
156 if my_show_cols:
157 inputs = []
158 for c in my_cols:
159 cell_id = my_cells[n_row]
160 if my_errors.has_key(cell_id):
161 err_info = my_errors[cell_id][0]
162 my_form = self.modify_form(cell_id, rForm[cell_id],
163 my_valign, max_cols, err_info)
164 else :
165 my_form = self.modify_form(cell_id, rForm[cell_id], my_valign, max_cols)
166 inputs.append(my_form)
167 n_row += 1
168 c = Context ( { 'title' : row,
169 'label' : cell_id,
170 'inputs' : inputs,
171 'max_cols': max_cols,
172 } )
173 ret += im.render(c)
174 else:
175 inputs = []
176 for c in my_cols:
177 cell_id = my_cells[n_row]
178 if my_errors.has_key(cell_id):
179 err_info = my_errors[cell_id][0]
180 my_form = self.modify_form(cell_id, rForm[cell_id],
181 my_valign, max_cols, err_info)
182 else :
183 my_form = self.modify_form(cell_id, rForm[cell_id], my_valign, max_cols)
184 inputs.append(my_form)
185 n_row += 1
186 c = Context ( { 'title' : row,
187 'label' : cell_id,
188 'inputs' : inputs,
189 'max_cols': max_cols,
190 } )
191 ret += im.render(c)
192 else:
193 form = rFormDict[my_id]
194 if my_errors.has_key(my_id) :
195 my_form = self.modify_form(my_id, rForm[my_id], valign, max_cols, my_errors[my_id])
196 else :
197 my_form = self.modify_form(my_id, rForm[my_id], valign, max_cols)
198 c = Context ( { 'title' : form['title'],
199 'label' : my_id,
200 'input' : my_form,
201 'help' : self.getJsHelp(my_help_url, form['help'],
202 form['title'], my_id),
203 'max_cols': max_cols,
204 } )
205 ret += i.render(c)
206 ret += '</table>\n'
207 return ret
Back to Top