From 09b93c69c1808a1ba7309addbe818ed71b4a7011 Mon Sep 17 00:00:00 2001 From: themrphantom Date: Mon, 4 Nov 2024 19:54:34 +0100 Subject: [PATCH] added pretix functionality --- backend/TaskScheduler.py | 6 ++++ backend/database/Queries.py | 72 +++++++++++++++++++++++++++++++++++++ backend/main.py | 3 ++ backend/util.py | 12 +++++++ 4 files changed, 93 insertions(+) diff --git a/backend/TaskScheduler.py b/backend/TaskScheduler.py index b949b8c..fb21501 100644 --- a/backend/TaskScheduler.py +++ b/backend/TaskScheduler.py @@ -20,3 +20,9 @@ def add_Daily_Task(self, task, *args) -> None: schedule.every().day.at("00:01").do(task, args) else: schedule.every().day.at("00:01").do(task) + + def add_5min_Task(self, task, *args) -> None: + if len(args) > 0: + schedule.every(5).minutes.do(task, args) + else: + schedule.every(5).minutes.do(task) diff --git a/backend/database/Queries.py b/backend/database/Queries.py index ee9a10c..d3b0b6e 100644 --- a/backend/database/Queries.py +++ b/backend/database/Queries.py @@ -626,6 +626,78 @@ def add_aliases_if_non_existend(self): self.session.commit() + def get_checkin_status_from_pretix(self): + # Define the endpoint + endpoint = f"{util.pretix_url}/api/v1/organizers/{util.pretix_organizer}/events/{util.pretix_event}/checkinlists/{util.pretix_checkin_list_id}/positions/" + headers = { + "Authorization": f"Token {util.pretix_api_token}", + "Content-Type": "application/json" + } + + try: + # Send the request + response = requests.get(endpoint, headers=headers) + response.raise_for_status() # Raise an error for unsuccessful requests + + # Parse JSON response + data = response.json() + + # Extract and print attendee name and check-in state + attendees = [] + + for position in data.get("results", []): + name = position.get("attendee_name", "No Name Provided") + checkin_state = position.get("checkins", []) + + if len(checkin_state) > 0: + checkin_state = checkin_state[0].get( + "type", "exit") == "entry" + else: + checkin_state = False + attendees.append((name, checkin_state)) + + nextPage = data.get("next") + while nextPage is not None: + response = requests.get(nextPage, headers=headers) + response.raise_for_status() + data = response.json() + for position in data.get("results", []): + name = position.get("attendee_name", "No Name Provided") + checkin_state = position.get("checkins", []) + + if len(checkin_state) > 0: + checkin_state = checkin_state[0].get( + "type", "exit") == "entry" + else: + checkin_state = False + attendees.append((name, checkin_state)) + nextPage = data.get("next") + + return attendees + + except requests.exceptions.RequestException as e: + print("Error fetching check-in data:", e) + return [] + + def enable_disable_pretix_user(self): + print("Checking pretix check-in") + attendee_list = self.get_checkin_status_from_pretix() + + for attendee in attendee_list: + name, checked_in = attendee + member: Member | None = self.session.query( + Member).filter_by(name=name.lower()).first() + if member is not None: + if checked_in: + self.change_user_visibility(member.id, visibility=False) + else: + self.change_user_visibility(member.id, visibility=True) + else: + print(f"User {name} not found in database") + # Add user to database + self.add_user(name, 0, util.standard_user_password, + name, hidden=not checked_in) + def add_token(self, token, member_id, time): session: Session = self.session.query( Session).filter_by(member_id=member_id).first() diff --git a/backend/main.py b/backend/main.py index f4d6c15..77bf0f5 100644 --- a/backend/main.py +++ b/backend/main.py @@ -35,6 +35,9 @@ taskScheduler = TaskScheduler.TaskScheduler() taskScheduler.add_Daily_Task(db.hide_inactive) + if util.pretix_url is not None: + db.enable_disable_pretix_user() + taskScheduler.add_5min_Task(db.enable_disable_pretix_user) taskScheduler.start() diff --git a/backend/util.py b/backend/util.py index 38abb3a..fe0cfb4 100644 --- a/backend/util.py +++ b/backend/util.py @@ -68,6 +68,18 @@ OIDC_USER_NEEDS_VERIFICATION = os.environ.get( "OIDC_USER_NEEDS_VERIFICATION")=="true" if os.environ.get("OIDC_USER_NEEDS_VERIFICATION") else True +pretix_url = os.environ.get("PRETIX_URL") if os.environ.get( + "PRETIX_URL") else None +pretix_checkin_list_id = os.environ.get("CHECKIN_LIST_ID") if os.environ.get( + "CHECKIN_LIST_ID") else None +pretix_organizer = os.environ.get("ORGANIZER") if os.environ.get( + "ORGANIZER") else None +pretix_event = os.environ.get("EVENT") if os.environ.get( + "EVENT") else None +pretix_api_token = os.environ.get("API_TOKEN") if os.environ.get( + "API_TOKEN") else None + + tempfile_path = "tempfiles" backup_file_name = "backup.json"