-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.py
More file actions
388 lines (307 loc) · 14 KB
/
Copy pathadmin.py
File metadata and controls
388 lines (307 loc) · 14 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import os
import threading
from datetime import datetime, timezone
from pathlib import Path
import bpy
import json
from . import auth, server_config, download_assets_operator, statusbar, filehash, util, thumbnails, version_control
def get_lib_path(context):
for lib in context.preferences.filepaths.asset_libraries:
if lib.name.lower() == "kanistra admin":
return lib.path
return None
def push_assets(self, context):
files = dict()
path = get_lib_path(context)
for root, _, fls in os.walk(path):
for file in fls:
filepath = os.path.join(root, file)
name = os.path.relpath(filepath, path).replace(os.path.sep, "/")
h = filehash.filehash(filepath)
files[h] = (filepath, name)
list_r = auth.get(context, f"{server_config.SERVER}/admin-files/files/")
if list_r.status_code != 200:
return
for file in list_r.json():
if file['hash'] in files.keys() and file['name'] == files[file['hash']][1]:
del files[file['hash']]
else:
auth.delete(context, f"{server_config.SERVER}/admin-files/files/{file['id']}")
for _, (filepath, name) in files.items():
with open(filepath, "rb") as f:
files = {'file': f}
data = {'name': name}
auth.post(context, f"{server_config.SERVER}/admin-files/files/", files=files, data=data)
def publish_assets(self, context):
list_r = auth.get(context, f"{server_config.SERVER}/blendfiles/")
server_pks = []
for i in list_r.json():
server_pks.append(i['id'])
path = get_lib_path(context)
for root, _, fls in os.walk(path):
for file in fls:
filepath = os.path.join(root, file)
name = os.path.relpath(filepath, path).replace(os.path.sep, "_")
with open(filepath, "rb") as f:
file_content = f.read()
self.report({"INFO"}, f"[{file}] File size: {len(file_content)} bytes")
f.seek(0)
files = {'file': f}
data = {'name': name, "is_free": False}
r = auth.post(context, f"{server_config.SERVER}/blendfiles/", files=files, data=data)
self.report({"INFO"}, f"[{file}] {r.status_code}: {r.text}")
for pk in server_pks:
auth.delete(context, f"{server_config.SERVER}/blendfiles/{pk}")
class OpenAdminOperator(bpy.types.Operator):
bl_idname = "kanistra.open_admin_operator"
bl_label = "Open Kanistra Admin"
bl_description = "Switch asset library to Kanistra Admin"
def execute(self, context):
context.space_data.params.asset_library_reference = "Kanistra Admin"
return {'FINISHED'}
class PushAdminOperator(bpy.types.Operator):
bl_idname = "kanistra.push_admin_operator"
bl_label = "Push/Publish"
current_file: str = ""
total_files: int = 1
files_done: int = 0
push_status = ""
thread: threading.Thread = None
publish: bpy.props.BoolProperty()
def invoke(self, context, event):
return context.window_manager.invoke_confirm(self, event)
def modal(self, context, event):
if event.type == "TIMER":
if self.total_files > 0:
statusbar.update_progress(context,
progress=self.files_done / self.total_files * 100,
filename=self.current_file,
progress_text=f"{self.push_status}: ")
if not self.thread.is_alive():
self.thread.join()
try:
bpy.ops.asset.library_refresh()
except RuntimeError:
pass
statusbar.end_progress(context)
return {"FINISHED"}
return {"PASS_THROUGH"}
def execute(self, context):
if download_assets_operator.downloading_status(context) != 'NONE':
return {"FINISHED"}
# self.push_status = ""
asset_lib = get_lib_path(context)
self.thread = threading.Thread(target=util.push_library,
args=(self, context, not self.publish, asset_lib))
self.thread.start()
context.window_manager.event_timer_add(0.1, window=context.window)
context.window_manager.modal_handler_add(self)
return {"RUNNING_MODAL"}
class PublishAdminOperator(bpy.types.Operator):
bl_idname = "kanistra.publish_admin_operator"
bl_label = "Publish"
current_file: str = ""
total_files: int = 1
files_done: int = 0
thread: threading.Thread = None
def invoke(self, context, event):
return context.window_manager.invoke_confirm(self, event)
def execute(self, context):
publish_assets(self, context)
return {"FINISHED"}
class PullAdminOperator(bpy.types.Operator):
bl_idname = "kanistra.pull_admin_operator"
bl_label = "Pull"
bl_options = {"REGISTER", "UNDO"}
downloading_thread: threading.Thread = None
total_size = 1
downloaded_size = 0
filename = None
def invoke(self, context, event):
return context.window_manager.invoke_confirm(self, event)
def modal(self, context, event):
if event.type == "TIMER":
if self.total_size > 0:
statusbar.update_progress(context, self.downloaded_size / self.total_size * 100, f"{self.filename}")
if not self.downloading_thread.is_alive():
self.downloading_thread.join()
try:
bpy.ops.asset.library_refresh()
except RuntimeError:
pass
statusbar.end_progress(context)
set_updates(context, 0)
download_assets_operator.downloading_status(context, status='NONE')
return {"FINISHED"}
return {"PASS_THROUGH"}
def execute(self, context):
if download_assets_operator.downloading_status(context) != 'NONE':
return {"FINISHED"}
asset_lib = get_lib_path(context)
self.downloading_thread = threading.Thread(target=util.download_from_source,
args=(self, context, f"{server_config.SERVER}/admin-files/files/",
f"{server_config.SERVER}/admin-files/download/", asset_lib,
"Pulled", True))
self.downloading_thread.start()
set_updates(context, 0)
self.report({"INFO"}, f"Downloading from kanistra in background...")
context.window_manager.event_timer_add(0.1, window=context.window)
context.window_manager.modal_handler_add(self)
download_assets_operator.downloading_status(context, status='DOWNLOADING')
return {"RUNNING_MODAL"}
def draw_operators(self, context):
lib_ref = context.space_data.params.asset_library_reference
props = context.window_manager.kanistra_props
if lib_ref.lower() == "Kanistra Assets".lower() and props.admin:
layout = self.layout
layout.operator("kanistra.open_admin_operator")
if lib_ref.lower() == "Kanistra Admin".lower() and props.admin:
layout = self.layout
row = layout.row()
if props.admin_updates != 0:
row.operator("kanistra.pull_admin_operator", text=f"Pull {props.admin_updates} "
f"updates ({props.admin_updates_size // 1024 // 1024}"
f" Mb)",
icon_value=thumbnails.get_thumbnails()["arrow-down"].icon_id)
row.operator("kanistra.push_admin_operator",
text="Push",
icon_value=thumbnails.get_thumbnails()["arrow-up"].icon_id).publish = False
row.operator("kanistra.push_admin_operator",
text="Publish",
icon_value=thumbnails.get_thumbnails()["arrow-up-right"].icon_id).publish = True
def set_updates(context, u, us=0):
context.window_manager.kanistra_props.admin_updates = u
context.window_manager.kanistra_props.admin_updates_size = us
for a in context.screen.areas:
if a.type == "FILE_BROWSER":
a.tag_redraw()
class UsersCountPanel(bpy.types.Panel):
bl_idname = "kanistra.users_count_panel"
bl_label = "Addon users"
bl_space_type = "FILE_BROWSER"
bl_region_type = "TOOLS"
bl_options = {"DEFAULT_CLOSED"}
@classmethod
def poll(self, context):
props = context.window_manager.kanistra_props
lib_ref = getattr(context.space_data.params, "asset_library_ref", None)
lib_ref = getattr(context.space_data.params, "asset_library_reference", lib_ref)
return context.area.ui_type == "ASSETS" and lib_ref.lower() in ["kanistra admin"] and props.admin
def draw(self, context):
layout = self.layout
props = context.window_manager.kanistra_props
col = layout.column()
users = json.loads(props.admin_users)
admin_users = sorted(list(filter(lambda x: x["is_staff"], users)), key=lambda u: u['email'])
default_users = sorted(list(filter(lambda x: not x["is_staff"], users)), key=lambda u: u['email'])
col.alert = True
col.label(text=f"Admins ({len(admin_users)})")
col.alert = False
col.separator()
for user in admin_users:
col.label(text=user['email'])
col.separator()
col.alert = True
col.label(text=f"Users ({len(default_users)})")
col.alert = False
col.separator()
for user in default_users:
col.label(text=f"{user['email']} {'(not active)' if not user['is_active'] else ''}")
class AdminIndexOperator(bpy.types.Operator):
bl_idname = "kanistra.admin_index_operator"
bl_label = "Index library"
current_file: str = ""
total_files: int = 1
files_done: int = 0
thread: threading.Thread = None
def invoke(self, context, event):
return context.window_manager.invoke_confirm(self, event)
def modal(self, context, event):
if event.type == "TIMER":
if self.total_files > 0:
statusbar.update_progress(context, self.files_done / self.total_files * 100,
f"{self.current_file}", progress_text="Indexing: ")
if not self.thread.is_alive():
self.thread.join()
try:
bpy.ops.asset.library_refresh()
except RuntimeError:
pass
statusbar.end_progress(context)
return {"FINISHED"}
return {"PASS_THROUGH"}
def execute(self, context):
if download_assets_operator.downloading_status(context) != 'NONE':
return {"FINISHED"}
asset_lib = get_lib_path(context)
self.thread = threading.Thread(target=util.index_library_draft,
args=(self, context, asset_lib))
self.thread.start()
context.window_manager.event_timer_add(0.1, window=context.window)
context.window_manager.modal_handler_add(self)
return {"RUNNING_MODAL"}
class MarkWithTagOperator(bpy.types.Operator):
bl_idname = "kanistra.mark_with_tag_operator"
bl_label = "Tag"
tag: bpy.props.StringProperty()
def execute(self, context):
selected_assets = context.selected_assets
paths = set()
for asset in selected_assets:
paths.add(asset.full_library_path)
for path in paths:
util.mark_file_with_tag(context, path, str(self.tag), "manage")
try:
bpy.ops.asset.library_refresh()
except RuntimeError:
pass
return {"FINISHED"}
class TagsPanel(bpy.types.Panel):
bl_idname = "kanistra.tags_panel"
bl_label = "Manage tags"
bl_space_type = 'FILE_BROWSER'
bl_region_type = 'TOOL_PROPS'
@classmethod
def poll(self, context):
lib_ref = context.space_data.params.asset_library_reference
props = context.window_manager.kanistra_props
return (lib_ref.lower() in ["kanistra admin"] and
props.admin and
context.selected_assets and
len(context.selected_assets) > 0)
def draw(self, context):
layout = self.layout
col = layout.column()
col.operator("kanistra.mark_with_tag_operator", text="Free").tag = "free"
col.operator("kanistra.mark_with_tag_operator", text="Draft").tag = "draft"
class StatisticsPanel(bpy.types.Panel):
bl_idname = "kanistra.admin_statistics_panel"
bl_label = "Statistics"
bl_space_type = 'FILE_BROWSER'
bl_region_type = 'TOOLS'
bl_options = {"DEFAULT_CLOSED"}
@classmethod
def poll(self, context):
props = context.window_manager.kanistra_props
lib_ref = getattr(context.space_data.params, "asset_library_ref", None)
lib_ref = getattr(context.space_data.params, "asset_library_reference", lib_ref)
return context.area.ui_type == "ASSETS" and lib_ref.lower() in ["kanistra admin"] and props.admin
def draw(self, context):
props = context.window_manager.kanistra_props
stats = json.loads(props.admin_statistics)
layout = self.layout
column = layout.column()
data = set(version_control.load_versions_data(context, admin=True)["version_tags"])
def format_datetime(datetime_str):
dt_obj = datetime.fromisoformat(datetime_str.replace("Z", "+00:00"))
dt_obj = dt_obj.replace(tzinfo=timezone.utc).astimezone(tz=None)
return dt_obj.strftime("%Y-%m-%d %H:%M:%S")
stats.sort(key=lambda x: x["tag"], reverse=True)
for entry in stats:
# if entry['tag'] not in data:
# continue
box = column.box()
col = box.column()
col.label(text=entry['tag'])
col.label(text=f"Downloads Anon+Auth: {entry['anon_counter']}+{entry['auth_counter']}")
col.label(text=f"Last Download: {format_datetime(entry['last_download'])}")