|
| 1 | +import enum |
| 2 | + |
| 3 | + |
| 4 | +class Include(enum.Enum): |
| 5 | + """ |
| 6 | + Include is enum that Provides Options to perform operation to query the result. |
| 7 | +
|
| 8 | + Available Options for QueryOperation are below. |
| 9 | + DEFAULT, ONLY, EXCEPT |
| 10 | +
|
| 11 | + Arguments: |
| 12 | + enum {Include} -- Type of IncludeReference |
| 13 | + """ |
| 14 | + EXCEPT = 'except' |
| 15 | + ONLY = 'only' |
| 16 | + DEFAULT = '' |
| 17 | + |
| 18 | + |
| 19 | +class EntryQueryable: |
| 20 | + """ |
| 21 | + This class is base class for the Entry and Query class that shares common functions |
| 22 | + """ |
| 23 | + def __init__(self): |
| 24 | + self.entry_queryable_param = {} |
| 25 | + |
| 26 | + def locale(self, locale: str): |
| 27 | + """ |
| 28 | + Language code of which the entries needs to be included. |
| 29 | + Only the entries published in this locale will be displayed |
| 30 | +
|
| 31 | + Arguments: |
| 32 | + locale {str} -- locale_code of the language of which the entries needs to be included. |
| 33 | + Only the entries published in this locale will be displayed |
| 34 | +
|
| 35 | + :return: self: so you can chain this call. |
| 36 | +
|
| 37 | + Example (locale for Entry): |
| 38 | + >>> import contentstack |
| 39 | + >>> stack = contentstack.Stack('api_key', 'delivery_token', 'environment') |
| 40 | + >>> content_type = stack.content_type('content_type_uid') |
| 41 | + >>> entry = content_type.entry(uid='entry_uid') |
| 42 | + >>> entry.locale('en-us') |
| 43 | + >>> result = entry.fetch() |
| 44 | +
|
| 45 | + Example (locale for Query): |
| 46 | + >>> import contentstack |
| 47 | + >>> stack = contentstack.Stack('api_key', 'delivery_token', 'environment') |
| 48 | + >>> content_type = stack.content_type('content_type_uid') |
| 49 | + >>> query = content_type.query() |
| 50 | + >>> query.locale('en-us') |
| 51 | + >>> result = query.find() |
| 52 | + """ |
| 53 | + self.entry_queryable_param['locale'] = locale |
| 54 | + return self |
| 55 | + |
| 56 | + def only(self, field_uid): |
| 57 | + """ |
| 58 | + Specifies an array of only keys in BASE object that would be included in the response. |
| 59 | + It refers to the top-level fields of the schema |
| 60 | + :param field_uid: Array of the only reference keys to be included in response |
| 61 | + Returns: |
| 62 | + self -- so you can chain this call. |
| 63 | + """ |
| 64 | + if field_uid is not None: |
| 65 | + self.entry_queryable_param['only[BASE][]'] = field_uid |
| 66 | + return self |
| 67 | + |
| 68 | + def excepts(self, field_uid): |
| 69 | + """ |
| 70 | + Specifies list of field_uid that would be excluded from the response. |
| 71 | + It refers to the top-level fields of the schema |
| 72 | + :param field_uid: to be excluded from the response. |
| 73 | + :return: self -- so you can chain this call. |
| 74 | + """ |
| 75 | + if field_uid is not None: |
| 76 | + self.entry_queryable_param['except[BASE][]'] = field_uid |
| 77 | + return self |
| 78 | + |
| 79 | + def include_reference(self, include_reference_type: Include, reference_field_uid: str, field_uid=None): |
| 80 | + """ |
| 81 | + **Include Reference:** |
| 82 | + When you fetch an entry of a content type that has a reference field, |
| 83 | + by default, the content of the referred entry is not fetched. |
| 84 | + It only fetches the UID of the referred entry, along with the content of |
| 85 | + the specified entry. |
| 86 | +
|
| 87 | + Arguments: |
| 88 | + reference_field_uid {str} -- Key who has reference to some other class object. |
| 89 | + Array of the only reference keys to be included in response |
| 90 | + include_type {Include} -- Provides three options, none, only and except |
| 91 | + i.e accepts list of field_uid |
| 92 | + field_uid {list} -- list of field_uid on which include operation to perform |
| 93 | +
|
| 94 | + Returns: |
| 95 | + self -- So you can chain this call. |
| 96 | + """ |
| 97 | + container = {} |
| 98 | + if reference_field_uid is None: |
| 99 | + raise KeyError("reference_field_uid can't be None") |
| 100 | + if include_reference_type.name == 'DEFAULT': |
| 101 | + self.entry_queryable_param["include[]"] = reference_field_uid |
| 102 | + else: |
| 103 | + container[reference_field_uid] = field_uid |
| 104 | + self.entry_queryable_param["include[]"] = {include_reference_type.value: container} |
| 105 | + return self |
| 106 | + |
| 107 | + def include_content_type(self): |
| 108 | + """ |
| 109 | + This method also includes the ContentType in the entry |
| 110 | + :return: self: so you can chain this call. |
| 111 | +
|
| 112 | + ------------------------------- |
| 113 | +
|
| 114 | + [Example: for Entry] |
| 115 | +
|
| 116 | + >>> import contentstack |
| 117 | + >>> stack = contentstack.Stack('api_key', 'delivery_token', 'environment') |
| 118 | + >>> content_type = stack.content_type('content_type_uid') |
| 119 | + >>> entry = content_type.entry('uid') |
| 120 | + >>> entry.include_content_type() |
| 121 | + >>> result = entry.fetch() |
| 122 | +
|
| 123 | + ------------------------------- |
| 124 | +
|
| 125 | + [Example: for Query:] |
| 126 | +
|
| 127 | + >>> import contentstack |
| 128 | + >>> stack = contentstack.Stack('api_key', 'delivery_token', 'environment') |
| 129 | + >>> content_type = stack.content_type('content_type_uid') |
| 130 | + >>> query = content_type.query() |
| 131 | + >>> query.include_content_type() |
| 132 | + >>> result = query.find() |
| 133 | + ------------------------------- |
| 134 | + """ |
| 135 | + self.entry_queryable_param['include_content_type'] = 'true' |
| 136 | + self.entry_queryable_param['include_global_field_schema'] = 'true' |
| 137 | + return self |
| 138 | + |
| 139 | + def include_reference_content_type_uid(self): |
| 140 | + """ |
| 141 | + This method also includes the content type UIDs |
| 142 | + of the referenced entries returned in the response |
| 143 | + Returns: |
| 144 | + :return: self: so you can chain this call. |
| 145 | +
|
| 146 | + [Example for Query] |
| 147 | + >>> import contentstack |
| 148 | + >>> stack = contentstack.Stack('api_key', 'delivery_token', 'environment') |
| 149 | + >>> content_type = stack.content_type('content_type_uid') |
| 150 | + >>> query = content_type.query() |
| 151 | + >>> query = query.include_reference_content_type_uid() |
| 152 | + >>> result = query.find() |
| 153 | +
|
| 154 | + [Example for Entry] |
| 155 | + >>> import contentstack |
| 156 | + >>> stack = contentstack.Stack('api_key', 'delivery_token', 'environment') |
| 157 | + >>> entry = stack.content_type('content_type_uid').entry('entry_uid') |
| 158 | + >>> entry = entry.include_reference_content_type_uid() |
| 159 | + >>> result = entry.fetch() |
| 160 | + """ |
| 161 | + self.entry_queryable_param['include_reference_content_type_uid'] = 'true' |
| 162 | + return self |
| 163 | + |
| 164 | + def add_param(self, key: str, value: str): |
| 165 | + """ |
| 166 | + This method adds key and value to an Entry. |
| 167 | + :param key: The key as string which needs to be added to an Entry |
| 168 | + :param value: The value as string which needs to be added to an Entry |
| 169 | + :return: self: object, so you can chain this call. |
| 170 | +
|
| 171 | + Example: Call from Query => |
| 172 | + >>> import contentstack |
| 173 | + >>> stack = contentstack.Stack('api_key', 'delivery_token', 'environment') |
| 174 | + >>> query = stack.content_type('content_type_uid').query() |
| 175 | + >>> query = query.include_reference_content_type_uid() |
| 176 | + >>> result = query.find() |
| 177 | +
|
| 178 | + Example: Call from Entry => |
| 179 | + >>> import contentstack |
| 180 | + >>> stack = contentstack.Stack('api_key', 'delivery_token', 'environment') |
| 181 | + >>> entry = stack.content_type('content_type_uid').entry('entry_uid') |
| 182 | + >>> entry = entry.include_reference_content_type_uid() |
| 183 | + >>> result = entry.fetch() |
| 184 | +
|
| 185 | + """ |
| 186 | + if None not in (key, value): |
| 187 | + self.entry_queryable_param[key] = value |
0 commit comments