Skip to content

Instantly share code, notes, and snippets.

bgcolor = document.get_color("article.background")
hex = bgcolor.value
video = document.get_embed("article.video")
# Html is the code to include to embed the object, and depends on the embedded service
html = video and video.as_html
# Date and Timestamp predicates
date_before = predicates.date_before("my.product.releaseDate", datetime.datetime(2014, 6, 1))
date_after = predicates.date_after("my.product.releaseDate", datetime.datetime(2014, 1, 1))
date_Between = predicates.date_between("my.product.releaseDate", datetime.datetime(2014, 1, 1), datetime.datetime(2014, 6, 1))
day_of_month = predicates.day_of_month("my.product.releaseDate", 14)
day_of_month_after = predicates.day_of_month_after("my.product.releaseDate", 14)
day_of_month_before = predicates.day_of_month_before("my.product.releaseDate", 14)
day_of_week = predicates.day_of_week("my.product.releaseDate", "Tuesday")
day_of_week_after = predicates.day_of_week_after("my.product.releaseDate", "Wednesday")
day_of_week_before = predicates.day_of_month_before("my.product.releaseDate", "Wednesday")
# Number predicates
gt = predicates.gt("my.product.price", 10)
lt = predicates.lt("my.product.price", 20)
in_range = predicates.in_range("my.product.price", 10, 20)
# Accessing number fields
price = doc.get_number("product.price").value
author = doc.get_text("blog-post.author")
if author is None:
author = "Anonymous"
# "at" predicate: equality of a fragment to a value.
at = predicates.at("document.type", "article")
# "any" predicate: equality of a fragment to a value.
any = predicates.any("document.type", ["article", "blog-post"])
# "fulltext" predicate: fulltext search in a fragment.
fulltext = predicates.fulltext("my.article.body", "sausage")
# "similar" predicate, with a document id as reference
similar = predicates.similar("UXasdFwe42D", 10)
api = prismic.get('https://lesbonneschoses.prismic.io/api')
response = api.form('everything')\
.ref(api.get_master())\
.query(predicates.at("document.type", "product"))\
.pageSize(100)\
.orderings('[my.product.price desc]')\
.submit()
# The products are now ordered by price, highest first
results = response.results
preview_token = 'MC5VbDdXQmtuTTB6Z0hNWHF3.c--_vVbvv73vv73vv73vv71EA--_vS_vv73vv70T77-9Ke-_ve-_vWfvv70ebO-_ve-_ve-_vQN377-9ce-_vRfvv70'
api = prismic.get('https://lesbonneschoses.prismic.io/api', preview_token)
st_patrick_ref = api.get_ref('St-Patrick specials')
# Now we'll use this reference for all our calls
response = api.form("everything")\
.ref(st_patrick_ref)\
.query(predicates.at("document.type", "product"))\
.submit()
# The documents object contains a Response object with all documents of type "product"
# including the new "Saint-Patrick's Cupcake"
# This will fail because the token is invalid, but this is how to access a private API
api = prismic.get('https://lesbonneschoses.prismic.io/api', 'MC5-XXXXXXX-vRfvv70')