44"""
55import copy
66import uuid
7+ from dateutil import parser
78from hydrus .data .db_models import Resource
89from hydrus .data .exceptions import (
910 ClassNotFound ,
1314 PropertyNotFound ,
1415 PropertyNotGiven ,
1516 MemberInstanceNotFound ,
17+ InvalidDateTimeFormat
1618)
1719from sqlalchemy import exists
1820from sqlalchemy .exc import InvalidRequestError , IntegrityError
1921from sqlalchemy .orm .scoping import scoped_session
2022from sqlalchemy .orm .exc import NoResultFound
23+ from hydra_python_core .doc_writer import HydraDoc
2124from typing import Dict , Any
2225
2326
@@ -34,6 +37,39 @@ def get_type(object_: Dict[str, Any]) -> str:
3437 return type_
3538
3639
40+ def get_modified_object (object_ : Dict [str , Any ], doc : HydraDoc , path : str ) -> Dict [str , Any ]:
41+ """
42+ Return the object after modifying properties.
43+ :param object_: Dict containing object properties
44+ :param doc: HydraDoc object
45+ :param path: Path of the Class or Collection
46+ :return: object_ containing properties
47+ """
48+ class_ = doc .parsed_classes [path ]
49+ properties = class_ ["class" ].supportedProperty
50+ datetimefields = []
51+ for prop in properties :
52+ kwargs = getattr (prop , 'kwargs' , None )
53+ range = kwargs .get ('range' )
54+ title = getattr (prop , 'title' )
55+ if range is not None :
56+ if "dateTime" in range :
57+ datetimefield = title
58+ datetimefields .append (datetimefield )
59+ if len (datetimefields ) != 0 :
60+ for field in datetimefields :
61+ try :
62+ datetime_value = object_ .get (field )
63+ dt_object = parser .isoparse (datetime_value )
64+ object_ [field ] = dt_object
65+ except ValueError :
66+ raise InvalidDateTimeFormat (field )
67+ except TypeError :
68+ datetime_value = object_ .get (field )
69+ object_ [field ] = datetime_value
70+ return object_
71+
72+
3773def get_database_class (type_ : str ):
3874 """
3975 Get the sqlalchemy class object from given classname
@@ -47,10 +83,11 @@ def get_database_class(type_: str):
4783
4884
4985def insert_object (
50- object_ : Dict [str , Any ], session : scoped_session , collection : bool = False
86+ doc_ : HydraDoc , object_ : Dict [str , Any ], session : scoped_session , collection : bool = False
5187) -> str :
5288 """
5389 Insert the object in the database
90+ :param doc_: HydraDoc object
5491 :param object_: Dict containing object properties
5592 :param session: sqlalchemy session
5693 :return: The ID of the inserted object
@@ -94,16 +131,20 @@ def insert_object(
94131 fk_column = fk .info ["column_name" ]
95132 try :
96133 fk_object = object_ [fk_column ]
134+ fk_object_path = fk_object .get ('@type' )
135+ fk_object = get_modified_object (fk_object , doc_ , fk_object_path )
97136 except KeyError as e :
98137 wrong_property = e .args [0 ]
99138 raise PropertyNotGiven (type_ = wrong_property )
100139 # insert the foreign key object
101- fk_object_id = insert_object (fk_object , session )
140+ fk_object_id = insert_object (doc_ , fk_object , session )
102141 # put the id of the foreign instance in this table's column
103142 object_ [fk_column ] = fk_object_id
104143 try :
105144 # remove the @type from object before using the object to make a
106145 # instance of it using sqlalchemy class
146+ object_path = object_ .get ('@type' )
147+ object_ = get_modified_object (object_ , doc_ , object_path )
107148 object_ .pop ("@type" )
108149 inserted_object = database_class (** object_ )
109150 except TypeError as e :
@@ -198,13 +239,15 @@ def delete_object(
198239
199240
200241def update_object (
242+ doc_ : HydraDoc ,
201243 object_ : Dict [str , Any ],
202244 query_info : Dict [str , str ],
203245 session : scoped_session ,
204246 collection : bool = False ,
205247) -> str :
206248 """
207249 Update the object from the database
250+ :param doc_: HydraDoc object
208251 :param object_: Dict containing updated object properties
209252 :param query_info: Dict containing the id and @type of object that has to retrieved
210253 :param session: sqlalchemy session
@@ -218,11 +261,11 @@ def update_object(
218261 # Try inserting new object
219262 try :
220263 object_ ["id" ] = id_
221- d = insert_object (object_ , session , collection )
264+ d = insert_object (doc_ , object_ , session , collection )
222265 except Exception as e :
223266 # Put old object back
224267 old_object ["id" ] = id_
225- d = insert_object (old_object , session , collection )
268+ d = insert_object (doc_ , old_object , session , collection )
226269 raise e
227270 return id_
228271
0 commit comments