예제 #1
0
def add_edit_role(request, *args, **kwargs):
    """Takes in the project id and allows you to add a new role."""

    project_name = kwargs['project_name']
    project = get_object_or_404(CollabProject, slug=project_name)
    id = project.id

    # If the user is not the project admin, just return him to the project page.
    if not project.admin.filter(
            id=request.user.id) and not request.user.is_superuser:
        request.user.message_set.create(
            message="You do not have privileges to edit roles!")
        return HttpResponseRedirect(project.get_absolute_url())

    if 'role_id' in kwargs:
        role_id = kwargs['role_id']
        role = get_object_or_404(Role, id=role_id)
        # Check if the role exists in that project!
        if role.project.id != project.id:
            request.user.message_set.create(
                message="The role does not exist in that project!")
            return HttpResponseRedirect(project.get_absolute_url())
        edit, instance, initial = True, role, None
    else:
        edit, instance, initial = False, None, {}

    if request.method == 'POST':
        form = RoleForm(project.id,
                        request.POST,
                        instance=instance,
                        clean=(not edit),
                        initial=initial)
        if form.is_valid():
            if not edit:
                new_role = Role()
                message = "The role was added."
            else:
                message = "The role was modified."
            new_role = form.save(commit=False)
            new_role.project = project
            new_role.save()
            # Need this as I have a many to many field and did commit=False.
            form.save_m2m()

            # It's possible that a user gave some Editable privileges without the corresponding Viewable. The
            # following code automatically adds those Viewable privileges.
            for privilege in form.cleaned_data["privileges"]:
                if privilege.permission_type == 'Editable':
                    new_privilege = get_object_or_404(
                        Privilege,
                        project=project,
                        related_model=privilege.related_model,
                        permission_type='Viewable')
                    new_role.privileges.add(new_privilege)
            new_role.save()

            if form.cleaned_data["make_default"] == True:
                if project.default_role != new_role:
                    project.default_role = new_role
                    project.save()

            request.user.message_set.create(message=message)
            return HttpResponseRedirect(
                reverse('roles_overview',
                        kwargs={'project_name': project.slug}))
    else:
        form = RoleForm(project.id, instance=instance, initial=initial)
    return render_to_response('project/addrole.html', {
        'form': form,
        'project': project,
        'edit': edit,
        'role': instance
    },
                              context_instance=RequestContext(request))