def deck_create(request, post_save_redirect=None, cancel_redirect='add_decks'): cancel_redirect = reverse(cancel_redirect) if post_save_redirect is not None: post_save_redirect = reverse(post_save_redirect) if request.method == 'POST': deck_form = DeckForm(request.POST) if deck_form.is_valid(): new_deck = deck_form.save(commit=False) new_deck.owner = request.user new_deck.save() if 'tags' in deck_form.cleaned_data: new_deck.tags = deck_form.cleaned_data['tags'] if post_save_redirect is None: post_save_redirect = new_deck.get_absolute_url() else: post_save_redirect = reverse(post_save_redirect) return HttpResponse(json_encode({'success': True, 'postRedirect': post_save_redirect}), mimetype='text/javascript') else: #FIXME post_redirect for failure? handle in ajax? return HttpResponse(json_encode({'success': False}), mimetype='text/javascript') else: deck_form = DeckForm() return render_to_response('flashcards/deck_form.html', {'form': deck_form, 'post_save_redirect': post_save_redirect, 'cancel_redirect': cancel_redirect, } , context_instance=RequestContext(request)) #todo:post/pre redirs
def deck_creator(request): if request.method == 'GET': form = DeckForm() elif request.method == 'POST': form = DeckForm(request.POST) if form.is_valid(): deck = form.save(commit=False) deck.owner = request.user deck.save() return redirect('deck-detail', pk=deck.pk, slug=deck.slug) return render(request, 'flashcards/deck_form.html', {'form': form})
def deck_update(request, deck_id): deck = get_object_or_404(Deck, pk=deck_id) if deck.owner_id != request.user.id: #and not request.User.is_staff(): raise forms.ValidationError('You do not have permission to access this flashcard deck.') if request.method == 'POST': deck_form = DeckForm(request.POST, instance=deck) if deck_form.is_valid(): deck = deck_form.save() return HttpResponse(json_encode({'success':True}), mimetype='text/javascript') else: deck_form = DeckForm(instance=deck) return render_to_response('flashcards/deck_form.html', {'form': deck_form, 'deck': deck, 'container_id': 'deckDialog', 'post_save_redirect': '/flashcards/decks'}) #todo:post/pre redirs
def deck_update(request, deck_id): deck = get_object_or_404(Deck, pk=deck_id) if deck.owner_id != request.user.id: #and not request.User.is_staff(): raise forms.ValidationError( 'You do not have permission to access this flashcard deck.') if request.method == 'POST': deck_form = DeckForm(request.POST, instance=deck) if deck_form.is_valid(): deck = deck_form.save() return HttpResponse(json_encode({'success': True}), mimetype='text/javascript') else: deck_form = DeckForm(instance=deck) return render_to_response( 'flashcards/deck_form.html', { 'form': deck_form, 'deck': deck, 'container_id': 'deckDialog', 'post_save_redirect': '/flashcards/decks' }) #todo:post/pre redirs
def deck_create(request, post_save_redirect=None, cancel_redirect='add_decks'): cancel_redirect = reverse(cancel_redirect) if post_save_redirect is not None: post_save_redirect = reverse(post_save_redirect) if request.method == 'POST': deck_form = DeckForm(request.POST) if deck_form.is_valid(): new_deck = deck_form.save(commit=False) new_deck.owner = request.user new_deck.save() if 'tags' in deck_form.cleaned_data: new_deck.tags = deck_form.cleaned_data['tags'] if post_save_redirect is None: post_save_redirect = new_deck.get_absolute_url() else: post_save_redirect = reverse(post_save_redirect) return HttpResponse(json_encode({ 'success': True, 'postRedirect': post_save_redirect }), mimetype='text/javascript') else: #FIXME post_redirect for failure? handle in ajax? return HttpResponse(json_encode({'success': False}), mimetype='text/javascript') else: deck_form = DeckForm() return render_to_response( 'flashcards/deck_form.html', { 'form': deck_form, 'post_save_redirect': post_save_redirect, 'cancel_redirect': cancel_redirect, }, context_instance=RequestContext(request)) #todo:post/pre redirs