Skip to content

Commit 9409662

Browse files
committed
Organize workspace app by org mviewer#161
1 parent a804c8b commit 9409662

File tree

5 files changed

+45
-33
lines changed

5 files changed

+45
-33
lines changed

srv/python/mviewerstudio_backend/route.py

+13-12
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def update_mviewer_config(id) -> Response:
9393
# get config as class model data
9494
config_data = config.as_data()
9595
# clean preview space if not empty
96-
clean_preview(current_app, config_data.id)
96+
clean_preview(current_app, config_data.url)
9797

9898
current_config = current_app.register.read_json(config_data.id)
9999

@@ -139,7 +139,7 @@ def publish_mviewer_config(id) -> Response:
139139
if not publish_dir or not path.exists(publish_dir):
140140
return BadRequest("Publish directory does not exists !")
141141

142-
workspace = path.join(current_app.config["EXPORT_CONF_FOLDER"], id)
142+
workspace = path.join(current_app.config["EXPORT_CONF_FOLDER"], current_user.organisation, id)
143143

144144
if not path.exists(workspace):
145145
return BadRequest("Application does not exists !")
@@ -197,7 +197,7 @@ def delete_config_workspace(id = None) -> Response:
197197
raise BadRequest("Empty list : no value to delete !")
198198

199199
logger.debug("START DELETE CONFIG : %s" % id)
200-
workspace = path.join(current_app.config["EXPORT_CONF_FOLDER"], id)
200+
workspace = path.join(current_app.config["EXPORT_CONF_FOLDER"], current_user.organisation, id)
201201
# update json
202202
config = current_app.register.read_json(id)
203203
if not config or not path.exists(workspace):
@@ -233,7 +233,8 @@ def get_all_app_versions(id) -> Response:
233233
if not config:
234234
raise BadRequest("This config doesn't exists !")
235235
config = config[0]
236-
workspace = path.join(current_app.config["EXPORT_CONF_FOLDER"], config["id"])
236+
org = current_user.organisation if current_user else current_app.config["DEFAULT_ORG"]
237+
workspace = path.join(current_app.config["EXPORT_CONF_FOLDER"], org, config["id"])
237238
git = Git_manager(workspace)
238239
versions = git.get_versions()
239240
return jsonify({"versions": versions, "config": config})
@@ -258,13 +259,13 @@ def switch_app_version(id, version="1") -> Response:
258259
if not config:
259260
raise BadRequest("This config doesn't exists !")
260261
config = config[0]
261-
workspace = path.join(current_app.config["EXPORT_CONF_FOLDER"], config["id"])
262+
workspace = path.join(current_app.config["EXPORT_CONF_FOLDER"], current_user.organisation, config["id"])
262263
git = Git_manager(workspace)
263264
git.switch_version(version, as_new)
264265
# Update register
265266
current_app.register.update_from_id(config["id"])
266267
# clean previews
267-
clean_preview(current_app, config["id"])
268+
clean_preview(current_app, config["url"])
268269
return (
269270
jsonify(
270271
{
@@ -293,14 +294,14 @@ def preview_app_version(id, version) -> Response:
293294
# create preview space
294295
config = config[0]
295296
init_preview(current_app, config["id"])
296-
workspace = path.join(current_app.config["EXPORT_CONF_FOLDER"], config["id"])
297+
workspace = path.join(current_app.config["EXPORT_CONF_FOLDER"], current_user.organisation, config["id"])
297298
git = Git_manager(workspace)
298299
git.switch_version(version, False)
299300
# copy past file to preview folder
300301
app_config = current_app.config
301302
src_file = app_config["EXPORT_CONF_FOLDER"] + config["url"]
302303
preview_file = path.join(config["id"], "preview", "%s.xml" % version)
303-
path_preview_file = path.join(app_config["EXPORT_CONF_FOLDER"], preview_file)
304+
path_preview_file = path.join(app_config["EXPORT_CONF_FOLDER"], config["organisation"], preview_file)
304305
copyfile(src_file, path_preview_file)
305306
# restor branch
306307
git.repo.git.checkout("master")
@@ -309,7 +310,7 @@ def preview_app_version(id, version) -> Response:
309310
jsonify(
310311
{
311312
"success": True,
312-
"file": path.join(app_config["CONF_PATH_FROM_MVIEWER"], preview_file),
313+
"file": path.join(app_config["CONF_PATH_FROM_MVIEWER"], config["organisation"], preview_file),
313314
}
314315
),
315316
200,
@@ -336,8 +337,8 @@ def preview_uncommited_app(id) -> Response:
336337
# get file name and path
337338
file_name = uuid.uuid1()
338339
preview_file = path.join(id, "preview", "%s.xml" % file_name)
339-
system_path = path.join(app_config["EXPORT_CONF_FOLDER"], preview_file)
340-
340+
system_path = path.join(app_config["EXPORT_CONF_FOLDER"], current_user.organisation, preview_file)
341+
clean_preview(current_app, path.join(current_user.organisation, id))
341342
# store file to preview folder
342343
with open(system_path, "w") as file:
343344
file.write(xml)
@@ -347,7 +348,7 @@ def preview_uncommited_app(id) -> Response:
347348
jsonify(
348349
{
349350
"success": True,
350-
"file": path.join(app_config["CONF_PATH_FROM_MVIEWER"], preview_file),
351+
"file": path.join(app_config["CONF_PATH_FROM_MVIEWER"], current_user.organisation, preview_file),
351352
}
352353
),
353354
200,

srv/python/mviewerstudio_backend/utils/commons.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from os import walk, remove, path, mkdir
2-
def clean_preview(app, id):
2+
def clean_preview(app, app_dir):
33
'''
44
Will remove each XML files in config preview directory
55
'''
6-
if not app or not id:
6+
if not app_dir:
77
return
8-
preview_dir = path.join(app.config["EXPORT_CONF_FOLDER"], id, "preview")
8+
9+
preview_dir = path.join(app.config["EXPORT_CONF_FOLDER"], app_dir, "preview")
910
for (root, dirs, files) in walk(preview_dir):
1011
if not files:
1112
break

srv/python/mviewerstudio_backend/utils/config_utils.py

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from os import path, mkdir, remove
1+
from os import path, makedirs
2+
from unidecode import unidecode
23
import logging
34
import xml.etree.ElementTree as ET
45
import re
@@ -11,6 +12,9 @@
1112
logger = logging.getLogger(__name__)
1213

1314

15+
def getWorkspace(app, path_id = ""):
16+
return path.join(app.config["EXPORT_CONF_FOLDER"], path_id)
17+
1418
def edit_xml_string(root, attribute, value):
1519
attr = root.find(".//{*}%s" % attribute)
1620
attr.text = value
@@ -44,9 +48,13 @@ def __init__(self, data = "", app = None, xml = None) -> None:
4448
self.xml = self._read_xml(xml)
4549
if self.xml is not None and app.register:
4650
self.register = app.register
47-
51+
# read org
52+
if not current_user and xml:
53+
org = self.meta.find("{*}organisation").text
54+
else :
55+
org = current_user.organisation if current_user else app.config["DEFAULT_ORG"]
4856
# target workspace path
49-
self.workspace = path.join(self.app.config["EXPORT_CONF_FOLDER"], self.uuid)
57+
self.workspace = unidecode(getWorkspace(app, path.join(org, self.uuid)))
5058
# create or update workspace
5159
self.create_workspace()
5260
# init or get repo
@@ -86,8 +94,8 @@ def create_workspace(self):
8694
'''
8795
if not path.exists(self.workspace):
8896
# create directory
89-
mkdir(self.workspace)
90-
mkdir(path.join(self.workspace, "preview"))
97+
makedirs(self.workspace)
98+
makedirs(path.join(self.workspace, "preview"))
9199

92100
def _get_xml_describe(self, xml):
93101
'''
@@ -121,18 +129,13 @@ def create_or_update_config(self, file):
121129
self.uuid = self.meta.find(".//{*}identifier").text
122130
file_name = self.meta.find("{*}title").text
123131
# save file
124-
normalize_file_name = re.sub('[^a-zA-Z0-9 \n\.]', "_", file_name).replace(" ", "_")
132+
decode_file_name = unidecode(file_name)
133+
normalize_file_name = re.sub('[^a-zA-Z0-9 \n\.]', "_", decode_file_name).replace(" ", "_")
125134
self.full_xml_path = path.join(self.workspace, "%s.xml" % normalize_file_name)
135+
126136
# write file
127137
self.write()
128138

129-
def clean_all_workspace_configs(self):
130-
'''
131-
Remove each XML found in app workspace
132-
'''
133-
for file in glob.glob("%s/*.xml" % self.workspace):
134-
remove(file)
135-
136139
def as_data(self):
137140
'''
138141
Index config metadata in register.

srv/python/mviewerstudio_backend/utils/register_utils.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,19 @@ def _configs_files_to_register(self):
7272
'''
7373
Will parse all app configs workspace to init class config for each.
7474
'''
75-
publish_path = self.app.config["MVIEWERSTUDIO_PUBLISH_PATH"]
76-
dirs = [
77-
dir for dir in listdir(self.store_directory) if isdir(path.join(self.store_directory, dir)) and dir != publish_path and glob.glob("%s/*.xml" % path.join(self.store_directory, dir))
75+
sub_store_dirs = [
76+
path.join(self.store_directory, dir) for dir in listdir(self.store_directory) if isdir(path.join(self.store_directory, dir)) and dir not in ["styles"]
7877
]
78+
# and glob.glob("%s/*.xml" % path.join(self.store_directory, dir))
79+
xml_dirs = []
80+
for sub_dir in sub_store_dirs:
81+
app_dirs = [
82+
path.join(sub_dir, dir) for dir in listdir(sub_dir) if isdir(path.join(sub_dir, dir)) and glob.glob("%s/*.xml" % path.join(sub_dir, dir))
83+
]
84+
print(app_dirs)
85+
xml_dirs = [*xml_dirs, *app_dirs]
7986

80-
for dir in dirs:
81-
app_path = path.join(self.store_directory, dir)
87+
for app_path in xml_dirs:
8288
for xml in glob.glob("%s/*.xml" % app_path):
8389
repo = init_or_get_repo(app_path)
8490
# to be sur each app is in master branch

srv/python/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ sentry-sdk[flask]>=0.13,<1
44
lxml>=4.4,<4.5
55
GitPython>=3
66
requests
7+
Unidecode

0 commit comments

Comments
 (0)