1
+ import logging
2
+ from urllib import parse
3
+
4
+ from contentstack .deep_merge_lp import DeepMergeMixin
5
+ from contentstack .entryqueryable import EntryQueryable
6
+
7
+ class Variants (EntryQueryable , ):
8
+ """
9
+ An entry is the actual piece of content that you want to publish.
10
+ Entries can be created for one of the available content types.
11
+
12
+ Entry works with
13
+ version={version_number}
14
+ environment={environment_name}
15
+ locale={locale_code}
16
+ """
17
+
18
+ def __init__ (self ,
19
+ http_instance = None ,
20
+ content_type_uid = None ,
21
+ entry_uid = None ,
22
+ variant_uid = None ,
23
+ params = None ,
24
+ logger = None ):
25
+
26
+ super ().__init__ ()
27
+ EntryQueryable .__init__ (self )
28
+ self .entry_param = {}
29
+ self .http_instance = http_instance
30
+ self .content_type_id = content_type_uid
31
+ self .entry_uid = entry_uid
32
+ self .variant_uid = variant_uid
33
+ self .logger = logger or logging .getLogger (__name__ )
34
+ self .entry_param = params or {}
35
+
36
+ def find (self , params = None ):
37
+ """
38
+ find the variants of the entry of a particular content type
39
+ :param self.variant_uid: {str} -- self.variant_uid
40
+ :return: Entry, so you can chain this call.
41
+ """
42
+ headers = self .http_instance .headers .copy () # Create a local copy of headers
43
+ if isinstance (self .variant_uid , str ):
44
+ headers ['x-cs-variant-uid' ] = self .variant_uid
45
+ elif isinstance (self .variant_uid , list ):
46
+ headers ['x-cs-variant-uid' ] = ',' .join (self .variant_uid )
47
+
48
+ if params is not None :
49
+ self .entry_param .update (params )
50
+ encoded_params = parse .urlencode (self .entry_param )
51
+ endpoint = self .http_instance .endpoint
52
+ url = f'{ endpoint } /content_types/{ self .content_type_id } /entries?{ encoded_params } '
53
+ self .http_instance .headers .update (headers )
54
+ result = self .http_instance .get (url )
55
+ self .http_instance .headers .pop ('x-cs-variant-uid' , None )
56
+ return result
57
+
58
+ def fetch (self , params = None ):
59
+ """
60
+ This method is useful to fetch variant entries of a paticular content type and entries of the of the stack.
61
+ :return:dict -- contentType response
62
+ ------------------------------
63
+ Example:
64
+
65
+ >>> import contentstack
66
+ >>> stack = contentstack.Stack('api_key', 'delivery_token', 'environment')
67
+ >>> content_type = stack.content_type('content_type_uid')
68
+ >>> some_dict = {'abc':'something'}
69
+ >>> response = content_type.fetch(some_dict)
70
+ ------------------------------
71
+ """
72
+ """
73
+ Fetches the variants of the entry
74
+ :param self.variant_uid: {str} -- self.variant_uid
75
+ :return: Entry, so you can chain this call.
76
+ """
77
+ if self .entry_uid is None :
78
+ raise ValueError ("entry_uid is required" )
79
+ else :
80
+ headers = self .http_instance .headers .copy () # Create a local copy of headers
81
+ if isinstance (self .variant_uid , str ):
82
+ headers ['x-cs-variant-uid' ] = self .variant_uid
83
+ elif isinstance (self .variant_uid , list ):
84
+ headers ['x-cs-variant-uid' ] = ',' .join (self .variant_uid )
85
+
86
+ if params is not None :
87
+ self .entry_param .update (params )
88
+ encoded_params = parse .urlencode (self .entry_param )
89
+ endpoint = self .http_instance .endpoint
90
+ url = f'{ endpoint } /content_types/{ self .content_type_id } /entries/{ self .entry_uid } ?{ encoded_params } '
91
+ self .http_instance .headers .update (headers )
92
+ result = self .http_instance .get (url )
93
+ self .http_instance .headers .pop ('x-cs-variant-uid' , None )
94
+ return result
0 commit comments