示例#1
0
 def notify(self, **kwargs):
     if not self.previous:
         all_recipients = set()
         posttree = self.get_ancestors()
         #recipients = set((wl.user for wl in WatchList.objects.filter(thread=self.thread) if wl.user not in all_recipients))
         recipients = []
         resources = {}  # Special feature, ha.
         watches_ids = {}  # extra information to use in template.
         for wl in WatchList.objects.select_related(depth=2).filter(
           post__in=posttree).order_by("snapboard_post.depth"):
             # Sorting is required to override resource requirement with
             # the one of the deepest post.
             if wl.user not in all_recipients:
                 # ! Actually, check post.user != wl.user.  Probably.
                 # !! TODO: Should automatically add watches for own posts, or something like that!
                 recipients.append(wl.user)
                 #watches[wl.user] = wl
                 watches_ids[wl.user] = wl.post.id_form_m()
             if wl.xmppresource:
                 resources[wl.user] = wl.xmppresource
             else:
                 # we're sending it to a bare jid. Allow simple
                 # resourcification without specifying post.
                 cache.set('nt_%s' % wl.user.username, [self.id,
                   time.time()])
         recipients = set(recipients)
         if recipients:
             send_notifications(
                 recipients,
                 'new_post_in_watched_thread',
                 extra_context={'post': self,
                   'watches_ids': watches_ids},
                 xmppresources=resources
             )
             all_recipients = all_recipients.union(recipients)
示例#2
0
def grant_group_admin_rights(request, group_id):
    """ """
    """ Although the Group model allows non-members to be admins, this view
    won"t let it.  """
    group = get_object_or_404(Group, pk=group_id)
    if not group.has_admin(request.user):
        raise PermissionError("What?")
    if request.method == 'POST':
        user = User.objects.get(pk=int(request.POST.get('user_id', 0)))
        if not group.has_user(user):
            request.user.message_set.create(
              message=_('The user %s is not a group member.') % user)
        elif group.has_admin(user):
            request.user.message_set.create(
              message=_('The user %s is already a group admin.') % user)
        else:
            group.admins.add(user)
            request.user.message_set.create(
              message=_('The user %s is now a group admin.') % user)
            send_notifications([user], 'group_admin_rights_granted',
              {'group': group})
            send_notifications(list(group.admins.all()), 'new_group_admin',
              {'new_admin': user, 'group': group})
    else:
        raise Http404("Ain't here!")
    return HttpResponse('ok')
示例#3
0
def answer_invitation(request, invitation_id):
    invitation = get_object_or_404(Invitation, pk=invitation_id,
      sent_to=request.user)  # requires testing!
    form = None
    if request.method == 'POST':
        if invitation.accepted is not None:
            return HttpResponseRedirect('')
        form = AnwserInvitationForm(request.POST)
        if form.is_valid():
            if int(form.cleaned_data['decision']):
                invitation.group.users.add(request.user)
                invitation.accepted = True
                request.user.message_set.create(message=_('You are now '
                  'a member of the group %s.') % invitation.group.name)
                send_notifications(list(invitation.group.admins.all()),
                  'new_group_member', {'new_member': request.user,
                  'group': invitation.group})
            else:
                invitation.accepted = False
                request.user.message_set.create(
                  message=_('The invitation has been declined.'))
            invitation.response_date = datetime.datetime.now()
            invitation.save()
    elif invitation.accepted is None:
        form = AnwserInvitationForm()
    return render_to_response('snapboard/invitation',
      {'form': form, 'invitation': invitation},
      context_instance=RequestContext(request, processors=extra_processors))
示例#4
0
def remove_user_from_group(request, group_id):
    group = get_object_or_404(Group, pk=group_id)
    if not group.has_admin(request.user):
        raise PermissionError("What?")
    if request.method == 'POST':
        done = False
        user = User.objects.get(pk=int(request.POST.get('user_id', 0)))
        only_admin = int(request.POST.get('only_admin', 0))
        if not only_admin and group.has_user(user):
            group.users.remove(user)
            done = True
        if group.has_admin(user):
            group.admins.remove(user)
            send_notifications([user], 'group_admin_rights_removed',
              {'group': group})
            done = True
        if done:
            if only_admin:
                request.user.message_set.create(
                  message=_('The admin rights of user %s were removed for '
                    'the group.') % user)
            else:
                request.user.message_set.create(
                  message=_('User %s was removed from the group.') % user)
        else:
            request.user.message_set.create(
              message=_('There was nothing to do for user %s.') % user)
    else:
        raise Http404("Ain't here!")  # ?
    return HttpResponse('ok')
示例#5
0
 def notify_cancelled(instance, **kwargs):
     '''
     Notifies of cancelled invitations.
     '''
     if instance.accepted is None:
         send_notifications(
             [instance.sent_to],
             'group_invitation_cancelled',
             {'invitation': instance})