from flask import Flask, render_template import contentful from dotenv import load_dotenv import os import markdown from ContentfulService import ContentfulService load_dotenv() app = Flask(__name__, static_folder='public') # Set your Contentful space ID and access token SPACE_ID = os.getenv('SPACE_ID') ACCESS_TOKEN = os.getenv('ACCESS_TOKEN') client = contentful.Client(SPACE_ID, ACCESS_TOKEN) contentful_service = ContentfulService(SPACE_ID, ACCESS_TOKEN) @app.context_processor def inject_general_info(): # Fetch the data here, inside the function that runs for every request. # This guarantees the data is always fresh from Contentful. festival_info = contentful_service.get_general_info() return dict(festival_general_info=festival_info) @app.route('/') def index(): sup_img, prio_img, prio_img_top = contentful_service.get_sponser_logos() return render_template('index.html', title='', sup_img_list=sup_img, prio_img=prio_img, prio_img_top=prio_img_top) @app.route('/grph') def graphics(): return render_template('grph.html') @app.route('/events') def events(): events = get_all_content('event') return render_template('list.html', title='Events', content=events) @app.route('/exhibitions') def exhibitions(): exhibition_info = contentful_service.get_exhibition_list_info()[::-1] print(exhibition_info) return render_template('list.html', title='Exhibitions', exhibition_info=exhibition_info) @app.route('/exhibitions/') def exhibition_type(ex_type): if ex_type == "local-artists-network": ex_type = "LAN" elif ex_type == "unsettling-the-algorithm": ex_type = "UTA" elif ex_type == "ethics-studio": ex_type = "ES" elif ex_type == "starts4waterii": ex_type = "S4W2" elif ex_type == "imma-living-canvas-x-beta": ex_type = "IMMA" exhibitions = get_all_content('exhibition') print(ex_type) return render_template('list-exhibitions.html', title="Exhibitions", content=exhibitions, ex_type=ex_type) @app.route('/conferences') def conference(): conferences = get_all_content('conference') return render_template('list.html', title='Conferences', content=conferences) @app.route('//') def event_article(type, title): data = get_content_by_title(title, type) return render_template('article.html', title=title, content=data) @app.route('/<type>/<ex_type>/<title>') def event_articles(type, title, ex_type): data = get_content_by_title(title, type) return render_template('article.html', title=title, content=data) @app.route('/locations') def locations(): location_info = contentful_service.get_location_info() print(location_info) return render_template('locations.html', title='Locations', locInfo=location_info) def get_all_content(type): entries = client.entries({'content_type': type, 'fields.beta25': 'true'}) content_list = process_content(entries, type) return content_list def get_content_by_title(title, pre_type): type = pre_type[:-1] print(title) entries = client.entries({ 'content_type': type, 'query': title, 'limit': 5, 'fields.beta25': 'true' }) for entry in entries: try: if getattr(entry, f'title_of_{type}') == title: exact_matches = [entry] except: pass content_list = process_content(exact_matches, type) return content_list def process_content(entries, type): content_list = [] exh_type = None for entry in entries: # Clear arrays times = [] dates = [] if type == 'event': times = getattr(entry, f'{type}_times') dates = getattr(entry, f'{type}_dates') if type == 'conference': date_time_obj = getattr(entry, f'{type}_start_date_time') date_str, time_str = format_datetime(date_time_obj) try: end_time_str = getattr(entry, f'{type}_end_date_time') except: end_time_str = None if end_time_str != None: end_time_str = end_time_str.strftime('%I%p').upper().lstrip('0') concat_time_str = time_str + '->' + end_time_str else: concat_time_str = time_str dates.append(date_str) times.append(concat_time_str) if type == 'exhibition': exh_type = getattr(entry, f'{type}_type') try: bookUrl = getattr(entry, 'book_url') except: bookUrl = None content = { 'title': getattr(entry, f'title_of_{type}'), 'information': markdown.markdown(getattr(entry, f'{type}_information')), 'image': 'https:{0}'.format(getattr(entry, f'{type}_reference_image').url()), 'artists': getattr(entry, f'{type}_artists'), 'dates': dates, 'times': times, 'location': getattr(entry, f'{type}_location'), 'bookUrl': bookUrl, 'exh_type': exh_type } content_list.append(content) print(content) print("\n") # Sort the content list by the 'title' key alphabetically content_list.sort(key=lambda x: x['title'].lower()) return content_list def format_datetime(dt): date_str = dt.strftime('%d.%m.%y') time_str = dt.strftime('%I%p').upper().lstrip('0') return date_str, time_str if __name__ == '__main__': app.run(debug=True)