""" Demo resource for the OpenEventDatabase. """ import falcon import requests import json from collections import defaultdict from oedb.utils.logging import logger class DemoResource: """ Resource for the demo endpoint. Handles the /demo endpoint and related demo pages. """ def on_get_edit(self, req, resp, id=None): """ Handle GET requests to the /demo/edit endpoint. Returns an HTML page with a form for editing an existing event. Args: req: The request object. resp: The response object. id: The event ID to edit. """ logger.info(f"Processing GET request to /demo/edit for event ID: {id}") if id is None: resp.status = falcon.HTTP_400 resp.text = "Event ID is required" return try: # Set content type to HTML resp.content_type = 'text/html' # Fetch the event data from the API response = requests.get(f'http://localhost/event/{id}') if response.status_code != 200: resp.status = falcon.HTTP_404 resp.text = f"Event with ID {id} not found" return event_data = response.json() # Create HTML response with form html = f""" Edit Event - OpenEventDatabase

Edit Event

Category of the event (e.g., sport.match.football, culture.festival)
Series or group the event belongs to (e.g., Euro 2024, Summer Festival 2023)
Specific location name (e.g., Eiffel Tower, Wembley Stadium)
Click on the map to set the event location
""" # Set the response body and status resp.text = html.replace('{event_data}', json.dumps(event_data)) resp.status = falcon.HTTP_200 logger.success(f"Successfully processed GET request to /demo/edit for event ID: {id}") except Exception as e: logger.error(f"Error processing GET request to /demo/edit: {e}") resp.status = falcon.HTTP_500 resp.text = f"Error: {str(e)}" def on_get(self, req, resp): """ Handle GET requests to the /demo endpoint. Returns an HTML page with a MapLibre map showing current events. Args: req: The request object. resp: The response object. """ logger.info("Processing GET request to /demo") try: # Set content type to HTML resp.content_type = 'text/html' # Create HTML response with MapLibre map html = """ OpenEventDatabase Demo

OpenEventDatabase Demo

This map shows current events from the OpenEventDatabase.

API Endpoints:

Demo Pages:

+ Add New Event

Source Code on Cipherbliss

""" # Set the response body and status resp.text = html resp.status = falcon.HTTP_200 logger.success("Successfully processed GET request to /demo") except Exception as e: logger.error(f"Error processing GET request to /demo: {e}") resp.status = falcon.HTTP_500 resp.text = f"Error: {str(e)}" def on_get_by_what(self, req, resp): """ Handle GET requests to the /demo/by-what endpoint. Returns an HTML page with links to events organized by their "what" type. Args: req: The request object. resp: The response object. """ logger.info("Processing GET request to /demo/by-what") try: # Set content type to HTML resp.content_type = 'text/html' # Fetch events from the API response = requests.get('http://localhost/event?limit=1000') events_data = response.json() # Group events by "what" type events_by_what = defaultdict(list) if events_data.get('features'): for feature in events_data['features']: properties = feature.get('properties', {}) what = properties.get('what', 'Unknown') events_by_what[what].append({ 'id': properties.get('id'), 'label': properties.get('label', 'Unnamed Event'), 'coordinates': feature.get('geometry', {}).get('coordinates', [0, 0]) }) # Create HTML response html = """ Events by Type - OpenEventDatabase

Events by Type

This page lists all events from the OpenEventDatabase organized by their type.

""" # Add event types and their events if events_by_what: # Sort event types alphabetically sorted_what_types = sorted(events_by_what.keys()) # Add quick navigation html += "

Quick Navigation

" # Add sections for each event type for what_type in sorted_what_types: events = events_by_what[what_type] html += f'

{what_type} ({len(events)} events)

' html += "" else: html += "

No events found in the database.

" html += """ """ # Set the response body and status resp.text = html resp.status = falcon.HTTP_200 logger.success("Successfully processed GET request to /demo/by-what") except Exception as e: logger.error(f"Error processing GET request to /demo/by-what: {e}") resp.status = falcon.HTTP_500 resp.text = f"Error: {str(e)}" def on_get_map_by_what(self, req, resp): """ Handle GET requests to the /demo/map-by-what endpoint. Returns an HTML page with a MapLibre map showing events filtered by "what" type. Args: req: The request object. resp: The response object. """ logger.info("Processing GET request to /demo/map-by-what") try: # Set content type to HTML resp.content_type = 'text/html' # Create HTML response with MapLibre map and filtering controls html = """ Map by Event Type - OpenEventDatabase

Map by Event Type

This map shows events from the OpenEventDatabase filtered by their type.

Use the filter panel on the right to show/hide different event types.

Loading events...

Filter by Event Type

""" # Set the response body and status resp.text = html resp.status = falcon.HTTP_200 logger.success("Successfully processed GET request to /demo/map-by-what") except Exception as e: logger.error(f"Error processing GET request to /demo/map-by-what: {e}") resp.status = falcon.HTTP_500 resp.text = f"Error: {str(e)}" events_by_what = defaultdict(list) if events_data.get('features'): for feature in events_data['features']: properties = feature.get('properties', {}) what = properties.get('what', 'Unknown') events_by_what[what].append({ 'id': properties.get('id'), 'label': properties.get('label', 'Unnamed Event'), 'coordinates': feature.get('geometry', {}).get('coordinates', [0, 0]) }) # Create HTML response html = """ Events by Type - OpenEventDatabase

Events by Type

This page lists all events from the OpenEventDatabase organized by their type.

""" # Add event types and their events if events_by_what: # Sort event types alphabetically sorted_what_types = sorted(events_by_what.keys()) # Add quick navigation html += "

Quick Navigation

" # Add sections for each event type for what_type in sorted_what_types: events = events_by_what[what_type] html += f'

{what_type} ({len(events)} events)

' html += "" else: html += "

No events found in the database.

" html += """ """ # Set the response body and status resp.text = html resp.status = falcon.HTTP_200 logger.success("Successfully processed GET request to /demo/by-what") except Exception as e: logger.error(f"Error processing GET request to /demo/by-what: {e}") resp.status = falcon.HTTP_500 resp.text = f"Error: {str(e)}" # Create a global instance of DemoResource demo = DemoResource()