Skip to content

Commit 5389f66

Browse files
shaileshmishrashaileshmishra
authored andcommitted
README.md updated
1 parent f051146 commit 5389f66

File tree

20 files changed

+815
-134
lines changed

20 files changed

+815
-134
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ MANIFEST
3131
.idea
3232
.vscode
3333
.report.log
34+
env
35+
venv
3436

3537
# PyInstaller
3638
# Usually these files are written by a python script from a template

README.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Contentstack Utility
2+
3+
This guide will help you get started with Contentstack Python Utils SDK to build apps powered by Contentstack.
4+
5+
## Prerequisites
6+
7+
The latest version of [PyCharm](https://www.jetbrains.com/pycharm/download/) or [Visual Studio Code](https://code.visualstudio.com/download)
8+
9+
[Python 3](https://docs.python-guide.org/starting/installation/#python-3-installation-guides)
10+
11+
[Create virtual environment](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/#creating-a-virtual-environment)
12+
13+
[Activate virtual environment](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/#activating-a-virtual-environment)
14+
15+
## SDK Installation and Setup
16+
17+
To set up Python Utils SDK, Go to terminal and locate the virtual environment path and type below.
18+
19+
```python
20+
pip install contentstack_utils
21+
```
22+
23+
If you are using Contentstack Python SDK in your project by running the following commands, then “contentstack_utils” is already imported into your project.
24+
25+
## For the latest version
26+
27+
```python
28+
pip install Contentstack
29+
```
30+
31+
## For the specific version
32+
33+
```python
34+
pip install Contentstack==1.3.0
35+
```
36+
37+
## Usage
38+
39+
Let’s learn how you can use Utils SDK to render embedded items.
40+
41+
### Create Render Option
42+
43+
To render embedded items on the front-end, use the renderContents function, and define the UI elements you want to show in the front-end of your website, as shown in the example code below:
44+
45+
```python
46+
47+
Utils.renderContents(rteArray, localJsonObj, (embedded_object, metadata) -> {
48+
49+
switch (metadata.get_style_type()) {
50+
51+
case BLOCK:
52+
title = embedded_object['title']
53+
multi_line = embedded_object['title']
54+
return '<p>'+title+'</p><span>'+multi_line+'</span>'
55+
56+
case INLINE:
57+
title_inline = embedded_object['title']
58+
ml_inline = embedded_object['multi_line']
59+
return ¸<p>'+title_inline+'</p><span>'+ml_inline+'</span>'
60+
61+
case LINKED:
62+
title_linked = embedded_object['title']
63+
ml_linked = embedded_object['multi_line']
64+
return '<p>'+title_linked+'</p><span>'+ml_linked+'</span>'
65+
66+
case DISPLAYABLE:
67+
title_diplayable = embedded_object['title']
68+
ml_diplayable = embedded_object['multi_line']
69+
return '<p>'+title_diplayable+'</p><span>'+ml_diplayable+ '</span>'
70+
71+
default:
72+
return null;
73+
}
74+
});
75+
76+
```
77+
78+
## Basic Queries
79+
80+
Contentstack Utils SDK lets you interact with the Content Delivery APIs and retrieve embedded items from the RTE field of an entry.
81+
82+
## Fetch Embedded Item(s) from a Single Entry
83+
84+
To get an embedded item of a single entry, you need to provide the stack API key, environment name, content type’s UID, and entry’s UID. Then, use the `entry.fetch` function as shown below:
85+
86+
```python
87+
stack = contentstack.Stack('api_key','delivery_token','environment')
88+
content_type = stack.content_type("content_type_uid")
89+
entry = content_type.entry("entry_uid")
90+
result = entry.fetch()
91+
```
92+
93+
## Fetch Embedded Item(s) from Multiple Entries
94+
95+
To get embedded items from multiple entries, you need to provide the stack API key, delivery token, environment name, and content type’s UID.
96+
97+
```python
98+
stack = contentstack.Stack('api_key','delivery_token','environment')
99+
query = stack.content_type("content_type_uid").query()
100+
result = query.find()
101+
```
102+
103+
Fetch entry/entries and Render RTE using GraphQL and ‘@contentstack_utils’ SDK

contentstack/__init__.py

Lines changed: 0 additions & 20 deletions
This file was deleted.

contentstack/embedded/item_type.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

contentstack/helper/metadata.py

Lines changed: 0 additions & 49 deletions
This file was deleted.

contentstack_utils/__init__.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# pip install -r requirements.txt
2+
# pytest --html=tests/report/test-report.html
3+
4+
"""
5+
The __init__.py files are required to make Python treat the directories as containing
6+
packages; this is done to prevent directories with a common name, such as string,
7+
from unintentionally hiding valid modules that occur later on the module search path
8+
Used: Safety checks your installed dependencies for known security vulnerabilities
9+
file __init__.py contains package information like
10+
11+
__author__, __status__, __version__, __endpoint__ and __email__
12+
13+
`Your code has been rated at 10.00/10`
14+
"""
15+
16+
from contentstack_utils.embedded.item_type import ItemType
17+
from contentstack_utils.embedded.styletype import StyleType
18+
from contentstack_utils.helper.metadata import Metadata
19+
from contentstack_utils.render.default_options import DefaultOptions
20+
from contentstack_utils.render.options import OptionsCallback
21+
from contentstack_utils.utils import Utils
22+
23+
__title__ = 'contentstack_utils'
24+
__author__ = 'contentstack'
25+
__status__ = 'debug'
26+
__version__ = '0.0.1'
27+
__endpoint__ = 'cdn.contentstack.io'
28+
__email__ = 'shailesh.mishra@contentstack.com'
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""
2+
ItemType is Enumeration class that conatains two options for ItemType:
3+
4+
ASSET
5+
6+
ENTRY
7+
"""
8+
9+
import enum
10+
11+
12+
class ItemType(enum.Enum):
13+
"""
14+
Contains Two option for ItemsType
15+
16+
ENTRY
17+
ASSET
18+
"""
19+
ENTRY = 'entry'
20+
ASSET = 'asset'
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
""""
2+
There are two types StyleType ENTRY and ASSETS
23
For `Entry`: StyleType.BLOCK, StyleType.INLINE, StyleType.LINKED,
34
For `Assets`: StyleType.DISPLAY, StyleType.DOWNLOADABLE
45
"""
@@ -7,6 +8,16 @@
78

89
class StyleType(enum.Enum):
910

11+
"""
12+
This StyleType contains four options like below.
13+
14+
BLOCK
15+
INLINE
16+
LINK
17+
DISPLAY
18+
DOWNLOADABLE
19+
"""
20+
1021
BLOCK = "block"
1122
INLINE = 'inline'
1223
LINK = 'link'
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
"""
2+
Metadata is the model class for embedded objects
3+
4+
Returns:
5+
str: text, item_type, item_uid, type_uid, style_type, outer_html and attributes
6+
"""
7+
8+
import enum
9+
10+
11+
# pylint: disable=too-many-arguments
12+
class StyleType(enum.Enum):
13+
"""
14+
This StyleType contains four options like below.
15+
16+
BLOCK
17+
INLINE
18+
LINK
19+
DISPLAY
20+
DOWNLOADABLE
21+
"""
22+
23+
BLOCK = "block"
24+
INLINE = 'inline'
25+
LINK = 'link'
26+
DISPLAY = 'displayable'
27+
DOWNLOADABLE = 'downloadable'
28+
29+
30+
class Metadata:
31+
"""
32+
model helper class to set and get value
33+
"""
34+
35+
def __init__(self, text: str, item_type: str, item_uid: str,
36+
content_type_uid: str, style_type: StyleType,
37+
outer_html: str, attributes: str):
38+
"""Used to set the value to the variables
39+
40+
Args:
41+
text (str): [text for embedded objects]
42+
item_type (str): [item_type for embedded objects]
43+
item_uid (str): [item_uid for embedded objects]
44+
content_type_uid (str): [content_type_uid for embedded objects]
45+
style_type (StyleType): [style_type for embedded objects]
46+
outer_html (str): [outer_html for embedded objects]
47+
attributes (str): [attributes for embedded objects]
48+
"""
49+
self.text = text
50+
self.item_type = item_type
51+
self.item_uid = item_uid
52+
self.content_type_uid = content_type_uid
53+
self.style_type = style_type
54+
self.outer_html = outer_html
55+
self.attributes = attributes
56+
57+
# getter get_text() method
58+
@property
59+
def get_text(self):
60+
"""retruns text for embedded objects
61+
62+
Returns:
63+
str : text for embedded objects
64+
"""
65+
return self.text
66+
67+
# getter get_item_type() method
68+
@property
69+
def get_item_type(self):
70+
"""retruns item_type for embedded objects
71+
72+
Returns:
73+
str : item_type for embedded objects
74+
"""
75+
return self.item_type
76+
77+
# getter get_item_uid() method
78+
@property
79+
def get_item_uid(self):
80+
"""retruns item_uid for embedded objects
81+
82+
Returns:
83+
str : item_uid for embedded objects
84+
"""
85+
return self.item_uid
86+
87+
# getter get_content_type_uid() method
88+
@property
89+
def get_content_type_uid(self):
90+
"""retruns content_type_uid for embedded objects
91+
92+
Returns:
93+
str : content_type_uid for embedded objects
94+
"""
95+
return self.content_type_uid
96+
97+
# getter get_style_type() method
98+
@property
99+
def get_style_type(self):
100+
"""retruns style_type for embedded objects
101+
102+
Returns:
103+
StyleType : style_type for embedded objects
104+
"""
105+
return self.style_type
106+
107+
# getter get_outer_html() method
108+
@property
109+
def get_outer_html(self):
110+
"""retruns outer_html for embedded objects
111+
112+
Returns:
113+
str : outer_html for embedded objects
114+
"""
115+
return self.outer_html
116+
117+
# getter get_attributes() method
118+
@property
119+
def get_attributes(self):
120+
""" retruns attributes for embedded objects
121+
122+
Returns:
123+
str : attributes for embedded objects
124+
"""
125+
return self.attributes

0 commit comments

Comments
 (0)