示例#1
0
def add_from_db(req):
	(uid, uname, editable) = jotools.get_login_user(req)
	if not editable:
		joheaders.error_page(req, _(u'You are not allowed to edit data'))
		return '\n'
	if req.method != 'GET':
		joheaders.error_page(req, _(u'Only GET requests are allowed'))
		return '\n'
	db = jodb.connect()
	words_per_page = 15
	category = jotools.get_param(req, 'category', None)
	if category == None: condition = ""
	else: condition = "AND coalesce(info, '') = '%s'" \
	                  % jotools.escape_sql_string(category)
	results = db.query("SELECT count(*) FROM raw_word WHERE processed = FALSE %s" \
	                   % condition)
	nwords = results.getresult()[0][0]
	if nwords <= words_per_page: limit = ""
	else: limit = "LIMIT %i OFFSET %i" % (words_per_page,
	              random.randint(0, nwords - words_per_page))
	results = db.query(("SELECT word, coalesce(notes, '') FROM raw_word " +
	                    "WHERE processed = FALSE %s " +
	                    "ORDER BY word %s") % (condition, limit))
	if results.ntuples() == 0 and category == None:
		joheaders.error_page(req, _(u'There are no words to be added'))
		return '\n'
	if results.ntuples() == 0 and category != None:
		joheaders.error_page(req, _(u'There are no words to be added') + u' ' +
		          _(u'in category %s') % jotools.escape_html(category))
		return '\n'
	class_res = db.query("select classid, name from wordclass").getresult()
	joheaders.page_header_navbar_level1(req, _(u"Add words"), uid, uname)
	jotools.write(req, u'<form method="post" action="add">\n')
	jotools.write(req, u'<table class="border">\n')
	jotools.write(req, u'<tr><th>%s</th><th>%s</th><th>%s</th></tr>\n' \
	                   % (_(u'Word'), _(u'Word class'), _(u'Notes')))
	i = 0
	for result in results.getresult():
		word = unicode(result[0], 'UTF-8')
		notes = unicode(result[1], 'UTF-8')
		jotools.write(req, u'<tr><td><input type="hidden" name="origword%i" value=%s />' \
			                   % (i, jotools.escape_form_value(word)))
		jotools.write(req, u'<input type="text" name="word%i" value=%s /></td><td>' \
		                   % (i, jotools.escape_form_value(word)))
		jotools.write(req, _get_class_selector(class_res, None, i, True))
		jotools.write(req, u'</td><td>')
		jotools.write(req, jotools.escape_html(notes))
		jotools.write(req, u'</td></tr>\n')
		i = i + 1
	jotools.write(req, u'</table>\n' +
	                   u'<p><input type="submit" value="%s"></p></form>\n' % _(u"Add words"))
	joheaders.page_footer_plain(req)
	return '\n'
示例#2
0
def _add_entry_fields(req, db, words = None, count = 1):
	class_res = db.query("select classid, name from wordclass").getresult()
	if words == None:
		for i in range(count):
			jotools.write(req, u'<tr><td><input type="text" name="word%i" /></td><td>' % i)
			for res in class_res:
				jotools.write(req, (u'<label><input type="radio" name="class%i" ' +
				                     'value="%i">%s</input></label>\n') \
				            % (i, res[0], jotools.escape_html(unicode(res[1], 'UTF-8'))))
			jotools.write(req, u'</td></tr>\n')
		return
	confirm_column = False
	for word in words:
		if word['try_again'] == True:
			confirm_column = True
			break
	i = 0
	for word in words:
		jotools.write(req, u'<tr><td>')
		if word['try_again']:
			if word['oword'] != None:
				jotools.write(req, u'<input type="hidden" name="origword%i" value=%s />' \
				                   % (i, jotools.escape_form_value(word['oword'])))
			jotools.write(req, u'<input type="hidden" name="word%i" value=%s />%s</td><td>' \
			                   % (i, jotools.escape_form_value(word['word']), 
				               jotools.escape_html(word['word'])))
			incnocls = word['oword'] != None
			jotools.write(req, _get_class_selector(class_res, word['cid'], i, incnocls))
			jotools.write(req, u'<td><input type="checkbox" name="confirm%i"></td><td>' % i)
			jotools.write(req, word['error'])
			jotools.write(req, u'</td>')
			i = i + 1
		else:
			jotools.write(req, jotools.escape_html(word['word']))
			jotools.write(req, u'</td><td>')
			jotools.write(req, _get_class_selector(class_res, word['cid'], -1, False))
			jotools.write(req, u'</td><td>')
			if confirm_column: jotools.write(req, u'&nbsp;</td><td>')
			jotools.write(req, word['error'])
			jotools.write(req, u'</td>')
		jotools.write(req, u'</tr>\n')