This repository was archived by the owner on Mar 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDataModels.py
225 lines (179 loc) · 6.75 KB
/
DataModels.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
# ***********************************************************************
# * *
# * Copyright (c) 2023 Ondsel *
# * *
# ***********************************************************************
import os
import json
from pathlib import Path
from PySide.QtCore import Qt, QAbstractListModel, QModelIndex
from PySide.QtGui import QStandardItemModel, QStandardItem
import FreeCAD
from APIClient import fancy_handle, APICallResult
CACHE_PATH = FreeCAD.getUserCachePath() + "Ondsel-Lens/"
class WorkspaceListModel(QAbstractListModel):
"""Workspaces is a list of dicts
workspaces =
[ {
"name" : "myWorkspace",
"description" : "This is my workspace description",
"url" : "url",
"type" : "local",
},
]
"""
def __init__(self, **kwargs):
parent = kwargs.get("parent", None)
super(WorkspaceListModel, self).__init__(parent)
self.api = kwargs["api"]
self.workspaceListFile = f"{CACHE_PATH}/workspaceList.json"
self.refreshModel()
def set_api(self, api):
self.api = api
def refreshModel(self):
def try_get_workspaces_connected():
self.workspaces = self.api.getWorkspaces()
self.beginResetModel()
api_result = fancy_handle(try_get_workspaces_connected)
if api_result == APICallResult.OK:
self.save()
else:
self.load()
self.endResetModel()
def rowCount(self, parent=QModelIndex()):
return len(self.workspaces)
def data(self, index, role=Qt.DisplayRole):
if role == Qt.DisplayRole:
return self.workspaces[index.row()]
return None
def updateData(self, workspaces):
self.beginResetModel()
self.workspaces = workspaces
self.endResetModel()
self.save()
# def addWorkspace(self, workspaceName, workspaceDesc, workspaceType, workspaceUrl,
# _id, organisation, rootDirectory):
# for workspace in reversed(self.workspaces):
# if workspace["name"] == workspaceName:
# if workspaceType == "Ondsel" and workspace["type"] == "Local":
# workspace["type"] = "Ondsel"
# self.save()
# return
# self.beginInsertRows(QtCore.QModelIndex(), self.rowCount(), self.rowCount())
# self.workspaces.append(
# {
# "name": workspaceName,
# "description": workspaceDesc,
# "type": workspaceType,
# "url": workspaceUrl,
# "_id": _id,
# "organizationId": organisation,
# "rootDirectory" : rootDirectory,
# "currentDirectory" : rootDirectory
# }
# )
# self.endInsertRows()
# self.save()
# def removeWorkspace(self, index):
# if index.isValid() and 0 <= index.row() < len(self.workspaces):
# self.beginRemoveRows(QtCore.QModelIndex(), index.row(), index.row())
# del self.workspaces[index.row()]
# self.endRemoveRows()
# self.save()
def removeWorkspaces(self):
self.beginResetModel()
self.workspaces = []
self.endResetModel()
def load(self):
self.workspaces = []
if os.path.exists(self.workspaceListFile):
with open(self.workspaceListFile, "r") as file:
dataStr = file.read()
if dataStr:
self.workspaces = json.loads(dataStr)
def save(self):
dirname = os.path.dirname(self.workspaceListFile)
Path(dirname).mkdir(parents=True, exist_ok=True)
with open(self.workspaceListFile, "w") as file:
file.write(json.dumps(self.workspaces))
def headerData(self, section, orientation, role=Qt.DisplayRole):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self.headers[section]
def dump(self):
"""
useful for debugging. This will return the contents in a printable form
"""
for row in range(self.rowCount()):
item_index = self.index(row)
item_data = self.data(item_index, Qt.DisplayRole)
print(item_data)
ROLE_TYPE = Qt.UserRole
ROLE_SHARE_MODEL_ID = Qt.UserRole + 1
TYPE_ORG = 0
TYPE_BOOKMARK = 1
def getBookmarkModel(apiClient):
model = QStandardItemModel()
def addBookmarks(item, orgSecondaryReferencesId):
secRefs = apiClient.getSecondaryRefs(orgSecondaryReferencesId)
for bookmark in secRefs["bookmarks"]:
if bookmark["collectionName"] == "shared-models":
summary = bookmark["collectionSummary"]
bookmarkItem = QStandardItem(summary["custFileName"])
bookmarkItem.setData(TYPE_BOOKMARK, ROLE_TYPE)
bookmarkItem.setData(summary["_id"], ROLE_SHARE_MODEL_ID)
item.appendRow(bookmarkItem)
root = model.invisibleRootItem()
if apiClient:
orgs = apiClient.getOrganizations()
for org in orgs:
orgItem = QStandardItem(org["name"])
orgItem.setData(TYPE_ORG, ROLE_TYPE)
root.appendRow(orgItem)
addBookmarks(orgItem, org["orgSecondaryReferencesId"])
return model
# Unused
class FilesData:
"""This class contains all the data of the workspaces and their files under the
structure of a dictionary :
[
{
"Name" : "myWorkspace",
"Description" : "This is my workspace description",
"Url" : "url",
"Type" : "local",
"Files" :
[
{
"custFileName" : basename,
"localUrl" : url,
"isFolder" : False,
"versions" :
[
basename,
basename.fcbak,
...
],
"currentVersion" : basename
"sharingLinks" :
[
]
}
]
}
{
...
}
]
"""
def __init__(self):
self.loadData()
def loadData(self):
with open("filesData.txt", "r") as file:
dataStr = file.read()
if dataStr:
self.data = json.loads(dataStr)
else:
self.data = []
def saveData(self):
with open("filesData.txt", "w") as file:
file.write(json.dumps(self.data))