# Path: dbtool/models.py
# class Sqllog(models.Model):
#     """
#     asset modle
#         create_time = models.DateTime(default=datetime.now)
#     """
#     STATUS = (
#         ('0', '已执行'),
#         ('1', '待执行'),
#         ('2', '作废'),
#     )
# 
# 
#     user_id = models.IntegerField(max_length=128, blank=True, null=True, verbose_name=u"user_id")
#     user_name = models.CharField(max_length=20, blank=True, null=True, verbose_name=u"user_name")
#     db_name = models.CharField(max_length=50, blank=True, null=True, verbose_name=u"db_name")
#     sqllog = models.TextField(max_length=2000, blank = True, null = True, verbose_name = u"sqllog")
#     create_time=models.DateTimeField(default=datetime.now,blank = True)
#     check_mod_rows = models.IntegerField(max_length=20, blank=True, null=True, verbose_name=u"check_mod_rows")
#     real_mod_rows = models.IntegerField(max_length=20, blank=True, null=True, verbose_name=u"real_mod_rows")
#     status = models.IntegerField(max_length=2, blank=True, null=True, verbose_name=u"status",choices=STATUS)
#     comments = models.CharField(max_length=20, blank=True, null=True, verbose_name=u"comments")
#     type = models.IntegerField(max_length=20, blank=True, null=True, verbose_name=u"type")
# 
# 
# 
#     def __unicode__(self):
#         return self.user_id
# 
# Path: dbtool/models.py
# class Dblist(models.Model):
#     """
#     asset modle
#         create_time = models.DateTime(default=datetime.now)
#     """
#     dbname=models.CharField(max_length=20, blank=True, null=True, verbose_name=u"DB_IP")
#     db_role = models.CharField(max_length=20, blank=True, null=True, verbose_name=u"db_role")
# 
# 
#     def __unicode__(self):
#         return self.dbname
# 
# Path: dbtool/forms.py
# class SqllogForm(forms.ModelForm):
# 
#     class Meta:
#         model = Sqllog
#         ordering = ['-create_time']
# 
#         fields = [
#             "user_id","user_name","db_name","sqllog","check_mod_rows","real_mod_rows","create_time","status","comments","type"
#         ]


from juser.user_api import *
from jperm.perm_api import get_group_user_perm
from dbtool.models import Sqllog
from dbtool.models import Dblist
from dbtool.forms import SqllogForm
from django.http import HttpResponse
from django.db import connection
from django.db.models import Q
from warnings import filterwarnings
import re
import json
import MySQLdb as mdb
import datetime
import re
import dbtool_api
import os
import ConfigParser
def sql_list(request):
    """ 显示日志 """
    header_title, path1 = u'审计', u'操作审计'
    date_seven_day = request.GET.get('start', '')
    date_now_str = request.GET.get('end', '')
    username_list = request.GET.getlist('username', [])
    dbname_list = request.GET.getlist('dbname', [])
    cmd = request.GET.get('cmd', '')
    status_list = request.GET.get('status', '')

    # if offset == 'online':
    #     keyword = request.GET.get('keyword', '')
    #     posts = Log.objects.filter(is_finished=False).order_by('-start_time')
    #     if keyword:
    #         posts = posts.filter(Q(user__icontains=keyword) | Q(host__icontains=keyword) |
    #                              Q(login_type__icontains=keyword))
    #
    # elif offset == 'exec':
    #     posts = ExecLog.objects.all().order_by('-id')
    #     keyword = request.GET.get('keyword', '')
    #     if keyword:
    #         posts = posts.filter(Q(user__icontains=keyword) | Q(host__icontains=keyword) | Q(cmd__icontains=keyword))
    # elif offset == 'file':
    #     posts = FileLog.objects.all().order_by('-id')
    #     keyword = request.GET.get('keyword', '')
    #     if keyword:
    #         posts = posts.filter(
    #             Q(user__icontains=keyword) | Q(host__icontains=keyword) | Q(filename__icontains=keyword))
    # else:
    # posts = Sqllog.objects.filter(status=0).order_by('create_time')

