Repository: pretix/pretix

Issue:
plugin api: add a signal for new event created
I'd like to add a new signal, that fires when a new event is created to set some sane default settings for our plugin. 

My suggestion would be to let `pretix.base.models.event.Event.set_defaults` fire a new event named `pretix.base.signals.event_created` to be in line with the `checkin_created` signal.

Before I start working on this, are you open to new plugin signals?



Discussion:
Generally, yes, I'm open to new signals if they make sense.

However, for the use case you describe, I don't think you need one.

If your plugin's settings live in our settings store, you can add defaults more elegantly like this: https://django-hierarkey.readthedocs.io/en/latest/quickstart.html#set-default-values

If they live e.g. in a model, how about doing it in

* the event copy signal for copied events
* the plugin's installed method to trigger after plugin activation instead of after event creation: https://docs.pretix.eu/en/latest/development/api/plugins.html#signals

You'd only really need an "on event create" for a plugin that's enable by default globally, wouldn't you? Maybe we should just call installed() in that case?

Resolving patch:
diff --git a/src/pretix/base/models/event.py b/src/pretix/base/models/event.py
--- a/src/pretix/base/models/event.py
+++ b/src/pretix/base/models/event.py
@@ -634,6 +634,11 @@ def copy_data_from(self, other):
             Quota,
         )
 
+        #  Note: avoid self.set_active_plugins(), it causes trouble e.g. for the badges plugin.
+        #  Plugins can create data in installed() hook based on existing data of the event.
+        #  Calling set_active_plugins() results in defaults being created while actually data
+        #  should come from the copied event. Instead plugins should use event_copy_data to move
+        #  over their data.
         self.plugins = other.plugins
         self.is_public = other.is_public
         if other.date_admission:
diff --git a/src/pretix/control/views/event.py b/src/pretix/control/views/event.py
--- a/src/pretix/control/views/event.py
+++ b/src/pretix/control/views/event.py
@@ -1440,7 +1440,7 @@ def form_valid(self, form):
             })
             quota.items.add(*items)
 
-        self.request.event.plugins = ",".join(plugins_active)
+        self.request.event.set_active_plugins(plugins_active, allow_restricted=True)
         self.request.event.save()
         messages.success(self.request, _('Your changes have been saved. You can now go on with looking at the details '
                                          'or take your event live to start selling!'))
diff --git a/src/pretix/control/views/main.py b/src/pretix/control/views/main.py
--- a/src/pretix/control/views/main.py
+++ b/src/pretix/control/views/main.py
@@ -251,7 +251,7 @@ def done(self, form_list, form_dict, **kwargs):
         with transaction.atomic(), language(basics_data['locale']):
             event = form_dict['basics'].instance
             event.organizer = foundation_data['organizer']
-            event.plugins = settings.PRETIX_PLUGINS_DEFAULT
+            event.set_active_plugins(settings.PRETIX_PLUGINS_DEFAULT.split(","), allow_restricted=True)
             event.has_subevents = foundation_data['has_subevents']
             event.testmode = True
             form_dict['basics'].save()


Test patch: