Commit 64a20e8a by benjaoming

Adding notifications for article edits and creations

parent 700e4662
...@@ -26,12 +26,10 @@ def notify(message, key, target_object=None, url=None): ...@@ -26,12 +26,10 @@ def notify(message, key, target_object=None, url=None):
if not isinstance(target_object, Model): if not isinstance(target_object, Model):
raise TypeError(_(u"You supplied a target_object that's not an instance of a django Model.")) raise TypeError(_(u"You supplied a target_object that's not an instance of a django Model."))
object_id = target_object.id object_id = target_object.id
content_type = ContentType.get_object_for_this_type(target_object)
else: else:
object_id = None object_id = None
content_type = None
objects = models.Notification.create_notifications(key, object_id=object_id, objects = models.Notification.create_notifications(key, object_id=object_id,
content_type=content_type) message=message, url=url)
return len(objects) return len(objects)
\ No newline at end of file
# -*- coding: utf-8 -*-
from django.db import models from django.db import models
from django.db.models import Q from django.db.models import Q
from django.contrib.auth.models import User from django.contrib.auth.models import User
...@@ -15,12 +16,18 @@ class NotificationType(models.Model): ...@@ -15,12 +16,18 @@ class NotificationType(models.Model):
blank=True, null=True) blank=True, null=True)
content_type = models.ForeignKey(ContentType, blank=True, null=True) content_type = models.ForeignKey(ContentType, blank=True, null=True)
def __unicode__(self):
return self.key
class Settings(models.Model): class Settings(models.Model):
user = models.ForeignKey(User) user = models.ForeignKey(User)
interval = models.SmallIntegerField(choices=settings.INTERVALS, verbose_name=_(u'interval'), interval = models.SmallIntegerField(choices=settings.INTERVALS, verbose_name=_(u'interval'),
default=settings.INTERVALS_DEFAULT) default=settings.INTERVALS_DEFAULT)
def __unicode__(self):
return self.user
class Subscription(models.Model): class Subscription(models.Model):
settings = models.ForeignKey(Settings) settings = models.ForeignKey(Settings)
...@@ -29,6 +36,9 @@ class Subscription(models.Model): ...@@ -29,6 +36,9 @@ class Subscription(models.Model):
help_text=_(u'Leave this blank to subscribe to any kind of object')) help_text=_(u'Leave this blank to subscribe to any kind of object'))
send_emails = models.BooleanField(default=True) send_emails = models.BooleanField(default=True)
def __unicode__(self):
return _("Subscription for: %s") % self.settings.user
class Notification(models.Model): class Notification(models.Model):
subscription = models.ForeignKey(Subscription) subscription = models.ForeignKey(Subscription)
...@@ -43,15 +53,20 @@ class Notification(models.Model): ...@@ -43,15 +53,20 @@ class Notification(models.Model):
if not key or not isinstance(key, str): if not key or not isinstance(key, str):
raise KeyError('No notification key (string) specified.') raise KeyError('No notification key (string) specified.')
notification_type = NotificationType.objects.get_or_create(key=key) object_id = kwargs.pop('object_id', None)
objects_created = [] objects_created = []
for subscription in Subscription.objects.filter(Q(key=key)|Q(key=None), subscriptions = Subscription.objects.filter(Q(notification_type__key=key) |
notification_type=notification_type, Q(notification_type__key=None),)
): if object_id:
subscriptions = subscriptions.filter(Q(object_id=object_id) |
Q(object_id=None))
for subscription in subscriptions:
objects_created.append( objects_created.append(
cls.objects.create(subscription=subscription, **kwargs) cls.objects.create(subscription=subscription, **kwargs)
) )
return objects_created return objects_created
\ No newline at end of file def __unicode__(self):
return "%s: %s" % (self.subscription.settings.user, self.message)
# -*- coding: utf-8 -*-
import os import os
from setuptools import setup, find_packages from setuptools import setup, find_packages
......
...@@ -35,5 +35,4 @@ def load(modname, verbose=False, failfast=False): ...@@ -35,5 +35,4 @@ def load(modname, verbose=False, failfast=False):
get_module(app, modname, verbose, failfast) get_module(app, modname, verbose, failfast)
def load_wiki_plugins(): def load_wiki_plugins():
load('wiki_plugin', verbose=False)
load('wiki_plugin')
from django.utils.importlib import import_module
_cache = {} _cache = {}
_settings_forms = [] _settings_forms = []
...@@ -17,4 +18,10 @@ def register(PluginClass): ...@@ -17,4 +18,10 @@ def register(PluginClass):
settings_form = getattr(PluginClass, 'settings_form', None) settings_form = getattr(PluginClass, 'settings_form', None)
if settings_form: if settings_form:
if isinstance(settings_form, basestring):
klassname = settings_form.split(".")[-1]
modulename = ".".join(settings_form.split(".")[:-1])
form_module = import_module(modulename)
settings_form = getattr(form_module, klassname)
_settings_forms.append(settings_form) _settings_forms.append(settings_form)
# -*- coding: utf-8 -*-
from django import forms from django import forms
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from itertools import chain from itertools import chain
......
from django import forms from django import forms
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
import models import settings
from django_notify.models import Settings, NotificationType from django_notify.models import Settings, NotificationType
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
...@@ -14,12 +14,12 @@ class SubscriptionForm(forms.Form): ...@@ -14,12 +14,12 @@ class SubscriptionForm(forms.Form):
settings_write_access = False settings_write_access = False
def __init__(self, article, user, *args, **kwargs): def __init__(self, article, user, *args, **kwargs):
import models
self.article = article self.article = article
self.user = user self.user = user
initial = kwargs.pop('initial', None) initial = kwargs.pop('initial', None)
self.settings = Settings.objects.get_or_create(user=user,)[0] self.settings = Settings.objects.get_or_create(user=user,)[0]
self.notification_type = NotificationType.objects.get_or_create(key=models.NOTIFICATION_ARTICLE_EDIT, self.notification_type = NotificationType.objects.get_or_create(key=settings.ARTICLE_EDIT,
content_type=ContentType.objects.get_for_model(article))[0] content_type=ContentType.objects.get_for_model(article))[0]
self.edit_notifications=models.ArticleSubscription.objects.filter(settings=self.settings, self.edit_notifications=models.ArticleSubscription.objects.filter(settings=self.settings,
article=article, article=article,
...@@ -41,6 +41,7 @@ class SubscriptionForm(forms.Form): ...@@ -41,6 +41,7 @@ class SubscriptionForm(forms.Form):
return _('Your notification settings were unchanged, so nothing saved.') return _('Your notification settings were unchanged, so nothing saved.')
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
import models
cd = self.cleaned_data cd = self.cleaned_data
if not self.changed_data: if not self.changed_data:
return return
......
# -*- coding: utf-8 -*-
from django_notify.models import Subscription from django_notify.models import Subscription
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.db.models import signals
from wiki.models import pluginbase from wiki import models as wiki_models
NOTIFICATION_ARTICLE_EDIT = 'article_edit' from django_notify import notify
from django.core.urlresolvers import reverse
class ArticleSubscription(pluginbase.ArticlePlugin, Subscription): import settings
class ArticleSubscription(wiki_models.pluginbase.ArticlePlugin, Subscription):
def __unicode__(self): def __unicode__(self):
return (_(u"%(user)s subscribing to %(article)s (%(type)s)") % return (_(u"%(user)s subscribing to %(article)s (%(type)s)") %
{'user': self.settings.user.username, {'user': self.settings.user.username,
'article': self.article.current_revision.title, 'article': self.article.current_revision.title,
'type': self.notification_type.label}) 'type': self.notification_type.label})
def post_article_save(instance, **kwargs):
if kwargs.get('created', False):
urlpath = wiki_models.URLPath.objects.filter(articles=instance)
if urlpath:
url = reverse('wiki:get_url', urlpath.path)
else:
url = None
notify(_(u'New article created: %s') % instance.title, settings.ARTICLE_CREATE,
target_object=instance.id, url=url)
def post_article_revision_save(instance, **kwargs):
if kwargs.get('created', False):
try:
urlpath = wiki_models.URLPath.objects.get(articles=instance.article)
url = reverse('wiki:get_url', args=(urlpath.path,))
except wiki_models.URLPath.DoesNotExist:
url = None
notify(_(u'Article modified: %s') % instance.title, settings.ARTICLE_EDIT,
target_object=instance.article, url=url)
# Create notifications when new articles are saved. We do NOT care
# about Article objects that are just modified, because many properties
# are modified without any notifications necessary!
signals.post_save.connect(post_article_save, sender=wiki_models.Article,)
# Whenever a new revision is created, we notifý users that an article
# was edited
signals.post_save.connect(post_article_revision_save, sender=wiki_models.ArticleRevision,)
# TODO: We should notify users when the current_revision of an article is
# changed...
from wiki.core import plugins_registry from wiki.core import plugins_registry
import forms
class NotifyPlugin(plugins_registry.BasePlugin): class NotifyPlugin(plugins_registry.BasePlugin):
settings_form = forms.SubscriptionForm settings_form = 'wiki.plugins.notifications.forms.SubscriptionForm'
def __init__(self): def __init__(self):
#print "I WAS LOADED!" #print "I WAS LOADED!"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment