# HG changeset patch # User reno # Date 1259585177 -3600 # Node ID f69ff46d324099ff299afa6dce2177a8f6fddd5d # Parent 7b8167c4aa6f457d695feb66fd35d04dc3518a79 various notification changes diff -r 7b8167c4aa6f -r f69ff46d3240 src/cm/client.py --- a/src/cm/client.py Thu Nov 26 12:03:53 2009 +0100 +++ b/src/cm/client.py Mon Nov 30 13:46:17 2009 +0100 @@ -208,6 +208,21 @@ ret['msg'] = _(u'comment saved') return ret +# DIRTY : this function has no error check but anyway errors are not listened to client side +@has_perm_on_text("can_create_comment") +def own_notify(request, key): + email_or_user = None if request.user.is_anonymous() else request.user + if not email_or_user : + email_or_user = request.POST.get('email', None) + if email_or_user : + email_or_user = email_or_user.lower().strip() + + text = Text.objects.get(key=key) + Notification.objects.set_notification(text=None, type='own', active=True, email_or_user=email_or_user) + ret = HttpResponse() + ret.status_code = 200 + return ret + @has_perm_on_text("can_create_comment") def add_comment(request, key): # if edit_comment_id : # @@ -240,6 +255,18 @@ comment_state = 'approved' if text_version.mod_posteriori else 'pending' comment = Comment.objects.create(state=comment_state, text_version=text_version, user=user, name=name, email=email, title=title, content=content, content_html=content_html, tags = tags, start_wrapper = start_wrapper, end_wrapper = end_wrapper, start_offset = start_offset, end_offset = end_offset, reply_to=reply_to) + ask_for_notification = True + if user : + workspace_notify_count = Notification.objects.filter(text=None,type='workspace',user=user, active=True).count() + text_notify_count = Notification.objects.filter(text=text,type='text',user=user, active=True).count() + if workspace_notify_count > 0 or text_notify_count > 0 : + ask_for_notification = False + + if ask_for_notification : + ask_for_notification = ( None == Notification.objects.get_notifications(text=None, type='own', email_or_user=(user if user else email))) + ret['ask_for_notification'] = ask_for_notification + ret['email'] = '' if user else email + if text_version.mod_posteriori or has_perm(request, 'can_view_unapproved_comment', text=text) : ret['comment'] = comment ret['msg'] = _(u"comment saved") @@ -247,7 +274,7 @@ ret['msg'] = _(u"comment saved, it is being held for moderation") if AUTO_CONTRIB_REGISTER: - Notification.objects.set_notification_to_own_discussions(text=text, email_or_user=user or email) + Notification.objects.set_notification(text=text, type='own', active=True, email_or_user=user or email) register_activity(request, "comment_created", text, comment) return ret diff -r 7b8167c4aa6f -r f69ff46d3240 src/cm/media/js/client/c_sync.js --- a/src/cm/media/js/client/c_sync.js Thu Nov 26 12:03:53 2009 +0100 +++ b/src/cm/media/js/client/c_sync.js Mon Nov 30 13:46:17 2009 +0100 @@ -124,6 +124,21 @@ else if (isReply()) this._hideNewReplyForm() ; + if ("ask_for_notification" in ret) { + if (ret['ask_for_notification']) { + // TODO ask for notification ...or use AUTO_CONTRIB ? + parent.f_yesNoDialog(gettext("Do you wan to subscribe to all replies notifications in discussions where you have participated?"), gettext("Warning"), null, null, null, + function() { // special case : no waiting for the return, no error check, nothing ! + var cfg = { + method: "POST", + data: urlEncode({'fun':'ownNotify', 'key':sv_key, 'email':ret['email']}) + } ; + CY.io(sv_client_url, cfg); + }, this, null) ; + } + } + + if ("comment" in ret) { // won't be when add with a priori moderation var comment = ret['comment'] ; diff -r 7b8167c4aa6f -r f69ff46d3240 src/cm/models.py --- a/src/cm/models.py Thu Nov 26 12:03:53 2009 +0100 +++ b/src/cm/models.py Mon Nov 30 13:46:17 2009 +0100 @@ -382,39 +382,30 @@ objects = AttachmentManager() class NotificationManager(KeyManager): - def create_notification(self, text, type, email_or_user): - prev_notification = self.get_notification_to_own_discussions(text, type, email_or_user) - if not prev_notification: - notification = self.create(text=text, type=type) - notification.set_email_or_user(email_or_user) - return notification - else: - return prev_notification + def create_notification(self, text, type, active, email_or_user): + notification = self.create(text=text, type=type, active=active) + notification.set_email_or_user(email_or_user) + return notification - def get_notification_to_own_discussions(self, text, type, email_or_user): + def get_notifications(self, text, type, email_or_user): if isinstance(email_or_user,unicode): prev_notifications = Notification.objects.filter(text=text, type=type, email=email_or_user) else: prev_notifications = Notification.objects.filter(text=text, type=type, user=email_or_user) + if prev_notifications: return prev_notifications[0] else: return None - def set_notification_to_own_discussions(self, text, email_or_user, active=True): - if active: - notification = self.create_notification(text, 'own', email_or_user) - if not notification.active: - notification.active = True - notification.save() - else: - notification = self.create_notification(text, 'own', email_or_user) - notification.active = False + def set_notification(self, text, type, active, email_or_user): + notification = self.get_notifications(text, type, email_or_user) + if notification == None : + self.create_notification(text, type, active, email_or_user) + else : + notification.active = active notification.save() - def subscribe_to_own_text(self, text, user): - return self.create_notification(text, None, user) - class Notification(KeyModel, AuthorModel): text = models.ForeignKey(Text, null=True, blank=True) type = models.CharField(max_length=30, null=True, blank=True) diff -r 7b8167c4aa6f -r f69ff46d3240 src/cm/templates/site/notifications.html --- a/src/cm/templates/site/notifications.html Thu Nov 26 12:03:53 2009 +0100 +++ b/src/cm/templates/site/notifications.html Mon Nov 30 13:46:17 2009 +0100 @@ -52,32 +52,35 @@ {% endif %}