table_delete.py
2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#author: 4N
#createtime: 2021/1/27
#email: nheweijun@sina.com
from ..models import Table,Database,DES
from osgeo.ogr import DataSource
from app.models import db
from app.util.component.ApiTemplate import ApiTemplate
from app.util.component.PGUtil import PGUtil
import configure
from flask import current_app
from app.util.component.UserCheck import UserCheck
class Api(ApiTemplate):
api_name = "删除表"
def process(self):
res = {}
pg_ds = None
va_ds = None
try:
table_guid = self.para.get("guid")
table = Table.query.filter_by(guid=table_guid)
if not table.one_or_none():
res["result"]=False
res["msg"]= "数据不存在!"
return res
table = table.one_or_none()
# 删除真实数据
database = Database.query.filter_by(guid=table.database_guid).one_or_none()
#验证权限
UserCheck.verify(database.creator)
if not database:
res["result"]=False
res["msg"]= "数据库不存在!"
return res
pg_ds: DataSource = PGUtil.open_pg_data_source(1, DES.decode(database.sqlalchemy_uri))
if configure.VACUATE_DB_URI:
va_ds: DataSource = PGUtil.open_pg_data_source(1, configure.VACUATE_DB_URI)
else:
va_ds: DataSource = PGUtil.open_pg_data_source(1, DES.decode(database.sqlalchemy_uri))
#删除抽稀表
vacuate_tables=table.relate_table_vacuates.all()
for vt in vacuate_tables:
db.session.delete(vt)
try:
va_ds.DeleteLayer(vt.name)
except:
current_app.logger.warning("{}不存在!".format(vt.name))
try:
pg_ds.DeleteLayer(table.name)
except:
current_app.logger.warning("{}不存在!".format(table.name))
# 删除元数据
db.session.delete(table)
db.session.commit()
res["result"] = True
res["msg"] ="删除成功!"
except Exception as e:
raise e
finally:
if pg_ds:
pg_ds.Destroy()
if va_ds:
va_ds.Destroy()
return res
api_doc={
"tags":["管理接口"],
"parameters":[
{"name": "guid",
"in": "formData",
"type": "string",
"description": "表guid","required":"true"},
],
"responses":{
200:{
"schema":{
"properties":{
}
}
}
}
}