def goto_post(db, userid, threadid, rel_idx): '''Given a relative index, move persistent pointer to post on deck accordingly.''' post_idx = done_posts(userid, threadid) + rel_idx if post_idx < 0: return "Earliest post." elif post_idx >= total_posts(threadid): set_finished(userid, threadid) return "Last post. This thread is finished!" thread = get_thread(threadid) post_id = thread[post_idx]['mongoid'] query( db, "UPDATE assignments SET done = done + %d, next_post = '%s' WHERE thread_id = '%s' AND user_id = %d" % (rel_idx, post_id, threadid, userid)) return None
def set_user(db): '''Attach user information to HTTP requests.''' g.user = None if 'user_id' in session: try: g.user = query( db, "SELECT id, username, first_name, last_name, superuser FROM users WHERE id = %s" % session['user_id']).next() except StopIteration: session.clear() g.user = query( db, "SELECT id, username, first_name, last_name, superuser FROM users WHERE id = %s" % session['user_id']).next()
def get_thread(db, threadid): '''Given a top-level post mongoid, return the corresponding thread.''' return query( db, "SELECT * FROM threads WHERE mongoid = '%s' UNION ALL SELECT * FROM threads WHERE comment_thread_id = '%s'" % (threadid, threadid), fetchall=True)
def assigned(db, thread_id, user_id): assns = query( db, "SELECT 1 FROM assignments WHERE thread_id = '%s' AND user_id = %d" % (thread_id, int(user_id)), fetchall=True) return bool(assns)
def admin(db): '''Logic for user admin page''' if request.method == 'POST': su = int(request.form.get('superuser') == 'on') query( db, "INSERT INTO users(username, first_name, last_name, email, pass_hash, superuser) VALUES ('%s','%s','%s','%s','%s','%s')" % (request.form['username'], request.form['first_name'], request.form['last_name'], request.form['email'], generate_password_hash(request.form['password']), su)) return redirect(url_for('admin')) users = query( db, 'select id, username, first_name, last_name, superuser from users', fetchall=True) return render_template('admin.html', users=users)
def userlist(): db_fiels = app.config.get('DB_FIELDS') all_field = db_fiels.get('USER_FIELD') result = dbutils.query('users', all_field) if result['code'] == 0: return render_template('userlist.html', msg=result['msg']) else: return render_template('userlsit.html', msg=result['errmsg'])
def annotate_thread(db, threadid): userid = g.user['id'] comments = None if request.method == 'POST': if 'next' in request.form.keys(): msg = goto_post(userid, threadid, 1) elif 'prev' in request.form.keys(): msg = goto_post(userid, threadid, -1) elif 'code' in request.form.keys(): code_value = request.form['codevalue'] comment = request.form['comment'] postid = request.form['postid'] code_type = "replymap" # Hard-coded, but should be generalized for other coder tasks existing = query( db, "SELECT code_value FROM codes WHERE post_id = '%s' AND user_id = %d" % (postid, userid), fetchall=True) user_codes = [ code for sublist in map(dict.values, existing) for code in sublist ] if code_value not in user_codes: query( db, "INSERT INTO codes(user_id, post_id, code_type, code_value, comment) VALUES ('%s', '%s', '%s', '%s', '%s')" % (userid, postid, code_type, code_value, comment)) msg = goto_post(userid, threadid, 1) if msg: flash(msg) next_post_id = query( db, "SELECT next_post FROM assignments WHERE thread_id = '%s' and user_id = %d" % (threadid, userid)).next().values()[0] comments = query( db, "SELECT code_value, comment FROM codes WHERE post_id = '%s' AND user_id = %d" % (next_post_id, userid), fetchall=True) posts, next_post = fetch_posts(threadid, next_post_id) return render_template('posts.html', threadid=threadid, posts=posts, next=next_post, comments=comments)
def userpage(db, username): '''Show assignment information for this user.''' user_id = g.user['id'] assignments = query(db, "SELECT * FROM assignments WHERE user_id = %d" % user_id, fetchall=True) return render_template('user.html', username=username, assignments=assignments)
def tables(db, tablename, limit='100'): '''Display route for database tables. For debugging purposes.''' table = query(db, "SELECT * FROM %s LIMIT %s" % (tablename, limit), fetchall=True) header = table[0].keys() return render_template('tables.html', tablename=tablename, table=table, header=header)
def assign(db): '''Logic for thread assigner''' threads = query(db, "SELECT mongoid, title FROM threads WHERE level = 1", fetchall=True) users = query(db, "SELECT id, first_name, last_name FROM users ORDER BY id", fetchall=True) if request.method == 'POST': for key in request.form.keys(): ids = eval(key) value = request.form[key] thread = ids['thread'] user = ids['user'] if value == 'on' and not assigned(thread, user): query( db, "INSERT INTO assignments(thread_id, user_id, next_post, finished) VALUES ('%s','%s','%s','%s')" % (thread, user, thread, 0)) return render_template('assignments.html', users=users, threads=threads)
def update(): db_fiels = app.config.get('DB_FIELDS') field = db_fiels.get('USER_FIELD') id = request.args.get('id') if request.method == 'POST': pass else: result = dbutils.query('users', field, id) return render_template('userlist.html', result=result['msg']) return "d"
def annotate(db): '''Logic for user annotation of forum posts.''' userid = g.user['id'] assigned = query( db, "SELECT a.thread_id, t.title, a.next_post FROM assignments a JOIN threads t ON thread_id = mongoid WHERE user_id = %d" % userid, fetchall=True) if request.method == 'POST': threadid = request.form['thread'] return redirect(url_for('annotate_thread', threadid=threadid)) return render_template('annotate.html', assigned=assigned)
def load_db(db): with open(application.config['THREADS']) as t: rows = DictReader(t) rowct = 0 for row in rows: row['body'] = row['body'].replace('"', '""') for key in row.keys(): if key not in [ 'created_at', 'updated_at', 'level', 'comment_count', 'author_id', 'finished', 'pinned', 'anonymous' ]: row[key] = '"%s"' % row[key] elif row[key] in ['NA', '0', 'False']: row[key] = str(0) if not row['pinned']: row['pinned'] = str(0) row['created_at'] = to_epoch(row['created_at']) row['updated_at'] = to_epoch(row['updated_at']) query( db, "INSERT INTO threads(%s) VALUES (%s)" % (','.join(row.keys()), ','.join(row.values()))) rowct += 1 print "Loaded %d forum posts to annotator." % rowct
def create(): db_fiels = app.config.get('DB_FIELDS') field = db_fiels.get('USER_FIELD') if request.method == 'POST': userdata = {k: v[0] for (k, v) in dict(request.form).items()} username = userdata['username'] userdata['createtime'] = datetime.datetime.now() #添加当前日期到表单字典 result = dbutils.query('users', field) # 查询数据库返回的结果 if result['code'] == 0: user = [x['username'] for x in result['msg']] if username in user: msg_exist = u'用户已存在' return render_template('create.html', msg_exist=msg_exist) else: field = ["%s='%s'" % (k, v) for k, v in userdata.items()] msg_status = dbutils.create('users', field) return render_template('create.html', msg_status=msg_status) return render_template('create.html')
def login(db): '''Log in as user.''' if g.user: return redirect(url_for('index')) if request.method == 'POST': user = None try: user = query( db, "SELECT id, pass_hash FROM users WHERE username = '******'" % request.form['username']).next() except StopIteration: pass if not user: flash("Invalid username.") elif not check_password_hash(user['pass_hash'], request.form['password']): flash("Invalid password.") else: session['user_id'] = user['id'] return redirect(url_for('index')) return render_template('login.html')
def set_finished(db, user_id, thread_id): query( db, "UPDATE assignments SET finished = 1 WHERE thread_id = '%s' and user_id = %d" % (thread_id, user_id))
def done_posts(db, user_id, thread_id): return query( db, "SELECT done FROM assignments WHERE user_id = %d AND thread_id = '%s'" % (user_id, thread_id)).next().values()[0]
def total_posts(db, thread_id): return query( db, "SELECT count(*) FROM threads WHERE comment_thread_id = '%s'" % thread_id).next().values()[0] + 1