|
| 1 | +import logging |
| 2 | +from argparse import ArgumentParser |
| 3 | +from urllib.parse import urljoin, urlparse |
| 4 | + |
| 5 | +import requests |
| 6 | +from django.conf import settings |
| 7 | +from django.core.management.base import BaseCommand, CommandError |
| 8 | + |
| 9 | +from pretalx.event.models import Event |
| 10 | + |
| 11 | + |
| 12 | +# The name of the Video container |
| 13 | +VIDEO_CONTAINER_NAME = 'eventyay-video' |
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +def replace_hostname(url: str) -> str: |
| 18 | + ''' |
| 19 | + Replace the hostname of the URL with the name of the Video container. |
| 20 | + ''' |
| 21 | + parsed_url = urlparse(url) |
| 22 | + parsed_url = parsed_url._replace(netloc=VIDEO_CONTAINER_NAME, scheme='http') |
| 23 | + return parsed_url.geturl() |
| 24 | + |
| 25 | + |
| 26 | +def push_to_video_site(event: Event): |
| 27 | + url = urljoin(event.venueless_settings.url, 'schedule_update') |
| 28 | + logger.info('Saved video API URL: %s', url) |
| 29 | + # In development with Docker, we use fake domain, so the URL can not reach the video site, |
| 30 | + # we need to replace the hostname. |
| 31 | + url = replace_hostname(url) |
| 32 | + # I haven't found the page to retrieve this token. |
| 33 | + # In development, I get it from ShortToken model in Video container. |
| 34 | + token = event.venueless_settings.token |
| 35 | + logger.info('To push schedule to rewritten video API URL instead: %s', url) |
| 36 | + post_data = { |
| 37 | + 'domain': event.custom_domain or settings.SITE_URL, |
| 38 | + 'event': event.slug, |
| 39 | + } |
| 40 | + logger.info('With post data: %s', post_data) |
| 41 | + logger.info('Authenticated with token: %s', token) |
| 42 | + response = requests.post( |
| 43 | + url, |
| 44 | + json=post_data, |
| 45 | + headers={ |
| 46 | + 'Authorization': f'Bearer {token}', |
| 47 | + }, |
| 48 | + ) |
| 49 | + if not response.ok: |
| 50 | + logger.error('Failed to push schedule to video site: %s', response.text) |
| 51 | + raise CommandError(f'Failed to push schedule to video site: {response.text}') |
| 52 | + logger.info('Response: %s', response.content) |
| 53 | + logger.info('Schedule pushed to video site successfully.') |
| 54 | + |
| 55 | + |
| 56 | +class Command(BaseCommand): |
| 57 | + ''' |
| 58 | + Inform the video site that the schedule has been updated. |
| 59 | + ''' |
| 60 | + def add_arguments(self, parser: ArgumentParser): |
| 61 | + parser.add_argument('event_slug', type=str) |
| 62 | + |
| 63 | + def handle(self, event_slug: str, **options): |
| 64 | + event = Event.objects.get(slug=event_slug) |
| 65 | + push_to_video_site(event) |
| 66 | + |
0 commit comments