示例#1
0
def edit_project(request, path=None, template='ponymine/edit_project.html',
    redirect_url=None, form_class=ProjectForm):
    """
    Allows authorized users to create and edit projects
    """
    data = {}
    project = utils.get_project_or_new(path, request.user)

    user_count = User.objects.count()
    MembershipFormSet = formset_factory(MembershipForm,
                                        extra=5,
                                        max_num=user_count)

    if request.method == 'POST':
        form = form_class(request.POST, instance=project)
        memberships = MembershipFormSet(request.POST,
                                        prefix='memberships')

        if form.is_valid() and memberships.is_valid():
            new_proj = form.save()

            # clear the previous memberships
            Membership.objects.filter(project=new_proj).delete()

            # set the memberships for this project
            for info in memberships.cleaned_data:
                # make sure we're not supposed to be removing this membership
                if len(info) and not info.get('remove', False):
                    membership = Membership.objects.create(
                                    project=new_proj,
                                    user=info.get('user'),
                                    role=info.get('role'))

            # redirect the user to the proper page
            if not redirect_url:
                redirect_url = new_proj.get_absolute_url()

            return HttpResponseRedirect(redirect_url)
    else:
        form = form_class(instance=project)

        if project.id:
            mems = Membership.objects.filter(project=project)
            initial = [dict(user=m.user.id, role=m.role.id) for m in mems]
        else:
            initial = []
        memberships = MembershipFormSet(initial=initial,
                                        prefix='memberships')
        #~ raise Exception(initial)

    # try to reduce the chance of a project having itself as a parent
    form.update_parents(project)

    data['title'] = project.id and _('Edit Project') or _('Create Project')
    data['project'] = project
    data['form'] = form
    data['membership_forms'] = memberships

    return render(template, data, context_instance=RequestContext(request))
示例#2
0
def configure_project(request, path,
    template='ponymine/configure_project.html', redirect_url=None):
    """
    Allows authorized users to configure a project
    """
    data = {}
    project = utils.get_project_or_new(path)

    data['title'] = _('Configure Project')
    data['project'] = project

    # TODO: ensure that this only allows superusers and folks with the actual
    # permissions necessary

    return render(template, data, context_instance=RequestContext(request))