def register(): from database.models.model import UserInfo form = RegisterForm() if form.validate_on_submit(): username = form.username.data email = form.email.data.lower() password = form.password.data ret = db.get_result({ "function": db.GET_UER_BY_NAME, "content": { "name": username } }) if ret["content"] is None: user = UserInfo(name=username, email=email, admin=False) user.set_password(password) ret = db.get_result({ "function": db.INSERT_USER, "content": user.to_dict() }) if ret["success"]: flash('注册%s!请登录' % ret["success"]) return redirect(url_for('auth.login')) else: flash('注册失败') return redirect(url_for('auth.register')) else: flash('用户名已存在!重新注册') return redirect(url_for('auth.register')) return render_template('auth/register.html', form=form)
def show_question(question_id): form = AnswerForm() ret = db.get_result({"function" : db.GET_QUESTION_BY_ID, "content" : {"quid" : question_id}}) question = ret["content"] # dict对象 ret = db.get_result({"function" : db.GET_ANSWER_BY_QUID, "content" : {"quid" : question_id}}) answers = ret["content"] # list对象 if form.validate_on_submit(): if current_user.is_authenticated: anscontent = form.answer.data quid = question_id uid = current_user.uid anstime = datetime.datetime.now() answer = AnswerInfo( anscontent=anscontent, anstime=anstime, uid=uid, # 回答者id quid=quid, unread=True ) db.get_result({"function" : db.INSERT_ANSWER, "content" : answer.to_dict()}) flash('Answer published!') return redirect(url_for('front.show_question', question_id=question_id)) else: return redirect(url_for('auth.login', next=request.full_path)) return render_template('front/qna.html', question=question, answers=answers, form=form)
def delete_user(name): db.get_result({ "function": db.DELETE_USER_BY_NAME, "content": { "name": name } }) flash('User deleted.', 'success') return redirect(url_for('.manage_users'))
def delete_questions(question_id): db.get_result({ "function": db.DELETE_QUESTION_BY_ID, "content": { "quid": question_id } }) flash('Question deleted.', 'success') return redirect_back()
def read_answer(ans_id): db.get_result({ "function": db.SET_ANSWER_READ, "content": { "ansid": ans_id } }) flash('Answer readed', 'success') return redirect(url_for('.manage_answers'))
def settings(): form = AdminForm() if form.validate_on_submit(): password = form.password.data new_admin = UserInfo(name=current_user.name, admin=True) new_admin.set_password(password) db.get_result({ "function": db.UPDATE_USER_PWD, "content": new_admin.to_dict() }) flash("change the password success", "success") return render_template('dashboard/settings.html', form=form)
def new_category(): form = CategoryForm() if form.validate_on_submit(): name = form.name.data newcate = Category(catname=name) db.get_result({ "function": db.INSERT_CATEGORY, "content": newcate.to_dict() }) flash("添加成功!", "success") return redirect(url_for('dashboard.manage_category')) return render_template('dashboard/new_category.html', form=form)
def initdb(drop): """Initialize the database.""" if drop: click.confirm( 'This operation will delete the database, do you want to continue?', abort=True) # 删除所有表并重新建表 db.get_result({ "function": db.DATABASE_INIT, "content": "", "dev": True }) click.echo('Drop tables.') click.echo('Initialized database.')
def init(username, email, password): from database.models.model import UserInfo click.echo('Initializing the database...') db.get_result({ "function": db.DATABASE_INIT, "content": "", "dev": True }) click.echo('Creating the temporary administrator account...') admin = UserInfo(name=username, email=email, admin=True) admin.set_password(password) db.get_result({"function": db.INSERT_USER, "content": admin.to_dict()}) click.echo('Done.')
def manage_answers(): page = request.args.get('page', 1, type=int) per_page = current_app.config['ANSWERS_PER_PAGE'] ret = db.get_result({ "function": db.GET_UNREAD_ANSWER, "content": { "number": per_page, "start": per_page * (page - 1) + 1 } }) if ret["content"]: page_record = Page(page, per_page) if page <= 1: page_record.has_prev = False else: page_record.has_prev = True if len(ret["content"]) < per_page: page_record.is_last = True else: # 啥都没有,说明最后了 page_record = Page(page, per_page, True) page_record.has_prev = True page_record.is_last = True unread_answers = ret['content'] return render_template('dashboard/manage_answers.html', answers=unread_answers, page=page_record)
def show_category(category_id, page): # number is the total amount of questions displayed catid = category_id '''need change''' # ret = db.get_result({"function" : db.GET_QUESTION_BY_CAT, "content" : # {"number": 5, "start": 0, "catid": category_id}}) per_page = current_app.config['QUESTIONS_PER_PAGE'] # 每页显示的数量 ret = db.get_result( {"function": db.GET_QUESTION_BY_CAT, "content": {"number": per_page, "start": per_page * (page - 1), "catid": category_id, "unread": False}}) if ret["content"]: page_record = Page(page, per_page) if page <= 1: page_record.has_prev = False else: page_record.has_prev = True if len(ret["content"]) < per_page: page_record.is_last = True else: page_record = Page(page, per_page, True) page_record.has_prev = True questions = ret["content"] # 一个list对象 return render_template('front/category.html', questions=questions, catid=catid, page=page_record)
def manage_questions(): page = request.args.get('page', 1, type=int) per_page = current_app.config['QUESTIONS_PER_PAGE'] # 每页显示的数量 ret = db.get_result({ "function": db.GET_RECOMMEND_QUESTION, "content": { "number": per_page, "start": per_page * (page - 1) } }) if ret["content"]: page_record = Page(page, per_page) if page <= 1: page_record.has_prev = False else: page_record.has_prev = True if len(ret["content"]) < per_page: page_record.is_last = True else: # 啥都没有,说明最后了 page_record = Page(page, per_page, True) page_record.has_prev = True page_record.is_last = True questions = ret["content"] return render_template('dashboard/manage_questions.html', questions=questions, page=page_record)
def login(): from database.models.model import UserInfo if current_user.is_authenticated: return redirect((url_for('front.home'))) form = LoginForm() if form.validate_on_submit(): username_input = form.username.data password_input = form.password.data remember = form.remember.data userinfo = db.get_result({ "function": db.GET_UER_BY_NAME, "content": { "name": username_input } })['content'] if userinfo: user = UserInfo(uid=userinfo['uid'], name=userinfo['name'], email=userinfo['email'], admin=userinfo['admin'], password_hash=userinfo['password_hash']) if user.validate_password(password_input): login_user(user, remember) flash('欢迎回来, %s!' % username_input) return redirect_back() else: flash('No account.', 'warning') return render_template('auth/login.html', form=form)
def newfollow(question_id): follow = Follow(uid=current_user.uid, quid=question_id) ret = db.get_result({"function": db.INSERT_FOLLOW, "content": follow.to_dict()}) if ret["success"]: flash('关注成功!', 'success') else: flash('您已关注', 'info') return redirect_back()
def delete_category(cat_name): category = db.get_result({ "function": db.GET_CAT_BY_NAME, "content": { "catname": cat_name } })['content'] if category['catid'] == 1: flash('You can not delete the default category.', 'warning') return redirect(url_for('blog.index')) db.get_result({ "function": db.DELETE_CATEGORY, "content": { "catid": category['catid'] } }) flash('Category deleted.', 'success') return redirect(url_for('.manage_category'))
def home(page): form = HomeForm() if current_user.is_authenticated: if form.validate_on_submit(): title = form.title.data description = form.description.data category = form.category.data userid = current_user.uid click.echo('%s, %s,user: %d' % (title, description, userid)) question = QuestionInfo( qucontent=description, qutitle=title, uid=userid, # 提问者id qutime=datetime.datetime.now(), catid=category, ansnumber=0, unread=True ) db.get_result({"function" : db.INSERT_QUESTION, "content" : question.to_dict()}) flash("提交问题成功!", "success") return redirect(url_for('front.home')) else: if form.validate_on_submit(): flash("请先登录!", "warning") per_page = current_app.config['QUESTIONS_PER_PAGE'] # 每页显示的数量 ret = db.get_result({"function" : db.GET_RECOMMEND_QUESTION, "content": { "number": per_page, "start" : per_page * (page - 1), "unread": False}}) if ret["content"]: page_record=Page(page, per_page) if page <= 1: page_record.has_prev = False else: page_record.has_prev = True if len(ret["content"]) < per_page: page_record.is_last = True else: page_record=Page(page, per_page, True) page_record.has_prev = True questions = ret["content"] return render_template('front/home.html', questions=questions, form=form, page=page_record)
def make_template_context(): categories = db.get_result({"function": db.GET_ALL_CATEGORY})["content"] # 用于显示边栏 unread_questions = len( db.get_result({ "function": db.GET_UNREAD_QUESTION, 'content': { "start": 0, "number": 99 } })["content"]) unread_answers = len( db.get_result({ "function": db.GET_UNREAD_ANSWER, "content": { "start": 0, "number": 99 } })["content"]) return dict(categories=categories, unread_answers=unread_answers, unread_questions=unread_questions)
def edit_category(cat_name): form = CategoryForm() form.name(render_kw={'placeholder': cat_name}) category = db.get_result({ "function": db.GET_CAT_BY_NAME, "content": { "catname": cat_name } })["content"] if category["catid"] == 1: flash('You can not edit the default category.', 'warning') return redirect(url_for('front.index')) if form.validate_on_submit(): '''需要数据库新功能''' db.get_result({ "function": db.UPDATE_CAT_NAME_BY_ID, "content": { "catid": category["catid"], "catname": form.name.data } }) return render_template('dashboard/edit_category.html', form=form)
def myquestion(user_id): page = request.args.get('page', 1, type=int) per_page = current_app.config['QUESTIONS_PER_PAGE'] ret = db.get_result({"function" : db.GET_QUESTION_BY_UID, "content" : { "uid" : user_id }}) if ret["content"]: page_record = Page(page, per_page) if page <= 1: page_record.has_prev = False else: page_record.has_prev = True if len(ret["content"]) < per_page: page_record.is_last = True else: # 啥都没有,说明最后了 page_record = Page(page, per_page, True) page_record.has_prev = True page_record.is_last = True questions = ret["content"] return render_template('front/myquestion.html', questions=questions, page=page_record)
def myfollow(user_id, page): page = page per_page = current_app.config['QUESTIONS_PER_PAGE'] ret = db.get_result({"function": db.GET_USER_FOLLOW, "content": { "name": current_user.name, "number": per_page, "start": per_page * (page - 1) }}) if ret["content"]: page_record = Page(page, per_page) if page <= 1: page_record.has_prev = False else: page_record.has_prev = True if len(ret["content"]) < per_page: page_record.is_last = True else: # 啥都没有,说明最后了 page_record = Page(page, per_page, True) page_record.has_prev = True page_record.is_last = True questions = ret["content"] return render_template('front/myfollow.html', questions=questions, page=page_record)
def myanswer(user_id): ret = db.get_result({"function": db.GET_ANSWER_BY_UID, "content": {"uid": user_id}}) answers = ret["content"] return render_template('front/myAnswer.html', answers=answers)
def delete_myanswer(ansid): db.get_result({"function": db.DELETE_ANSWER_BY_ID, "content": { "ansid": ansid }}) flash('Answer deleted!', 'success') return redirect(url_for('.myanswer', user_id=current_user.uid))