catalog_create.py
3.1 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
# coding=utf-8
#author: 4N
#createtime: 2021/3/9
#email: nheweijun@sina.com
import uuid
from ..models import *
from app.util.component.ApiTemplate import ApiTemplate
class Api(ApiTemplate):
api_name = "创建目录"
def para_check(self):
if not self.para.get("database_guid"):
raise Exception("缺乏database_guid参数")
if not self.para.get("pguid"):
raise Exception("缺乏pguid参数")
if not self.para.get("name"):
raise Exception("缺乏name参数")
def process(self):
# 返回结果
res = {}
res["result"] = False
try:
# 业务逻辑
if not Database.query.filter_by(guid=self.para.get("database_guid")).one_or_none():
res["msg"]="数据库不存在!"
return res
if Catalog.query.filter_by(name=self.para.get("name"),
pguid=self.para.get("pguid"),
database_guid=self.para.get("database_guid")).one_or_none():
res["msg"]="目录已经存在!"
return res
guid = uuid.uuid1().__str__()
path = guid
# 获得目录的全路径
pguid = self.para.get("pguid")
count = 0
while pguid !="0" and count<100:
count+=1
path = pguid+":"+path
p_catalog = Catalog.query.filter_by(guid=pguid).one_or_none()
pguid = p_catalog.pguid
if count==100:
raise Exception("目录结构出现问题!")
path = "0" + ":" + path
sort = Catalog.query.filter_by(pguid=self.para.get("pguid")).count()
catalog = Catalog(guid=guid,
pguid=self.para.get("pguid"),name=self.para.get("name"),
sort=sort,
description=self.para.get("description"),
database_guid=self.para.get("database_guid"),
path=path)
db.session.add(catalog)
db.session.commit()
res["msg"] = "目录创建成功!"
res["data"] = guid
res["result"] = True
except Exception as e:
db.session.rollback()
raise e
return res
api_doc={
"tags":["矢量数据目录接口"],
"parameters":[
{"name": "name",
"in": "formData",
"type": "string",
"description":"目录名"},
{"name": "pguid",
"in": "formData",
"type": "string","description":"父目录guid,创建根目录时为0"},
{"name": "database_guid",
"in": "formData",
"type": "string","description":"数据库guid"}
],
"responses":{
200:{
"schema":{
"properties":{
}
}
}
}
}