From 06cd41b96fb2c1728eb8c81fbe1c918012ec8a33 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Felix=20Sch=C3=A4fer?= <felix@thegcat.net>
Date: Wed, 4 Oct 2023 10:23:46 +0200
Subject: [PATCH] Schedule tasks only after DB transactions are committed

https://github.com/pretix/pretix/issues/3630
---
 CHANGELOG.md                   |  2 ++
 pretix_matrix_inviter/forms.py | 16 ++++------------
 pretix_matrix_inviter/tasks.py |  6 +++---
 3 files changed, 9 insertions(+), 15 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8d65301..247b7ce 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
 and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
 ## [Unreleased]
+### Fixed
+- Schedule tasks only after DB transactions are committed
 
 ## [1.7.0] - 2023-02-09
 ### Added
diff --git a/pretix_matrix_inviter/forms.py b/pretix_matrix_inviter/forms.py
index f3d4245..80cd5d5 100644
--- a/pretix_matrix_inviter/forms.py
+++ b/pretix_matrix_inviter/forms.py
@@ -1,4 +1,5 @@
-from django.core.exceptions import ValidationError
+import re
+from django.core.validators import URLValidator
 from django.forms import (
     CharField,
     CheckboxSelectMultiple,
@@ -8,8 +9,6 @@ from django.forms import (
 from django.utils.translation import gettext_lazy
 from i18nfield.forms import I18nFormField, I18nTextInput
 from pretix.base.forms import SettingsForm
-from urllib3.exceptions import LocationParseError
-from urllib3.util import parse_url
 
 from .helpers import matrix_room_info_for_event
 
@@ -31,9 +30,10 @@ class MatrixInviterSettingsForm(SettingsForm):
             "your own Matrix account but from a dedicated Matrix account."
         ),
     )
-    matrix_inviter_matrix_server = CharField(
+    matrix_inviter_matrix_server = RegexField(
         label=gettext_lazy("Matrix server"),
         strip=True,
+        regex=re.compile(r"^" + URLValidator.host_re + r"\Z", re.IGNORECASE),
         help_text=gettext_lazy(
             "The matrix server the above access token is valid for."
         ),
@@ -61,14 +61,6 @@ class MatrixInviterSettingsForm(SettingsForm):
         strip=True,
     )
 
-    def clean_matrix_inviter_matrix_server(self):
-        url = self.cleaned_data["matrix_inviter_matrix_server"]
-        try:
-            return parse_url(url).host
-        except LocationParseError:
-            raise ValidationError("Please enter a valid hostname")
-        return url
-
     def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)
         self.fields["matrix_inviter_items"].choices = [
diff --git a/pretix_matrix_inviter/tasks.py b/pretix_matrix_inviter/tasks.py
index 0c62c9d..9b189ec 100644
--- a/pretix_matrix_inviter/tasks.py
+++ b/pretix_matrix_inviter/tasks.py
@@ -2,7 +2,7 @@ import logging
 import requests
 from celery.exceptions import MaxRetriesExceededError
 from pretix.base.models import Order, OrderPosition
-from pretix.base.services.tasks import ProfiledEventTask
+from pretix.base.services.tasks import TransactionAwareProfiledEventTask
 from pretix.celery_app import app
 from urllib.parse import quote as url_quote
 
@@ -12,7 +12,7 @@ logger = logging.getLogger(__name__)
 
 
 @app.task(
-    base=ProfiledEventTask,
+    base=TransactionAwareProfiledEventTask,
     bind=True,
     max_retries=10,
     retry_backoff=True,
@@ -98,7 +98,7 @@ def matrix_inviter_invite(
 
 
 @app.task(
-    base=ProfiledEventTask,
+    base=TransactionAwareProfiledEventTask,
     bind=True,
     max_retries=10,
     retry_backoff=True,
-- 
GitLab