Example #1
0
def Dlog():
    if request.method == "POST":
        form = LoginForm(request.values)
        if form.validate():
            b = Blogger.query.filter_by(email=form.email.data).first()
            if b and b.check_pwd(form.password.data):
                if b.role == "Faith":
                    if not current_app.config.get('LOGIN_EMAIL'):
                        set_login(b)
                        emit('receive',
                             trueReturn(data=url_for('admin.index')),
                             namespace='/chat',
                             broadcast=True)
                        return jsonify(falseReturn())
                    token = b.generate_token()
                    login_time = datetime.now()
                    send_mail(to=form.email.data,
                              subject='登录确认',
                              template='email/confirm_login',
                              user=b,
                              token=token,
                              login_time=login_time)
                    return jsonify(trueReturn(msg='E-mail sent successfully!'))
                else:
                    return jsonify(falseReturn(msg='Authentication failure'))
            else:
                return jsonify(
                    falseReturn(msg="Email doesn't exist or Password error!"))
        else:
            return jsonify(falseReturn(msg=get_errors_msg(form.errors)))

    if request.method == "GET":
        return render_template('Dlog.html')
Example #2
0
def comment():
    form = CommentForm(request.values)
    if form.validate():
        email = form.email.data
        guest = Guest.query.filter_by(guest_email=email).first()
        if guest:
            if guest.state and not guest.black:  # 检验成功,未被拉黑
                c = Comment()
                res = c.add_comment(form, guest.nickname, yes=True)
                if res is not None:
                    return jsonify(trueReturn(msg="留言成功!", data=res))
                if res is None:
                    return jsonify(falseReturn(msg='网络错误'))
            if guest.black:
                return jsonify(falseReturn(msg='此邮箱在小黑屋反省中...'))
        if not guest or not guest.state:
            c = Comment()
            res = c.add_comment(form)
            token = Guest.generate_token({"email": email, "id": c.id})
            send_mail(to=email,
                      subject="Dlog留言",
                      template='email/confirm_comment',
                      token=token)
            return jsonify(
                trueReturn(msg='此邮箱为第一次留言,为确认是否有效,请登录邮箱查看并确认,后续不再检验.'),
                data="")
    if not form.validate():
        msg = get_errors_msg(form.errors)
        return jsonify(falseReturn(msg=msg))
Example #3
0
def get_blog_detail():
    """
    # data = [dict(zip(result.keys(), result)) for result in results]
    # print(data)
    :return: ('','')
    """
    code = request.values.get('code')
    """博客内容"""
    bolg_title = db.session.query(
        Blog.body_html).filter_by(blog_id=code).first()
    if not bolg_title:
        return jsonify(falseReturn(msg="出错了~"))
    """评论内容"""
    comment = db.session.query(Comment.guest_name, Comment.comment,
                               Comment.create_time).filter(
                                   and_(Comment.blog_id == code,
                                        Comment.display == True)).all()
    data = []

    if comment:
        for result in comment:
            temp = {}
            for key, res in zip(result.keys(), result):
                if key == "create_time":
                    temp[key] = res.strftime("%Y年 %m月 %d日 %H时:%M分")
                else:
                    temp[key] = res
            data.append(temp)
    return jsonify(
        trueReturn(data={
            "bolg_title": bolg_title,
            "comment": data,
            "comment_count": len(data)
        }))
Example #4
0
 def publish(self):
     if request.method == 'GET':
         return self.render('admin/write_md.html')
     if request.method == 'POST':
         title = request.values.get("title")
         category = request.values.get("category")
         md = request.values.get('doc')
         html = request.values.get("html")
         if not title or not category or not md:
             msg = falseReturn(msg="是不是有什么没写就提交了啊")
             return self.render("admin/write_md.html",
                                msg=msg,
                                title=title,
                                body=md)
         data = Blog()
         data.title = title
         data.category = category
         data.body = md
         data.body_html = html
         error = data.add(data)
         if not error:
             msg = trueReturn(msg='发布成功~')
             return self.render("admin/write_md.html", msg=msg)
         else:
             msg = falseReturn(msg="出错了~")
             return self.render("admin/write_md.html",
                                msg=msg,
                                title=title,
                                body=md)
Example #5
0
def search():
    keyword = request.values.get('keyword')
    results = db.session.query(Blog.blog_id, Blog.title,
                               Blog.create_time).filter(
                                   Blog.title.like(keyword)).all()
    if not results:
        return jsonify(falseReturn(msg='^_^||没有找到你要的东西~'))
    data = []
    for result in results:
        temp = {}
        for key, res in zip(result.keys(), result):
            if key == "create_time":
                temp[key] = res.strftime("%Y年 %m月 %d日 %H时:%M分")
            else:
                temp[key] = res
        data.append(temp)
    return jsonify(trueReturn(data=data))
Example #6
0
def active_login():
    token = request.values.get('token')
    result = Blogger.decode_token(token)
    if result == "签名过期":
        print(result)
        return render_template('page_404.html')
    uid = result['uid']
    b = Blogger.query.filter_by(uid=uid).first()
    if b:
        set_login(b)
        emit('receive',
             trueReturn(data=url_for('admin.index')),
             namespace='/chat',
             broadcast=True)
        print("login success")
        return render_template('email/active_login.html', msg='登录成功!请查看')
    print("not eamil")
    return render_template('page_404.html')
Example #7
0
def get_blog_list():
    """

    :return: results:[(blog_id,title,create_time),(blog_id,title,create_time)]
    """
    code = request.values.get('code')
    results = db.session.query(
        Blog.blog_id, Blog.title,
        Blog.create_time).filter_by(category=code).order_by(
            Blog.create_time).all()
    if not results:
        return jsonify(falseReturn(msg='暂无文章'))
    data = []
    for result in results:
        temp = {}
        for key, res in zip(result.keys(), result):
            if key == "create_time":
                temp[key] = res.strftime("%Y年 %m月 %d日 %H时:%M分")
            else:
                temp[key] = res
        data.append(temp)
    return jsonify(trueReturn(data=data))
Example #8
0
def barrage():
    if request.method == "GET":
        id = request.args.get('id')
        key = "movie" + str(id)
        if redis.llen(key):                   # redis.llen返回列表的长度,如果没有则返回空值
            msgs = redis.lrange(key, 0, 2999)    # redis.lrange返回列表中的元素,0-2999
            res = {
                "code": 1,
                "danmaku": [json.loads(v) for v in msgs]    # json.loads()函数是将json格式数据转换为字典
            }
        else:
            res = {
                "code": 1,
                "danmaku": []
            }
    else:
        data = json.loads(request.get_data())
        msg = {
            "__v": 0,
            "author": data["author"],
            "time": data["time"],
            "text": data["text"],
            "color": data["color"],
            "type": data['type'],
            "ip": request.remote_addr,
            "_id": datetime.datetime.now().strftime("%Y%m%d%H%M%S") + uuid.uuid4().hex,
            "player": [
                data["player"]
            ]
        }
        res = {
            "code": 1,
            "data": msg
        }
        redis.lpush("movie" + str(data["player"]), json.dumps(msg))   # redis.lpush将一个或多个值插入到列表头部
    return jsonify(trueReturn(res,'200'))