def edit_project(request, project_id): profile = get_object_or_404(UserProfile, user=request.user.pk) project = get_object_or_404(Project, pk=project_id) # If the user doesn't own this project, return a 403 response if not project.is_owned_by(profile): return HttpResponseForbidden("403 - Forbidden") if request.method == 'POST': form = ProjectForm(request.POST, instance=project) formset = ProjectItemFormset(request.POST, request.FILES, instance=project) if form.is_valid() and formset.is_valid(): form.save() formset.save() return redirect('profiles:project_detail', project_id=project_id) else: form = ProjectForm(instance=project) formset = ProjectItemFormset(instance=project) c = { "form": form, "formset": formset, "project": project, "profile": profile } c.update(csrf(request)) return render(request, "edit_project.jade", c)
def project_add(request): if request.method == 'POST': new_project_form = ProjectForm(request.POST) if new_project_form.is_valid(): new_project_form.save() return HttpResponseRedirect(reverse('user_page')) else: new_project_form = ProjectForm() context = {'new_project_form': new_project_form} return render(request, 'profiles/project.html', context)
def edit_project(request, pk): project = Project.objects.get(pk=pk) form = ProjectForm(request.POST or None, instance=project) if form.is_valid(): form.save() return redirect("projects") context = { 'user': request.user, 'user_roles': [role for role in request.user.roles], 'edit_project_form': form } return render(request, "project/edit_project.html", context)
def project_update(request, project_id): project = get_object_or_404(Project, pk=project_id) # if project.user != request.user: # return HttpResponseForbidden() project_update_form = ProjectForm(initial={ 'title': project.title, 'synopsis': project.synopsis }) if request.method == 'POST': project_update_form = ProjectForm(request.POST, instance=request.project) if project_update_form.is_valid(): project_details = project_update_form.save(commit=False) project_details.save( update_fields=['title', 'synopsis', 'is_added_to_map']) project_update_form.save_m2m() return redirect('/projects') else: project_update_form = ProjectForm() context = {'project_update_form': project_update_form} return render(request, 'profiles/project.html', context)
def project_update(request): if request.method == 'POST': project_update_form = ProjectForm(request.POST) if project_update_form.is_valid(): project_details = project_update_form.save(commit=False) project_details.save(update_fields=[ 'title', 'label', 'synopsis', 'material', 'is_added_to_map' ]) return redirect('project_update') else: project_update_form = ProjectForm() context = {'project_update_form': project_update_form} return render(request, 'profiles/my-projects.html', context)
def new_project(request): if request.method == 'POST': new_project_form = ProjectForm(request.POST) if new_project_form.is_valid(): project = new_project_form.save() project.users.add(request.user) return redirect("projects") else: new_project_form = ProjectForm() context = { 'user': request.user, 'user_roles': [role for role in request.user.roles], 'new_project_form': new_project_form } return render(request, "project/new_project.html", context)
def project_update(request, project_id): project = get_object_or_404(Project, pk=project_id) # if project.user != request.user: # return HttpResponseForbidden() project_update_form = ProjectForm(initial={'title':project.title, 'synopsis':project.synopsis}) if request.method == 'POST': project_update_form = ProjectForm(request.POST, instance=request.project) if project_update_form.is_valid(): project_details = project_update_form.save(commit=False) project_details.save(update_fields=['title', 'synopsis', 'is_added_to_map']) project_update_form.save_m2m() return redirect('/projects') else: project_update_form = ProjectForm() context = {'project_update_form': project_update_form} return render(request, 'profiles/project.html', context)
def update_project(request): submitted = False try: inst = Project.objects.get(researcher=request.user) except ObjectDoesNotExist: return HttpResponseRedirect(reverse('dashboard')) if request.method =='POST': project_form = ProjectForm(data=request.POST, instance=inst) if project_form.is_valid(): project = project_form.save(commit=False) project.save() submitted=True else: print project_form.errors else: project_form = ProjectForm(instance=inst) return render(request, 'update_project.html', {'project_form': project_form, 'submitted': submitted})
def create_project(request): if request.method == 'POST': profile = get_object_or_404(UserProfile, user=request.user.pk) form = ProjectForm(request.POST) if form.is_valid(): project = form.save(commit=False) project.profile = profile project.save() formset = ProjectItemFormset(request.POST, request.FILES, instance=project) if formset.is_valid(): formset.save() else: # TODO: Handle this more gracefully! raise Http404 return redirect('profiles:dashboard') else: form = ProjectForm() formset = ProjectItemFormset() c = {"form": form, "formset": formset} return render(request, "create_project.jade", c)