def submit_quote(request): title = "Submit a quote | def programming" description = "Use this form to submit a quote. Please send only quotes about programming, coding, software industry." sent = False form = QuoteForm(request.POST or None) if form.is_valid(): form.save() messages.success(request, "Your quote was successfully submitted, thank you! :-)") return render_to_response('quotes/submit_quote.html', locals(), context_instance=RequestContext(request))
def test_invalid_form(self): user = User(username='******', password='******') user.save() profile = ProfileUser(user=user, favorite_book=None, location=None, profile_picture=None) profile.save() q = Quote.objects.create(user=profile, text='Foo', author='', image=None) data = {'user': q.user, 'text': q.text, 'author': q.author, 'image': q.image} form = QuoteForm(data=data) self.assertFalse(form.is_valid())
def submit_quote(request): title = "Submit a quote | def programming" description = "Use this form to submit a quote. Please send only quotes about programming, coding, software industry." sent = False form = QuoteForm(request.POST or None) if form.is_valid(): form.save() messages.success( request, "Your quote was successfully submitted, thank you! :-)") return render_to_response('quotes/submit_quote.html', locals(), context_instance=RequestContext(request))
def test_valid_form(self): """Test form with good and bad inputs""" # give optional fields = valid data = { 'quote': 'Good Artists Copy; Great Artists Steal', 'author': 'Pablo Picasso', 'source': 'https://quoteinvestigator.com/2013/03/06/artists-steal/', # noqa E501 'cover': 'http://media.bsix12.com/2011/05/picasso-great-artists-steal.jpg' } # noqa E501 form = QuoteForm(data=data) self.assertTrue(form.is_valid()) # go over author max len = invalid data = { 'quote': 'Good Artists Copy; Great Artists Steal', 'author': 'Pablo Picasso' * 10 } form = QuoteForm(data=data) self.assertFalse(form.is_valid())
def quote_req(request): submitted = False if request.method == 'POST': form = QuoteForm(request.POST, request.FILES) if form.is_valid(): form.save() return HttpResponseRedirect('/quote/?submitted=True') else: form = QuoteForm() if 'submitted' in request.GET: submitted = True return render(request, 'quotes/quote.html', { 'form': form, 'page_list': Page.objects.all(), 'submitted': submitted })
def submit_quote(request): title = "Submit a quote | def programming" description = "Use this form to submit a quote. Please send only quotes about programming, coding, software industry." sent = False if request.method == 'POST': form = QuoteForm(request.POST) if form.is_valid(): form.save(request.POST); sent = True else: form = QuoteForm() return render_to_response('quotes/submit_quote.html', locals(), context_instance=RequestContext(request))
def quote_create(request: HttpRequest) -> HttpResponse: """Create a new quote.""" # noqa: DAR201, DAR101 form = QuoteForm(request.POST or None) if form.is_valid(): # Create form instance, like it was saved, but without committing it # to the database. form = form.save(commit=False) form.user = request.user form.save() messages.success(request, "Created quote.") return redirect("quotes:quotes_list") return render( request, "quotes/quote_form.html", {"form": form}, )
def quote_req(request): submitted = False if request.method == "POST": form = QuoteForm(request.POST, request.FILES) if form.is_valid(): quote = form.save(commit=False) try: quote.username = request.user except Exception: pass quote.save() return HttpResponseRedirect("/quote/?submitted=True") else: form = QuoteForm() if submitted in request.GET: submitted = True context = { "form": form, "submitted": submitted, "page_list": Page.objects.all() } return render(request, "quotes/quote.html", context=context)
def quote_update(request: HttpRequest, pk: int) -> HttpResponse: """Update a quote.""" # noqa: DAR201, DAR101 quote = get_object_or_404( Quote, pk=pk, user=request.user, # Only owner can update ) form = QuoteForm(request.POST or None, instance=quote) if form.is_valid(): form.save() messages.success(request, "Quote updated.") return redirect("quotes:quotes_list") return render( request, "quotes/quote_form.html", { "quote": quote, "form": form, }, )