def edit_card(request, cardid): ''' POST: Replaces a card's information with new information from the user. Returns new card info in HTML form to be appended into the page. DELETE: Deletes the card from the deck. ''' card = Card.objects.get(pk=cardid) userID = request.user.id user = User.objects.get(pk=userID) if card.deck.author == user: if request.method == 'POST': # Replace the old card info with new info front = request.POST.get('editfront') back = request.POST.get('editback') form = cardForm({'front': front, 'back': back}) if form.is_valid(): card.front = form.cleaned_data['front'] card.back = form.cleaned_data['back'] card.save() content = '''<option class=skill{0} value="{1}"> {2} -- {3} </option>'''.format(card.score, card.id, card.front, card.back) return HttpResponse(status=200, content=content, content_type='text/html') elif request.method == 'DELETE': card.delete() return HttpResponse(status=200)
def view_deck(request): ''' Returns a page with all the information about a deck ''' deckid = request.GET.get('did') deck = Deck.objects.get(pk=deckid) # deck form is pre-filled with deck info deckform = deckForm(instance=deck) # card form is empty since it's used to add new cards cardform = cardForm() cards = deck.card_set.all() context_dict = {'deckform': deckform, 'cardform': cardform, 'cards': cards, 'deck': deck} return render(request, 'notecards/view_deck.html', context_dict)
def view_deck(request): ''' Returns a page with all the information about a deck ''' deckid = request.GET.get('did') deck = Deck.objects.get(pk=deckid) # deck form is pre-filled with deck info deckform = deckForm(instance=deck) # card form is empty since it's used to add new cards cardform = cardForm() cards = deck.card_set.all() context_dict = { 'deckform': deckform, 'cardform': cardform, 'cards': cards, 'deck': deck } return render(request, 'notecards/view_deck.html', context_dict)
def create_card(request, deckid): ''' Accepts an AJAX POST request to create a new card. Returns card information in HTML to be inserted into the page. ''' if request.method == 'POST': userID = request.user.id user = User.objects.get(pk=userID) # redundant filter to make sure user owns the deck deck = get_object_or_404(Deck, pk=deckid, author=user) form = cardForm(request.POST) if form.is_valid(): front = form.cleaned_data['front'] back = form.cleaned_data['back'] card = Card(front=front, back=back, deck=deck) card.save() # Card goes into a select box so we use the <option> tag content = '<option>{0} -- {1}</option>'.format(front, back) return HttpResponse(status=201, content=content, content_type='text/html') else: return render(request, 'notecards/build.html', {'form': form})