def create(request): if request.method == "POST": form = FlashCardForm(request.POST) promptForm = PromptForm(request.POST) if form.is_valid() and promptForm.is_valid(): new_flashcard = form.save() new_flashcard.user = request.user new_flashcard.vote = 0 new_flashcard.save() for i in xrange(settings.QuestNumber): if request.POST['Prompt_%d' % (i+1)] != '': prompt = request.POST['Prompt_%d' % (i+1)] answer = request.POST['Answer_%d' % (i+1)] quest = Question.objects.create(prompt=prompt,answer=answer,flashcardID = new_flashcard) quest.save() return HttpResponseRedirect('/flashcard/' + str(new_flashcard.id)) else: form = FlashCardForm() promptForm = PromptForm() return render_to_response('flashcard/create.html', { 'form': form, 'promptForm': promptForm, 'user':request.user, 'value': 1, },context_instance=RequestContext(request))
def edit(request, flashcard_id): if request.method == "POST": form = FlashCardForm(request.POST) promptForm = PromptForm(request.POST) if form.is_valid() and promptForm.is_valid(): new_flashcard = form.save() card = get_object_or_404(FlashCard, pk = flashcard_id) quests = Question.objects.filter(flashcardID__exact = card) card.copy(new_flashcard) card.save() # delete all old questions for q in quests: q.delete() # add new questions for i in xrange(settings.QuestNumber): if request.POST['Prompt_%d' % (i+1)] != '': prompt = request.POST['Prompt_%d' % (i+1)] answer = request.POST['Answer_%d' % (i+1)] quest = Question.objects.create(prompt=prompt,answer=answer,flashcardID = card) quest.save() return HttpResponseRedirect('/flashcard/' + str(flashcard_id)) else: fc = get_object_or_404(FlashCard, pk = flashcard_id) if fc.user.username != request.user.username: return HttpResponseRedirect('/flashcard/') list = dict([]) list['title'] = fc.title list['description']=fc.description list['grade']=fc.grade list['subject']=fc.subject quests = Question.objects.filter(flashcardID__exact = fc) i = 0 pList = dict([]) for q in quests.all(): i += 1 pList['Prompt_%s' % i] = q.prompt pList['Answer_%s' % i] = q.answer form = FlashCardForm(list) promptForm = PromptForm(pList) return render_to_response('flashcard/create.html', { 'form': form, 'promptForm': promptForm, 'user':request.user, 'value': 2, },context_instance=RequestContext(request))