Two issues with custom notification app

Issue #120 resolved
Martin Pauly created an issue

For our project we are using django-postman with a custom notifications app. This has resulted in two issues recently:

1.) Line 32 in apps.py requires the presence of a model NoticeType. If that line were part of the try-block a bit earlier one would need to have such a model.

2.) utils.py assumes that the corresponding function send() is part of a Django models.py file. For us this is not the case. It would be very handy if in https://bitbucket.org/psam/django-postman/src/a0abd6b3fee23272420357990921e2d984ab6ed2/postman/utils.py#lines-24 the code would also try to import notification=import_module(notifier_app_label) .

Both changes would make it a lot easier to write custom notification functions for django-postman. Would you be willing to merge a PR with these changes?

Comments (6)

  1. Patrick Samson repo owner
    • changed status to open

    OK to analyse the possibility. Could you drop an extract of your notification module (in __init__.py?) to fully understand your design?

  2. Martin Pauly
    def send(users, label, extra_context):
        for user in users:
            # do some custom checks if the user should get a message
            if user.notify_on_new_message:
                message = extra_context['pm_message']
                # send an email notification
                email_message = Email(
                    html_template='messages/email/new_message.html',
                    subject=_("New message from {sender}").format(sender=message.sender.get_full_name()),
                    context=extra_context,
                    reply_to=message.sender.email,
                )
                email_message.send(to=user.email)
    

    This is a simplified version of what we are using. I could put this into its own Django app but this seems like a bit of an overkill… It would be really handy if I could just put that into a file notify.py. With the suggested change, notification=import_module('somewhere.notify'), postman then would work as expected without requiring a full Django App. If you agree this might be userful, then I can prepare a Pull request.

  3. Patrick Samson repo owner

    I don’t think there is anything to change for point 1, the create_notice_types() isn't called in the case of the exception.

    For point 2, here is the planned code:

    # make use of a favourite notifier app such as pinax.notifications, or a custom module,
    # but if not installed or not desired, fallback will be to do basic emailing
    notification = None
    notifier_app_id = getattr(settings, 'POSTMAN_NOTIFIER_APP', 'pinax_notifications')
    if notifier_app_id:
        try:  # priority to an app label
            notifier_app_config = apps.get_app_config(notifier_app_id)
        except LookupError:  # probably means the app is not in INSTALLED_APPS, which is valid
            try:  # fallback to a custom module path
                notification = import_module(notifier_app_id)
            except ModuleNotFoundError:  # is valid for a default configuration, without notifier app or module
                pass
        else:
            notification = notifier_app_config.models_module  # "None if the application doesn’t contain a models module"
    

    Tell me if it's enough for your need.

  4. Martin Pauly

    Regarding 1.) you are right, I guess that problem just arose during some of my experiments.

    The planned code looks excactly like what I would need, so if you could add this, that would be great - thanks a lot!

  5. Martin Pauly

    Hi, I just wanted to briefly ask if you already have an estimate of when you will get around to adding this code? Is there anything that I can help out with? And thanks again in advance for adding it! 🙂

  6. Log in to comment