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)
def _html(req, db, query): offset_s = `jotools.toint(jotools.get_param(req, 'offset', u'0'))` limit_s = `jotools.toint(jotools.get_param(req, 'limit', u'200'))` if limit_s == u'0': limit_s = u'ALL' param_s = u'' for field in req.form.list: if not field.name in ['limit', 'offset'] and jotools.checkid(field.name): param_s = param_s + field.name + u'=' + jotools.get_param(req, field.name, u'') + u'&' results = db.query("%s LIMIT %s OFFSET %s" % (query, limit_s, offset_s)) if results.ntuples() == 0: joheaders.error_page(req, _(u'No matching words were found')) return "\n" elif results.ntuples() == 1: joheaders.redirect_header(req, _config.WWW_ROOT_DIR + "/word/edit?wid=%i" \ % results.getresult()[0][0]) return "\n" else: (uid, uname, editable) = jotools.get_login_user(req) joheaders.page_header_navbar_level1(req, _('Search results'), uid, uname) jotools.write(req, u'<table><tr><th>%s</th><th>%s</th></tr>\n' \ % (_("Word"), _("Word class"))) for result in results.getresult(): jotools.write(req, _print_html_line(db, result[0], unicode(result[1], 'UTF-8'), unicode(result[2], 'UTF-8'))) jotools.write(req, u"</table>\n") if not limit_s == u'ALL' and results.ntuples() == jotools.toint(limit_s): jotools.write(req, (u'<p><a href="wlist?%soffset=%i&limit=%s">' + u"%s ...</a></p>\n") % (param_s, int(offset_s)+int(limit_s), limit_s, _(u'More results'))) joheaders.page_footer_plain(req) return '\n'
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'
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'
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'
def work(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' joheaders.frame_header(req, u"Joukahainen > %s %i" % (_(u'task'), tid)) jotools.write(req, u'<frameset cols="20%, 80%">\n') jotools.write(req, u'<frame name="left" src="show?tid=%i" />\n' % tid) jotools.write(req, u'<frame name="right" />\n') jotools.write(req, u'</frameset>\n') joheaders.frame_footer(req) return '\n'
def logout(req, wid = None): if req.method != 'POST': joheaders.error_page(req, _(u'Only POST requests are allowed')) return '\n' session = jotools.get_session(req) if session != '': db = jodb.connect_private() db.query(("update appuser set session_key = NULL, session_exp = NULL " + "where session_key = '%s'") % session) req.headers_out['Set-Cookie'] = 'session=; path=%s; expires=Thu, 01-Jan-1970 00:00:01 GMT' \ % _config.WWW_ROOT_DIR if wid == None: wid_n = 0 else: wid_n = jotools.toint(wid) if wid_n == 0: joheaders.redirect_header(req, _config.WWW_ROOT_DIR + u"/") else: joheaders.redirect_header(req, _config.WWW_ROOT_DIR + u"/word/edit?wid=%i" % wid_n) return "</html>"
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'
def passwdform(req): (uid, uname, editable) = jotools.get_login_user(req) if uid == None: joheaders.error_page(req, _(u'You must be logged in to do this')) return '\n' joheaders.page_header_navbar_level1(req, _(u'Change password'), uid, uname) jotools.write(req, u''' <form method="post" action="changepasswd"> <table> <tr><td>%s</td><td><input type="password" name="oldpw" /></td></tr> <tr><td>%s</td><td><input type="password" name="newpw" /></td></tr> </table> <input type="submit" value="%s" /> </form> ''' % (_(u'Old password'), _(u'New password'), _(u'Change password'))) joheaders.page_footer_plain(req) return '\n'
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'
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'
def login(req, wid = None): if req.method != 'POST': joheaders.error_page(req, _(u'Only POST requests are allowed')) return '\n' password = jotools.get_param(req, 'password', None) username = jotools.get_param(req, 'username', None) if username == None or password == None or not jotools.checkuname(username): joheaders.error_page(req, _(u"Missing or incorrect username or password")) return '\n' pwhash = sha.new((_config.PW_SALT + password).encode('UTF-8')).hexdigest() db = jodb.connect_private() results = db.query(("select uid, isadmin from appuser where uname = '%s' and pwhash = '%s' " + "and disabled = FALSE") % (username.encode('UTF-8'), pwhash)) if results.ntuples() == 0: joheaders.error_page(req, _(u"Incorrect username or password")) return '\n' (uid, isadmin) = results.getresult()[0] if isadmin == 'f' and _config.ONLY_ADMIN_LOGIN_ALLOWED: joheaders.error_page(req, _(u"Only administrator logins are allowed at the moment")) return '\n' # Generate session key sesssha = sha.new() sesssha.update(username) sesssha.update(pwhash) if hasattr(os, 'urandom'): # this is only available in Python >= 2.4 sesssha.update(os.urandom(15)) else: sesssha.update(`time.time()`) sesssha.update(`random.random()`) sesssha.update(`os.times()`) sesskey = sesssha.hexdigest() db.query(("update appuser set session_key = '%s', session_exp = CURRENT_TIMESTAMP + " + "interval '%i seconds' where uid = %i") % (sesskey, _config.SESSION_TIMEOUT, uid)) if _config.WWW_ROOT_DIR == '': cookiepath = '/' else: cookiepath = _config.WWW_ROOT_DIR req.headers_out['Set-Cookie'] = 'session=%s; path=%s' % (sesskey, cookiepath) if wid == None: wid_n = 0 else: wid_n = jotools.toint(wid) if wid_n != 0: joheaders.redirect_header(req, _config.WWW_ROOT_DIR + u"/word/edit?wid=%i" % wid_n) elif jotools.get_param(req, 'redir', None) != None: joheaders.redirect_header(req, _config.WWW_ROOT_DIR + jotools.get_param(req, 'redir', u'')) else: joheaders.redirect_header(req, _config.WWW_ROOT_DIR + u"/") return "</html>"
def addform(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' joheaders.page_header_navbar_level1(req, _(u'Add user'), uid, uname) jotools.write(req, u''' <form method="post" action="add"> <table> <tr><td>%s</td><td><input type="text" name="firstname" /></td></tr> <tr><td>%s</td><td><input type="text" name="lastname" /></td></tr> <tr><td>%s</td><td><input type="text" name="uname" /></td></tr> <tr><td>%s</td><td><input type="text" name="email" /></td></tr> <tr><td>%s</td><td><input type="text" name="passwd" /></td></tr> </table> <input type="submit" value="%s" /> </form> ''' % (_(u'First name'), _(u'Last name'), _(u'Username'), _(u'Email address'), _(u'Password'), _(u'Add user'))) joheaders.page_footer_plain(req) return '\n'
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'
def changepasswd(req): (uid, uname, editable) = jotools.get_login_user(req) if uid == None: joheaders.error_page(req, _(u'You must be logged in to do this')) return '\n' oldpw = jotools.get_param(req, 'oldpw', u'') newpw = jotools.get_param(req, 'newpw', u'') if oldpw == u'' or newpw == u'': joheaders.error_page(req, _(u'Required field is missing')) return '\n' oldpwhash = sha.new((_config.PW_SALT + oldpw).encode('UTF-8')).hexdigest() db = jodb.connect_private() results = db.query(("select uid from appuser where uid = %i and pwhash = '%s'") \ % (uid, oldpwhash)) if results.ntuples() == 0: joheaders.error_page(req, _(u"Incorrect old password")) return '\n' newpwhash = sha.new((_config.PW_SALT + newpw).encode('UTF-8')).hexdigest() db.query("update appuser set pwhash = '%s' where uid = %i" % (newpwhash, uid)) joheaders.ok_page(req, _(u'Password was changed succesfully')) return '\n'
def classlist(req): (uid, uname, editable) = jotools.get_login_user(req) joheaders.page_header_navbar_level1(req, u'Etsi sanalle taivutusluokka', uid, uname) word = jotools.get_param(req, 'word', u'') if not jotools.checkword(word): joheaders.error_page(req, u'Sanassa on kiellettyjä merkkejä') return '\n' # Sanaa ei annettu, joten näytetään pelkkä lomake if len(word) == 0: _display_form(req, 1, u'-', u'') joheaders.page_footer_plain(req) return '\n' classid = jotools.toint(jotools.get_param(req, 'class', u'0')) if classid == 1: classdatafile = VOIKKO_DATA + "/subst.aff" elif classid == 3: classdatafile = VOIKKO_DATA + "/verb.aff" elif classid == 0: joheaders.page_footer_plain(req) return '\n' else: joheaders.error_page(req, u'Sanaluokkaa ei ole olemassa') return '\n' grad_type = jotools.get_param(req, 'gclass', u'-') if not grad_type in [u'-', u'av1', u'av2', u'av3', u'av4', u'av5', u'av6']: joheaders.error_page(req, u'Taivutusluokkaa ei ole olemassa') return '\n' if grad_type == u'-': grad_type_s = u'' else: grad_type_s = u'-' + grad_type _display_form(req, classid, grad_type, word) word_classes = voikkoinfl.readInflectionTypes(classdatafile) for word_class in word_classes: if len(word_class.joukahainenClasses) == 0: continue infclass_main = word_class.joukahainenClasses[0] inflected_words = voikkoinfl.inflectWordWithType(word, word_class, infclass_main, grad_type) if inflected_words == []: continue previous_inflected = voikkoinfl.InflectedWord() inflist = [] inflected_words.append(voikkoinfl.InflectedWord()) jotools.write(req, '<hr /><h2 class="infclass">' + infclass_main + grad_type_s + '</h2>') if word_class.note != u'': jotools.write(req, u'<p>%s</p>\n' % word_class.note) jotools.write(req, u'<p>Kotus-luokka: %s</p>' % \ reduce(lambda x, y: u"%s, %s" % (x, y), word_class.kotusClasses)) jotools.write(req, u'<table class="border">\n') for inflected_word in inflected_words: if previous_inflected.formName != inflected_word.formName: if previous_inflected.formName != u"" and len(inflist) > 0: if previous_inflected.isCharacteristic: infs = reduce(lambda x, y: u"%s, %s" % (x, y), inflist) jotools.write(req, (u"<tr><td>%s</td><td>%s</td></tr>\n" % (previous_inflected.formName, infs))) inflist = [] previous_inflected = inflected_word if not inflected_word.inflectedWord in inflist: inflist.append(inflected_word.inflectedWord) jotools.write(req, u'</table>\n') joheaders.page_footer_plain(req) return '\n'
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'
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'
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'
def wlist(req): # The select clause qselect = "SELECT w.wid, w.word, c.name AS classname, w.class FROM word w, wordclass c" # Initial conditions conditions = ["w.class = c.classid"] # Word form conditions word = jotools.get_param(req, 'word', u'') if word != u'': if not jotools.checkre(word): joheaders.error_page(req, _(u'Word has forbidden characters in it')) return "\n" if jotools.get_param(req, 'wordre', u'') == u'on': compop = '~*' compword = jotools.expandre(word) elif jotools.get_param(req, 'wordsimplere', u'') == u'on': compop = 'ILIKE' compword = word else: compop = '=' compword = word # Use subquery if searching from alternative forms cond = "w.word %s '%s'" % (compop, jotools.escape_sql_string(compword)) if jotools.get_param(req, 'altforms', u'') == u'on': cond = cond + " OR w.wid IN (" + \ "SELECT rw.wid FROM related_word rw WHERE " + \ "replace(replace(rw.related_word, '=', ''), '|', '') %s '%s')" \ % (compop, jotools.escape_sql_string(compword)) conditions.append(cond) # Word class conditions wclass = jotools.toint(jotools.get_param(req, 'wordclass', u'')) if wclass > 0: conditions.append("w.class = %i" % wclass) # Text attribute conditions aid = jotools.toint(jotools.get_param(req, 'textaid', u'')) if aid != 0: value = jotools.get_param(req, 'textvalue', u'') if value == u'': cond = "w.wid NOT IN (SELECT wid FROM string_attribute_value WHERE aid = %i)" % aid else: cond = ("w.wid IN (SELECT wid FROM string_attribute_value " + "WHERE aid = %i AND value = '%s')") % (aid, jotools.escape_sql_string(value)) conditions.append(cond) # Flag conditions for field in req.form.list: if field.name.startswith('flagon'): aid = jotools.toint(field.name[6:]) if jotools.get_param(req, 'flagon%i' % aid, u'') == u'on': cond = "w.wid IN (SELECT wid FROM flag_attribute_value WHERE aid = %i)" % aid conditions.append(cond) if field.name.startswith('flagoff'): aid = jotools.toint(field.name[7:]) if jotools.get_param(req, 'flagoff%i' % aid, u'') == u'on': cond = "w.wid NOT IN (SELECT wid FROM flag_attribute_value WHERE aid = %i)" % aid conditions.append(cond) # FIXME: user should be able to select the order order = "ORDER BY w.word, c.name, w.wid" # Build the full select clause if len(conditions) == 0: select = qselect + " " + order elif len(conditions) == 1: select = qselect + " WHERE (" + conditions[0] + ") " + order else: select = qselect + " WHERE (" + conditions[0] for condition in conditions[1:]: select = select + ") AND (" + condition select = select + ") " + order outputtype = jotools.get_param(req, "listtype", u'html') jooutput.call(req, outputtype, select) return "\n"
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'