#!/usr/bin/env python3  
# -*- coding: utf-8 -*-  
"""
@Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
@AUTHOR: ASJRD018
@NAME: Ansjer
@software: PyCharm
@DATE: 2018/6/22 11:42
@Version: python3.6
@MODIFY DECORD:ansjer dev
@file: LogAccess.py
@Contact: chanjunkai@163.com
"""
from Service.ModelService import ModelService
from Object.mongodb import mongodb
from django.views.decorators.csrf import csrf_exempt
from Object.ResponseObject import ResponseObject
from Object.TokenObject import TokenObject


@csrf_exempt
def LogAccess(request):
    if request.method == 'GET':
        request.encoding = 'utf-8'
        token = request.GET.get('token', None)
        page = request.GET.get('page', None)
        line = request.GET.get('line', None)
        # order = request.GET.get('order', None)
        # 开始时间
        starttime = request.GET.get('starttime', None)
        # 结束时间
        endtime = request.GET.get('endtime', None)
        response = ResponseObject()
        if token is not None:
            tko = TokenObject(token)
            response.lang = tko.lang
            if tko.code == 0:
                userID = tko.userID
                if userID is not None:
                    if page is not None and line is not None:
                        own_per = ModelService.check_perm(userID=userID, permID=30)
                        if own_per is True:
                            query = {}
                            if starttime is not None and starttime != '' and endtime is not None and endtime != '':
                                query['time'] = {'$gte': int(starttime), '$lte': int(endtime)}
                            elif starttime is not None and starttime != '':
                                query['time'] = {'$gte': int(starttime)}
                            elif endtime is not None and endtime != '':
                                query['time'] = {'$lte': int(endtime)}
                            col = "log_access"
                            mdb = mongodb()
                            qs = mdb.findAll(col=col, page=int(page), line=int(line), query=query)
                            return response.json(0, qs)
                        else:
                            return response.json(404)
                    else:
                        return response.json(444,'page,line')
                else:
                    return response.json(309)
            else:
                return response.json(tko.code)
        else:
            return response.json(309)