-
Notifications
You must be signed in to change notification settings - Fork 0
download pretix data #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b5dd81a
download pretix data
artcz e1e5d4a
fix tests and add vouchers and products endpoints
artcz 848063d
add missing migration
artcz 877be24
add PretixDataAdmin
artcz ab65c94
add cron
artcz a2e807b
remove unused comment
artcz eca77f4
Update intbot/core/integrations/pretix.py
artcz 47bf893
Update intbot/core/integrations/pretix.py
artcz a4582de
fix cron descriptions
artcz 733f236
add pretix/pretalx api tokens to env example
artcz 7cfdb70
fix typo
artcz 6fa2d4c
make format
artcz 7541717
add sanity check tests for management commands
artcz 4b99319
fix previous
artcz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import logging | ||
from typing import Any | ||
|
||
import httpx | ||
from core.models import PretixData | ||
from django.conf import settings | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
PRETALX_EVENTS = [ | ||
"2022", | ||
"ep2023", | ||
"ep2024", | ||
"ep2025", | ||
] | ||
|
||
ENDPOINTS = { | ||
PretixData.PretixResources.orders: "orders/", | ||
PretixData.PretixResources.products: "items/", | ||
PretixData.PretixResources.vouchers: "vouchers/", | ||
} | ||
|
||
|
||
JsonType = dict[str, Any] | ||
|
||
|
||
def get_event_url(event): | ||
artcz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert event in PRETALX_EVENTS | ||
|
||
pretix_url = "https://tickets.europython.eu" | ||
url = f"{pretix_url}/api/v1/organizers/europython/events/{event}/" | ||
return url | ||
artcz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def fetch_pretix_data( | ||
event: str, resource: PretixData.PretixResources | ||
) -> list[JsonType]: | ||
headers = { | ||
"Authorization": f"Token {settings.PRETIX_API_TOKEN}", | ||
"Content-Type": "application/json", | ||
} | ||
|
||
base_url = get_event_url(event) | ||
endpoint = ENDPOINTS[resource] | ||
url = f"{base_url}{endpoint}" | ||
|
||
# Pretix paginates the output, so we will need to do multiple requests and | ||
# then merge multiple pages to one big dictionary | ||
results = [] | ||
page = 0 | ||
|
||
# This takes advantage of the fact that url will contain a url to the | ||
# next page, until there is more data to fetch. If this is the last page, | ||
# then the url will be None (falsy), and thus stop the while loop. | ||
while url: | ||
page += 1 | ||
response = httpx.get(url, headers=headers) | ||
|
||
if response.status_code != 200: | ||
raise Exception(f"Error {response.status_code}: {response.text}") | ||
|
||
logger.info("Fetching data from %s, page %s", url, page) | ||
|
||
data = response.json() | ||
results += data["results"] | ||
url = data["next"] | ||
|
||
return results | ||
|
||
|
||
def download_latest_orders(event: str) -> PretixData: | ||
data = fetch_pretix_data(event, PretixData.PretixResources.orders) | ||
|
||
pretix_data = PretixData.objects.create( | ||
resource=PretixData.PretixResources.orders, | ||
content=data, | ||
) | ||
|
||
return pretix_data | ||
|
||
def download_latest_products(event: str) -> PretixData: | ||
data = fetch_pretix_data(event, PretixData.PretixResources.products) | ||
|
||
pretix_data = PretixData.objects.create( | ||
resource=PretixData.PretixResources.products, | ||
content=data, | ||
) | ||
|
||
return pretix_data | ||
|
||
def download_latest_vouchers(event: str) -> PretixData: | ||
data = fetch_pretix_data(event, PretixData.PretixResources.vouchers) | ||
|
||
pretix_data = PretixData.objects.create( | ||
resource=PretixData.PretixResources.vouchers, | ||
content=data, | ||
) | ||
|
||
return pretix_data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from core.integrations.pretalx import ( | ||
PRETIX_EVENTS, | ||
download_latest_orders, | ||
download_latest_products, | ||
download_latest_vouchers, | ||
) | ||
from django.core.management.base import BaseCommand | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Downloads latest pretix data" | ||
|
||
def add_arguments(self, parser): | ||
# Add keyword argument event | ||
parser.add_argument( | ||
"--event", | ||
choices=PRETIX_EVENTS, | ||
help="slug of the event (for example `ep2025`)", | ||
required=True, | ||
) | ||
|
||
def handle(self, **kwargs): | ||
event = kwargs["event"] | ||
|
||
self.stdout.write(f"Downloading latest products from pretix... {event}") | ||
download_latest_products(event) | ||
|
||
self.stdout.write(f"Downloading latest vouchers from pretix... {event}") | ||
download_latest_vouchers(event) | ||
|
||
self.stdout.write(f"Downloading latest orders from pretix... {event}") | ||
download_latest_orders(event) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Generated by Django 5.1.4 on 2025-04-24 22:08 | ||
|
||
import uuid | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('core', '0005_add_pretalx_data_model'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='PretixData', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('uuid', models.UUIDField(default=uuid.uuid4)), | ||
('resource', models.CharField(choices=[('orders', 'Orders'), ('products', 'Products'), ('vouchers', 'Vouchers')], max_length=255)), | ||
('content', models.JSONField()), | ||
('created_at', models.DateTimeField(auto_now_add=True)), | ||
('modified_at', models.DateTimeField(auto_now=True)), | ||
('processed_at', models.DateTimeField(blank=True, null=True)), | ||
artcz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
], | ||
), | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.