|
| 1 | +from hmac import new |
1 | 2 | from typing import Iterable
|
2 | 3 | from collections import defaultdict, deque
|
3 | 4 | import flask
|
| 5 | +from requests import get |
4 | 6 |
|
5 | 7 | from backend.common.backend_exceptions import require_permissions
|
6 | 8 |
|
7 | 9 | 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 |
8 | 11 | from onshape_api.api.api_base import Api
|
9 | 12 | from onshape_api.endpoints.permissions import Permission
|
10 | 13 |
|
11 | 14 | from flask import current_app
|
12 | 15 | from onshape_api.endpoints import documents, versions
|
13 | 16 | from onshape_api.paths.instance_type import InstanceType
|
14 | 17 | from onshape_api.paths.paths import ElementPath, InstancePath
|
| 18 | +from onshape_api.utils.str_utils import parens |
15 | 19 |
|
16 | 20 | router = flask.Blueprint("references", __name__)
|
17 | 21 |
|
@@ -151,17 +155,55 @@ def push_version_recursive(**kwargs):
|
151 | 155 |
|
152 | 156 | body = connect.get_body("instancesToUpdate")
|
153 | 157 |
|
154 |
| - instances_to_update = [ |
155 |
| - InstancePath(temp["documentId"], temp["instanceId"]) for temp in body |
156 |
| - ] |
| 158 | + |
157 | 159 |
|
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) |
160 | 202 |
|
161 | 203 | versions.create_version(api, curr_instance, name, description)
|
162 | 204 |
|
163 | 205 | updated_references = 0
|
164 |
| - for update_instance in instances_to_update: |
| 206 | + for update_instance in sorted_list: |
165 | 207 | updated_references += do_update_references(
|
166 | 208 | api, update_instance, [curr_instance.document_id]
|
167 | 209 | )
|
|
0 commit comments