Skip to content

Commit 46fa8fe

Browse files
committed
add bfs and recursive updating ... broken?
1 parent 9cbaedb commit 46fa8fe

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

backend/endpoints/logfile.txt

Whitespace-only changes.

backend/endpoints/references.py

+48-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1+
from hmac import new
12
from typing import Iterable
23
from collections import defaultdict, deque
34
import flask
5+
from requests import get
46

57
from backend.common.backend_exceptions import require_permissions
68

79
from backend.common import connect, database
10+
from backend.endpoints.linked_documents import LinkType, db_id_to_path, get_linked_documents, make_document, path_to_db_id
811
from onshape_api.api.api_base import Api
912
from onshape_api.endpoints.permissions import Permission
1013

1114
from flask import current_app
1215
from onshape_api.endpoints import documents, versions
1316
from onshape_api.paths.instance_type import InstanceType
1417
from onshape_api.paths.paths import ElementPath, InstancePath
18+
from onshape_api.utils.str_utils import parens
1519

1620
router = flask.Blueprint("references", __name__)
1721

@@ -151,17 +155,55 @@ def push_version_recursive(**kwargs):
151155

152156
body = connect.get_body("instancesToUpdate")
153157

154-
instances_to_update = [
155-
InstancePath(temp["documentId"], temp["instanceId"]) for temp in body
156-
]
158+
157159

158-
for instance in instances_to_update:
159-
require_permissions(api, instance, Permission.WRITE)
160+
161+
162+
163+
164+
def get_linked_parents(db, instance):
165+
document_db_id = path_to_db_id(instance)
166+
doc = db.linked_documents.document(document_db_id).get()
167+
linked_parents = []
168+
if doc.exists and (data := doc.to_dict()):
169+
for document_db_id in data.get(LinkType.PARENTS, []):
170+
linked_parents.append(db_id_to_path(document_db_id))
171+
172+
return linked_parents
173+
174+
unvisited_nodes = [curr_instance]
175+
176+
sorted_list = []
177+
178+
while unvisited_nodes:
179+
curr_node = unvisited_nodes.pop(0)
180+
sorted_list.append(curr_node)
181+
curr_node_parents = get_linked_parents(db, curr_node)
182+
for parent in curr_node_parents:
183+
if parent not in sorted_list:
184+
unvisited_nodes.append(parent)
185+
else:
186+
raise Exception("Cycle detected")
187+
188+
with open("backend/endpoints/logfile.txt", "a") as log_file:
189+
190+
log_file.write("curr_instance begin\n")
191+
log_file.write(f"{curr_instance}\n")
192+
log_file.write("curr_instance end\n\n")
193+
194+
log_file.write("sorted_list begin\n")
195+
for node in sorted_list:
196+
log_file.write(f"{documents.get_document(api, node)["name"]}\n")
197+
log_file.write("sorted_list end\n\n")
198+
199+
200+
for instance in sorted_list:
201+
require_permissions(api, instance, Permission.WRITE , Permission.LINK)
160202

161203
versions.create_version(api, curr_instance, name, description)
162204

163205
updated_references = 0
164-
for update_instance in instances_to_update:
206+
for update_instance in sorted_list:
165207
updated_references += do_update_references(
166208
api, update_instance, [curr_instance.document_id]
167209
)

0 commit comments

Comments
 (0)