Created
June 15, 2018 07:16
-
-
Save andrewserong/59bbb66496253bb22e2a1c86c9e83c06 to your computer and use it in GitHub Desktop.
An example of a simple Contentful middleware written for the Wonderland exhibition at ACMI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import contentful | |
import markdown | |
from flask import current_app | |
def get_contentful_posts(character_filter=None): | |
""" | |
Get contentful content for posts and return it as a dictionary | |
:character_filter: a string to filter posts by associated character (can only select one at a time) | |
:return: a dictionary of post data, that can easily be converted to a JSON response using Flask's jsonify function | |
""" | |
# Reference for searching and filtering: | |
# https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters | |
# https://www.contentful.com/r/knowledgebase/searching-on-references/ | |
client = contentful.Client(current_app.config["CONTENTFUL_SPACE_ID"], current_app.config["CONTENTFUL_API_KEY"]) | |
all_entries = [] | |
entries = client.entries({'content_type': 'post'}) | |
for entry in entries: | |
if entry.content_type.id == 'post': | |
this_hero_images = [] | |
this_post_body = None | |
this_characters = [] | |
this_links = [] | |
try: | |
for hero_image in entry.hero_images: | |
this_hero_images.append( | |
{ | |
"url": 'https:' + hero_image.url(), | |
"title": hero_image.title or None, | |
"description": hero_image.description or None | |
} | |
) | |
except AttributeError as err: | |
pass | |
# print("Couldn't find a hero image for this entry: {}".format(str(err))) | |
try: | |
for link in entry.links: | |
this_links.append(link) | |
except AttributeError as err: | |
pass | |
# print("Couldn't find a list of links for this entry: {}".format(str(err))) | |
try: | |
this_post_body = entry.post_body | |
except AttributeError as err: | |
pass | |
# print("Couldn't find a post body attribute: {}".format(str(err))) | |
try: | |
for character in entry.character: | |
this_character = { | |
'id': character.id, | |
'character_name': character.character_name, | |
'slug': character.slug | |
} | |
this_characters.append(this_character) | |
except AttributeError as err: | |
pass | |
# print("Couldn't find an associated character: {}".format(str(err))) | |
processed_post_body = None | |
if this_post_body is not None: | |
processed_post_body = markdown.markdown(this_post_body) | |
this_entry = { | |
'id': getattr(entry, 'id', None), | |
'heading': getattr(entry, 'heading', None), | |
'sort_order': getattr(entry, 'sort_order', None), | |
'post_body_html': processed_post_body, | |
'post_body_raw': this_post_body, | |
'hero_images': this_hero_images, | |
'characters': this_characters, | |
'links': this_links, | |
'quote': getattr(entry, 'quote', None) | |
} | |
# Filter by character if one is specified | |
if character_filter is not None and this_entry.get('characters') is not None: | |
for character in this_entry['characters']: | |
if character_filter in character.get('character_name'): | |
all_entries.append(this_entry) | |
else: | |
all_entries.append(this_entry) | |
data = { | |
"entries": all_entries | |
} | |
return data | |
def get_posts(character_filter=None): | |
""" | |
Generic function to get and retrieve posts, callable from elsewhere in the app. This function should act like a | |
generic interface for retrieving posts, which currently involves using Contentful. The body of this function | |
can be replaced later to retrieve static JSON instead. | |
:param character_filter: a String representing a character to use a filter for retrieving posts, e.g "The Mad Hatter" | |
:return: a dictionary of post (entry) data | |
""" | |
data = get_contentful_posts(character_filter=character_filter) | |
return data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment