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):
if not isinstance(target_object, Model):
raise TypeError(_(u"You supplied a target_object that's not an instance of a django Model."))
object_id = target_object.id
content_type = ContentType.get_object_for_this_type(target_object)
else:
object_id = None
content_type = None
objects = models.Notification.create_notifications(key, object_id=object_id,
content_type=content_type)
message=message, url=url)
return len(objects)
\ No newline at end of file
# -*- coding: utf-8 -*-
from django.db import models
from django.db.models import Q
from django.contrib.auth.models import User
......@@ -15,12 +16,18 @@ class NotificationType(models.Model):
blank=True, null=True)
content_type = models.ForeignKey(ContentType, blank=True, null=True)
def __unicode__(self):
return self.key
class Settings(models.Model):
user = models.ForeignKey(User)
interval = models.SmallIntegerField(choices=settings.INTERVALS, verbose_name=_(u'interval'),
default=settings.INTERVALS_DEFAULT)
def __unicode__(self):
return self.user
class Subscription(models.Model):
settings = models.ForeignKey(Settings)
......@@ -29,6 +36,9 @@ class Subscription(models.Model):
help_text=_(u'Leave this blank to subscribe to any kind of object'))
send_emails = models.BooleanField(default=True)
def __unicode__(self):
return _("Subscription for: %s") % self.settings.user
class Notification(models.Model):
subscription = models.ForeignKey(Subscription)
......@@ -43,15 +53,20 @@ class Notification(models.Model):
if not key or not isinstance(key, str):
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 = []
for subscription in Subscription.objects.filter(Q(key=key)|Q(key=None),
notification_type=notification_type,
):
subscriptions = Subscription.objects.filter(Q(notification_type__key=key) |
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(
cls.objects.create(subscription=subscription, **kwargs)
)
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
from setuptools import setup, find_packages
......
......@@ -35,5 +35,4 @@ def load(modname, verbose=False, failfast=False):
get_module(app, modname, verbose, failfast)
def load_wiki_plugins():
load('wiki_plugin')
load('wiki_plugin', verbose=False)
from django.utils.importlib import import_module
_cache = {}
_settings_forms = []
......@@ -17,4 +18,10 @@ def register(PluginClass):
settings_form = getattr(PluginClass, 'settings_form', None)
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)
# -*- coding: utf-8 -*-
from django import forms
from django.utils.translation import ugettext as _
from itertools import chain
......
from django import forms
from django.utils.translation import ugettext as _
import models
import settings
from django_notify.models import Settings, NotificationType
from django.contrib.contenttypes.models import ContentType
from django.utils.safestring import mark_safe
......@@ -14,12 +14,12 @@ class SubscriptionForm(forms.Form):
settings_write_access = False
def __init__(self, article, user, *args, **kwargs):
import models
self.article = article
self.user = user
initial = kwargs.pop('initial', None)
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]
self.edit_notifications=models.ArticleSubscription.objects.filter(settings=self.settings,
article=article,
......@@ -41,6 +41,7 @@ class SubscriptionForm(forms.Form):
return _('Your notification settings were unchanged, so nothing saved.')
def save(self, *args, **kwargs):
import models
cd = self.cleaned_data
if not self.changed_data:
return
......
# -*- coding: utf-8 -*-
from django_notify.models import Subscription
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):
return (_(u"%(user)s subscribing to %(article)s (%(type)s)") %
{'user': self.settings.user.username,
'article': self.article.current_revision.title,
'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
import forms
class NotifyPlugin(plugins_registry.BasePlugin):
settings_form = forms.SubscriptionForm
settings_form = 'wiki.plugins.notifications.forms.SubscriptionForm'
def __init__(self):
#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