def get(self): args = parser.parse_args() token = args.get("token") # 获取对应用户的缓存,如果存在则可以激活,如果不存在则说明已经超过5分钟 userid = cache.get(token) # 如果有缓存,则激活 if userid: # 修改token对应用户的激活状态 users = User.query.filter_by(user_token=token) if users.count() > 0: user = users.first() user.is_active = True # 将激活状态改为True(已激活) db.session.commit() # 删除缓存cache cache.delete(token) return {"msg": "success,激活成功!"} else: return {"msg": "fail,用户不存在,无法激活!"} # 如果没有缓存,则不可以激活 else: return {"msg": "fail,已过期,激活失败!"}
def info(id): if request.method == 'GET': print(datetime.datetime.now()) if id == 0: art = Article.query.paginate(1, 1, False) else: num = Article.query.filter(Article.id <= id, Article.ispublic == True).all() page = len(num) art = Article.query art = art.filter(Article.ispublic == True) art = art.paginate(page, 1, False) ip = request.base_url data = datetime.datetime.now() artid = Article.query.get(art.items[0].id) if not cache.get(ip): cache.set(ip, data, timeout=60 * 60) artid.read_num += 1 db.session.commit() cate_name = Category.query.get(artid.categoryid).name data = artnav() comms = comment(artid.id) return render_template('blog/info.html', data=data, art=art, cate_name=cate_name, comms=comms) elif request.method == "POST": ip = request.base_url ip = ip + 'praise' data = datetime.datetime.now() artid = Article.query.get(id) if not cache.get(ip): cache.set(ip, data, timeout=60 * 60 * 24) artid.praise_num += 1 db.session.commit() print(artid.praise_num) return jsonify({'pra_num': artid.praise_num})
def info(id, per_page=1): # 文章部分 page = Article.query.filter(Article.id.__le__(id)).count() article = Article.query.paginate(page, per_page, error_out=False) captcha = captchas() user_ip = request.remote_addr cache_ip = cache.get(user_ip) cache_aid = cache.get(int(id)) if not cache_ip and not cache_aid: article.items[0].readersum += 1 cache.set(user_ip, user_ip, timeout=10) cache.set(cache_aid, cache_aid, timeout=10) db.session.commit() data = { "article": article, 'captcha': captcha, } data.update(get_column()) data.update(get_type()) return render_template('blog/info.html', **data)
def user_register(): if request.method == 'POST': phone = request.form.get('phone') email = request.form.get('email') password = request.form.get('password') username = request.form.get('username') vcode = request.form.get('vcode') # 验证手机号是否存在 if phone: active_user = list(User.query.filter_by(phone=phone)) if active_user: return jsonify({'code': 201, 'msg': '手机号已存在'}) # 检测验证码是否正确 cache_vcode = cache.get('vcode') if cache_vcode: cache_vcode = cache_vcode.lower() if vcode: vcode = vcode.lower() if vcode == cache_vcode: user = User() user.phone = phone user.username = username user.password = password user.email = email try: db.session.add(user) db.session.commit() user = User.query.filter_by(phone=phone).first() session_data = {'user_id': user.id, 'user_name': user.username} session['user'] = session_data return jsonify({'code': 200, 'msg': '注册成功'}) except: db.session.rollback() db.session.flush() return jsonify({'code': 201, 'msg': '注册失败'}) else: return jsonify({'code': 203, 'msg': '验证码错误'}) if request.method == 'GET': vcode = make_vcode() return render_template('shudong/register.html', image=vcode)
def addcomment(): ip = request.base_url ip = ip + 'addcomm' data = datetime.datetime.now() if not cache.get(ip): cache.set(ip, data, timeout=60 * 5) else: return jsonify({'code': 0}) if request.method == 'GET': artval = request.args.get('artval') art_id = request.args.get('art_id') com = add_com( artval, 0, art_id, ) com = { 'id': com.id, 'info': com.info, 'name': com.name, 'date': com.date, } return jsonify({'com': com, 'code': 1}) if request.method == 'POST': artval = request.form.get('artval') fa_id = request.form.get('userid') art_id = request.form.get('art_id') print(fa_id, artval, art_id) toname = Comments.query.get(fa_id).name com = add_com(artval, fa_id, art_id, toname) com = { 'id': com.id, 'info': com.info, 'name': com.name, 'toname': com.toname, 'fa_id': fa_id, 'date': com.date, } return jsonify({'com': com, 'code': 1})
def register(): if request.method == 'GET': return render_template('register.html') else: telephone = request.form.get('telephone') code = request.form.get('code') username = request.form.get('username') password = request.form.get('password') password1 = request.form.get('password2') # 验证表单 if not all([telephone, username, password, password1]): flash('参数不足') return redirect(url_for('blue_account.register')) else: user = User.query.filter(User.telephone == telephone).first() if user: flash('该号码已被注册过') return redirect(url_for('blue_account.register')) elif password != password1: flash('两次密码不相等,请核对后输入') return redirect(url_for('blue_account.register')) else: # 缓存中获取验证码 cache_code = cache.get(telephone) print(type(code)) print(type(cache_code)) # 如果用户输入的验证码与缓存中的验证码不相等,返回验证失败给前端 if code != cache_code: return '验证失败' user = User(telephone=telephone, username=username, password=password) # 添加数据到数据库 db.session.add(user) db.session.commit() return redirect(url_for('blue_account.login'))
def info(id=1): try: id = int(id) except: id = 1 if request.method == "GET": user = User.query.get(1) types = Articletype.query.all() # 文章类型 paihang = Article.query.filter().order_by(desc("readnum")).limit( 8) # 所有文章按点击排行,也就是阅读量排行 biaoqian = Articlelabel.query.filter().limit(8) # 标签只取8个 article = Article.query.get(id) article.readnum += 1 comts = Comment.query.filter(Comment.articleid == id) comments = [] for i in comts: comments.append({ "name": i.name, "msg": i.msg, "time": i.time, }) list = [] lanmu = "" if article.type: # 如果文章类型存在 lanmu = Articletype.query.get(article.type).title # 则写道lanmu中 al = Alabel.query.filter(Alabel.articleid == article.id) # 在三方表中查找标签集 for j in al: # 遍历查找到的标签集 lab = Articlelabel.query.get(j.labelid) # 把标签集里的标签id放入 if lab: # 如果标签存在 list.append(lab.labelname) data = { "user": user, "types": types, "paihang": paihang, "biaoqian": biaoqian, "comments": comments, "article": { "articleid": article.id, "lanmu": lanmu, "labels": list, "article": article, }, } db.session.commit() return render_template('blog/info.html', **data) elif request.method == "POST": zanid = request.form.get("zan") username = request.form.get("username") key = request.form.get("key") saytext = request.form.get("saytext") if key: if not (key.lower() == (cache.get(request.remote_addr + "vcode")).lower()): return "验证码错误" elif key == "": return "验证码错误" if username and key and saytext: if key.lower() == (cache.get(request.remote_addr + "vcode")).lower(): comment = Comment() comment.articleid = id comment.name = username comment.msg = saytext comment.time = datetime.datetime.now() db.session.add(comment) db.session.commit() return redirect("/info/%s" % id) else: return "验证码错误" try: if zanid: zanid = int(zanid) except: zanid = None if zanid and not (username or key or saytext): if not cache.get(request.remote_addr + "zan"): cache.set(request.remote_addr + "zan", []) ca = cache.get(request.remote_addr + "zan") if id not in ca: article = Article.query.get(zanid) article.zan += 1 db.session.commit() ca.append(id) cache.set(request.remote_addr + "zan", ca, timeout=60 * 60 * 24) # 设置一个ip一天只能赞一篇文章一次 else: return "一天只能赞一次哦" return "ok"
def get_captcha(): vcode = make_vcode() print(cache.get('vcode')) return jsonify({'vcode': vcode})
def info(id): ''' 文章详细内容 :return: ''' if request.method == "GET": article = Article.query.get(id) # print(article.visit) if article.visit: article.visit += 1 else: article.visit = 1 db.session.commit() # 获取上一篇和下一篇的内容 if id != 1: prvart = Article.query.filter(Article.id.__lt__(id)).first() else: prvart = None nextart = Article.query.filter(Article.id.__gt__(id)).first() # 查询所有的文章 arts = Article.query.all() # 查看自己分类 types = AriticleType.query.all() commands = Commend.query.filter( Commend.articleid == article.id).order_by(desc("time")) tags = article.tag.split("、") typeclass = [] tmp = article.ariticletype flags = 1 while flags: typeclass.append(tmp.name) if tmp.father_type: print(tmp.father_type) tmp = AriticleType.query.get(tmp.father_type) else: flags = 0 print(typeclass) data = { "prvart": prvart, "nextart": nextart, "article": article, "typeclass": typeclass, "tags": tags, "arts": arts, "commands": list(commands), "types": types, } return render_template("blog/info.html", **data) elif request.method == "PUT": ''' 修改点赞量。 ''' if cache.get(request.remote_addr) != id: try: id = request.form.get("id") cache.set(request.remote_addr, id, 5) art = Article.query.get(id) art.num += 1 db.session.commit() return jsonify({"code": 1, "art_num": art.num}) except: return jsonify({"code": 0}) else: cache.set(request.remote_addr, id, 5) return jsonify({"code": 2}) elif request.method == "POST": try: id = request.cookies.get("user") or "1" nameip = request.remote_addr artid = request.form.get("artid") name = request.form.get("name") or nameip info = request.form.get("info") user = User() user.name = name user.icon = "http://img4.imgtn.bdimg.com/it/u=2641899507,2178808967&fm=26&gp=0.jpg" user.time = datetime.datetime.now() db.session.add(user) db.session.commit() commend = Commend() # 反向插入id必须找到对方的值。 commend.comment_user = user commend.articleid = artid commend.info = info commend.time = datetime.datetime.now() db.session.add(commend) db.session.commit() return jsonify({"code": 1}) except: return jsonify({"code": 0})