Пример #1
0
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
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))