# -*- coding: utf-8 -*-
from flask import Blueprint, render_template,request
from flask_login import login_required
from application import app,db
from models.log import Log
from models.user import  User
from sqlalchemy import desc
from pypages import Paginator
log = Blueprint('log', __name__)

@log.route("/",methods=['GET', 'POST'])
@log.route("/page/<int:pnum>",methods=['GET', 'POST'])
@login_required
def index(pnum=1):
    page=pnum
    pageSize=50;
    page=int(page)
    loglist=Log.query.order_by(desc(Log.id)).paginate(page,pageSize,False)
    pagecount=Log.query.count()
    pages=Paginator(pagecount, per_page=pageSize, current=page, range_num=10)
    return render_template("tools/log.html",loglist=loglist.items,paginate=pages)

def addlog(content,flag=1):
    from flask_login import current_user
    from models.log import Log
    from application import db
    if current_user:
        uid = current_user.id
    else:
        uid = 0
        current_user = User.query.filter(User.id == uid).first()
    content="用户:  %s  " %current_user.cn_name + content
    logtarget=Log(uid,flag,content)
    db.session.add(logtarget)
    db.session.commit()
