/
views.py
244 lines (203 loc) · 8.87 KB
/
views.py
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#coding=utf-8
from uliweb import expose, functions, request, Response
from uliweb.orm import get_model
from uliweb.utils.filedown import filedown
import logging
import json as json_
import os
log = logging.getLogger('linci.artifact')
def linci_artifact_has_permission(perm):
return bool(functions.has_permission(request.user,perm))
@expose('/linci/artifact')
class Artifact(object):
def _get_new_index(self):
LinciArtifact = get_model("linciartifact")
site = settings.LINCI.artifact_site
last_arti = LinciArtifact.filter(LinciArtifact.c.asite==site).order_by(LinciArtifact.c.aindex.desc()).one()
if last_arti:
nindex = last_arti.aindex +1
else:
nindex = 1
while 1:
if LinciArtifact.filter(LinciArtifact.c.asite==site,LinciArtifact.c.aindex==nindex).count()==0:
return nindex
nindex += 1
def _get_artifact_item(self):
id_ = request.values.get("id")
aid = request.values.get("aid")
LinciArtifact = get_model("linciartifact")
if id_:
item = LinciArtifact.get(int(id_))
elif aid:
asite,aindex = aid.split("-")
item = LinciArtifact.get(LinciArtifact.c.asite==asite,LinciArtifact.c.aindex==int(aindex))
else:
item = None
return item
#--- api ---
def api_new(self):
if not functions.linci_artifact_has_permission("linci_artifact_new"):
return json({"success":False,"msg":"error: no permission"})
asite = request.values.get("asite",settings.LINCI.artifact_site)
aindex = request.values.get("aindex")
atype = request.values.get("atype","default")
if not asite:
return json({"success":False,"msg":"error: parameter 'asite' needed"})
asite = asite.upper()
local_site = (asite == settings.LINCI.artifact_site)
if local_site:
if aindex:
return json({"success":False,"msg":"error: local artifact creation don't accept aindex parameter"})
else:
aindex = self._get_new_index()
else:
if not aindex:
return json({"success":False,"msg":"error: non-local artifact creation need aindex parameter"})
try:
aindex = int(aindex)
except ValueError as e:
return json({"success":False,"msg":"error: aindex not integer"})
if not settings.LINCI_ARTIFACT_TYPE.get(atype):
return json({"success":False,"msg":"error: bad artifact type %s"%(atype)})
LinciArtifact = get_model("linciartifact")
linciartifact = LinciArtifact(asite = asite,aindex = aindex,type = atype)
ret = linciartifact.save()
if ret:
aid = '%s-%s'%(linciartifact.asite,linciartifact.aindex)
return json({"success":True,"aid":aid,"msg":"new artifact '%s' OK"%(aid)})
else:
json({"success":False,"msg":"error: unknown error when create new artifact"})
def api_list_bootstraptable_data(self):
if not functions.linci_artifact_has_permission("linci_artifact_read"):
return json({"total":0, "rows": []})
if request.data:
data = json_.loads(request.data)
else:
data = {}
LinciArtifact = get_model("linciartifact")
l = LinciArtifact.all()
sort = data.get("sort")
order = data.get("order")
limit = data.get("limit")
offset = data.get("offset")
if sort:
sort_key = getattr(LinciArtifact.c,sort)
if order:
sort_key = getattr(sort_key,order)()
l = l.order_by(sort_key)
if limit:
l = l.limit(limit)
if offset:
l = l.offset(offset)
return json({"total":l.count(), "rows": [i.to_dict() for i in l]})
def api_upload(self):
item = self._get_artifact_item()
if not item:
return json({"success":False,"msg":"error: artifact not found"})
if item.fixed:
return json({"success":False,"msg":"error: artifact had been fixed"})
item.add_files(request.files)
flist_str = ",".join([item.normalize_path(request.files[k].filename) for k in request.files])
return json({"success":True,"msg":"artifact file uploaded OK: %s"%(flist_str)})
def api_get(self):
if not functions.linci_artifact_has_permission("linci_artifact_read"):
return json({"success":False,"msg":"error: have no permission"})
item = self._get_artifact_item()
if not item:
return json({"success":False,"msg":"error: artifact not found"})
d = item.to_dict()
perm_update = functions.linci_artifact_has_permission("linci_artifact_update")
d["aid"] = "%s-%s"%(d["asite"],d["aindex"])
tclass = functions.get_linci_artifact_scheme_class(item.type)
d["type_label"] = tclass.name
d["action_fix"] = (not item.fixed) and item.ready and perm_update
d["action_set_ready"] = (not item.ready) and perm_update
files = request.values.get("files","false")
if files=="true":
LinciArtifactFile = get_model("linciartifactfile")
l = LinciArtifactFile.filter(LinciArtifactFile.c.artifact==item.id)
d["files"] = [i.to_dict() for i in l]
return json({"success":True,"item":d})
def api_fix(self):
if not functions.linci_artifact_has_permission("linci_artifact_update"):
return json({"success":False,"msg":"error: have no permission"})
item = self._get_artifact_item()
item.fixed = True
item.save()
return json({"success":True,"msg":"artifact fix OK"})
def api_set_ready(self):
if not functions.linci_artifact_has_permission("linci_artifact_update"):
return json({"success":False,"msg":"error: have no permission"})
item = self._get_artifact_item()
item.ready = bool(request.values.get("ready","true").lower()=="true")
item.save()
return json({"success":True,"msg":"artifact set ready OK"})
def api_artifactfile_list_bootstraptable_data(self):
from uliweb.utils.common import convert_bytes
if not functions.linci_artifact_has_permission("linci_artifact_read"):
return json({"total":0, "rows": []})
if request.data:
data = json_.loads(request.data)
else:
data = {}
LinciArtifactFile = get_model("linciartifactfile")
l = LinciArtifactFile.all()
sort = data.get("sort")
order = data.get("order")
limit = data.get("limit")
offset = data.get("offset")
item_id = data.get("item_id")
if not item_id:
return json({"total":0, "rows": []})
l = l.filter(LinciArtifactFile.c.artifact==item_id)
if sort:
if sort=="size_str":
sort = "size"
sort_key = getattr(LinciArtifactFile.c,sort)
if order:
sort_key = getattr(sort_key,order)()
l = l.order_by(sort_key)
if limit:
l = l.limit(limit)
if offset:
l = l.offset(offset)
def get_info(i):
d = i.to_dict()
d["size_str"] = convert_bytes(i.size)
return d
return json({"total":l.count(), "rows": [get_info(i) for i in l]})
def api_artifactfile_download(self):
if not functions.linci_artifact_has_permission("linci_artifact_read"):
return json({"success":False,"msg":"have no permission"}, status=403)
not_found_response = json({"success":False,"msg":"not found"}, status=404)
id_ = request.values.get("id")
if not id_:
return not_found_response
LinciArtifactFile = get_model("linciartifactfile")
afile = LinciArtifactFile.get(int(id_))
if not afile:
return not_found_response
real_filename = os.path.join(afile.artifact.get_artifact_dpath(),afile.store_path)
filename = os.path.basename(afile.path)
if not os.path.isfile(real_filename):
return not_found_response
return filedown(request.environ,filename,cache=False,real_filename=real_filename,action="download")
#--- web admin ---
def list(self):
if not functions.linci_artifact_has_permission("linci_artifact_read"):
errmsg = "no permission"
else:
errmsg = ""
return {"errmsg":errmsg}
def view(self):
item = self._get_artifact_item()
if item:
errmsg = ""
tclass = functions.get_linci_artifact_scheme_class(item.type)
if tclass:
view_template = getattr(tclass,"view_template",None)
if view_template:
response.template = view_template
else:
errmsg = "artifact not found"
return {"errmsg":errmsg,"item":item}