Skip to content

Commit

Permalink
Adding check for subscription generator class on pre_save
Browse files Browse the repository at this point in the history
  • Loading branch information
newearthmartin committed Jun 10, 2024
1 parent 03bfd61 commit 349a733
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions newsletter/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ def get_templates(self, action):
def get_subscription_generator(self):
if self.subscription_generator_class:
try:
class_data = self.subscription_generator_class.split(".")
module_name = ".".join(class_data[:-1]) if len(class_data) > 1 else ''
class_name = class_data[-1]
if "." not in self.subscription_generator_class:
raise ModuleNotFoundError("missing module for subscription generator class")
module_name, class_name = self.subscription_generator_class.rsplit(".", 1)
module = importlib.import_module(module_name)
self.subscription_generator = getattr(module, class_name)()
return self.subscription_generator
except (AttributeError, ModuleNotFoundError) as e:
logger.error("Could not load subscriber generator class '%s' - %s" % (self.subscription_generator_class, e))
logger.error(f"Could not load subscriber generator class '%s' - %s" % (self.subscription_generator_class, e))
raise e
else:
return None
Expand Down Expand Up @@ -154,6 +154,14 @@ def get_default(cls):
return None


def newsletter_presave(sender, instance, **kwargs):
if instance.subscription_generator_class:
instance.get_subscription_generator()


models.signals.pre_save.connect(newsletter_presave, Newsletter)


class Subscription(models.Model):
user = models.ForeignKey(
AUTH_USER_MODEL, blank=True, null=True, verbose_name=_('user'),
Expand Down

0 comments on commit 349a733

Please sign in to comment.