from flask import Flask, render_template import contentful from dotenv import load_dotenv import os import datetime 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) @app.route('/') def index(): return render_template('index.html', title='Homepage') @app.route('/events') def events(): events = get_all_content('event') return render_template('list.html', title='Events', content=events) @app.route('/exhibitions') def exhibitions(): return render_template('list.html', title='Exhibitions') @app.route('/conferences') def conference(): return render_template('list.html', title='Conferences') def get_all_content(type): content_list = [] entries = client.entries({'content_type': type}) for entry in entries: date_time_obj = getattr(entry, f'{type}_date_time') date_str, time_str = format_datetime(date_time_obj) end_time_str = getattr(entry, f'{type}_end_date_time') end_time_str = end_time_str.strftime('%I%p').upper().lstrip('0') content = { 'title': getattr(entry, f'title_of_{type}'), 'information': getattr(entry, f'{type}_information'), 'image': 'https:{0}'.format(getattr(entry, f'{type}_reference_image').url()), 'artists': getattr(entry, f'{type}_artists'), 'date': date_str, 'time': time_str, 'end_time': end_time_str, 'location': getattr(entry, f'{type}_location') } content_list.append(content) print(content_list) 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)