Ejemplo n.º 1
0
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})
Ejemplo n.º 2
0
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})
Ejemplo n.º 3
0
def clone_deck(request):
    '''Creates a copy of a user's deck for another user to own.'''
    deckid = request.GET.get('did')
    userID = request.user.id
    user = User.objects.get(pk=userID)
    deck = Deck.objects.get(pk=deckid)
    # Make sure user isn't trying to clone their own deck
    if deck.author != user:
        newDeck, created = Deck.objects.get_or_create(author=user,
                                                      title=deck.title)
        # Check to make sure that user doesn't already have a deck the
        # same title
        if not created:
            return HttpResponse('Error: You already own a deck with '
                                'this title')
        # Copy the deck
        newDeck.slug = deck.slug
        newDeck.description = deck.description
        newDeck.published = False
        newDeck.save()
        # Copy the tags
        tags = deck.tags.names()
        for tag in tags:
            newDeck.tags.add(tag)
        newDeck.save()
        # Copy the cards
        for card in deck.card_set.all():
            newCard = Card(front=card.front,
                           back=card.back,
                           deck=newDeck,
                           score=0)
            newCard.save()
        # Redirect user to their newly cloned deck
        url = reverse('view_deck') + '?did=' + str(newDeck.id)
        return HttpResponseRedirect(url)
    else:
        return HttpResponse('Error: You already own this deck')
Ejemplo n.º 4
0
def clone_deck(request):
    '''Creates a copy of a user's deck for another user to own.'''
    deckid = request.GET.get('did')
    userID = request.user.id
    user = User.objects.get(pk=userID)
    deck = Deck.objects.get(pk=deckid)
    # Make sure user isn't trying to clone their own deck
    if deck.author != user:
        newDeck, created = Deck.objects.get_or_create(author=user,
                                                      title=deck.title)
        # Check to make sure that user doesn't already have a deck the
        # same title
        if not created:
            return HttpResponse('Error: You already own a deck with '
                                'this title')
        # Copy the deck
        newDeck.slug = deck.slug
        newDeck.description = deck.description
        newDeck.published = False
        newDeck.save()
        # Copy the tags
        tags = deck.tags.names()
        for tag in tags:
            newDeck.tags.add(tag)
        newDeck.save()
        # Copy the cards
        for card in deck.card_set.all():
            newCard = Card(front=card.front,
                           back=card.back,
                           deck=newDeck,
                           score=0)
            newCard.save()
        # Redirect user to their newly cloned deck
        url = reverse('view_deck') + '?did=' + str(newDeck.id)
        return HttpResponseRedirect(url)
    else:
        return HttpResponse('Error: You already own this deck')