Esempio n. 1
0
def edit_group_membership(request):
    form = forms.EditGroupMembershipForm(request.POST)
    if form.is_valid():
        group_name = form.cleaned_data['group_name']
        user_id = form.cleaned_data['user_id']
        try:
            user = models.User.objects.get(id = user_id)
        except models.User.DoesNotExist:
            raise exceptions.PermissionDenied(
                'user with id %d not found' % user_id
            )

        action = form.cleaned_data['action']
        #warning: possible race condition
        if action == 'add':
            group_params = {'group_name': group_name, 'user': user}
            group = models.Tag.group_tags.get_or_create(**group_params)
            request.user.edit_group_membership(user, group, 'add')
            template = get_template('widgets/group_snippet.html')
            return {
                'name': group.name,
                'description': getattr(group.tag_wiki, 'text', ''),
                'html': template.render({'group': group})
            }
        elif action == 'remove':
            try:
                group = models.Tag.group_tags.get_by_name(group_name = group_name)
                request.user.edit_group_membership(user, group, 'remove')
            except models.Tag.DoesNotExist:
                raise exceptions.PermissionDenied()
        else:
            raise exceptions.PermissionDenied()
    else:
        raise exceptions.PermissionDenied()
Esempio n. 2
0
def edit_group_membership(request):
    # TODO: this call may need to go.
    # it used to be the one creating groups
    # from the user profile page
    # we have a separate method
    form = forms.EditGroupMembershipForm(request.POST)
    if form.is_valid():
        group_name = form.cleaned_data['group_name']
        user_id = form.cleaned_data['user_id']
        try:
            user = models.User.objects.get(id=user_id)
        except models.User.DoesNotExist:
            raise exceptions.PermissionDenied('user with id %d not found' %
                                              user_id)

        action = form.cleaned_data['action']
        # warning: possible race condition
        if action == 'add':
            try:
                group = models.Group.objects.get(name=group_name)
                request.user.edit_group_membership(user, group, 'add')
                return {
                    'name':
                    group.name,
                    'description':
                    getattr(group.description, 'text', ''),
                    'html':
                    render_to_string('widgets/group_snippet.jinja',
                                     {'group': group}),
                }
            except models.Group.DoesNotExist:
                raise exceptions.PermissionDenied(
                    _('Group %(name)s does not exist') % {'name': group_name})

        elif action == 'remove':
            try:
                group = models.Group.objects.get(name=group_name)
                request.user.edit_group_membership(user, group, 'remove')
            except models.Group.DoesNotExist:
                raise exceptions.PermissionDenied()
        else:
            raise exceptions.PermissionDenied()
    else:
        raise exceptions.PermissionDenied()