Skip to content

Commit

Permalink
Allow notice_setting_for_user to be overriden
Browse files Browse the repository at this point in the history
  • Loading branch information
paltman committed Feb 17, 2017
1 parent a2c20e4 commit aa6dd18
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 45 deletions.
4 changes: 2 additions & 2 deletions pinax/notifications/backends/base.py
Expand Up @@ -3,7 +3,7 @@
from django.contrib.sites.models import Site

from ..conf import settings
from ..utils import notice_setting_for_user
from ..hooks import hookset


class BaseBackend(object):
Expand All @@ -20,7 +20,7 @@ def can_send(self, user, notice_type, scoping):
Determines whether this backend is allowed to send a notification to
the given user and notice_type.
"""
return notice_setting_for_user(user, notice_type, self.medium_id, scoping).send
return hookset.notice_setting_for_user(user, notice_type, self.medium_id, scoping).send

def deliver(self, recipient, sender, notice_type, extra_context):
"""
Expand Down
4 changes: 4 additions & 0 deletions pinax/notifications/conf.py
Expand Up @@ -49,6 +49,7 @@ class PinaxNotificationsAppConf(AppConf):
GET_LANGUAGE_MODEL = None
LANGUAGE_MODEL = None
QUEUE_ALL = False
HOOKSET = "pinax.notifications.hooks.DefaultHookSet"
BACKENDS = [
("email", "pinax.notifications.backends.email.EmailBackend"),
]
Expand All @@ -73,5 +74,8 @@ def configure_get_language_model(self, value):
if value is None:
return lambda: load_model(settings.PINAX_NOTIFICATIONS_LANGUAGE_MODEL)

def configure_hookset(self, value):
return load_path_attr(value)()

class Meta:
prefix = "pinax_notifications"
49 changes: 49 additions & 0 deletions pinax/notifications/hooks.py
@@ -0,0 +1,49 @@
from django.core.exceptions import ObjectDoesNotExist

from django.contrib.contenttypes.models import ContentType

from .conf import settings
from .utils import load_media_defaults


class DefaultHookSet(object):

def notice_setting_for_user(self, user, notice_type, medium, scoping=None):
kwargs = {
"notice_type": notice_type,
"medium": medium
}
if scoping:
kwargs.update({
"scoping_content_type": ContentType.objects.get_for_model(scoping),
"scoping_object_id": scoping.pk
})
else:
kwargs.update({
"scoping_content_type__isnull": True,
"scoping_object_id__isnull": True
})
try:
return user.noticesetting_set.get(**kwargs)
except ObjectDoesNotExist:
_, NOTICE_MEDIA_DEFAULTS = load_media_defaults()
if scoping is None:
kwargs.pop("scoping_content_type__isnull")
kwargs.pop("scoping_object_id__isnull")
kwargs.update({
"scoping_content_type": None,
"scoping_object_id": None
})
default = (NOTICE_MEDIA_DEFAULTS[medium] <= notice_type.default)
kwargs.update({"send": default})
setting = user.noticesetting_set.create(**kwargs)
return setting


class HookProxy(object):

def __getattr__(self, attr):
return getattr(settings.PINAX_NOTIFICATIONS_HOOKSET, attr)


hookset = HookProxy()
5 changes: 3 additions & 2 deletions pinax/notifications/models.py
Expand Up @@ -15,7 +15,8 @@

from .compat import GenericForeignKey
from .conf import settings
from .utils import load_media_defaults, notice_setting_for_user
from .utils import load_media_defaults
from .hooks import hookset


NOTICE_MEDIA, NOTICE_MEDIA_DEFAULTS = load_media_defaults()
Expand Down Expand Up @@ -92,7 +93,7 @@ def for_user(cls, user, notice_type, medium, scoping=None):
@@@ consider deprecating
"""
return notice_setting_for_user(user, notice_type, medium, scoping)
return hookset.notice_setting_for_user(user, notice_type, medium, scoping)

class Meta:
verbose_name = _("notice setting")
Expand Down
39 changes: 0 additions & 39 deletions pinax/notifications/utils.py
@@ -1,7 +1,3 @@
from django.core.exceptions import ObjectDoesNotExist

from django.contrib.contenttypes.models import ContentType

from .conf import settings


Expand All @@ -13,38 +9,3 @@ def load_media_defaults():
media.append(key)
defaults[key[0]] = backend.spam_sensitivity
return media, defaults


def notice_setting_for_user(user, notice_type, medium, scoping=None):
"""
@@@ candidate for overriding via a hookset method so you can customize lookup at site level
"""
kwargs = {
"notice_type": notice_type,
"medium": medium
}
if scoping:
kwargs.update({
"scoping_content_type": ContentType.objects.get_for_model(scoping),
"scoping_object_id": scoping.pk
})
else:
kwargs.update({
"scoping_content_type__isnull": True,
"scoping_object_id__isnull": True
})
try:
return user.noticesetting_set.get(**kwargs)
except ObjectDoesNotExist:
_, NOTICE_MEDIA_DEFAULTS = load_media_defaults()
if scoping is None:
kwargs.pop("scoping_content_type__isnull")
kwargs.pop("scoping_object_id__isnull")
kwargs.update({
"scoping_content_type": None,
"scoping_object_id": None
})
default = (NOTICE_MEDIA_DEFAULTS[medium] <= notice_type.default)
kwargs.update({"send": default})
setting = user.noticesetting_set.create(**kwargs)
return setting
4 changes: 2 additions & 2 deletions pinax/notifications/views.py
Expand Up @@ -4,7 +4,7 @@

from .compat import login_required
from .models import NoticeType, NOTICE_MEDIA
from .utils import notice_setting_for_user
from .hooks import hookset


class NoticeSettingsView(TemplateView):
Expand All @@ -19,7 +19,7 @@ def scoping(self):
return None

def setting_for_user(self, notice_type, medium_id):
return notice_setting_for_user(
return hookset.notice_setting_for_user(
self.request.user,
notice_type,
medium_id,
Expand Down

0 comments on commit aa6dd18

Please sign in to comment.