@@ -103,7 +103,7 @@ def write_error(self, status_code, **kwargs):
103
103
self .write (getattr (exc_info [1 ], 'body' , None ))
104
104
self .finish ()
105
105
106
- async def validate (self , data , schema ):
106
+ async def validate (self , data , schema , ** kwargs ):
107
107
"""
108
108
Method to validate parameters.
109
109
Raises HTTPError(400) with error info for invalid data.
@@ -723,6 +723,10 @@ class ApiItemHandler(ApiHandler):
723
723
Supports R, U, D from CRUDL.
724
724
"""
725
725
726
+ def __init__ (self , * args , ** kwargs ):
727
+ super (ApiItemHandler , self ).__init__ (* args , ** kwargs )
728
+ self ._instance = None
729
+
726
730
@property
727
731
def get_schema_input (self ):
728
732
"""
@@ -782,28 +786,30 @@ def delete_schema_output(self): # pragma: no cover
782
786
async def get_item (self , item_id ):
783
787
"""
784
788
Fetches item from database by PK.
789
+ Result is cached in self._instance for multiple calls
785
790
786
791
:raises: HTTP 404 if no item found.
787
792
:returns: raw object if exists.
788
793
:rtype: ORM model instance.
789
794
"""
790
- try :
791
- return await self .application .objects .get (self .model_cls ,
792
- ** {
793
- self .model_cls ._meta .primary_key .name : item_id })
794
- except (self .model_cls .DoesNotExist , ValueError ) as e :
795
- raise HTTPError (
796
- 404 ,
797
- body = self .get_response (
798
- errors = [
799
- {
800
- 'code' : '' ,
801
- 'message' : 'Item not found' ,
802
- 'detail' : str (e )
803
- }
804
- ]
795
+ if not self ._instance :
796
+ try :
797
+ self ._instance = await self .application .objects .get (self .model_cls ,
798
+ ** {self .model_cls ._meta .primary_key .name : item_id })
799
+ except (self .model_cls .DoesNotExist , ValueError ) as e :
800
+ raise HTTPError (
801
+ 404 ,
802
+ body = self .get_response (
803
+ errors = [
804
+ {
805
+ 'code' : '' ,
806
+ 'message' : 'Item not found' ,
807
+ 'detail' : str (e )
808
+ }
809
+ ]
810
+ )
805
811
)
806
- )
812
+ return self . _instance
807
813
808
814
async def get (self , item_id ):
809
815
"""
@@ -813,7 +819,7 @@ async def get(self, item_id):
813
819
2. Writes serialized object of ORM model instance to response.
814
820
"""
815
821
await self .validate ({k : self .get_argument (k ) for k in self .request .query_arguments .keys ()},
816
- self .get_schema_input )
822
+ self .get_schema_input , item_id = item_id )
817
823
item = await self .get_item (item_id )
818
824
819
825
self .response (result = await self .serialize (item ))
@@ -835,7 +841,7 @@ async def put(self, item_id):
835
841
"""
836
842
item = await self .get_item (item_id )
837
843
838
- data = await self .validate (self .request .body , self .put_schema_input )
844
+ data = await self .validate (self .request .body , self .put_schema_input , item_id = item_id )
839
845
try :
840
846
item = await item ._update (self .application , data )
841
847
except AttributeError as e :
@@ -881,7 +887,7 @@ async def delete(self, item_id):
881
887
:raises: HTTPError 405 if model object is not deletable.
882
888
"""
883
889
# DELETE usually does not have body to validate.
884
- await self .validate (self .request .body or {}, self .delete_schema_input )
890
+ await self .validate (self .request .body or {}, self .delete_schema_input , item_id = item_id )
885
891
item = await self .get_item (item_id )
886
892
try :
887
893
# We can only delete item if model method _delete() is implemented
0 commit comments