def notify(sender, **kwargs): from cm.security import get_viewable_comments, has_perm allready_notified = set() # avoid sending multiple notifications to same user activity = kwargs["instance"] if activity.type in Activity.VIEWABLE_ACTIVITIES.get("view_users"): # user activity: only viewed by managers notifications = Notification.objects.filter(text=None, active=True).exclude(type="own") for notification in notifications: if notification.user: from cm.security import user_has_perm # import here! if user_has_perm(notification.user, "can_manage_workspace"): send_notification(activity, notification) allready_notified.add(notification.user) elif activity.type in Activity.VIEWABLE_ACTIVITIES.get("view_comments"): notifications = Notification.objects.filter(Q(text=activity.text) | Q(text=None), active=True) for notification in notifications: viewable = get_viewable_comments( FakeRequest(notification.user), Comment.objects.filter(id__in=[activity.comment.id]), text=activity.text ) if viewable and ( ( notification.type == "own" and activity.comment.user != notification.user and activity.comment.top_comment().user == notification.user ) or (notification.type != "own") ): if not notification.user in allready_notified: send_notification(activity, notification) allready_notified.add(notification.user) elif activity.type in Activity.VIEWABLE_ACTIVITIES.get("view_texts"): notifications = Notification.objects.filter(Q(text=activity.text) | Q(text=None), active=True).exclude( type="own" ) for notification in notifications: if notification.user: from cm.security import user_has_perm # import here! if ( user_has_perm(notification.user, "can_view_text", text=activity.text) and not notification.user in allready_notified ): send_notification(activity, notification) allready_notified.add(notification.user) else: if has_perm(None, "can_view_text", text=activity.text) and not notification.email in allready_notified: send_notification(activity, notification) allready_notified.add(notification.email)
def notify(sender, **kwargs): from cm.security import get_viewable_comments, has_perm allready_notified = set( ) # avoid sending multiple notifications to same user activity = kwargs['instance'] if activity.type in Activity.VIEWABLE_ACTIVITIES.get( 'view_users'): # user activity: only viewed by managers notifications = Notification.objects.filter( text=None, active=True).exclude(type='own') for notification in notifications: if notification.user: from cm.security import user_has_perm # import here! if user_has_perm(notification.user, 'can_manage_workspace'): send_notification(activity, notification) allready_notified.add(notification.user) elif activity.type in Activity.VIEWABLE_ACTIVITIES.get('view_comments'): notifications = Notification.objects.filter(Q(text=activity.text) | Q(text=None), active=True) for notification in notifications: viewable = get_viewable_comments( FakeRequest(notification.user), Comment.objects.filter(id__in=[activity.comment.id]), text=activity.text) if viewable and \ ((notification.type == 'own' and activity.comment.user != notification.user and activity.comment.top_comment().user == notification.user) or (notification.type != 'own')): if not notification.user in allready_notified: send_notification(activity, notification) allready_notified.add(notification.user) elif activity.type in Activity.VIEWABLE_ACTIVITIES.get('view_texts'): notifications = Notification.objects.filter( Q(text=activity.text) | Q(text=None), active=True).exclude(type='own') for notification in notifications: if notification.user: from cm.security import user_has_perm # import here! if user_has_perm( notification.user, 'can_view_text', text=activity.text ) and not notification.user in allready_notified: send_notification(activity, notification) allready_notified.add(notification.user) else: if has_perm(None, 'can_view_text', text=activity.text ) and not notification.email in allready_notified: send_notification(activity, notification) allready_notified.add(notification.email)
def text_followup(request, key): text = get_text_by_keys_or_404(key) user = request.user if request.user.is_authenticated() else None from cm.security import user_has_perm # import here! anonymous_can_view_text = user_has_perm(None, 'can_view_text', text=text) text_notify_check = Notification.objects.filter(text=text,type='text',user=user, active=True).count() workspace_notify_check = Notification.objects.filter(text=None,type='workspace',user=user, active=True).count() if request.method == 'POST': if 'activate' in request.POST: text.private_feed_key = generate_key() text.save() display_message(request, _(u"Private feed activated.")) if 'reset' in request.POST: text.private_feed_key = generate_key() text.save() display_message(request, _(u"Private notifications feed reseted.")) if request.POST.get('notif_id',None): notif_id = request.POST.get('notif_id') notif_val = request.POST.get(notif_id,None) if notif_val != None : Notification.objects.set_notification(text=text, type='text', active=(notif_val == 'true'), email_or_user=request.user) template_dict = { 'text' : text, 'workspace_notify_check' : workspace_notify_check, 'text_notify_check' : text_notify_check, 'anonymous_can_view_text' : anonymous_can_view_text, } return render_to_response('site/text_followup.html', template_dict , context_instance=RequestContext(request))
def text_followup(request, key): text = get_text_by_keys_or_404(key) user = request.user if request.user.is_authenticated() else None from cm.security import user_has_perm # import here! anonymous_can_view_text = user_has_perm(None, 'can_view_text', text=text) text_notify_check = Notification.objects.filter(text=text, type='text', user=user, active=True).count() workspace_notify_check = Notification.objects.filter(text=None, type='workspace', user=user, active=True).count() if request.method == 'POST': if 'activate' in request.POST: text.private_feed_key = generate_key() text.save() display_message(request, _(u"Private feed activated.")) if 'reset' in request.POST: text.private_feed_key = generate_key() text.save() display_message(request, _(u"Private notifications feed reseted.")) if request.POST.get('notif_id', None): notif_id = request.POST.get('notif_id') notif_val = request.POST.get(notif_id, None) if notif_val != None: Notification.objects.set_notification( text=text, type='text', active=(notif_val == 'true'), email_or_user=request.user) template_dict = { 'text': text, 'workspace_notify_check': workspace_notify_check, 'text_notify_check': text_notify_check, 'anonymous_can_view_text': anonymous_can_view_text, } return render_to_response('site/text_followup.html', template_dict, context_instance=RequestContext(request))