示例#1
0
文件: views.py 项目: cns-iu/nwb
def create_project(request):
    if request.method != 'POST':
        new_project_form = ProjectForm()
        project_datasets = ProjectDataSetFormSet(prefix='project_datasets')
    else:
        project_datasets = ProjectDataSetFormSet(request.POST, prefix='project_datasets')
        new_project_form = ProjectForm(request.POST)
        
        if new_project_form.is_valid() and project_datasets.is_valid():
            name = new_project_form.cleaned_data['name']
            category = new_project_form.cleaned_data['category']
            description = new_project_form.cleaned_data['description']
            new_project = Project.objects.create(
                creator=request.user,
                name=name,
                category=category,
                description=description,
                is_active=True)
            
            for dataset_form in project_datasets.forms:
                if dataset_form.is_valid() and 'dataset' in dataset_form.cleaned_data:
                    dataset = dataset_form.cleaned_data['dataset']
                    new_project.datasets.add(dataset)
            
            view_project_url = get_item_url(new_project, 'epic.projects.views.view_project')
            
            return HttpResponseRedirect(view_project_url)
    
    return render_to_response(
        'projects/create_project.html',
        {'new_project_form': new_project_form, 'project_datasets': project_datasets},
        context_instance=RequestContext(request))
示例#2
0
文件: views.py 项目: cns-iu/nwb
def create_project(request):
    if request.method != 'POST':
        new_project_form = ProjectForm()
    else:
        new_project_form = ProjectForm(request.POST)
        
        if new_project_form.is_valid():
            name = new_project_form.cleaned_data['name']
            description = new_project_form.cleaned_data['description']
            slug = slugify(name)
            
            new_project = Project.objects.create(creator=request.user,
                                                 name=name,
                                                 description=description,
                                                 slug=slug,
                                                 is_active=True)
            
            edit_project_url = \
                get_item_url(new_project, 'epic.projects.views.edit_project')
            
            return HttpResponseRedirect(edit_project_url)
    
    return render_to_response(
        'projects/create_project.html',
        {'new_project_form': new_project_form,},
        context_instance=RequestContext(request))
示例#3
0
文件: views.py 项目: cns-iu/nwb
def edit_project(request, item_id, slug):
    project = get_object_or_404(Project, pk=item_id)
    user = request.user
    
    view_project_url = get_item_url(project, 'epic.projects.views.view_project')
    
    if not user_is_item_creator(user, project):
        return HttpResponseRedirect(view_project_url)
    
    if request.method != "POST":
        initial_edit_project_data = {'name': project.name, 'description': project.description,}
       
        if project.category is not None:
            initial_edit_project_data['category'] = project.category.id
        
        edit_form = ProjectForm(initial=initial_edit_project_data)
        initial_project_datasets = []

        for dataset in project.datasets.all():
            initial_project_datasets.append(
                {'dataset_url': dataset.get_absolute_url()})
            
        project_datasets = ProjectDataSetFormSet(
            prefix='project_datasets', initial=initial_project_datasets)
    else:
        edit_form = ProjectForm(request.POST)
        project_datasets = ProjectDataSetFormSet(
            request.POST, prefix='project_datasets')
            
        if edit_form.is_valid() and project_datasets.is_valid():
            project.name = edit_form.cleaned_data['name']
            project.category = edit_form.cleaned_data['category']
            project.description = edit_form.cleaned_data['description']
            project.save()

            for dataset in project.datasets.all():
                project.datasets.remove(dataset)

            for dataset_form in project_datasets.forms:
                if dataset_form.is_valid():
                    if 'dataset' in dataset_form.cleaned_data:
                        dataset = dataset_form.cleaned_data['dataset']
                        project.datasets.add(dataset)

            view_project_url = get_item_url(project, 'epic.projects.views.view_project')

            return HttpResponseRedirect(view_project_url)
    
    render_to_response_data = {
        'project': project,
        'edit_project_form': edit_form,
        'project_datasets': project_datasets,
        'datasets': project.datasets.all(),
    }
            
    return render_to_response(
        'projects/edit_project.html',
        render_to_response_data,
        context_instance=RequestContext(request))
示例#4
0
文件: views.py 项目: cns-iu/nwb
def edit_project(request, item_id, slug):
    project = get_object_or_404(Project, pk=item_id)
    user = request.user
    
    view_project_url = \
        get_item_url(project, 'epic.projects.views.view_project')
    
    if not user_is_item_creator(user, project):
        return HttpResponseRedirect(view_project_url)
    
    if request.method != "POST":
        initial_edit_project_data = {
            'name': project.name,
            'description': project.description,
        }
    
        edit_form = ProjectForm(initial=initial_edit_project_data)
        add_dataset_form = AddDatasetToProjectForm()
    else:
        edit_form = ProjectForm(request.POST)
        add_dataset_form = AddDatasetToProjectForm(request.POST)
            
        if edit_form.is_valid() and add_dataset_form.is_valid():
            _save_project(edit_form, project)
            add_dataset_form = \
                _update_datasets_for_project(request, add_dataset_form, project)
        
        if 'save_and_finish_editing' in request.POST:
            return HttpResponseRedirect(view_project_url)
    
    
    render_to_response_data = {
        'project': project,
        'edit_project_form': edit_form,
        'add_dataset_to_project_form': add_dataset_form,
        'datasets': project.datasets.all(),
    }
            
    return render_to_response('projects/edit_project.html', 
                              render_to_response_data,
                              context_instance=RequestContext(request))