from flask import request
import random
import re
from flask import current_app, jsonify
from flask import g
from flask import make_response
from flask import redirect
from flask import render_template
from flask import request
from flask import session
from flask import url_for
import time
from info import constants, db
from info import redis_store
from info.lib.yuntongxun.sms import CCP
from info.utils.captcha.captcha import captcha
from info.utils.image_storage import storage
from info.utils.response_code import RET
from info.modules.passport import passport_blu
from info.models import User, Category, News
from info.modules.profile import profile_blu
from info.utils.common import user_login_data
from datetime import datetime, timedelta

from . import admin_blu


@admin_blu.route("/login", methods=["GET", "POST"])
def admin_login():
    if request.method == "GET":
        # 去session　中取到指定的值
        user_id = session.get("user_id", None)
        is_admin = session.get("is_admin", False)
        if user_id and is_admin:
            return redirect(url_for("admin_index"))
        return render_template("admin/login.html")

    # 取到登陆的参数
    username = request.form.get("username")
    password = request.form.get("password")
    if not all([username, password]):
        return render_template("admin/login.html", errmsg="参数错误")

    try:
        user = User.query.filter(User.mobile == username).first()
    except Exception as e:
        current_app.logger.error(e)
        return render_template("admin/login.html", errmsg="数据错误")

    if not user:
        return render_template("admin/login.html", errmsg="用户名错误")

    if not user.check_password(password):
        return render_template("admin/login.html", errmsg="密码错误")

    if not user.is_admin:
        return render_template("admin/login.html", errmsg="用户不是管理员")

    session["user_id"] = user.id
    session["nick_name"] = user.nick_name
    session["mobile"] = user.mobile
    session["is_admin"] = True

    #  跳转到后台管理主页，暂未实现
    return redirect(url_for("admin.admin_index"))


@admin_blu.route("/index")
@user_login_data
def admin_index():
    user = g.user
    return render_template("admin/index.html", user=user.to_dict())


@admin_blu.before_request
def before_request():
    # 判断如果不是登陆页面的请求
    if not request.url.endswith(url_for("admin.admin_login")):
        user_id = session.get("user_id")
        is_admin = session.get("is_admin", False)

        if not user_id or not is_admin:
            # 判断当前是否有用户登陆，或者是否是管理员，如果不是，直接重定向到项目首页
            return redirect("/")


@admin_blu.route("/user_count")
def user_count():
    # 查询总人数
    total_count = 0
    try:
        total_count = User.query.filter(User.is_admin == False).count()
    except Exception as e:
        current_app.logger.error(e)

    # 查询月新增数
    mon_count = 0
    try:
        now = time.localtime()
        mon_begin = "%d-%02d-01" % (now.tm_year, now.tm_mon)
        mon_begin_date = datetime.strptime(mon_begin, "%Y-%m-%d")
        mon_count = User.query.filter(User.is_admin==False,
                                      User.create_time > mon_begin_date).count()
    except Exception as e:
        current_app.logger.error(e)

    day_count = 0
    try:
        day_begin = "%d-%02d-%02d" % (now.tm_year, now.tm_mon, now.tm_mday)
        day_begin_date = datetime.strptime(day_begin, "%Y-%m-%d")
        day_count = User.query.filter(User.is_admin==False,
                                      User.create_time >= day_begin_date).count()
    except Exception as e:
        current_app.logger.error(e)

    # 查询图表信息
    # 获取到当天00:00:00时间
    now_date = datetime.strptime(datetime.now().strftime("%Y-%m-%d"), "%Y-%m-%d")
    print(now_date)
    # 定义空数组，保存数据
    active_date = list()
    active_count = list()

    # 依次添加数据，再反转
    for i in range(0, 31):
        begin_date = now_date - timedelta(days=i)
        end_date = now_date - timedelta(days=(i - 1))
        active_date.append(begin_date.strftime("%Y-%m-%d"))
        count = 0
        try:
            count = User.query.filter(User.is_admin == False,
                                     User.last_login >= begin_date,
                                     User.last_login < end_date).count()
            print(count)
        except Exception as e:
            current_app.logger.error(e)
        active_count.append(count)

    active_date.reverse()
    active_count.reverse()

    data = {"total_count": total_count, "mon_count": mon_count, "day_count": day_count,
            "active_date": active_date, "active_count": active_count}

    return render_template("admin/user_count.html", data=data)


@admin_blu.route("/user_list")
def user_list():
    """获取用户列表"""
    # 获取参数
    page = request.args.get("p", 1)
    try:
        print(page)
        page = int(page)
    except Exception as e:
        current_app.logger.error(e)
        page = 1

    # 设置变量默认值
    users = []
    current_page = 1
    total_page = 1

    #查询数据
    try:
        paginate = User.query.filter(User.is_admin == False)\
            .order_by(User.last_login.desc())\
            .paginate(page, constants.ADMIN_NEWS_PAGE_MAX_COUNT, False)
        users = paginate.items
        current_page = paginate.page
        total_page = paginate.pages
    except Exception as e:
        current_app.logger.error(e)

    # 将模型列表转换成字典列表
    users_list = []
    for user in users:
        users_list.append(user.to_admin_dict())

    context = {
        "total_page": total_page,
        "current_page": current_page,
        "users": users_list
    }
    return render_template("admin/user_list.html", data=context)


