def test_deckForm(self): # save without tags form = deckForm({'title': 'Test Deck', 'description': 'The description', }) self.assertTrue(form.is_valid()) # save with tags form = deckForm({'title': 'Test Deck 2', 'description': 'Description 2', 'tags': 'testing'}) self.assertTrue(form.is_valid())
def test_deckForm(self): # save without tags form = deckForm({ 'title': 'Test Deck', 'description': 'The description', }) self.assertTrue(form.is_valid()) # save with tags form = deckForm({ 'title': 'Test Deck 2', 'description': 'Description 2', 'tags': 'testing' }) self.assertTrue(form.is_valid())
def create_deck(request): ''' GET: Present the user with the form to create a new deck. POST: Process user input and create the new deck. ''' if request.method == 'POST': form = deckForm(request.POST) if form.is_valid(): userID = request.user.id user = User.objects.get(pk=userID) title = form.cleaned_data['title'] desc = form.cleaned_data['description'] deck = Deck(author=user, title=title, description=desc) deck.save() for tag in form.cleaned_data['tags']: deck.tags.add(tag) deck.save() return HttpResponseRedirect(reverse('decks')) else: return render(request, 'notecards/create_deck.html', {'form': form}) form = deckForm() return render(request, 'notecards/create_deck.html', {'form': form})
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 edit_deck(request, deckid): ''' Replaces deck information with new information submitted by user. ''' userID = request.user.id user = User.objects.get(pk=userID) # Make sure user owns deck deck = get_object_or_404(Deck, author=user, pk=deckid) if user == deck.author: form = deckForm(request.POST) if form.is_valid(): deck.title = form.cleaned_data['title'] deck.description = form.cleaned_data['description'] deck.tags.clear() for tag in form.cleaned_data['tags']: deck.tags.add(tag) deck.save() queryParam = '?did={0}'.format(deckid) return HttpResponseRedirect(reverse('view_deck') + queryParam)
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)