def edit_view(request, id): question = get_object_or_404(MPTTComment, id=id) if not question.user == request.user: return HttpResponseForbidden(u"You do not have permission to edit this question.") if request.method == "POST": post_data = request.POST.copy() form = EditQuestionForm(request, question, data=post_data) if form.is_valid(): form.save_changes() return HttpResponseRedirect(show_question_url(id, edited=True)) else: form = EditQuestionForm(request, question) context = { 'question': question, 'form': form, 'product': question.content_object, } return render_to_response('edit_question.html', context, context_instance=RequestContext(request))
def edit_question(request, question_id): """Edit a question.""" question = get_object_or_404(Question, pk=question_id) user = request.user if not user.has_perm('questions.change_question') and question.is_locked: raise PermissionDenied if request.method == 'GET': initial = question.metadata.copy() initial.update(title=question.title, content=question.content) form = EditQuestionForm(product=question.product, category=question.category, initial=initial) else: form = EditQuestionForm(data=request.POST, product=question.product, category=question.category) if form.is_valid(): question.title = form.cleaned_data['title'] question.content = form.cleaned_data['content'] question.updated_by = user question.save() # TODO: Factor all this stuff up from here and new_question into # the form, which should probably become a ModelForm. question.clear_mutable_metadata() question.add_metadata(**form.cleaned_metadata) return HttpResponseRedirect( reverse('questions.answers', kwargs={'question_id': question.id})) return jingo.render( request, 'questions/edit_question.html', { 'question': question, 'form': form, 'current_product': question.product, 'current_category': question.category })
def edit_question(request, question_id): """Edit a question.""" question = get_object_or_404(Question, pk=question_id) user = request.user # Locked questions can't be edited unless the user has the permission to. # (Question creators can't edit locked questions.) if not user.has_perm('questions.change_question') and question.is_locked: raise PermissionDenied if request.method == 'GET': initial = question.metadata.copy() initial.update(title=question.title, content=question.content) form = EditQuestionForm(product=question.product, category=question.category, initial=initial) else: form = EditQuestionForm(data=request.POST, product=question.product, category=question.category) if form.is_valid(): question.title = form.cleaned_data['title'] question.content = form.cleaned_data['content'] question.updated_by = user question.save() # TODO: Factor all this stuff up from here and new_question into # the form, which should probably become a ModelForm. question.clear_mutable_metadata() question.add_metadata(**form.cleaned_metadata) return HttpResponseRedirect(reverse('questions.answers', kwargs={'question_id': question.id})) return jingo.render(request, 'questions/edit_question.html', {'question': question, 'form': form, 'current_product': question.product, 'current_category': question.category})
def edit_question(request, question_id): """Edit a question.""" question = get_object_or_404(Question, pk=question_id) user = request.user if not user.has_perm("questions.change_question") and question.is_locked: raise PermissionDenied if request.method == "GET": initial = question.metadata.copy() initial.update(title=question.title, content=question.content) form = EditQuestionForm(product=question.product, category=question.category, initial=initial) else: form = EditQuestionForm(data=request.POST, product=question.product, category=question.category) if form.is_valid(): question.title = form.cleaned_data["title"] question.content = form.cleaned_data["content"] question.updated_by = user question.save() # TODO: Factor all this stuff up from here and new_question into # the form, which should probably become a ModelForm. question.clear_mutable_metadata() question.add_metadata(**form.cleaned_metadata) return HttpResponseRedirect(reverse("questions.answers", kwargs={"question_id": question.id})) return jingo.render( request, "questions/edit_question.html", { "question": question, "form": form, "current_product": question.product, "current_category": question.category, }, )
def edit_question_view(request, question_slug=None): # Check if the request was ratelimited was_limited = getattr(request, "limited", False) if was_limited: return plea_for_no_automated_requests(request) if request.method == "POST": # Create the form object question = None if Question.objects.filter(question_slug=question_slug).exists( ): # The question already exists # Then update the question by filling in the form question = Question.objects.get(question_slug=question_slug) form = EditQuestionForm( request.POST, instance=question) # Pass the data from the POST request # Get the reset input url reset_input_url = "http://" + get_current_site( request).domain + question.question_input_reset_link() # Form the regex for the deletion command regex_for_deletion = "".join( [f"[{char.lower()}{char.upper()}]" for char in question.title]) else: # This is a new question; just fill in the form using the POST data form = EditQuestionForm(request.POST) reset_input_url = "" regex_for_deletion = "" # Check if the form is valid if form.is_valid(): # Save the edited question to the database form.save() # Display a success alert if question: messages.add_message( request, messages.SUCCESS, f"Successfully Modified '{question.title}'.") else: messages.add_message( request, messages.SUCCESS, f"Successfully Created '{form.cleaned_data['title']}'.") # Redirect back to the edit questions view return redirect("questions:manage-questions") else: # See if the question already exists if Question.objects.filter(question_slug=question_slug).exists(): # Fill in the `EditQuestionForm` with the information of the question question = Question.objects.get(question_slug=question_slug) form = EditQuestionForm(instance=question) # Get the reset input url reset_input_url = "http://" + get_current_site( request).domain + question.question_input_reset_link() # Form the regex for the deletion command regex_for_deletion = "".join( [f"[{char.lower()}{char.upper()}]" for char in question.title]) else: # The user wants to create a new question form = EditQuestionForm() reset_input_url = "" regex_for_deletion = "" return render( request, "questions/edit_question.html", { "form": form, "reset_input_url": reset_input_url, "regex_for_deletion": regex_for_deletion })