def login(): user = User() username = request.form.get('username').strip() password = request.form.get('password').strip() vcode = request.form.get('vcode').lower().strip() # 校验图像验证码是否正确,0000用来做测试 if vcode != session.get('vcode') and vcode != '0000': return 'vcode-error' else: # 实现登录功能 # 密码加密 password = hashlib.md5(password.encode()).hexdigest() result = user.find_by_username(username) if len(result) == 1 and result[0].password == password: session['islogin'] = '******' session['userid'] = result[0].userid session['username'] = username session['nickname'] = result[0].nickname session['role'] = result[0].role # 更新积分详情表 Credit().insert_detail(type='正常登录', target='0', credit=1) user.update_credit(1) # 将Cookie写入浏览器,持久化存储 response = make_response('login-pass') response.set_cookie('username', username, max_age=30 * 24 * 3600) response.set_cookie('password', password, max_age=30 * 24 * 3600) return response else: return 'login-fail'
def register(): user = User() username = str(request.form.get('username')).strip() nickname = str(request.form.get('nickname')).strip() password = str(request.form.get('password')).strip() ecode = str(request.form.get('ecode')).strip().lower() print("ecode=" + ecode) print("e_code=" + e_code) # 校验邮箱验证码 if ecode != e_code and ecode != '0000': return 'ecode-error' # 验证邮箱地址的正确性和密码的有效性 elif not re.match('.+@.+\..+', username) or len(password) < 6: return 'invalid' # 验证用户是否已被注册 elif len(user.find_by_username(username)) > 0: return 'user-repeated' else: # 进行注册 password = hashlib.md5(password.encode()).hexdigest() result = user.do_register(username, nickname, password) return 'reg-pass'
def get_my_collection(): collection = Collection() user = User() question = Question() username = request.args.get("username") idu = user.find_by_username(username)[0].id res = collection.find_by_idu(idu=idu) data = [] for item in res: col = {} idq = item.idq q = question.find_by_id(id=idq) title = q.title category = q.category description = q.description view_count = q.view_count answer_count = q.answer_count collect_count = q.collect_count gmt_create = q.gmt_create col['idq'] = idq col['title'] = title col['category'] = category col['description'] = description col['view_count'] = view_count col['collect_count'] = collect_count col['answer_count'] = answer_count col['gmt_create'] = gmt_create data.append(col) return jsonify({'data': data})
def home(): if session.get('islogin') is None: username = request.cookies.get('username') password = request.cookies.get('password') if username != None and password != None: user = User() result = user.find_by_username(username) if len(result) == 1 and result[0].password == password: session['islogin'] = '******' session['userid'] = result[0].userid session['username'] = username session['nickname'] = result[0].nickname session['role'] = result[0].role article = Article() result = article.find_limit_with_user(0, 10) # print(result, end='---------------\n') total = math.ceil(article.get_total_count() / 10) # 向上取整 # 以下是在首页添加侧边栏的内容,side-jinja2.html中使用到,但是这种方式有一个缺陷: # 需要在每个HTML页面中引用引用一下代码,重复率高,改良版在:side.html中 last, most, recommended = article.find_most_recommended() return render_template('index-base.html', result=result, page=1, total=total, last=last, most=most, recommended=recommended)
def count_my_answer(): answer = Answer() user = User() username = request.args.get('username') idu = user.find_by_username(username=username)[0].id data = {} c_answer = answer.count_by_idu(idu=idu) c_upvote = answer.count_upvote_by_idu(idu=idu) ratio = 0 if c_answer != 0: ratio = round(c_upvote/c_answer, 2) data['c_answer'] = c_answer data['c_upvote'] = c_upvote data['ratio'] = ratio avg_answer = answer.count_avg() avg_upvote = answer.count_avg_upvote() avg_ratio = round(avg_upvote/avg_answer, 2) data['avg_answer'] = avg_answer data['avg_upvote'] = avg_upvote data['avg_ratio'] = avg_ratio return jsonify({'data': data})
def login(): user = User() username = request.form.get('username').strip() password = request.form.get('password').strip() vcode = request.form.get('logincode').strip().lower() print(vcode, session.get('vcode')) if vcode != session.get('vcode') or vcode == 0000: return 'vcode-error' else: password = hashlib.md5(password.encode()).hexdigest() result = user.find_by_username(username) if len(result) == 1 and result[0].password == password: session['islogin'] = '******' session['userid'] = result[0].userid session['username'] = username session['nickname'] = result[0].nickname session['role'] = result[0].role # 更新积分表 Credit().insert_detail(type='正常登录', target='0', credit=1) user.update_credit(1) # 写入cookie response = make_response('login-pass') response.set_cookie('username', username, max_age=1 * 3600) response.set_cookie('password', password, max_age=1 * 3600) return response else: return 'login-fail'
def register(): user = User() username = request.form.get('username').strip() password = request.form.get('password').strip() code = request.form.get('code').strip() if code != session.get('ecode'): print(code, session.get('ecode')) return 'ecode-error' elif not re.match('.+@.+\..+', username) or len(password) < 5: return 'up-invalid' elif len(user.find_by_username(username)) > 0: return 'user-repeated' else: password = hashlib.md5(password.encode()).hexdigest() result = user.do_regisiter(username, password) print(result) session['islogin'] = '******' session['userid'] = result.userid session['username'] = username session['nickname'] = result.nickname session['role'] = result.role Credit().insert_detail(type='用户注册', target='0', credit=50) return 'reg-pass'
def register(): user = User() username = request.form.get('username').strip() password = request.form.get('password').strip() ecode = request.form.get('ecode').strip() # 校验邮箱验证码是否正确 if ecode != session.get('ecode'): return 'ecode-error' # 验证邮箱地址的正确性和密码的有效性, 数据库中username是邮箱格式 elif not re.match('.+@.+\..+', username) or len(password) < 5: return 'up-invalid' # 验证用户是否已经注册 elif len(user.find_by_username(username)) > 0: return 'user-repeated' else: # 实现注册功能 # 密码加密 password = hashlib.md5(password.encode()).hexdigest() result = user.do_register(username, password) session['islogin'] = '******' session['userid'] = result.userid session['username'] = username session['nickname'] = result.nickname session['role'] = result.role # 更新积分详情表 Credit().insert_detail(type='用户注册', target='0', credit=50) return 'reg-pass'
def add(): answer = Answer() user = User() question = Question() username = request.form.get('username').strip() print(username) print(user.find_by_username(username)) idu = user.find_by_username(username)[0].id # 根据用户名获取idu idq = request.form.get('idq').strip() content = request.form.get('content').strip() question.add_answer_count(idq) # 问题回答数+1 res = answer.add_answer(idq, idu, content) if res == 1: return 'add-success' else: return 'add-error'
def get_user_info(): user = User() username = request.args.get('username') res = user.find_by_username(username=username)[0] data = {} data['id'] = res.id data['nickname'] = res.nickname data['avatar_url'] = res.avatar_url start = res.gmt_create end = datetime.datetime.now() data['age'] = (end - start).days return jsonify({'data': data})
def add_history(): history = History() user = User() question = Question() # 获取请求中的参数 username = request.form.get("username") idq = request.form.get("idq") # 根据用户名获取id idu = user.find_by_username(username)[0].id # 更新表question和history question.add_view_count(idq=idq) history.add(idu=idu, idq=idq) return 'pass'
def new_upvote(): user = User() answer = Answer() username = request.args.get('username') ida = request.args.get('ida') idu2 = user.find_by_username(username)[0].id idu1 = answer.find_by_id(ida).idu notification = Notification() notification.newupvote(ida=ida, idu1=idu1, idu2=idu2) return 'success'
def get_notification_0(): notification = Notification() user = User() username = request.args.get("username") idu = user.find_by_username(username)[0].id data = [] res = notification.find_by_idu_0(idu=idu) for item in res: nt = {} nt['id'] = item.id nt['content'] = item.content nt['gmt_create'] = item.gmt_create data.append(nt) return jsonify({'data': data})
def add_upvote(): upvote = Upvote() user = User() answer = Answer() username = request.form.get("username") ida = request.form.get("ida") idu = user.find_by_username(username)[0].id res = upvote.find_by_iduida(idu=idu, ida=ida) if len(res) > 0: return 'repeated' else: upvote.add(idu=idu, ida=ida) answer.add_upvote(id=ida) return 'success'
def get_history(): history = History() user = User() question = Question() username = request.args.get("username") idu = user.find_by_username(username)[0].id res = history.find_by_idu(idu=idu) data = [] for item in res: col = {} idq = item.idq title = question.find_by_id(id=idq).title col['idq'] = idq col['title'] = title data.append(col) return jsonify({'data': data})
def add_collection(): collection = Collection() user = User() question = Question() # 获取请求中的参数 username = request.form.get("username") idq = request.form.get("idq") # 获取用户id idu = user.find_by_username(username)[0].id res = collection.find_by_iduidq(idu=idu, idq=idq) if len(res) > 0: return 'repeated' else: # 更新两张表question和collection question.add_collect_count(idq=idq) # 问题表收藏数+1 collection.add(idu=idu, idq=idq) return 'success'
def get_my_answer(): answer = Answer() user = User() question = Question() data = [] username = request.args.get('username') idu = user.find_by_username(username=username)[0].id res = answer.find_by_idu(idu=idu) for item in res: idq = item.idq title = question.find_by_id(id=idq).title a = {} a['id'] = item.id a['idq'] = idq a['title'] = title a['content'] = item.content a['upvote'] = item.upvote a['gmt_create'] = item.gmt_create data.append(a) return jsonify({'data': data})
def login(): user = User() username = request.form.get('username').strip() password = request.form.get('password').strip() # 管理员登录 if username == 'admin' and password == 'admin': return 'admin' # 用户登录 password = hashlib.md5(password.encode()).hexdigest() result = user.find_by_username(username) if len(result) == 0: return 'user-not-exist' if len(result) == 1 and result[0].password == password: nickname = result[0].nickname status = result[0].banned if status == '已封禁': return 'banned' else: islogin = True return nickname # 获取nickname存到localStorage中 else: return 'login-fail'
def get_recommend(): question = Question() user = User() answer = Answer() collection = Collection() history = History() username = request.args.get('username') idu = user.find_by_username(username=username)[0].id dict = {} dict['campus'] = 0 dict['focus'] = 0 dict['finance'] = 0 dict['technology'] = 0 dict['culture'] = 0 dict['education'] = 0 dict['depth'] = 0 dict['entertain'] = 0 dict['fashion'] = 0 dict['sports'] = 0 w1 = 1 # 浏览权重 w2 = 3 # 回答权重 w3 = 5 # 收藏权重 ''' 1.遍历该用户的所有浏览记录,为分类+1 2.遍历该用户的所有回答记录,为分类+3 3.遍历该用户的所有收藏记录,为分类+5 ''' # 遍历history,为分类赋权重 res1 = history.find_by_idu(idu=idu) for item in res1: idq = item.idq q = question.find_by_id(id=idq) cate = str(q.category) dict[cate] += w1 # 遍历answer res2 = answer.find_by_idu(idu=idu) for item in res2: idq = item.idq q = question.find_by_id(id=idq) cate = str(q.category) dict[cate] += w2 # 遍历collection res3 = collection.find_by_idu(idu=idu) for item in res3: idq = item.idq q = question.find_by_id(id=idq) cate = str(q.category) dict[cate] += w3 # 对权重进行排序 rec = sorted(dict.items(), key=lambda item:item[1], reverse=True) # 获取三个类别 cate_0 = rec[0][0] cate_1 = rec[1][0] cate_2 = rec[2][0] data = [] list = [] list.extend(question.find_by_category(category=cate_0)) list.extend(question.find_by_category(category=cate_1)) list.extend(question.find_by_category(category=cate_2)) for item in list: q = {} q['id'] = item.id q['title'] = item.title q['category'] = item.category q['description'] = item.description q['view_count'] = item.view_count q['answer_count'] = item.answer_count q['collect_count'] = item.collect_count q['gmt_create'] = item.gmt_create data.append(q) random.shuffle(data) return jsonify({'data': data})