Ejemplo n.º 1
1
def save(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 != 'POST':
		joheaders.error_page(req, _(u'Only POST requests are allowed'))
		return '\n'
	tid = jotools.toint(jotools.get_param(req, "tid", "0"))
	if tid == 0:
		joheaders.error_page(req, _(u'Parameter %s is required') % u'tid')
		return '\n'
	db = jodb.connect()
	for field in req.form.list:
		if field.name.startswith('checked'):
			wid = jotools.toint(field.name[7:])
			if wid == 0: continue
			db.query("INSERT INTO task_word(tid, wid, uid) VALUES(%i, %i, %i)" %
			         (tid, wid, uid))
	joheaders.redirect_header(req, u"show?tid=%i" % tid)
Ejemplo n.º 2
0
def index(req):
    db = jodb.connect()
    (uid, uname, editable) = jotools.get_login_user(req)
    static_vars = {"UID": uid, "UNAME": uname, "EDITABLE": editable}
    jotools.process_template(req, db, static_vars, "index_index", _config.LANG, "joindex", 0)
    joheaders.page_footer_plain(req)
    return "\n"
Ejemplo n.º 3
0
def add(req):
	(uid, uname, editable) = jotools.get_login_user(req)
	if not jotools.is_admin(uid):
		joheaders.error_page(req, _(u'You must be an administrator to do this'))
		return '\n'
	datafields = ['firstname', 'lastname', 'uname', 'email', 'passwd']
	values = {}
	for datafield in datafields:
		values[datafield] = jotools.get_param(req, datafield, u'')
		if datafield != 'passwd':
			values[datafield] = jotools.escape_sql_string(values[datafield])
		if datafield not in ['email', 'passwd'] and values[datafield] == '':
			joheaders.error_page(req, _(u'Required field %s is missing') % datafield)
			return '\n'
	if values['passwd'] == u'':
		joheaders.error_page(req, _(u'Required field %s is missing') % u'passwd')
		return '\n'
	pwhash = sha.new((_config.PW_SALT + values['passwd']).encode('UTF-8')).hexdigest()
	privdb = jodb.connect_private()
	newuid = privdb.query("SELECT nextval('appuser_uid_seq')").getresult()[0][0]
	try:
		privdb.query(("INSERT INTO appuser(uid, uname, firstname, lastname, email, pwhash)" +
		              "VALUES(%i, '%s', '%s', '%s', '%s', '%s')") % (newuid, values['uname'],
			    values['firstname'], values['lastname'], values['email'], pwhash))
	except ProgrammingError:
		joheaders.error_page(req, _(u'User name is already in use'))
		return '\n'
	db = jodb.connect()
	db.query(("INSERT INTO appuser(uid, uname, firstname, lastname, email)" +
	          "VALUES(%i, '%s', '%s', '%s', '%s')") % (newuid, values['uname'],
		values['firstname'], values['lastname'], values['email']))
	joheaders.ok_page(req, _(u'New user was added succesfully'))
	return '\n'
Ejemplo n.º 4
0
def list(req):
	(uid, uname, editable) = jotools.get_login_user(req)
	db = jodb.connect()
	tasks = db.query("SELECT t.tid, t.descr, t.sql, COUNT(DISTINCT tw.wid) FROM task t " +
	                 "LEFT JOIN task_word tw ON (t.tid = tw.tid) " +
		       "GROUP BY t.tid, t.descr, t.sql ORDER BY t.tid")
	if tasks.ntuples() == 0:
		joheaders.error_page(req, _(u'There are no tasks.'))
		return '\n'
	joheaders.page_header_navbar_level1(req, _(u"tasks"), uid, uname)
	jotools.write(req, u"<p>%s:</p>\n" % _(u'Choose a task'))
	jotools.write(req, (u'<table class="border"><tr><th>%s</th><th>%s</th>' +
	                   u'<th>%s *</th><th>%s *</th></tr>\n') \
		         % (_(u'Task'), _(u'Total words'), _(u'Words left'), _(u'Completed')))
	for task in tasks.getresult():
		wordcount = db.query("SELECT COUNT(*) FROM (%s) AS q" % task[2]).getresult()[0][0]
		jotools.write(req, u'<tr><td><a href="work?tid=%i">' % task[0])
		jotools.write(req, u'%s</a></td>' % jotools.escape_html(unicode(task[1],'UTF-8')))
		jotools.write(req, u'<td>%i</td>' % wordcount)
		jotools.write(req, u'<td>%i</td>' % (wordcount - task[3]))
		if wordcount == 0: pleft = u'-'
		else: pleft = task[3] * 100 / wordcount
		jotools.write(req, u'<td>%s %%</td></tr>\n' % pleft)
	jotools.write(req, u"</table>\n")
	# "Words left" is an approximation, because all of the checked words may not belong to
	# this task any more. Calculating the exact numbers is too slow to do here.
	jotools.write(req, u"<p>*) %s.</p>" % _(u'Number of words left is an estimate'))
	joheaders.page_footer_plain(req)
	return '\n'
Ejemplo n.º 5
0
def categories(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'
	db = jodb.connect()
	results = db.query("SELECT coalesce(info, ''), count(*) FROM raw_word " +
	                   "WHERE processed = FALSE " +
	                   "GROUP BY coalesce(info, '') " +
	                   "ORDER BY coalesce(info, '') ")
	if results.ntuples() == 0:
		joheaders.error_page(req, _(u'There are no words to be added'))
		return '\n'
	joheaders.page_header_navbar_level1(req, _(u"Add words"), uid, uname)
	jotools.write(req, u"<p>%s:</p>\n" \
	                   % _(u'Choose a category from which you want to add words'))
	jotools.write(req, u"<table><tr><th>%s</th><th>%s</th></tr>\n" \
	                   % (_(u'Category'), _(u'Words left')))
	for result in results.getresult():
		cat = unicode(result[0], 'UTF-8')
		if cat == u'': cats = u'(' + _(u'no category') + u')'
		else: cats = cat
		jotools.write(req, (u'<tr><td><a href="add_from_db?category=%s">%s</a></td>' +
		                    u'<td>%i</td></tr>\n') \
		  % (jotools.escape_url(result[0]), jotools.escape_html(cats), result[1]))
	jotools.write(req, u"</table>\n")
	jotools.write(req, u'<p><a href="add_from_db">%s ...</a></p>\n' % _(u'All words'))
	joheaders.page_footer_plain(req)
	return '\n'
Ejemplo n.º 6
0
def form(req):
	db = jodb.connect()
	(uid, uname, editable) = jotools.get_login_user(req)
	joheaders.page_header_navbar_level1(req, _(u'Search database'), uid, uname)
	jotools.write(req, u'<form method="get" action="wlist">\n<p>')
	jotools.write(req, u'<label>%s: <input type="text" name="word" /></label></p>\n' % _(u'Word'))
	jotools.write(req, u'<p><label><input type="checkbox" name="wordre" /> %s</label>\n' \
	              % _(u'Use regular expression'))
	jotools.write(req, u' <b>%s</b> <label><input type="checkbox" name="wordsimplere" /> %s</label><br />\n' \
	              % (_(u'or'), _(u'Case insensitive search')))
	jotools.write(req, u'<label><input type="checkbox" name="altforms" /> %s</label></p>\n' \
	              % _(u'Search from alternative spellings'))
	
	wclasses = db.query("SELECT classid, name FROM wordclass ORDER BY classid").getresult()
	jotools.write(req, u'<h2>%s</h2>\n' % _(u'Word class'))
	jotools.write(req, u'<p>%s ' % _(u'Word class is'))
	jotools.write(req, u'<select name="wordclass">\n')
	jotools.write(req, u'<option selected="selected" value="">(%s)</option>\n' % _(u'any'))
	for (classid, name) in wclasses:
		jotools.write(req, u'<option value="%i">%s</option>\n' % (classid, unicode(name, 'UTF-8')))
	jotools.write(req, u'</select></p>\n')
	
	textattrs = db.query("SELECT aid, descr FROM attribute WHERE type = 1 ORDER BY descr, aid").getresult()
	jotools.write(req, u'<h2>%s</h2>\n' % _(u'Text attributes'))
	jotools.write(req, u'<p><select name="textaid">\n')
	jotools.write(req, u'<option selected="selected" value="">(%s)</option>\n' % _(u'select attribute'))
	for (aid, dsc) in textattrs:
		jotools.write(req, u'<option value="%i">%s</option>\n' % (aid, unicode(dsc, 'UTF-8')))
	jotools.write(req, u'</select> %s <input type="text" name="textvalue" /><br />\n' % _(u'is'))
	
	flagattrs = db.query("SELECT aid, descr FROM attribute WHERE type = 2 ORDER BY descr, aid").getresult()
	jotools.write(req, u'</p><h2>%s</h2>' % _(u'Flags set'))
	jotools.write(req, u'<ul class="cblist">')
	for (aid, dsc) in flagattrs:
		jotools.write(req, u'<li><label><input type="checkbox" name="flagon%i" />%s</label></li>\n' \
		              % (aid, unicode(dsc, 'UTF-8')))
	jotools.write(req, u'</ul>\n')
	jotools.write(req, u'<h2>%s</h2>' % _(u'Flags not set'))
	jotools.write(req, u'<ul class="cblist">')
	for (aid, dsc) in flagattrs:
		jotools.write(req, u'<li><label><input type="checkbox" name="flagoff%i" />%s</label></li>\n' \
		              % (aid, unicode(dsc, 'UTF-8')))
	jotools.write(req, u'</ul>\n')
	
	jotools.write(req, u'<h2>%s</h2>\n<p>' % _(u'Output type'))
	for (tname, tdesc) in jooutput.list_supported_types():
		if tname == 'html': selected = u'checked="checked"'
		else: selected = u''
		jotools.write(req, (u'<label><input type="radio" name="listtype" value="%s" %s />' +
		                    u'%s</label><br />\n') % (tname, selected, tdesc))
	jotools.write(req, u'</p><p><input type="submit" value="%s" /><input type="reset" value="%s" /></p>\n' \
	              % (_(u'Search'), _(u'Reset')))
	jotools.write(req, u'</form>\n')
	joheaders.page_footer_plain(req)
	return '\n'
Ejemplo n.º 7
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'
Ejemplo n.º 8
0
def _edit(req, wid):
	if (wid == None):
		joheaders.error_page(req, _(u'Parameter %s is required') % u'wid')
		return '\n'
	wid_n = jotools.toint(wid)
	db = jodb.connect()
	results = db.query("select word, class from word where wid = %i" % wid_n)
	if results.ntuples() == 0:
		joheaders.error_page(req, _(u'Word %i does not exist') % wid_n)
		return '\n'
	wordinfo = results.getresult()[0]
	(uid, uname, editable) = jotools.get_login_user(req)
	static_vars = {'WID': wid_n, 'WORD': unicode(wordinfo[0], 'UTF-8'), 'CLASSID': wordinfo[1],
	               'UID': uid, 'UNAME': uname, 'EDITABLE': editable}
	jotools.process_template(req, db, static_vars, u'word_edit', _config.LANG, u'joeditors', 1)
	joheaders.page_footer_plain(req)
	return '\n'
Ejemplo n.º 9
0
def add_manual(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
	joheaders.page_header_navbar_level1(req, _(u"Add words"), uid, uname)
	jotools.write(req, u'<form method="post" action="add">\n' +
	                   u'<table class="border">\n<tr><th>%s</th><th>%s</th></tr>\n' \
	                   % (_(u'Word'), _(u'Word class')))
	_add_entry_fields(req, db, None, words_per_page)
	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'
Ejemplo n.º 10
0
def show(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'
	tid = jotools.toint(jotools.get_param(req, "tid", "0"))
	if tid == 0:
		joheaders.error_page(req, _(u'Parameter %s is required') % u'tid')
		return '\n'
	words_per_page = 20
	db = jodb.connect()
	taskq = db.query("SELECT sql, orderby FROM task WHERE tid = %i" % tid)
	if taskq.ntuples() != 1:
		joheaders.error_page(req, u'Parameter %s is wrong' % u'tid')
		return '\n'
	tasksql = taskq.getresult()[0][0]
	taskorder = taskq.getresult()[0][1]
	results = db.query(("SELECT w.wid, w.word FROM word w, (%s) t " +
	                    "WHERE t.wid = w.wid AND w.wid NOT IN " +
		          "(SELECT tw.wid FROM task_word tw WHERE tw.tid = %i)" +
			"ORDER BY %s") % (tasksql, tid, taskorder))
	joheaders.page_header_nonavbar(req, u"%s %i" % (_(u'task'), tid))
	jotools.write(req, u'<form method="post" action="save">\n')
	jotools.write(req, u'<table class="border">\n<tr><th>%s</th><th>%s</th></tr>\n' \
	                   % (_(u'OK'), _(u'Word')))
	firstword = random.randint(0, max(results.ntuples() - words_per_page, 0))
	restuples = results.getresult()
	for i in range(firstword, min(firstword + words_per_page, results.ntuples())):
		word = restuples[i]
		jotools.write(req, u'<tr><td><input type="checkbox" name="checked%i" /></td>' \
		                   % word[0])
		jotools.write(req, (u'<td><a href="../word/edit?wid=%i" target="right">%s' +
		                    u'</a></td></tr>\n') \
				% (word[0], jotools.escape_html(unicode(word[1], 'UTF-8'))))
	jotools.write(req, u'</table>')
	jotools.write(req, u'<p><input type="hidden" name="tid" value="%i" />' % tid)
	jotools.write(req, u'<input type="submit" value="%s"></form></p>' % _(u'Save checked'))
	jotools.write(req, u'<p><a href="../" target="_top">%s</a></p>\n' \
	                   %_(u'Back to main page'))
	joheaders.page_footer_plain(req)
	return '\n'
Ejemplo n.º 11
0
def index(req):
	db = jodb.connect()
	privdb = jodb.connect_private()
	(uid, uname, editable) = jotools.get_login_user(req)
	joheaders.page_header_navbar_level1(req, u"Ehdota uusia sanoja", uid, uname)
	
	word = jotools.get_param(req, "word", u"").strip()
	wtype = jotools.get_param(req, "type", u"").strip()
	comment = jotools.get_param(req, "comment", u"").strip()
	
	if word != u"":
		if not jotools.checkword(word):
			jotools.write(req, u'<p class="error">Sanassa on kiellettyjä merkkejä.</p>')
			_print_entry_form(req, db)
		else:
			db.query("BEGIN")
			error = _is_old_word(req, db, word)
			if error != None:
				jotools.write(req, u'<p class="error">%s</p>' % error)
				_print_entry_form(req, db)
			elif not (editable or _allow_new_word(req, privdb, True)):
				_print_error_forbidden(req)
			else:
				db.query("INSERT INTO raw_word(word, info, notes) " +
				         "VALUES('%s', '%s', '%s')" % \
				         (jotools.escape_sql_string(word),
				          jotools.escape_sql_string(wtype),
				          jotools.escape_sql_string(comment)))
				jotools.write(req, u'<p class="ok">Ehdotuksesi on tallennettu. ' +
				                   u'Kiitos avusta!</p>')
				_print_entry_form(req, db)
			db.query("COMMIT")
	
	elif editable or _allow_new_word(req, privdb, False):
		_print_entry_form(req, db)
	else:
		_print_error_forbidden(req)
	joheaders.page_footer_plain(req)
	return '\n'
Ejemplo n.º 12
0
def change(req, wid = None):
	if req.method != 'POST':
		joheaders.error_page(req, _(u'Only POST requests are allowed'))
		return '\n'
	(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 (wid == None):
		joheaders.error_page(req, _(u'Parameter %s is required') % u'wid')
		return '\n'
	
	wid_n = jotools.toint(wid)
	db = jodb.connect()
	db.query("begin")
	wclass_results = db.query("select class from word where wid = %i" % wid_n)
	if wclass_results.ntuples() == 0:
		joheaders.error_page(req, _(u'Word %i does not exist') % wid_n)
		db.query("rollback")
		return '\n'
	wclass = wclass_results.getresult()[0][0]
	edfield_results = db.query(("select a.type, a.aid, a.descr from attribute a, attribute_class ac " +
	                            "where a.aid = ac.aid and ac.classid = %i and a.editable = TRUE") % wclass)
	eid = db.query("select nextval('event_eid_seq')").getresult()[0][0]
	event_inserted = False
	messages = []
	
	for attribute in edfield_results.getresult():
		if attribute[0] == 1: # string attribute
			html_att = 'string%i' % attribute[1]
			newval = jotools.get_param(req, html_att, None)
			if newval == None: continue
			
			vresults = db.query(("select s.value from string_attribute_value s where " +
			                     "s.wid = %i and s.aid = %i") % (wid_n, attribute[1]))
			if vresults.ntuples() == 0: oldval = u""
			else: oldval = unicode(vresults.getresult()[0][0], 'UTF-8')
			if oldval == newval: continue
			if not event_inserted:
				db.query("insert into event(eid, eword, euser) values(%i, %i, %i)" % \
				         (eid, wid_n, uid))
				event_inserted = True
			if newval == u'':
				db.query(("delete from string_attribute_value where wid = %i " +
				          "and aid = %i") % (wid_n, attribute[1]))
			elif oldval == u'':
				db.query(("insert into string_attribute_value(wid, aid, value, eevent) " +
				          "values(%i, %i, '%s', %i)") % (wid_n, attribute[1],
					                        jotools.escape_sql_string(newval), eid))
			else:
				db.query(("update string_attribute_value set value='%s', eevent=%i " +
				          "where wid=%i and aid=%i") %
					(jotools.escape_sql_string(newval), eid, wid_n, attribute[1]))
			messages.append(u"%s: '%s' -> '%s'" % (unicode(attribute[2], 'UTF-8'),
			                oldval, newval))
		if attribute[0] == 3: # integer attribute
			html_att = 'int%i' % attribute[1]
			newval_s = jotools.get_param(req, html_att, None)
			if newval_s == None: continue
			newval_s = newval_s.strip()
			if newval_s == u'':
				newval = None
			else:
				try: newval = int(newval_s)
				except ValueError: continue
				# Limit value range to prevent troubles with storing the
				# value into the database
				if newval < -1000000 or newval > 1000000: continue
			
			vresults = db.query(("select i.value from int_attribute_value i where " +
			                     "i.wid = %i and i.aid = %i") % (wid_n, attribute[1]))
			if vresults.ntuples() == 0: oldval = None
			else: oldval = vresults.getresult()[0][0]
			if oldval == newval: continue
			if not event_inserted:
				db.query("insert into event(eid, eword, euser) values(%i, %i, %i)" % \
				         (eid, wid_n, uid))
				event_inserted = True
			if newval == None:
				db.query(("delete from int_attribute_value where wid = %i " +
				          "and aid = %i") % (wid_n, attribute[1]))
			elif oldval == None:
				db.query(("insert into int_attribute_value(wid, aid, value, eevent) " +
				          "values(%i, %i, %i, %i)") % (wid_n, attribute[1],
					                             newval, eid))
			else:
				db.query(("update int_attribute_value set value=%i, eevent=%i " +
				          "where wid=%i and aid=%i") %
					(newval, eid, wid_n, attribute[1]))
			if oldval == None: oldval_s = _(u'(None)')
			else: oldval_s = `oldval`
			if newval == None: newval_s = _(u'(None)')
			else: newval_s = `newval`
			messages.append(u"%s: %s -> %s" % (unicode(attribute[2], 'UTF-8'),
			                oldval_s, newval_s))
	
	comment = jotools.get_param(req, 'comment', u'')
	
	if comment != u'':
		if not event_inserted:
			db.query("insert into event(eid, eword, euser) values(%i, %i, %i)" % \
			         (eid, wid_n, uid))
			event_inserted = True
		db.query("update event set comment = '%s' where eid = %i" \
		         % (jotools.escape_sql_string(comment), eid))
	if event_inserted and len(messages) > 0:
		mess_str = jotools.escape_sql_string(reduce(lambda x, y: x + u"\n" + y, messages, u""))
		db.query("update event set message = '%s' where eid = %i" % (mess_str, eid))
	db.query("commit")
	joheaders.redirect_header(req, u'edit?wid=%i' % wid_n)
	return '\n'
Ejemplo n.º 13
0
def rwords(req, wid = None):
	(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 wid == None:
		joheaders.error_page(req, _(u'Parameter %s is required') % u'wid')
		return '\n'
	wid_n = jotools.toint(wid)
	db = jodb.connect()
	results = db.query("select word, class from word where wid = %i" % wid_n)
	if results.ntuples() == 0:
		joheaders.error_page(req, _(u'Word %i does not exist') % wid_n)
		return '\n'
	wordinfo = results.getresult()[0]
	if req.method == 'GET': # show editor
		word = unicode(wordinfo[0], 'UTF-8')
		classid = wordinfo[1]
		title1 = _(u'Word') + u': ' + word
		link1 = u'edit?wid=%i' % wid_n
		title2 = _(u'related words')
		joheaders.page_header_navbar_level2(req, title1, link1, title2, uid, uname, wid_n)
		jotools.write(req, u'<p>%s</p>\n' % joeditors.call(db, u'word_class', [classid]))
		jotools.write(req, joeditors.call(db, u'rwords_edit_form', [wid_n]))
		joheaders.page_footer_plain(req)
		return '\n'
	if req.method != 'POST':
		joheaders.error_page(req, _(u'Only GET and POST requests are allowed'))
		return '\n'
	db.query("begin")
	rword_results = db.query("SELECT rwid, related_word FROM related_word WHERE wid = %i" % wid_n)
	rword_res = rword_results.getresult()
	eid = db.query("select nextval('event_eid_seq')").getresult()[0][0]
	event_inserted = False
	messages = []
	
	for attribute in rword_res:
		html_att = 'rword%i' % attribute[0]
		if jotools.get_param(req, html_att, u'') == u'on': remove = True
		else: remove = False
		
		if not remove: continue
		if not event_inserted:
			db.query("insert into event(eid, eword, euser) values(%i, %i, %i)" % \
			         (eid, wid_n, uid))
			event_inserted = True
		db.query("delete from related_word where wid = %i and rwid = %i" \
		         % (wid_n, attribute[0]))
		messages.append(_(u"Alternative spelling removed: '%s'") \
		                % jotools.escape_html(unicode(attribute[1], 'UTF-8')))
	
	newwords = jotools.get_param(req, 'add', u'')
	for word in jotools.unique(newwords.split()):
		if not jotools.checkword(word): continue
		already_listed = False
		for attribute in rword_res:
			if word == unicode(attribute[1], 'UTF-8'): 
				already_listed = True
				break
		if already_listed: continue
		if not event_inserted:
			db.query("insert into event(eid, eword, euser) values(%i, %i, %i)" % \
			         (eid, wid_n, uid))
			event_inserted = True
		db.query("insert into related_word(wid, eevent, related_word) values(%i, %i, '%s')" \
		         % (wid_n, eid, jotools.escape_sql_string(word)))
		messages.append(_(u"Alternative spelling added: '%s'") % jotools.escape_html(word))
	
	comment = jotools.get_param(req, 'comment', u'')
	
	if comment != u'':
		if not event_inserted:
			db.query("insert into event(eid, eword, euser) values(%i, %i, %i)" % \
			         (eid, wid_n, uid))
			event_inserted = True
		db.query("update event set comment = '%s' where eid = %i" \
		         % (jotools.escape_sql_string(comment), eid))
	if event_inserted and len(messages) > 0:
		mess_str = jotools.escape_sql_string(reduce(lambda x, y: x + u"\n" + y, messages, u""))
		db.query("update event set message = '%s' where eid = %i" % (mess_str, eid))
	db.query("commit")
	joheaders.redirect_header(req, u'edit?wid=%i' % wid_n)
	return '\n'
Ejemplo n.º 14
0
def call(req, outputtype, query):
	db = jodb.connect()
	if outputtype == 'html':
		_html(req, db, query)
	else:
		_apply_config.jooutput_call(req, outputtype, db, query)
Ejemplo n.º 15
0
def listchanges(req, sdate = None, edate = None):
	db = jodb.connect()
	(uid, uname, editable) = jotools.get_login_user(req)
	joheaders.page_header_navbar_level1(req, _(u'List changes'), uid, uname)
	
	edt = datetime.datetime.now()
	sdt = edt - datetime.timedelta(days=1)
	if sdate != None:
		try:
			stime = time.strptime(sdate, u'%Y-%m-%d')
			sdt = datetime.datetime(*stime[0:5])
		except:
			jotools.write(req, "<p>%s</p>\n" % _("Invalid start date"))
	if edate != None:
		try:
			etime = time.strptime(edate, u'%Y-%m-%d')
			edt = datetime.datetime(*etime[0:5])
		except:
			jotools.write(req, "<p>%s</p>\n" % _("Invalid end date"))
	sdate_s = sdt.strftime('%Y-%m-%d')
	edate_s = edt.strftime('%Y-%m-%d')
	
	jotools.write(req, u"""
<form method="get" action="listchanges">
<label>%s <input type="text" name="sdate" value="%s"/></label><br />
<label>%s <input type="text" name="edate" value="%s"/></label><br />
<input type="submit" /> <input type="reset" />
</form>
	""" % (_(u'Start date'), sdate_s, _(u'End date'), edate_s))
	
	# Increase edt by one day to make the the SQL between operator act on timestamps
	# in a more intuitive way.
	edt = edt + datetime.timedelta(days=1)
	edate_s = edt.strftime('%Y-%m-%d')
	
	results = db.query("""
	SELECT u.uname, to_char(w.ctime, 'YYYY-MM-DD HH24:MI:SS'),
	       coalesce(u.firstname, ''), coalesce(u.lastname, ''),
	       '%s', NULL, w.wid, w.word
	FROM word w, appuser u WHERE w.cuser = u.uid AND w.ctime BETWEEN '%s' AND '%s'
	UNION
	SELECT u.uname, to_char(e.etime, 'YYYY-MM-DD HH24:MI:SS'),
	       coalesce(u.firstname, ''), coalesce(u.lastname, ''),
	       e.message, e.comment, w.wid, w.word
	FROM appuser u, event e, word w WHERE u.uid = e.euser AND e.eword = w.wid
	AND e.etime BETWEEN '%s' AND '%s'
	ORDER BY 2 DESC""" % (_(u'Word created').encode('UTF-8'), sdate_s, edate_s, sdate_s, edate_s));
	
	if results.ntuples() > 1000:
		jotools.write(req, u'<p>%s</p>' % _(u'Too many changes, use narrower date interval.'))
		joheaders.page_footer_plain(req)
		return '\n'
	retstr = u''
	for result in results.getresult():
		wordlink = u'<a href="../word/edit?wid=%i">%s</a>' \
		           % (result[6], jotools.escape_html(unicode(result[7], 'UTF-8')))
		date = result[1]
		user = jotools.escape_html(unicode(result[2], 'UTF-8')) + u" " + \
		       jotools.escape_html(unicode(result[3], 'UTF-8')) + u" (" + \
		       jotools.escape_html(unicode(result[0], 'UTF-8')) + u")"
		retstr = retstr + (u'<div class="logitem"><p class="date">%s %s %s</p>\n' \
		                   % (wordlink, user, date))
		if result[4] != None:
			msg = jotools.escape_html(unicode(result[4], 'UTF-8')).strip()
			msg = msg.replace(u'\n', u'<br />\n')
			retstr = retstr + u'<p class="logmsg">%s</p>\n' % msg
		if result[5] != None:
			comment = jotools.escape_html(unicode(result[5], 'UTF-8')).strip()
			comment = comment.replace(u'\n', u'<br />\n')
			comment = jotools.comment_links(comment)
			retstr = retstr + u'<p class="comment">%s</p>\n' % comment
		retstr = retstr + u"</div>\n"
	
	jotools.write(req, retstr)
	joheaders.page_footer_plain(req)
	return '\n'
Ejemplo n.º 16
0
def flags(req, wid = None):
	(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 wid == None:
		joheaders.error_page(req, _(u'Parameter %s is required') % u'wid')
		return '\n'
	wid_n = jotools.toint(wid)
	db = jodb.connect()
	results = db.query("select word, class from word where wid = %i" % wid_n)
	if results.ntuples() == 0:
		joheaders.error_page(req, _(u'Word %i does not exist') % wid_n)
		return '\n'
	wordinfo = results.getresult()[0]
	if req.method == 'GET': # show editor
		word = unicode(wordinfo[0], 'UTF-8')
		classid = wordinfo[1]
		title1 = _(u'Word') + u': ' + word
		link1 = u'edit?wid=%i' % wid_n
		title2 = _(u'flags')
		joheaders.page_header_navbar_level2(req, title1, link1, title2, uid, uname, wid_n)
		jotools.write(req, u'<p>%s</p>\n' % joeditors.call(db, u'word_class', [classid]))
		jotools.write(req, joeditors.call(db, u'flag_edit_form', [wid_n, classid]))
		joheaders.page_footer_plain(req)
		return '\n'
	if req.method != 'POST':
		joheaders.error_page(req, _(u'Only GET and POST requests are allowed'))
		return '\n'
	db.query("begin")
	edfield_results = db.query(("SELECT a.aid, a.descr, CASE WHEN fav.wid IS NULL THEN 'f' ELSE 't' END " +
	                    "FROM attribute_class ac, attribute a " +
	                    "LEFT OUTER JOIN flag_attribute_value fav ON (a.aid = fav.aid and fav.wid = %i) " +
	                    "WHERE a.aid = ac.aid AND ac.classid = %i AND a.type = 2" +
	                    "ORDER BY a.descr") % (wid_n, wordinfo[1]))
	eid = db.query("select nextval('event_eid_seq')").getresult()[0][0]
	event_inserted = False
	messages = []
	
	for attribute in edfield_results.getresult():
		html_att = 'attr%i' % attribute[0]
		if jotools.get_param(req, html_att, u'') == u'on': newval = True
		else: newval = False
		
		if attribute[2] == 't': oldval = True
		else: oldval = False
		
		if oldval == newval: continue
		if not event_inserted:
			db.query("insert into event(eid, eword, euser) values(%i, %i, %i)" % \
			         (eid, wid_n, uid))
			event_inserted = True
		if newval == False:
			db.query(("delete from flag_attribute_value where wid = %i " +
			          "and aid = %i") % (wid_n, attribute[0]))
			messages.append(_(u"Flag removed: '%s'") % unicode(attribute[1], 'UTF-8'))
		if newval == True:
			db.query(("insert into flag_attribute_value(wid, aid, eevent) " +
			          "values(%i, %i, %i)") % (wid_n, attribute[0], eid))
			messages.append(_(u"Flag added: '%s'") % unicode(attribute[1], 'UTF-8'))
	
	comment = jotools.get_param(req, 'comment', u'')
	
	if comment != u'':
		if not event_inserted:
			db.query("insert into event(eid, eword, euser) values(%i, %i, %i)" % \
			         (eid, wid_n, uid))
			event_inserted = True
		db.query("update event set comment = '%s' where eid = %i" \
		         % (jotools.escape_sql_string(comment), eid))
	if event_inserted and len(messages) > 0:
		mess_str = jotools.escape_sql_string(reduce(lambda x, y: x + u"\n" + y, messages, u""))
		db.query("update event set message = '%s' where eid = %i" % (mess_str, eid))
	db.query("commit")
	joheaders.redirect_header(req, u'edit?wid=%i' % wid_n)
	return '\n'
Ejemplo n.º 17
0
def add(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'
	db = jodb.connect()
	if req.method != 'POST':
		joheaders.error_page(req, _(u'Only POST requests are allowed'))
		return '\n'
	db.query("BEGIN")
	if jotools.get_param(req, 'confirm', u'') == u'on': confirm = True
	else: confirm = False
	nwordlist = []
	added_count = 0
	need_confirm_count = 0
	i = -1
	while True:
		i = i + 1
		nword = jotools.get_param(req, 'word%i' % i, u'')
		if nword == u'': break
		word = {'word': nword, 'try_again': True, 'confirmed': False, 'wid': None}
		word['oword'] = jotools.get_param(req, 'origword%i' % i, None)
		nclass = jotools.get_param(req, 'class%i' % i, None)
		if not nclass in [None, u'']: nclass = jotools.toint(nclass)
		else: nclass = None
		word['cid'] = nclass
		if confirm and nclass != 0 and jotools.get_param(req, 'confirm%i' % i, u'') != u'on':
			word['error'] = _(u'Word was not added')
			word['try_again'] = False
		if jotools.get_param(req, 'confirm%i' % i, u'') == u'on': word['confirmed'] = True
		stored_word = _store_word(db, word, uid)
		if stored_word['wid'] != None: added_count = added_count + 1
		if stored_word['try_again']: need_confirm_count = need_confirm_count + 1
		nwordlist.append(stored_word)
	db.query("COMMIT")
	if added_count == 1 and len(nwordlist) == 1:
		# No confirmation screen if exactly 1 word was successfully added
		joheaders.redirect_header(req, "edit?wid=%i" % nwordlist[0]['wid'])
		return '\n'
	joheaders.page_header_navbar_level1(req, _(u"Add words"), uid, uname)
	if need_confirm_count > 0:
		jotools.write(req, u'<p>' + _(u'''Adding some words failed or requires confirmation.
Make the required changes and mark the words that you still want to add.''') + u'</p>')
		jotools.write(req, u'<form method="post" action="add">\n')
		jotools.write(req,
		  u'<table class="border"><tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n' \
		  % (_(u'Word'), _(u'Word class'), _(u'Confirm addition'), _(u'Notes')))
		_add_entry_fields(req, db, nwordlist, None)
		jotools.write(req, u'</table>\n<p>' +
		                   u'<input type="hidden" name="confirm" value="on">' +
		                   u'<input type="submit" value="%s"></p></form>\n' % _(u'Continue'))
		joheaders.page_footer_plain(req)
		return '\n'
	else:
		jotools.write(req, u'<p>%s:</p>' % _(u'The following changes were made'))
		jotools.write(req,
		  u'<table class="border"><tr><th>%s</th><th>%s</th><th>%s</th></tr>\n' \
		  % (_(u'Word'), _(u'Word class'), _(u'Notes')))
		_add_entry_fields(req, db, nwordlist, None)
		jotools.write(req, u'</table>\n')
		jotools.write(req, u'<p><a href="../">%s ...</a></p>\n' \
		                   % _(u'Back to main page'))
		joheaders.page_footer_plain(req)
		return '\n'