# Path: dbtool/models.py
# class Sqllog(models.Model):
#     """
#     asset modle
#         create_time = models.DateTime(default=datetime.now)
#     """
#     STATUS = (
#         ('0', '已执行'),
#         ('1', '待执行'),
#         ('2', '作废'),
#     )
# 
# 
#     user_id = models.IntegerField(max_length=128, blank=True, null=True, verbose_name=u"user_id")
#     user_name = models.CharField(max_length=20, blank=True, null=True, verbose_name=u"user_name")
#     db_name = models.CharField(max_length=50, blank=True, null=True, verbose_name=u"db_name")
#     sqllog = models.TextField(max_length=2000, blank = True, null = True, verbose_name = u"sqllog")
#     create_time=models.DateTimeField(default=datetime.now,blank = True)
#     check_mod_rows = models.IntegerField(max_length=20, blank=True, null=True, verbose_name=u"check_mod_rows")
#     real_mod_rows = models.IntegerField(max_length=20, blank=True, null=True, verbose_name=u"real_mod_rows")
#     status = models.IntegerField(max_length=2, blank=True, null=True, verbose_name=u"status",choices=STATUS)
#     comments = models.CharField(max_length=20, blank=True, null=True, verbose_name=u"comments")
#     type = models.IntegerField(max_length=20, blank=True, null=True, verbose_name=u"type")
# 
# 
# 
#     def __unicode__(self):
#         return self.user_id
# 
# Path: dbtool/models.py
# class Dblist(models.Model):
#     """
#     asset modle
#         create_time = models.DateTime(default=datetime.now)
#     """
#     dbname=models.CharField(max_length=20, blank=True, null=True, verbose_name=u"DB_IP")
#     db_role = models.CharField(max_length=20, blank=True, null=True, verbose_name=u"db_role")
# 
# 
#     def __unicode__(self):
#         return self.dbname
# 
# Path: dbtool/forms.py
# class SqllogForm(forms.ModelForm):
# 
#     class Meta:
#         model = Sqllog
#         ordering = ['-create_time']
# 
#         fields = [
#             "user_id","user_name","db_name","sqllog","check_mod_rows","real_mod_rows","create_time","status","comments","type"
#         ]


# Path: dbtool/views.py
from juser.user_api import *
from jperm.perm_api import get_group_user_perm
from dbtool.models import Sqllog
from dbtool.models import Dblist
from dbtool.forms import SqllogForm
from django.http import HttpResponse
from django.db import connection
from django.db.models import Q
from warnings import filterwarnings
import re
import json
import MySQLdb as mdb
import datetime
import re
import dbtool_api
import os
import ConfigParser

def sql_list(request):
    """ 显示日志 """
    header_title, path1 = u'审计', u'操作审计'
    date_seven_day = request.GET.get('start', '')
    date_now_str = request.GET.get('end', '')
    username_list = request.GET.getlist('username', [])
    dbname_list = request.GET.getlist('dbname', [])
    cmd = request.GET.get('cmd', '')
    status_list = request.GET.get('status', '')

    # if offset == 'online':
    #     keyword = request.GET.get('keyword', '')
    #     posts = Log.objects.filter(is_finished=False).order_by('-start_time')
    #     if keyword:
    #         posts = posts.filter(Q(user__icontains=keyword) | Q(host__icontains=keyword) |
    #                              Q(login_type__icontains=keyword))
    #
    # elif offset == 'exec':
    #     posts = ExecLog.objects.all().order_by('-id')
    #     keyword = request.GET.get('keyword', '')
    #     if keyword:
    #         posts = posts.filter(Q(user__icontains=keyword) | Q(host__icontains=keyword) | Q(cmd__icontains=keyword))
    # elif offset == 'file':
    #     posts = FileLog.objects.all().order_by('-id')
    #     keyword = request.GET.get('keyword', '')
    #     if keyword:
    #         posts = posts.filter(
    #             Q(user__icontains=keyword) | Q(host__icontains=keyword) | Q(filename__icontains=keyword))
    # else:
    # posts = Sqllog.objects.filter(status=0).order_by('create_time')

    posts = Sqllog.objects.order_by('-create_time')