def change_observers(watch, decision, watcher): if watch: if not notification.is_observing(decision, watcher): notification.observe(decision, watcher, DECISION_CHANGE) else: if notification.is_observing(decision, watcher): notification.stop_observing(decision, watcher)
def form_valid(self, form): form.instance.editor = self.request.user form.instance.last_status = self.last_status if not form.cleaned_data['watch'] and notification.is_observing(self.object, self.request.user): notification.stop_observing(self.object, self.request.user) elif form.cleaned_data['watch'] and not notification.is_observing(self.object, self.request.user): notification.observe(self.object, self.request.user, 'decision_change') return super(DecisionUpdate, self).form_valid(form)
def form_valid(self, form, *args, **kwargs): form.instance.editor = self.request.user if not notification.is_observing(self.object.decision, self.request.user): notification.observe(self.object.decision, self.request.user, 'decision_change') return super(FeedbackUpdate, self).form_valid(form, *args, **kwargs)
def object_comment(sender, instance, created, **kwargs): if isinstance(instance.content_object, Image): observed = instance.content_object signal = notice_type_label = "photos_image_comment" observer = user = instance.user if notification and created: if not notification.is_observing(observed, observer, signal): notification.observe(observed, observer, notice_type_label, signal) notice_uid = '{0}_{1}_{2}'.format( notice_type_label, Site.objects.get_current().pk, instance.pk ) notification.send_observation_notices_for( observed, signal, extra_context={ "context_object": instance, "notice_uid": notice_uid, "user": user, "image": observed, "comment": instance, "group": observed.group, } )
def object_comment(sender, instance, created, **kwargs): if isinstance(instance.content_object, Topic): observed = instance.content_object signal = notice_type_label = 'topic_comment' observer = user = instance.user Topic.objects.filter(pk=observed.pk).update(modified=datetime.now()) # Don't send a signal if notification and created: if not notification.is_observing(observed, observer, signal): notification.observe(observed, observer, notice_type_label, signal) notice_uid = '{0}_{1}_{2}'.format( notice_type_label, Site.objects.get_current().pk, instance.pk ) notification.send_observation_notices_for( observed, signal, extra_context={ "context_object": instance, "user": user, "topic": observed, "comment": instance, "group": observed.group, "notice_uid": notice_uid, } )
def render_tag(self, context, obj, signal, notice_type, kwa, varname): observer = context['request'].user content_type = ContentType.objects.get_for_model(obj) observed = False if is_observing(observed=obj, observer=observer, signal=signal): observed = True result = '' perm = permission_by_label(obj, 'view') allowed = observer.is_authenticated() and ( observer.has_perm(perm, obj) or observed # Can stop oserving for closed observed object ) if allowed: local_context = copy.copy(context) local_context.update(kwa) local_context.update({ "object": obj, "object_id": obj.pk, "content_type_id": content_type.pk, 'observer': observer, "signal": signal, "notice_type": notice_type, "observed": observed, }) result = render_to_string('notification/observe_link.html', local_context) if varname: context[varname] = result return '' return result
def observe_toggle(request, content_type_id, object_id, signal, notice_type_label): success = False observing = None try: content_type = ContentType.objects.get(pk=content_type_id) observed = content_type.get_object_for_this_type(pk=object_id) if not is_observing(observed=observed, observer=request.user, signal=signal): observe(observed=observed, observer=request.user, notice_type_label=notice_type_label, signal=signal) observing = True else: stop_observing(observed=observed, observer=request.user, signal=signal) observing = False success = True except: pass return HttpResponse( json.dumps({"success": success, "observing": observing, }), mimetype='application/json; charset=utf-8', status=200 )
def form_valid(self, form): form.instance.editor = self.request.user form.instance.last_status = self.last_status form.instance.minor_edit = form.cleaned_data['minor_edit'] if (not form.cleaned_data['watch'] and notification.is_observing(self.object, self.request.user)): notification.stop_observing(self.object, self.request.user) elif (form.cleaned_data['watch'] and not notification.is_observing(self.object, self.request.user)): notification.observe( self.object, self.request.user, DECISION_CHANGE ) return super(DecisionUpdate, self).form_valid(form)
def stop_observing_article(request, title, group_slug=None, group_slug_field=None, group_qs=None, article_qs=ALL_ARTICLES, template_name='recentchanges.html', template_dir='wiki', extra_context=None, is_member=None, is_private=None, *args, **kw): article_args = {'title': title} group = None if group_slug is not None: group = get_object_or_404(group_qs, **{group_slug_field: group_slug}) article_args.update({ 'content_type': get_ct(group), 'object_id': group.id }) allow_read = has_read_perm(request.user, group, is_member, is_private) else: allow_read = True if not allow_read: return HttpResponseForbidden() article = get_object_or_404(article_qs, **article_args) if notification.is_observing(article, request.user): notification.stop_observing(article, request.user) return redirect(article)
def object_comment(sender, instance, created, **kwargs): if isinstance(instance.content_object, Post): observed = instance.content_object signal = notice_type_label = "blog_post_comment" observer = user = instance.user # Post.objects.filter(pk=observed.pk).update(updated_at=datetime.now()) # Don't send a signal if notification and created: if not notification.is_observing(observed, observer, signal): notification.observe(observed, observer, notice_type_label, signal) notice_uid = '{0}_{1}_{2}'.format( notice_type_label, Site.objects.get_current().pk, instance.pk ) notification.send_observation_notices_for( observed, signal, extra_context={ "context_object": instance, "notice_uid": notice_uid, "user": user, "post": observed, "comment": instance } )
def topic(request, topic_id, group_slug=None, edit=False, template_name="forums/topic.html", bridge=None): if bridge: try: group = bridge.get_group(group_slug) except ObjectDoesNotExist: raise Http404 else: group = None if group: topics = group.content_objects(Topic) else: topics = Topic.objects.filter(object_id=None) topic = get_object_or_404(topics, id=topic_id) if notification is not None: is_observing = notification.is_observing(topic, request.user) else: is_observing = False if (request.method == "POST" and edit == True and (request.user == topic.creator or request.user == topic.group.creator)): topic.body = request.POST["body"] topic.save() return HttpResponseRedirect(topic.get_absolute_url(group)) if group: group_base = bridge.group_base_template() else: group_base = None if notification: # send notification to company member who are watching the topic members = topic.group.members.all() watching_users = [] for u in members: if notification.is_observing(topic, u): watching_users.append(u) notification.send(watching_users, "forums_observed_topic_changed", {"user": request.user, "company": topic.group}) return render_to_response(template_name, { "topic": topic, "edit": edit, "group": group, "group_base": group_base, "is_observing": is_observing, "can_observe": True }, context_instance=RequestContext(request))
def subscribe_creator(sender, instance, created, **kwargs): if notification and created and isinstance(instance, Article): for observer, notice_type_label, signal in ( (instance.creator, 'wiki_article_edited', 'post_save'), (instance.creator, 'wiki_article_comment', 'wiki_article_comment'), ): if observer and not notification.is_observing(instance, observer, signal): notification.observe(instance, observer, notice_type_label, signal)
def view_article( request, title, ArticleClass=Article, # to create an unsaved instance group_slug=None, bridge=None, article_qs=ALL_ARTICLES, template_name='view.html', template_dir='wiki', extra_context=None, is_member=None, is_private=None, *args, **kw): if request.method == 'GET': if group_slug is not None: try: group = bridge.get_group(group_slug) except ObjectDoesNotExist: raise Http404 allow_read = has_read_perm(request.user, group, is_member, is_private) allow_write = has_write_perm(request.user, group, is_member) else: group = None allow_read = allow_write = True if not allow_read: return HttpResponseForbidden() try: article = article_qs.get_by(title, group) if notification is not None: is_observing = notification.is_observing(article, request.user) else: is_observing = False except ArticleClass.DoesNotExist: article = ArticleClass(title=title) is_observing = False template_params = {'article': article, 'allow_write': allow_write} if notification is not None: template_params.update({ 'is_observing': is_observing, 'can_observe': True }) template_params['group'] = group if extra_context is not None: template_params.update(extra_context) return render_to_response(os.path.join(template_dir, template_name), template_params, context_instance=RequestContext(request)) return HttpResponseNotAllowed(['GET'])
def additional_message_required(user, decision, level): organization = decision.organization result = notification.is_observing(decision, user) notification_settings, _ = NotificationSettings.objects.get_or_create(user=user, organization=organization) result = not result and notification_settings.notification_level < level return result
def additional_message_required(user, decision, level): organization = decision.organization result = notification.is_observing(decision, user) notification_settings, _ = NotificationSettings.objects.get_or_create( user=user, organization=organization) result = not result and notification_settings.notification_level < level return result
def view_article(request, title, ArticleClass=Article, # to create an unsaved instance group_slug=None, bridge=None, article_qs=ALL_ARTICLES, template_name='view.html', template_dir='wiki', extra_context=None, is_member=None, is_private=None, *args, **kw): if request.method == 'GET': if group_slug is not None: try: group = bridge.get_group(group_slug) except ObjectDoesNotExist: raise Http404 allow_read = has_read_perm(request.user, group, is_member, is_private) allow_write = has_write_perm(request.user, group, is_member) else: group = None allow_read = allow_write = True if not allow_read: return HttpResponseForbidden() try: article = article_qs.get_by(title, group) if notification is not None: is_observing = notification.is_observing(article, request.user) else: is_observing = False except ObjectDoesNotExist: article = ArticleClass(title=title) is_observing = False template_params = {'article': article, 'allow_write': allow_write} if notification is not None: template_params.update({'is_observing': is_observing, 'can_observe': True}) if group_slug is not None: template_params['group'] = group if extra_context is not None: template_params.update(extra_context) return render_to_response(os.path.join(template_dir, template_name), template_params, context_instance=RequestContext(request)) return HttpResponseNotAllowed(['GET'])
def view_article(request, title, ArticleClass=Article, # to create an unsaved instance group_slug=None, group_slug_field=None, group_qs=None, article_qs=ALL_ARTICLES, template_name='view.html', template_dir='wiki', extra_context=None, is_member=None, is_private=None, *args, **kw): if request.method == 'GET': article_args = {'title': title} if group_slug is not None: group = get_object_or_404(group_qs,**{group_slug_field: group_slug}) article_args.update({'content_type': get_ct(group), 'object_id': group.id}) allow_read = has_read_perm(request.user, group, is_member, is_private) allow_write = has_write_perm(request.user, group, is_member) else: allow_read = allow_write = True if not allow_read: return HttpResponseForbidden() try: article = article_qs.get(**article_args) if notification is not None: is_observing = notification.is_observing(article, request.user) else: is_observing = False except ArticleClass.DoesNotExist: article = ArticleClass(**article_args) is_observing = False template_params = {'article': article, 'allow_write': allow_write} if notification is not None: template_params.update({'is_observing': is_observing, 'can_observe': True}) if group_slug is not None: template_params['group'] = group if extra_context is not None: template_params.update(extra_context) return render_to_response('/'.join([template_dir, template_name]), template_params, context_instance=RequestContext(request)) return HttpResponseNotAllowed(['GET'])
def is_watched(self, user, signal=None): """ Return a boolean value if an object is watched by an user or not It is possible also verify if it is watched by a user in a specific signal, passing the signal as a second parameter """ if signal: return is_observing(self, user, signal) if isinstance(user, AnonymousUser): return False ctype = ContentType.objects.get_for_model(self) observed_items = ObservedItem.objects.filter(content_type=ctype, object_id=self.id, user=user) if observed_items: return True else: return False
def view_article( request, title, revision=None, ArticleClass=Article, # to create an unsaved instance group_slug=None, group_slug_field=None, group_qs=None, article_qs=ALL_ARTICLES, template_name='view.html', template_dir='wiki', extra_context=None, is_member=None, is_private=None, *args, **kw): if request.method == 'GET': article_args = {'title': title} if group_slug is not None: group = get_object_or_404(group_qs, **{group_slug_field: group_slug}) article_args.update({ 'content_type': get_ct(group), 'object_id': group.id }) allow_read = has_read_perm(request.user, group, is_member, is_private) allow_write = has_write_perm(request.user, group, is_member) else: allow_read = allow_write = True if not allow_read: return HttpResponseForbidden() is_observing = False redirected_from = None try: article = article_qs.get(**article_args) if notification is not None: is_observing = notification.is_observing(article, request.user) except ArticleClass.DoesNotExist: try: # try to find an article that once had this title article = ChangeSet.objects.filter( old_title=title).order_by('-revision')[0].article redirected_from = title # if article is not None: # return redirect(article, permanent=True) except IndexError: article = ArticleClass(**article_args) if revision is not None: changeset = get_object_or_404(article.changeset_set, revision=revision) article.content = changeset.get_content() template_params = { 'article': article, 'revision': revision, 'redirected_from': redirected_from, 'allow_write': allow_write } if notification is not None: template_params.update({ 'is_observing': is_observing, 'can_observe': True }) if group_slug is not None: template_params['group'] = group if extra_context is not None: template_params.update(extra_context) return render( request, '/'.join([template_dir, template_name]), template_params, ) return HttpResponseNotAllowed(['GET'])
def forum(request, forum_id, group_slug=None, edit=False, template_name="forums/forum.html", bridge=None, form_class=ForumTopicForm, new_topic=False): if bridge: try: group = bridge.get_group(group_slug) except ObjectDoesNotExist: raise Http404 else: group = None if not request.user.is_authenticated(): is_member = False else: if group: is_member = group.user_is_member(request.user) else: is_member = True if group: forums = group.content_objects(Forum) topics = group.content_objects(Topic).filter(forum=forum_id) search_terms = request.GET.get('search', '') if search_terms: topics = (topics.filter(title__icontains=search_terms) | topics.filter(body__icontains=search_terms)) else: forums = Forum.objects.filter(object_id=None) topics = Topic.objects.filter(object_id=None) forum = get_object_or_404(forums, id=forum_id) if notification is not None: is_observing = notification.is_observing(forum, request.user) else: is_observing = False if (request.method == "POST" and edit == True and (request.user == forum.creator or request.user == forum.group.creator)): forum.body = request.POST["body"] forum.save() return HttpResponseRedirect(forum.get_absolute_url(group)) elif request.method == "POST": if request.user.is_authenticated(): if is_member: topic_form = form_class(request.POST) if topic_form.is_valid(): topic = topic_form.save(commit=False) if group: group.associate(topic, commit=False) topic.creator = request.user topic.forum_id = forum_id topic.save() request.user.message_set.create(message=_("You have started the topic %(topic_title)s") % {"topic_title": topic.title}) topic_form = form_class() # @@@ is this the right way to reset it? if notification: # send notification to company member who are watching the forum members = forum.group.members.all() watching_users = [] for u in members: if notification.is_observing(forum, u): watching_users.append(u) notification.send(watching_users, "forums_observed_forum_changed", {"user": request.user, "company": forum.group}) else: request.user.message_set.create(message=_("You are not a member and so cannot start a new topic")) topic_form = form_class() else: return HttpResponseForbidden() else: topic_form = form_class() if group: group_base = bridge.group_base_template() else: group_base = None return render_to_response(template_name, { "is_member": is_member, "forum": forum, "topics": topics, "topic_form": topic_form, "edit": edit, "group": group, "group_base": group_base, "is_observing": is_observing, "can_observe": True }, context_instance=RequestContext(request))
def get(self, request, *args, **kwargs): decision = self.get_object() user = self.get_user() if notification.is_observing(decision, user): notification.stop_observing(decision, user) return HttpResponseRedirect(request.GET['next'])
def subscribe_creator(sender, instance, created, **kwargs): if notification and created and isinstance(instance, Topic): signal = notice_type_label = 'topic_comment' observer = instance.creator if observer and not notification.is_observing(instance, observer, signal): notification.observe(instance, observer, notice_type_label, signal)
def subscribe_creator(sender, instance, created, **kwargs): if notification and created and isinstance(instance, Image): signal = notice_type_label = "photos_image_comment" observer = instance.member if observer and not notification.is_observing(instance, observer, signal): notification.observe(instance, observer, notice_type_label, signal)
def unsubscribe_user(user, feedback): """ Unsubscribe a user to any change of a given feedback """ if notification.is_observing(feedback, user, signal='feedback_updated'): notification.stop_observing(feedback, user, 'feedback_updated')
def view_article( request, title, ArticleClass=Article, # to create an unsaved instance group_slug=None, group_slug_field=None, group_qs=None, article_qs=ALL_ARTICLES, template_name='view.html', template_dir='wiki', extra_context=None, is_member=None, is_private=None, *args, **kw): if request.method == 'GET': article_args = {'title': title} if group_slug is not None: group = get_object_or_404(group_qs, **{group_slug_field: group_slug}) article_args.update({ 'content_type': get_ct(group), 'object_id': group.id }) allow_read = has_read_perm(request.user, group, is_member, is_private) allow_write = has_write_perm(request.user, group, is_member) else: allow_read = allow_write = True if not allow_read: return HttpResponseForbidden() try: article = article_qs.get(**article_args) if notification is not None: is_observing = notification.is_observing(article, request.user) else: is_observing = False except ArticleClass.DoesNotExist: article = ArticleClass(**article_args) is_observing = False ##### if group_slug is not None: # template_params['group'] = group new_article = ArticleClass(title="NewArticle", content_type=get_ct(group), object_id=group.id) else: new_article = ArticleClass(title="NewArticle") # template_params['new_article'] = new_article ##### template_params = { 'new_article': new_article, 'article': article, 'allow_write': allow_write } if notification is not None: template_params.update({ 'is_observing': is_observing, 'can_observe': True }) if group_slug is not None: template_params['group'] = group if extra_context is not None: template_params.update(extra_context) return render_to_response('/'.join([template_dir, template_name]), template_params, context_instance=RequestContext(request)) return HttpResponseNotAllowed(['GET'])
def subscribe_creator(sender, instance, created, **kwargs): if notification and created and isinstance(instance, Post): signal = notice_type_label = "blog_post_comment" observer = instance.author if observer and not notification.is_observing(instance, observer, signal): notification.observe(instance, observer, notice_type_label, signal)