Example #1
0
def reject_request(request, requestid, email_text_template='groups/email/rejected.txt', email_html_template='groups/email/rejected.html'):

    obj = get_object_or_404(GroupRequest, id=requestid)
    if request.user in obj.group.groupinformation.admins.all() or request.user.is_superuser:
        obj.status = REQUEST_REJECTED
        obj.changed_by = request.user
        obj.save()

        messages.add_message(request, messages.INFO, "%s has been rejected for %s" % (obj.user, obj.group))
        send_group_email(obj, [obj.user.email], '[Auth] Your membership to %s has been rejected.' % obj.group.name, email_text_template, email_html_template)

    return HttpResponseRedirect(reverse('groups.views.admin_group', args=[obj.group.id]))
Example #2
0
def create_request(request, groupid, email_text_template='groups/email/request.txt', email_html_template='groups/email/request.html'):

    group = get_object_or_404(Group, id=groupid)

    if request.user in group.user_set.all() or not group.groupinformation.requestable:
        return HttpResponseRedirect(reverse('groups.views.group_list'))

    if group.groupinformation.parent_groups.count() and not set(group.groupinformation.parent_groups.all()) & set(request.user.groups.all()):
       return HttpResponseRedirect(reverse('groups.views.group_list'))

    if group.requests.filter(status=REQUEST_PENDING,user=request.user).count():
        messages.add_message(request, messages.INFO, "You already have a pending request for %s" % group.name)
        return HttpResponseRedirect(reverse('groups.views.group_list'))

    if not group.groupinformation.moderated:
        # Unmoderated group, so accept the application
        request.user.groups.add(group)
        update_user_access.delay(request.user.id)
        messages.add_message(request, messages.INFO, "You are now a member of %s" % (group))
        return HttpResponseRedirect(reverse('groups.views.group_list'))

    if request.method == 'POST':
        form = GroupRequestForm(request.POST)
        if form.is_valid():
            obj = form.save(commit=False)
            obj.user = request.user
            obj.group = group
            obj.changed_by = request.user
            obj.save()

            messages.add_message(request, messages.INFO, "You membership request has been created.")
            to_email = obj.group.groupinformation.admins.values_list('email', flat=True)
            # If no group admins are set, send to the superusers
            if len(to_email) == 0:
                to_email = User.objects.filter(is_superuser=True).values_list('email', flat=True)
            send_group_email(obj, to_email, '[Auth] %s has requested membership to %s' % (obj.user.username, obj.group.name), email_text_template, email_html_template)

            return HttpResponseRedirect(reverse('groups.views.group_list')) # Redirect after POST
    else:
        form = GroupRequestForm() # An unbound form

    return render_to_response('groups/create_request.html', locals(), context_instance=RequestContext(request))