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