[Django]-Building pluggable apps: how to include forks of popular libraries and prevent name conflicts?

0👍

You can solve it with a function. Create a function that does both things:

  • Make sure you can get hold of what you want afterwards
  • Make sure the message is sent.

Or you can do some more heavy plumbing. Make sure to document it well and try to make other people not try this at home for every futile purpose, instead, contact the Django or Django-mailer teams and ask them if they can arrange a better solution.

  • decorate the EmailMessage class from the django.core.mail module: make sure that when a message is successfully sent, the information you want is also delivered somewhere else.

    # wrappedmailer.py
    from django.core.mail import EmailMessage
    
    class WrappedEmailMessage(object):
        def __init__(self, message):
            self.__message = message
            # more initialization
    
        # override all EmailMessage methods:
        # do what you want with what is provided,
        # then return self.__message.method(...)
    
  • Then when your application is initialized, before django-mailer is loaded, you can do the following:

       import django.core.mail
       import newmailer
    
       django.core.mail.EmailMessage = newmailer.WrappedEmailMessage
    

The django.core.mail module stays in the cache within the same Python process, so whenever django.core.mail is imported, the EmailMessage class will in fact be the WrappedEmailMessage class.

Leave a comment