1111import empty
1212
1313from contentstack .basequery import BaseQuery
14+ from contentstack .deep_merge_lp import DeepMergeMixin
1415from contentstack .entryqueryable import EntryQueryable
1516
1617log = logging .getLogger (__name__ )
@@ -262,9 +263,21 @@ def include_embedded_items(self):
262263 return self
263264
264265 def include_metadata (self ):
265- """
266- include_metadata includes metadata in the response
267- """
266+ """include_metadata instance of Query
267+ includes metadata in the response (Entries and Assets) along with entry/entries details.
268+ :return: Query, so we can chain the call
269+
270+ ----------------------------
271+ Example:
272+
273+ >>> import contentstack
274+ >>> stack = contentstack.Stack('api_key', 'delivery_token', 'environment')
275+ >>> content_type = stack.content_type('content_type_uid')
276+ >>> query = content_type.query()
277+ >>> query = query.include_metadata()
278+ >>> result = query.find()
279+ ----------------------------
280+ """
268281 self .query_params ['include_metadata' ] = 'true'
269282 return self
270283
@@ -303,15 +316,6 @@ def find_one(self):
303316 self .query_params ["limit" ] = 1
304317 return self .__execute_network_call ()
305318
306- def __validate_live_preview (self ):
307- live_preview = self .http_instance .live_preview
308- if 'enable' in live_preview and live_preview ['enable' ] \
309- and self .content_type_uid == live_preview ['content_type_uid' ]:
310- if 'live_preview' in live_preview :
311- self .query_params ['live_preview' ] = live_preview ['live_preview' ]
312- else :
313- self .query_params ['live_preview' ] = 'init' # initialise
314-
315319 def __execute_network_call (self ):
316320 if len (self .entry_queryable_param ) > 0 :
317321 self .query_params .update (self .entry_queryable_param )
@@ -321,29 +325,29 @@ def __execute_network_call(self):
321325 self .query_params ['environment' ] = self .http_instance .headers ['environment' ]
322326 encoded_string = parse .urlencode (self .query_params , doseq = True )
323327 url = f'{ self .base_url } ?{ encoded_string } '
324- if self .http_instance .live_preview ['enable' ]:
325- if self .http_instance .live_preview ['content_type_uid' ] == self .content_type_uid :
326- _rq = self .http_instance .get (url )['entries' ]
327- _preview = self .http_instance .live_preview ['resp' ]
328- return self ._merge_preview (_rq , _preview )
329- self ._validate_live_preview ()
330- return self .http_instance .get (url )
331-
332- def _validate_live_preview (self ):
333- lp = self .http_instance .live_preview
334- if 'content_type_uid' in lp and lp ['content_type_uid' ] is not None :
335- if lp ['content_type_uid' ] != str (self .content_type_uid ):
336- self .http_instance .live_preview ['enable' ] = False
328+ self ._impl_live_preview ()
329+ response = self .http_instance .get (url )
330+ if self .http_instance .live_preview is not None and not 'errors' in response :
331+ self .http_instance .live_preview ['entry_response' ] = response ['entries' ]
332+ return self ._merged_response ()
333+ return response
334+
335+ def _impl_live_preview (self ):
336+ lv = self .http_instance .live_preview
337+ if lv is not None and lv ['enable' ] and 'content_type_uid' in lv and lv [
338+ 'content_type_uid' ] == self .content_type_uid :
339+ url = lv ['url' ]
340+ self .http_instance .headers ['authorization' ] = lv ['management_token' ]
341+ lp_resp = self .http_instance .get (url )
342+ if lp_resp is not None and not 'error_code' in lp_resp :
343+ self .http_instance .live_preview ['lp_response' ] = lp_resp
344+ return None
345+ return None
346+
347+ def _merged_response (self ):
348+ if 'entry_response' in self .http_instance .live_preview and 'lp_response' in self .http_instance .live_preview :
349+ entry_response = self .http_instance .live_preview ['entry_response' ]['entries' ]
350+ lp_response = self .http_instance .live_preview ['lp_response' ]
351+ merged_response = DeepMergeMixin (entry_response , lp_response )
352+ return merged_response .entry_response
337353 pass
338-
339- # def _merge_preview(self, qresp, _preview):
340- # if isinstance(qresp, dict):
341- # if 'uid' in qresp and qresp['uid'] == _preview['uid']:
342- # merged = {**qresp, **_preview} # TODO: Check merging is properly written or not
343- # else:
344- # for key in dict.keys():
345- # qresp[key] = self._merge_preview(qresp[key])
346- # elif isinstance(qresp, list):
347- # for index, it in enumerate(qresp):
348- # qresp[index] = self._merge_preview(it, _preview)
349- # return qresp
0 commit comments