def list_details(request, list_id): if not request.user.is_staff: return HttpResponse("Unauthorized") list = QuestionList.objects.get(pk=list_id) questions_list = QuestionListOrder.objects.filter(question_list=list).order_by('order') edit_form = QuestionListForm(instance=list) add_question_form = QuestionListAddForm(list) invalid_count = False if request.method == "POST": if request.POST.has_key('edit_list'): edit_form = QuestionListForm(request.POST, instance=list) if edit_form.is_valid(): list = edit_form.save() return HttpResponseRedirect(reverse('quanda_list_details', args=[list.id])) elif request.POST.has_key('reorder'): for value in request.POST.values(): if value != '0' and request.POST.values().count(value) > 1: invalid_count = True break if not invalid_count: for k, v in request.POST.items(): if k != 'reorder': list_item = questions_list.get(question__id=k) if v == '0': list_item.delete() else: list_item.order = v list_item.save() return HttpResponseRedirect(reverse('quanda_list_details', args=[list.id])) elif request.POST.has_key('add_question'): add_question_form = QuestionListAddForm(list, request.POST) if add_question_form.is_valid(): question = get_object_or_404(Question, pk=add_question_form.cleaned_data['question']) list_questions = QuestionListOrder.objects.filter(question_list=list) if not list_questions: new_item = QuestionListOrder(question_list=list, question=question, order=1) new_item.save() return HttpResponseRedirect(reverse('quanda_list_details', args=[list.id])) else: order_number = list_questions.order_by('-order')[0].order + 1 new_item = QuestionListOrder(question_list=list, question=question, order=order_number) new_item.save() return HttpResponseRedirect(reverse('quanda_list_details', args=[list.id])) return render_to_response("quanda/list_details.html", { 'list': list, 'edit_form': edit_form, 'add_question_form': add_question_form, 'questions_list': questions_list, 'invalid_count': invalid_count, }, context_instance=RequestContext(request))
def lists(request): if not request.user.is_staff: return HttpResponse("Unauthorized") if request.method == 'POST': form = QuestionListForm(request.POST) if form.is_valid(): list = form.save() return HttpResponseRedirect(reverse('quanda_lists')) else: form = QuestionListForm() return render_to_response("quanda/lists.html", { 'form': form, 'lists': QuestionList.objects.all(), }, context_instance=RequestContext(request))
def list_details(request, list_id): if not request.user.is_staff: return HttpResponse("Unauthorized") list = QuestionList.objects.get(pk=list_id) questions_list = QuestionListOrder.objects.filter( question_list=list).order_by('order') edit_form = QuestionListForm(instance=list) add_question_form = QuestionListAddForm(list) invalid_count = False if request.method == "POST": if request.POST.has_key('edit_list'): edit_form = QuestionListForm(request.POST, instance=list) if edit_form.is_valid(): list = edit_form.save() return HttpResponseRedirect( reverse('quanda_list_details', args=[list.id])) elif request.POST.has_key('reorder'): for value in request.POST.values(): if value != '0' and request.POST.values().count(value) > 1: invalid_count = True break if not invalid_count: for k, v in request.POST.items(): if k != 'reorder': list_item = questions_list.get(question__id=k) if v == '0': list_item.delete() else: list_item.order = v list_item.save() return HttpResponseRedirect( reverse('quanda_list_details', args=[list.id])) elif request.POST.has_key('add_question'): add_question_form = QuestionListAddForm(list, request.POST) if add_question_form.is_valid(): question = get_object_or_404( Question, pk=add_question_form.cleaned_data['question']) list_questions = QuestionListOrder.objects.filter( question_list=list) if not list_questions: new_item = QuestionListOrder(question_list=list, question=question, order=1) new_item.save() return HttpResponseRedirect( reverse('quanda_list_details', args=[list.id])) else: order_number = list_questions.order_by( '-order')[0].order + 1 new_item = QuestionListOrder(question_list=list, question=question, order=order_number) new_item.save() return HttpResponseRedirect( reverse('quanda_list_details', args=[list.id])) return render_to_response("quanda/list_details.html", { 'list': list, 'edit_form': edit_form, 'add_question_form': add_question_form, 'questions_list': questions_list, 'invalid_count': invalid_count, }, context_instance=RequestContext(request))