def edit_walk(request, walk=None): """ Edit an existing walk. """ # Give our admins a Walk Form w/ added features if request.user.is_superuser: WalkForm = WalkFormAdmin else: from forms import WalkForm if not walk: return create_walk(request) try: walk = Walk.objects.get(id=walk) if request.user.id != walk.creator.id: if not request.user.is_superuser: error = "You do not have permission to edit this walk." return halt(request, error) except ObjectDoesNotExist: error = "That walk does not appear to exist" return error_404(request, error) if request.method == 'POST': form = WalkForm(request.POST, instance=walk) if form.is_valid(): walk = form.save() return HttpResponseRedirect(reverse(profile)) else: form = WalkForm(request.POST) template = 'edit_walk.html' ctxt = { 'form' : form, 'request' : request, 'page_name' : 'Edit Walk', 'media_url' : MEDIA_URL, } return render_to_response(template, ctxt) else: form = WalkForm(instance=walk) template = 'edit_walk.html' ctxt = { 'form' : form, 'request' : request, 'walk' : walk, 'page_name' : 'Edit Walk', 'media_url' : MEDIA_URL, } return render_to_response(template, ctxt)
def create_walk(request): """ View to create a new walk. """ # Give our admins a Walk Form w/ added features if request.user.is_superuser: WalkForm = WalkFormAdmin else: from forms import WalkForm if request.method == 'POST': form = WalkForm(request.POST) if form.is_valid(): walk = form.save(commit=False) walk.creator = request.user walk.save() form.save_m2m() return HttpResponseRedirect(reverse(profile)) else: form = WalkForm(request.POST) template = 'edit_walk.html' ctxt = { 'form' : form, 'request' : request, 'page_name' : 'Create Walk', 'media_url' : MEDIA_URL, } return render_to_response(template, ctxt) else: form = WalkForm(initial={ 'creator' : User.objects.get(username=request.user).id }) template = 'edit_walk.html' ctxt = { 'form' : form, 'request' : request, 'walk' : None, 'page_name' : 'Create Walk', 'media_url' : MEDIA_URL, } return render_to_response(template, ctxt)