@admin_blu.route("/news_review")
def news_review():
    """返回待审核新闻列表"""
    page = request.args.get("p", 1)
    keywords = request.args.get("keywords", "")
    try:
        page = int(page)
    except Exception as e:
        current_app.logger.error(e)
        page = 1

    news_list = list()
    current_page = 1
    total_page = 1

    try:
        filters = [News.status != 0]
        # 如果有关键词
        if keywords:
            # 添加关键字检索选项
            filters.append(News.title.contains(keywords))
        paginate = News.query.filter(*filters)\
            .order_by(News.create_time.desc())\
            .paginate(page, constants.ADMIN_NEWS_PAGE_MAX_COUNT, False)
        news_list = paginate.items
        current_page = paginate.page
        total_page = paginate.pages
    except Exception as e:
        current_app.error(e)

    news_dict_list = list()
    for news in news_list:
        news_dict_list.append(news.to_review_dict())

    data = {
        "total_page": total_page,
        "current_page": current_page,
        "news_list": news_dict_list
    }
    return render_template("admin/news_review.html", data=data)


@admin_blu.route("/news_review_detail", methods=["GET", "POST"])
def news_review_detail():
    """新闻审核"""
    # 获取新闻id
    if request.method == "GET":
        news_id = request.args.get("news_id")
        if not news_id:
            data = {
                "errmsg": "未查询到数据"
            }
            return render_template("admin/news_review_detail.html", data=data)
        # 通过id查询新闻
        news = None
        try:
            news = News.query.get(news_id)
        except Exception as e:
            current_app.logger.error(e)

        if not news:
            data = {
                "errmsg": "未查询到数据"
            }
            return render_template("admin/news_review_detail.html", data=data)

        # 返回数据
        data = {
            "news": news.to_dict()
        }
        return render_template("admin/news_review_detail.html", data=data)

    # 执行审核操作
    # 1. 获取参数
    news_id = request.json.get("news_id")
    action = request.json.get("action")

    #2. 判断参数
    if not all([news_id, action]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")
    if action not in ("accept", "reject"):
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")

    news = None
    try:
        #　3. 查询新闻
        news = News.query.get(news_id)
    except Exception as e:
        current_app.logger.error(e)

    if not news:
        return jsonify(errno=RET.NODATA, errmsg="未查询到数据")

    if action == "accept":
        news.status = 0
    else:
        # 拒绝通过，需要获取原因
        reason = request.json.get("reason")
        if not reason:
            return jsonify(errno=RET.PARAMERR, errmsg="参数错误")
        news.reason = reason
        news.status = -1
    # 保存数据库
    try:
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        return jsonify(errno=RET.DBERR, errmsg="保存数据失败")
    return jsonify(errno=RET.OK, errmsg="操作成功")


@admin_blu.route("/news_edit", methods=["GET", "POST"])
def news_edit():
    """返回新闻列表"""

    page = request.args.get("p", "1")
    print(page)
    a = re.match(r"^\d*", page)
    b = re.findall(r"""keywords=(\w*)""", page)
    print(b)
    page = a.group()
    if b != []:
        b = b[0]
        keywords = b
    else:
        keywords = None
        b = ""
    try:
        page = int(page)
    except Exception as e:
        current_app.logger.error(e)
        page = 1

    news_list = list()
    current_page = 1
    total_page = 1
    try:
        filters = list()
        # 如果有关键词
        if keywords:
            # 添加关键词的检索选项
            filters.append(News.title.contains(keywords))
        # 查询
        paginate = News.query.filter(*filters)\
            .order_by(News.create_time.desc())\
            .paginate(page, constants.ADMIN_NEWS_PAGE_MAX_COUNT, False)
        news_list = paginate.items
        current_page = paginate.page
        total_page = paginate.pages
    except Exception as e:
        current_app.logger.error(e)

    news_dict_list = list()
    for news in news_list:
        news_dict_list.append(news.to_basic_dict())

    data = {
        "total_page": total_page,
        "current_page": current_page,
        "new_list": news_dict_list,
        "last_input": b
    }
    if request.method == "GET":
        return render_template("admin/news_edit.html", data=data)
    # return jsonify(errno=RET.OK, errmsg="OK")
    return render_template("admin/news_edit.html", data=data)


@admin_blu.route("/news_edit_detail", methods=["GET", "POST"])
def news_edit_detail():
    """新闻编辑详情"""
    if request.method == "GET":
        # 获取参数
        news_id = request.args.get("news_id")
        if not news_id:
            data = {
                "errmsg": "没有找到新闻"
            }
            return render_template("admin/news_edit_detail.html", data=data)
        # 查询新闻
        news = None
        try:
            news = News.query.get(news_id)
        except Exception as e:
            current_app.logger.error(e)

        if not news:
            data = {
                "errmsg": "没有找到新闻"
            }
            return render_template("admin/news_edit_detail.html", data=data)

        categories = Category.query.all()
        categories_li = []
        for category in categories:
            c_dict = category.to_dict()
            c_dict["is_selected"] = False
            if category.id == News.category_id:
                c_dict["is_selected"] = True
            categories_li.append(c_dict)

        # 移除最新分类
        categories_li.pop(0)
        data = {
            "news": news.to_dict(),
            "categories": categories_li
        }
        return render_template("admin/news_edit_detail.html", data=data)

    news_id = request.form.get("news_id")
    title = request.form.get("title")
    digest= request.form.get("digest")
    content = request.form.get("content")
    index_image = request.form.get("index-image")
    categery_id = request.form.get("category_id")
    # 1.1　判断数据是否有值：
    if not all([title, digest, content, categery_id]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数有误")
    print(title, digest, content, categery_id)
    news = None
    try:
        news = News.query.get(news_id)
    except Exception as e:
        current_app.logger.error(e)
    if not news:
        return jsonify(errno=RET.NODATA, errmsg="未找到新闻数据")

    # 1.2 尝试读取图片
    if index_image:
        try:
            index_image = index_image.read()
        except Exception as e:
            return jsonify(errno=RET.PARAMERR, errmsg="参数有误")

        # 2. 将标题图片上传到七牛
        try:
            key = storage(index_image)
        except Exception as e:
            current_app.logger.error(e)
            return jsonify(errno=RET.THIRDERR, errmsg="上传图片错误")
        news.index_image_url = constants.QINIU_DOMIN_PREFIX + key

    # 3. 设置相关数据
    news.title = title
    news.digest = digest
    news.content = content
    news.category_id = categery_id

    # 4. 保存到数据库
    try:
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        return jsonify(errno=RET.DBERR, errmsg="保存数据失败")
    # 5. 返回结果

    return jsonify(errno=RET.OK, errmsg="编辑成功")


@admin_blu.route("/news_category")
def get_news_category():
    # 获取所有的分类数据
    categories = Category.query.all()
    # 定义列表保存分类数据
    categories_dicts = []

    for category in categories:
        # 获取字典
        cate_dict = category.to_dict()
        # 拼接内容
        categories_dicts.append(cate_dict)

    categories_dicts.pop(0)
    # 返回内容
    data = {
        "categories": categories_dicts
    }
    return render_template("admin/news_type.html", data=data)


@admin_blu.route("/add_category", methods=["POST"])
def add_category():
    """修改或者添加分类"""
    category_id = request.json.get("id")
    category_name = request.json.get("name")
    print(category_name)
    if not category_name:
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")
    # 判断是否有分类id
    if category_id:
        try:
            category = Category.query.get(category_id)
        except Exception as e:
            current_app.logger.error(e)
            return jsonify(errno=RET.DBERR, errmsg="查询数据失败")

        if not category:
            return jsonify(errno=RET.NODATA, errmsg="未查询到分类信息")

        category.name = category_name
        return jsonify(errno=RET.OK, errmsg="保存数据成功")

    else:
        # 如果没有分类id, 添加分类
        try:
            new_category = Category()
            new_category.id = category_id
            new_category.name = category_name
            db.session.add(new_category)
            db.session.commit()
        except Exception as e:
            current_app.logger.error(e)
            db.session.rollback()
            return jsonify(errno=RET.DBERR, errmsg="保存数据失败")
        return jsonify(errno=RET.OK, errmsg="保存数据成功")


[((1169, 1197), 'flask.request.form.get', 'request.form.get', (['"""username"""'], {}), "('username')\n", (1185, 1197), False, 'from flask import request\n'), ((1213, 1241), 'flask.request.form.get', 'request.form.get', (['"""password"""'], {}), "('password')\n", (1229, 1241), False, 'from flask import request\n'), ((4568, 4619), 'flask.render_template', 'render_template', (['"""admin/user_count.html"""'], {'data': 'data'}), "('admin/user_count.html', data=data)\n", (4583, 4619), False, 'from flask import render_template\n'), ((4709, 4733), 'flask.request.args.get', 'request.args.get', (['"""p"""', '(1)'], {}), "('p', 1)\n", (4725, 4733), False, 'from flask import request\n'), ((5551, 5604), 'flask.render_template', 'render_template', (['"""admin/user_list.html"""'], {'data': 'context'}), "('admin/user_list.html', data=context)\n", (5566, 5604), False, 'from flask import render_template\n'), ((5690, 5714), 'flask.request.args.get', 'request.args.get', (['"""p"""', '(1)'], {}), "('p', 1)\n", (5706, 5714), False, 'from flask import request\n'), ((5730, 5762), 'flask.request.args.get', 'request.args.get', (['"""keywords"""', '""""""'], {}), "('keywords', '')\n", (5746, 5762), False, 'from flask import request\n'), ((6688, 6740), 'flask.render_template', 'render_template', (['"""admin/news_review.html"""'], {'data': 'data'}), "('admin/news_review.html', data=data)\n", (6703, 6740), False, 'from flask import render_template\n'), ((7645, 7672), 'flask.request.json.get', 'request.json.get', (['"""news_id"""'], {}), "('news_id')\n", (7661, 7672), False, 'from flask import request\n'), ((7686, 7712), 'flask.request.json.get', 'request.json.get', (['"""action"""'], {}), "('action')\n", (7702, 7712), False, 'from flask import request\n'), ((8620, 8656), 'flask.jsonify', 'jsonify', ([], {'errno': 'RET.OK', 'errmsg': '"""操作成功"""'}), "(errno=RET.OK, errmsg='操作成功')\n", (8627, 8656), False, 'from flask import current_app, jsonify\n'), ((8761, 8787), 'flask.request.args.get', 'request.args.get', (['"""p"""', '"""1"""'], {}), "('p', '1')\n", (8777, 8787), False, 'from flask import request\n'), ((8812, 8835), 're.match', 're.match', (['"""^\\\\d*"""', 'page'], {}), "('^\\\\d*', page)\n", (8820, 8835), False, 'import re\n'), ((8844, 8879), 're.findall', 're.findall', (['"""keywords=(\\\\w*)"""', 'page'], {}), "('keywords=(\\\\w*)', page)\n", (8854, 8879), False, 'import re\n'), ((10123, 10173), 'flask.render_template', 'render_template', (['"""admin/news_edit.html"""'], {'data': 'data'}), "('admin/news_edit.html', data=data)\n", (10138, 10173), False, 'from flask import render_template\n'), ((11435, 11462), 'flask.request.form.get', 'request.form.get', (['"""news_id"""'], {}), "('news_id')\n", (11451, 11462), False, 'from flask import request\n'), ((11475, 11500), 'flask.request.form.get', 'request.form.get', (['"""title"""'], {}), "('title')\n", (11491, 11500), False, 'from flask import request\n'), ((11513, 11539), 'flask.request.form.get', 'request.form.get', (['"""digest"""'], {}), "('digest')\n", (11529, 11539), False, 'from flask import request\n'), ((11554, 11581), 'flask.request.form.get', 'request.form.get', (['"""content"""'], {}), "('content')\n", (11570, 11581), False, 'from flask import request\n'), ((11600, 11631), 'flask.request.form.get', 'request.form.get', (['"""index-image"""'], {}), "('index-image')\n", (11616, 11631), False, 'from flask import request\n'), ((11650, 11681), 'flask.request.form.get', 'request.form.get', (['"""category_id"""'], {}), "('category_id')\n", (11666, 11681), False, 'from flask import request\n'), ((12889, 12925), 'flask.jsonify', 'jsonify', ([], {'errno': 'RET.OK', 'errmsg': '"""编辑成功"""'}), "(errno=RET.OK, errmsg='编辑成功')\n", (12896, 12925), False, 'from flask import current_app, jsonify\n'), ((13021, 13041), 'info.models.Category.query.all', 'Category.query.all', ([], {}), '()\n', (13039, 13041), False, 'from info.models import User, Category, News\n'), ((13339, 13389), 'flask.render_template', 'render_template', (['"""admin/news_type.html"""'], {'data': 'data'}), "('admin/news_type.html', data=data)\n", (13354, 13389), False, 'from flask import render_template\n'), ((13501, 13523), 'flask.request.json.get', 'request.json.get', (['"""id"""'], {}), "('id')\n", (13517, 13523), False, 'from flask import request\n'), ((13544, 13568), 'flask.request.json.get', 'request.json.get', (['"""name"""'], {}), "('name')\n", (13560, 13568), False, 'from flask import request\n'), ((924, 952), 'flask.session.get', 'session.get', (['"""user_id"""', 'None'], {}), "('user_id', None)\n", (935, 952), False, 'from flask import session\n'), ((972, 1002), 'flask.session.get', 'session.get', (['"""is_admin"""', '(False)'], {}), "('is_admin', False)\n", (983, 1002), False, 'from flask import session\n'), ((1103, 1138), 'flask.render_template', 'render_template', (['"""admin/login.html"""'], {}), "('admin/login.html')\n", (1118, 1138), False, 'from flask import render_template\n'), ((1295, 1345), 'flask.render_template', 'render_template', (['"""admin/login.html"""'], {'errmsg': '"""参数错误"""'}), "('admin/login.html', errmsg='参数错误')\n", (1310, 1345), False, 'from flask import render_template\n'), ((1584, 1635), 'flask.render_template', 'render_template', (['"""admin/login.html"""'], {'errmsg': '"""用户名错误"""'}), "('admin/login.html', errmsg='用户名错误')\n", (1599, 1635), False, 'from flask import render_template\n'), ((1694, 1744), 'flask.render_template', 'render_template', (['"""admin/login.html"""'], {'errmsg': '"""密码错误"""'}), "('admin/login.html', errmsg='密码错误')\n", (1709, 1744), False, 'from flask import render_template\n'), ((1787, 1840), 'flask.render_template', 'render_template', (['"""admin/login.html"""'], {'errmsg': '"""用户不是管理员"""'}), "('admin/login.html', errmsg='用户不是管理员')\n", (1802, 1840), False, 'from flask import render_template\n'), ((2027, 2055), 'flask.url_for', 'url_for', (['"""admin.admin_index"""'], {}), "('admin.admin_index')\n", (2034, 2055), False, 'from flask import url_for\n'), ((2359, 2381), 'flask.session.get', 'session.get', (['"""user_id"""'], {}), "('user_id')\n", (2370, 2381), False, 'from flask import session\n'), ((2401, 2431), 'flask.session.get', 'session.get', (['"""is_admin"""', '(False)'], {}), "('is_admin', False)\n", (2412, 2431), False, 'from flask import session\n'), ((2840, 2856), 'time.localtime', 'time.localtime', ([], {}), '()\n', (2854, 2856), False, 'import time\n'), ((2943, 2983), 'datetime.datetime.strptime', 'datetime.strptime', (['mon_begin', '"""%Y-%m-%d"""'], {}), "(mon_begin, '%Y-%m-%d')\n", (2960, 2983), False, 'from datetime import datetime, timedelta\n'), ((3317, 3357), 'datetime.datetime.strptime', 'datetime.strptime', (['day_begin', '"""%Y-%m-%d"""'], {}), "(day_begin, '%Y-%m-%d')\n", (3334, 3357), False, 'from datetime import datetime, timedelta\n'), ((6912, 6939), 'flask.request.args.get', 'request.args.get', (['"""news_id"""'], {}), "('news_id')\n", (6928, 6939), False, 'from flask import request\n'), ((7543, 7602), 'flask.render_template', 'render_template', (['"""admin/news_review_detail.html"""'], {'data': 'data'}), "('admin/news_review_detail.html', data=data)\n", (7558, 7602), False, 'from flask import render_template\n'), ((7777, 7819), 'flask.jsonify', 'jsonify', ([], {'errno': 'RET.PARAMERR', 'errmsg': '"""参数错误"""'}), "(errno=RET.PARAMERR, errmsg='参数错误')\n", (7784, 7819), False, 'from flask import current_app, jsonify\n'), ((7878, 7920), 'flask.jsonify', 'jsonify', ([], {'errno': 'RET.PARAMERR', 'errmsg': '"""参数错误"""'}), "(errno=RET.PARAMERR, errmsg='参数错误')\n", (7885, 7920), False, 'from flask import current_app, jsonify\n'), ((7980, 8003), 'info.models.News.query.get', 'News.query.get', (['news_id'], {}), '(news_id)\n', (7994, 8003), False, 'from info.models import User, Category, News\n'), ((8100, 8142), 'flask.jsonify', 'jsonify', ([], {'errno': 'RET.NODATA', 'errmsg': '"""未查询到数据"""'}), "(errno=RET.NODATA, errmsg='未查询到数据')\n", (8107, 8142), False, 'from flask import current_app, jsonify\n'), ((8244, 8270), 'flask.request.json.get', 'request.json.get', (['"""reason"""'], {}), "('reason')\n", (8260, 8270), False, 'from flask import request\n'), ((8439, 8458), 'info.db.session.commit', 'db.session.commit', ([], {}), '()\n', (8456, 8458), False, 'from info import constants, db\n'), ((10013, 10063), 'flask.render_template', 'render_template', (['"""admin/news_edit.html"""'], {'data': 'data'}), "('admin/news_edit.html', data=data)\n", (10028, 10063), False, 'from flask import render_template\n'), ((10345, 10372), 'flask.request.args.get', 'request.args.get', (['"""news_id"""'], {}), "('news_id')\n", (10361, 10372), False, 'from flask import request\n'), ((10897, 10917), 'info.models.Category.query.all', 'Category.query.all', ([], {}), '()\n', (10915, 10917), False, 'from info.models import User, Category, News\n'), ((11362, 11419), 'flask.render_template', 'render_template', (['"""admin/news_edit_detail.html"""'], {'data': 'data'}), "('admin/news_edit_detail.html', data=data)\n", (11377, 11419), False, 'from flask import render_template\n'), ((11772, 11814), 'flask.jsonify', 'jsonify', ([], {'errno': 'RET.PARAMERR', 'errmsg': '"""参数有误"""'}), "(errno=RET.PARAMERR, errmsg='参数有误')\n", (11779, 11814), False, 'from flask import current_app, jsonify\n'), ((11902, 11925), 'info.models.News.query.get', 'News.query.get', (['news_id'], {}), '(news_id)\n', (11916, 11925), False, 'from info.models import User, Category, News\n'), ((12021, 12064), 'flask.jsonify', 'jsonify', ([], {'errno': 'RET.NODATA', 'errmsg': '"""未找到新闻数据"""'}), "(errno=RET.NODATA, errmsg='未找到新闻数据')\n", (12028, 12064), False, 'from flask import current_app, jsonify\n'), ((12693, 12712), 'info.db.session.commit', 'db.session.commit', ([], {}), '()\n', (12710, 12712), False, 'from info import constants, db\n'), ((13635, 13677), 'flask.jsonify', 'jsonify', ([], {'errno': 'RET.PARAMERR', 'errmsg': '"""参数错误"""'}), "(errno=RET.PARAMERR, errmsg='参数错误')\n", (13642, 13677), False, 'from flask import current_app, jsonify\n'), ((14058, 14096), 'flask.jsonify', 'jsonify', ([], {'errno': 'RET.OK', 'errmsg': '"""保存数据成功"""'}), "(errno=RET.OK, errmsg='保存数据成功')\n", (14065, 14096), False, 'from flask import current_app, jsonify\n'), ((14526, 14564), 'flask.jsonify', 'jsonify', ([], {'errno': 'RET.OK', 'errmsg': '"""保存数据成功"""'}), "(errno=RET.OK, errmsg='保存数据成功')\n", (14533, 14564), False, 'from flask import current_app, jsonify\n'), ((1457, 1484), 'flask.current_app.logger.error', 'current_app.logger.error', (['e'], {}), '(e)\n', (1481, 1484), False, 'from flask import current_app, jsonify\n'), ((1500, 1550), 'flask.render_template', 'render_template', (['"""admin/login.html"""'], {'errmsg': '"""数据错误"""'}), "('admin/login.html', errmsg='数据错误')\n", (1515, 1550), False, 'from flask import render_template\n'), ((2310, 2338), 'flask.url_for', 'url_for', (['"""admin.admin_login"""'], {}), "('admin.admin_login')\n", (2317, 2338), False, 'from flask import url_for\n'), ((2543, 2556), 'flask.redirect', 'redirect', (['"""/"""'], {}), "('/')\n", (2551, 2556), False, 'from flask import redirect\n'), ((2757, 2784), 'flask.current_app.logger.error', 'current_app.logger.error', (['e'], {}), '(e)\n', (2781, 2784), False, 'from flask import current_app, jsonify\n'), ((3160, 3187), 'flask.current_app.logger.error', 'current_app.logger.error', (['e'], {}), '(e)\n', (3184, 3187), False, 'from flask import current_app, jsonify\n'), ((3535, 3562), 'flask.current_app.logger.error', 'current_app.logger.error', (['e'], {}), '(e)\n', (3559, 3562), False, 'from flask import current_app, jsonify\n'), ((3846, 3863), 'datetime.timedelta', 'timedelta', ([], {'days': 'i'}), '(days=i)\n', (3855, 3863), False, 'from datetime import datetime, timedelta\n'), ((3894, 3915), 'datetime.timedelta', 'timedelta', ([], {'days': '(i - 1)'}), '(days=i - 1)\n', (3903, 3915), False, 'from datetime import datetime, timedelta\n'), ((4823, 4850), 'flask.current_app.logger.error', 'current_app.logger.error', (['e'], {}), '(e)\n', (4847, 4850), False, 'from flask import current_app, jsonify\n'), ((5278, 5305), 'flask.current_app.logger.error', 'current_app.logger.error', (['e'], {}), '(e)\n', (5302, 5305), False, 'from flask import current_app, jsonify\n'), ((5832, 5859), 'flask.current_app.logger.error', 'current_app.logger.error', (['e'], {}), '(e)\n', (5856, 5859), False, 'from flask import current_app, jsonify\n'), ((6419, 6439), 'flask.current_app.error', 'current_app.error', (['e'], {}), '(e)\n', (6436, 6439), False, 'from flask import current_app, jsonify\n'), ((7053, 7112), 'flask.render_template', 'render_template', (['"""admin/news_review_detail.html"""'], {'data': 'data'}), "('admin/news_review_detail.html', data=data)\n", (7068, 7112), False, 'from flask import render_template\n'), ((7184, 7207), 'info.models.News.query.get', 'News.query.get', (['news_id'], {}), '(news_id)\n', (7198, 7207), False, 'from info.models import User, Category, News\n'), ((7390, 7449), 'flask.render_template', 'render_template', (['"""admin/news_review_detail.html"""'], {'data': 'data'}), "('admin/news_review_detail.html', data=data)\n", (7405, 7449), False, 'from flask import render_template\n'), ((8039, 8066), 'flask.current_app.logger.error', 'current_app.logger.error', (['e'], {}), '(e)\n', (8063, 8066), False, 'from flask import current_app, jsonify\n'), ((8313, 8355), 'flask.jsonify', 'jsonify', ([], {'errno': 'RET.PARAMERR', 'errmsg': '"""参数错误"""'}), "(errno=RET.PARAMERR, errmsg='参数错误')\n", (8320, 8355), False, 'from flask import current_app, jsonify\n'), ((8494, 8521), 'flask.current_app.logger.error', 'current_app.logger.error', (['e'], {}), '(e)\n', (8518, 8521), False, 'from flask import current_app, jsonify\n'), ((8530, 8551), 'info.db.session.rollback', 'db.session.rollback', ([], {}), '()\n', (8549, 8551), False, 'from info import constants, db\n'), ((8567, 8608), 'flask.jsonify', 'jsonify', ([], {'errno': 'RET.DBERR', 'errmsg': '"""保存数据失败"""'}), "(errno=RET.DBERR, errmsg='保存数据失败')\n", (8574, 8608), False, 'from flask import current_app, jsonify\n'), ((9090, 9117), 'flask.current_app.logger.error', 'current_app.logger.error', (['e'], {}), '(e)\n', (9114, 9117), False, 'from flask import current_app, jsonify\n'), ((9678, 9705), 'flask.current_app.logger.error', 'current_app.logger.error', (['e'], {}), '(e)\n', (9702, 9705), False, 'from flask import current_app, jsonify\n'), ((10486, 10543), 'flask.render_template', 'render_template', (['"""admin/news_edit_detail.html"""'], {'data': 'data'}), "('admin/news_edit_detail.html', data=data)\n", (10501, 10543), False, 'from flask import render_template\n'), ((10611, 10634), 'info.models.News.query.get', 'News.query.get', (['news_id'], {}), '(news_id)\n', (10625, 10634), False, 'from info.models import User, Category, News\n'), ((10817, 10874), 'flask.render_template', 'render_template', (['"""admin/news_edit_detail.html"""'], {'data': 'data'}), "('admin/news_edit_detail.html', data=data)\n", (10832, 10874), False, 'from flask import render_template\n'), ((11961, 11988), 'flask.current_app.logger.error', 'current_app.logger.error', (['e'], {}), '(e)\n', (11985, 11988), False, 'from flask import current_app, jsonify\n'), ((12310, 12330), 'info.utils.image_storage.storage', 'storage', (['index_image'], {}), '(index_image)\n', (12317, 12330), False, 'from info.utils.image_storage import storage\n'), ((12748, 12775), 'flask.current_app.logger.error', 'current_app.logger.error', (['e'], {}), '(e)\n', (12772, 12775), False, 'from flask import current_app, jsonify\n'), ((12784, 12805), 'info.db.session.rollback', 'db.session.rollback', ([], {}), '()\n', (12803, 12805), False, 'from info import constants, db\n'), ((12821, 12862), 'flask.jsonify', 'jsonify', ([], {'errno': 'RET.DBERR', 'errmsg': '"""保存数据失败"""'}), "(errno=RET.DBERR, errmsg='保存数据失败')\n", (12828, 12862), False, 'from flask import current_app, jsonify\n'), ((13750, 13781), 'info.models.Category.query.get', 'Category.query.get', (['category_id'], {}), '(category_id)\n', (13768, 13781), False, 'from info.models import User, Category, News\n'), ((13959, 14003), 'flask.jsonify', 'jsonify', ([], {'errno': 'RET.NODATA', 'errmsg': '"""未查询到分类信息"""'}), "(errno=RET.NODATA, errmsg='未查询到分类信息')\n", (13966, 14003), False, 'from flask import current_app, jsonify\n'), ((14173, 14183), 'info.models.Category', 'Category', ([], {}), '()\n', (14181, 14183), False, 'from info.models import User, Category, News\n'), ((14284, 14312), 'info.db.session.add', 'db.session.add', (['new_category'], {}), '(new_category)\n', (14298, 14312), False, 'from info import constants, db\n'), ((14325, 14344), 'info.db.session.commit', 'db.session.commit', ([], {}), '()\n', (14342, 14344), False, 'from info import constants, db\n'), ((1064, 1086), 'flask.url_for', 'url_for', (['"""admin_index"""'], {}), "('admin_index')\n", (1071, 1086), False, 'from flask import url_for\n'), ((1371, 1413), 'info.models.User.query.filter', 'User.query.filter', (['(User.mobile == username)'], {}), '(User.mobile == username)\n', (1388, 1413), False, 'from info.models import User, Category, News\n'), ((2672, 2713), 'info.models.User.query.filter', 'User.query.filter', (['(User.is_admin == False)'], {}), '(User.is_admin == False)\n', (2689, 2713), False, 'from info.models import User, Category, News\n'), ((3004, 3080), 'info.models.User.query.filter', 'User.query.filter', (['(User.is_admin == False)', '(User.create_time > mon_begin_date)'], {}), '(User.is_admin == False, User.create_time > mon_begin_date)\n', (3021, 3080), False, 'from info.models import User, Category, News\n'), ((3378, 3455), 'info.models.User.query.filter', 'User.query.filter', (['(User.is_admin == False)', '(User.create_time >= day_begin_date)'], {}), '(User.is_admin == False, User.create_time >= day_begin_date)\n', (3395, 3455), False, 'from info.models import User, Category, News\n'), ((3632, 3646), 'datetime.datetime.now', 'datetime.now', ([], {}), '()\n', (3644, 3646), False, 'from datetime import datetime, timedelta\n'), ((4280, 4307), 'flask.current_app.logger.error', 'current_app.logger.error', (['e'], {}), '(e)\n', (4304, 4307), False, 'from flask import current_app, jsonify\n'), ((6077, 6106), 'info.models.News.title.contains', 'News.title.contains', (['keywords'], {}), '(keywords)\n', (6096, 6106), False, 'from info.models import User, Category, News\n'), ((7251, 7278), 'flask.current_app.logger.error', 'current_app.logger.error', (['e'], {}), '(e)\n', (7275, 7278), False, 'from flask import current_app, jsonify\n'), ((9323, 9352), 'info.models.News.title.contains', 'News.title.contains', (['keywords'], {}), '(keywords)\n', (9342, 9352), False, 'from info.models import User, Category, News\n'), ((10678, 10705), 'flask.current_app.logger.error', 'current_app.logger.error', (['e'], {}), '(e)\n', (10702, 10705), False, 'from flask import current_app, jsonify\n'), ((12211, 12253), 'flask.jsonify', 'jsonify', ([], {'errno': 'RET.PARAMERR', 'errmsg': '"""参数有误"""'}), "(errno=RET.PARAMERR, errmsg='参数有误')\n", (12218, 12253), False, 'from flask import current_app, jsonify\n'), ((12374, 12401), 'flask.current_app.logger.error', 'current_app.logger.error', (['e'], {}), '(e)\n', (12398, 12401), False, 'from flask import current_app, jsonify\n'), ((12421, 12465), 'flask.jsonify', 'jsonify', ([], {'errno': 'RET.THIRDERR', 'errmsg': '"""上传图片错误"""'}), "(errno=RET.THIRDERR, errmsg='上传图片错误')\n", (12428, 12465), False, 'from flask import current_app, jsonify\n'), ((13825, 13852), 'flask.current_app.logger.error', 'current_app.logger.error', (['e'], {}), '(e)\n', (13849, 13852), False, 'from flask import current_app, jsonify\n'), ((13872, 13913), 'flask.jsonify', 'jsonify', ([], {'errno': 'RET.DBERR', 'errmsg': '"""查询数据失败"""'}), "(errno=RET.DBERR, errmsg='查询数据失败')\n", (13879, 13913), False, 'from flask import current_app, jsonify\n'), ((14388, 14415), 'flask.current_app.logger.error', 'current_app.logger.error', (['e'], {}), '(e)\n', (14412, 14415), False, 'from flask import current_app, jsonify\n'), ((14428, 14449), 'info.db.session.rollback', 'db.session.rollback', ([], {}), '()\n', (14447, 14449), False, 'from info import constants, db\n'), ((14469, 14510), 'flask.jsonify', 'jsonify', ([], {'errno': 'RET.DBERR', 'errmsg': '"""保存数据失败"""'}), "(errno=RET.DBERR, errmsg='保存数据失败')\n", (14476, 14510), False, 'from flask import current_app, jsonify\n'), ((4029, 4134), 'info.models.User.query.filter', 'User.query.filter', (['(User.is_admin == False)', '(User.last_login >= begin_date)', '(User.last_login < end_date)'], {}), '(User.is_admin == False, User.last_login >= begin_date, \n    User.last_login < end_date)\n', (4046, 4134), False, 'from info.models import User, Category, News\n'), ((5042, 5064), 'info.models.User.last_login.desc', 'User.last_login.desc', ([], {}), '()\n', (5062, 5064), False, 'from info.models import User, Category, News\n'), ((6178, 6201), 'info.models.News.create_time.desc', 'News.create_time.desc', ([], {}), '()\n', (6199, 6201), False, 'from info.models import User, Category, News\n'), ((9437, 9460), 'info.models.News.create_time.desc', 'News.create_time.desc', ([], {}), '()\n', (9458, 9460), False, 'from info.models import User, Category, News\n'), ((4977, 5018), 'info.models.User.query.filter', 'User.query.filter', (['(User.is_admin == False)'], {}), '(User.is_admin == False)\n', (4994, 5018), False, 'from info.models import User, Category, News\n'), ((6127, 6154), 'info.models.News.query.filter', 'News.query.filter', (['*filters'], {}), '(*filters)\n', (6144, 6154), False, 'from info.models import User, Category, News\n'), ((9386, 9413), 'info.models.News.query.filter', 'News.query.filter', (['*filters'], {}), '(*filters)\n', (9403, 9413), False, 'from info.models import User, Category, News\n')]