Skip to content

Commit

Permalink
WIP notes for using uid ?
Browse files Browse the repository at this point in the history
  • Loading branch information
duboyal committed Apr 5, 2024
1 parent 71210d3 commit b191cc8
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 11 deletions.
68 changes: 61 additions & 7 deletions src/cript/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,14 +431,26 @@ def save(self, new_node):
quit()
self._internal_save(new_node, _no_condense_uuid=False)

def _internal_save(self, new_node: PrimaryBaseNode, _no_condense_uuid: bool) -> None:
print("_no_condense_uuid")
print(_no_condense_uuid)
def _internal_save(self, new_node: PrimaryBaseNode, preknown_uid: str, _no_condense_uuid: bool) -> None:
"""
NOTE: for Ludwig
WIP NOTES:
unfinished WIP, but the idea is we want to send
a preknown_uuid
I'm imagining something like
data = new_node.get_json(preknown_uid="preknown uid into here").json
in the same way we did the _no_condense_uuid
but, then , I see where I would add a preknown uuid
scroll down to
data = new_node.get_json(_no_condense_uuid=_no_condense_uuid).json
WIP NOTES CONTINUED:
"""

print("data")
print(data)
print("----------\\------------\n")

node_class_name = new_node.node_type.capitalize()
Expand All @@ -454,15 +466,31 @@ def _internal_save(self, new_node: PrimaryBaseNode, _no_condense_uuid: bool) ->
# or else its a patch handled by previous node

if new_node.node_type.lower() == "project":
# data = new_node.get_json(_no_condense_uuid=_no_condense_uuid).json
# data = new_node.get_json(sort_keys=False, condense_to_uuid={}, indent=2).json # indent=2

# data = new_node.get_json(_no_condense_uuid=_no_condense_uuid).json

data = new_node.get_json(_no_condense_uuid=_no_condense_uuid).json

print("---- data -----")
# data = new_node.get_json(condense_to_uuid={}).json
print("---- data 2 -----")
# print(type(data2))
# print(type(json.loads(data2)))
# data = json.loads(data)
# print(type(data))
# print(str(data))
print(data)
# data = str(data)
# data = json.dumps(data)
# data = data.replace('""', "[]")
# print("now data\n", data)
print("---- data end -----")
# if _no_condense_uuid is true do a POST if its false do a patch,
# but wouldnt we then just find the existing node above in the generator?
response = self._capsule_request(url_path="/project/", method="POST", data=data)
if response.status_code in [200, 201]:
print("FINALLY_WORKED!")
return # Return here, since we successfully Posting
else: # debug for now
print("GET HERE ALI")
Expand Down Expand Up @@ -506,6 +534,32 @@ def _internal_save(self, new_node: PrimaryBaseNode, _no_condense_uuid: bool) ->

url_path = f"/{new_node.node_type}/{new_node.uuid}"

"""
WIP NOTES CONTINUED:
this is where we would need to make a map of the uids
patch_map[uid_] ? constructed above?
problem right now is ,
we need the uids to be in place for the original POST json
which happens up above
so I'm wondeing how to go about that,
and I think I would need to rewalk the original get_json for the post
switching any {uuid: "uuid"} for {uid:"uid"}
AND I TRY TO DO THAT if you look inside
json.py , you'd find the following below
######## WIP HERE ################
if self.preknown_uid:
element = {"uid": str(uid)}
return element, uid
but we need this uid map and i'm not so sure where to put this ?
"""
for uuid_ in reversed(patch_map.keys_sorted_by_last_modified()):
node = patch_map[uuid_]

Expand Down
4 changes: 4 additions & 0 deletions src/cript/nodes/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ def get_json(
"Collection": {"member", "admin"},
},
_no_condense_uuid: bool = False,
preknown_uid: str = "",
**kwargs,
):
"""
Expand Down Expand Up @@ -489,6 +490,7 @@ class ReturnTuple:

previous_no_condense_uuid = copy.deepcopy(NodeEncoder.no_condense_uuid)
NodeEncoder.no_condense_uuid = _no_condense_uuid
NodeEncoder.preknown_uid = preknown_uid

try:
tmp_json = json.dumps(self, cls=NodeEncoder, **kwargs)
Expand All @@ -508,6 +510,8 @@ class ReturnTuple:
NodeEncoder.suppress_attributes = previous_suppress_attributes
NodeEncoder.condense_to_uuid = previous_condense_to_uuid
NodeEncoder.no_condense_uuid = previous_no_condense_uuid
# HERE??
# NodeEncoder.preknown_uid = preknown_uid

def find_children(self, search_attr: dict, search_depth: int = -1, handled_nodes: Optional[List] = None) -> List:
"""
Expand Down
16 changes: 12 additions & 4 deletions src/cript/nodes/util/json.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This module contains classes and functions that help with the json serialization and deserialization of nodes.
"""

import dataclasses
import inspect
import json
Expand Down Expand Up @@ -67,6 +68,7 @@ class NodeEncoder(json.JSONEncoder):
condense_to_uuid: Dict[str, Set[str]] = dict()
suppress_attributes: Optional[Dict[str, Set[str]]] = None
no_condense_uuid: bool = False
preknown_uid: str = ""

def default(self, obj):
"""
Expand Down Expand Up @@ -183,12 +185,18 @@ def strip_to_edge_uuid(element):
except AttributeError:
uid = element["uid"]

if self.no_condense_uuid:
element = ""
else:
element = {"uuid": str(uuid)}
######## WIP HERE ################
if self.preknown_uid:
element = {"uid": str(uid)}
return element, uid

#########################
# if self.no_condense_uuid:
# element = ""
# else:
# element = {"uuid": str(uuid)}
# return element, uid

# Processes an attribute based on its type (list or single element)
if isinstance(attribute, List):
processed_elements = []
Expand Down

0 comments on commit b191cc8

Please sign in to comment.