You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
3.1 KiB
94 lines
3.1 KiB
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')
|
|
|
|
@app.route('/<type>/<title>')
|
|
def event_article(type, title):
|
|
data = get_content_by_title(title, type)
|
|
return render_template('article.html', title=title, content=data)
|
|
|
|
|
|
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)
|
|
return content_list
|
|
|
|
def get_content_by_title(title, pre_type):
|
|
print(title)
|
|
content_list = []
|
|
type = pre_type[:-1]
|
|
entries = client.entries({'query': title, 'limit': 5})
|
|
exact_matches = [entry for entry in entries if getattr(entry, f'title_of_{type}') == title]
|
|
for entry in exact_matches:
|
|
|
|
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)
|