# 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
    with con:
        cur = con.cursor()
        cur.execute(cmd)
        rows = cur.fetchall()
        desc = cur.description
    mylist=[]
    for i in rows:
        mydisc={}
        for num in range(0, len(i)):
            #values=str(i[num]).decode("ascii").encode("utf-8")
            values = str(i[num])
            mydisc[desc[num][0]] = values
            # print chardet.detect(str(i[num]))
            #print chardet.detect(values)
        mylist.append(mydisc)
    myjson = {}
    myjson['total'] = 2800
    myjson['rows'] = mylist
    return HttpResponse(json.dumps(myjson), content_type="application/json")


@require_role(role='user')
def dbtool_dblistjson(request):
    db_list=[{"id": '请选择数据库', "text": "请选择数据库", "icon": "database.ico", "selected": "true"}]
    defend_attack(request)
    mylist=dbtool_api.db_list("slave")
    for i in mylist:
        i=i.dbname.encode("utf-8")
        db_list.append({"id": i, "text": i, "icon": "database.ico"})
    # db_list = [{"id": 'zzjr_server', "text": "zzjr_server", "icon": "database.ico"}, {"id": 'zentao', "text": "zentao", "icon": "database.ico"},{"id": 'zzjr_bank', "text": "zzjr_bank", "icon": "database.ico", "selected": "true"}]

# 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

    with con:
        cur = con.cursor()
        cur.execute(cmd)
        rows = cur.fetchall()
        desc = cur.description
    mylist=[]
    for i in rows:
        mydisc={}
        for num in range(0, len(i)):
            #values=str(i[num]).decode("ascii").encode("utf-8")
            values = str(i[num])
            mydisc[desc[num][0]] = values
            # print chardet.detect(str(i[num]))
            #print chardet.detect(values)
        mylist.append(mydisc)
    myjson = {}
    myjson['total'] = 2800
    myjson['rows'] = mylist
    return HttpResponse(json.dumps(myjson), content_type="application/json")


@require_role(role='user')
def dbtool_dblistjson(request):
    db_list=[{"id": '请选择数据库', "text": "请选择数据库", "icon": "database.ico", "selected": "true"}]
    defend_attack(request)
    mylist=dbtool_api.db_list("slave")
    for i in mylist:
        i=i.dbname.encode("utf-8")
        db_list.append({"id": i, "text": i, "icon": "database.ico"})
    # db_list = [{"id": 'zzjr_server', "text": "zzjr_server", "icon": "database.ico"}, {"id": 'zentao', "text": "zentao", "icon": "database.ico"},{"id": 'zzjr_bank', "text": "zzjr_bank", "icon": "database.ico", "selected": "true"}]

    return HttpResponse(json.dumps(db_list), content_type="application/json")