def home(): form = FoodForm() # receive data if form.is_submitted(): # get food choices food_choices = backend_functions.get_recipes(form.food.data) # load next page choiceForm = ChoiceForm() # receive data if choiceForm.validate_on_submit(): # get recipe food_string = choiceForm.choice.data.replace(" ", "_") result = backend_functions.print_food(food_string) ### tryna add youtube stuff food_name = choiceForm.choice.data url = backend_functions.build_search_url(food_name) answer = backend_functions.get_result(url) table = backend_functions.addlist(answer) backend_functions.printStuff(table) result.append("\nYouTube Vidoes\n") for name, link, thumbnail in table: line = name + ": " + link + "\." result.append(line) ### # load results return render_template( "results.html", result=result) # maybe add a redirect to home? return render_template("choices.html", choiceForm=choiceForm, options=food_choices) return render_template("home.html", title="ZotYum", form=form)
def extra_word(): global data, extra_word, col_name, theme_name if request.method == 'GET': col_name, theme_name, data, extra_word = getData0() form = ChoiceForm() form.choose.choices = data.items() if form.validate_on_submit(): if data[form.choose.data] == extra_word: answer = 1 message = 'Вы правильно определили лишнее слово.' else: answer = 0 message = 'Вы ошиблись в выборе лишнего слова. Лишнее слово - ' + extra_word.encode( 'utf-8') putData(col_name, theme_name, 0, answer) return redirect(url_for('.interpreted', message=message)) message = request.args['message'] return render_template('extra_word.html', title='Research', form=form, message=message)
def create_choice(request, question_id): choice_form = ChoiceForm(request.POST or None) if choice_form.is_valid(): #and choice_form.is_valid(): choice = choice_form.save(commit=False) choice.question = get_object_or_404(Question, pk=question_id) choice.save() return redirect(reverse("polls:detail", kwargs={'pk':question_id})) return render(request, 'polls/create_choice.html', {'choice_form':choice_form, 'question_id':question_id})
def test_choice_form(self): """Testing whether the choice form is valid.""" form_data = { 'text': "A great python framework", 'point': 0, 'question': self.question.id } form = ChoiceForm(data=form_data) self.assertTrue(form.is_valid())
def edit_choice(request, question_id, choice_id): a_choice = get_object_or_404(Choice, pk=choice_id) choice_form = ChoiceForm (request.POST or None, instance = a_choice) if choice_form.is_valid(): choice = choice_form.save(commit=False) choice.question = get_object_or_404(Question, pk=question_id) choice.save() return redirect(reverse("polls:detail", kwargs={'pk':question_id,})) return render (request, "polls/edit_choice.html", {'choice_form': choice_form, 'choice_id':choice_id, 'question_id':question_id})
def registration(): form = ChoiceForm() if form.validate_on_submit(): choice = form.choice.data if choice == "student": return redirect(url_for('register_student')) else: return redirect(url_for('register_tutor')) return render_template("registration.html", pages=nav_bar_pages_list, form=form)
def create_choice(request, poll_id): poll = get_object_or_404(Poll, id=poll_id) choice = Choice(poll=poll) if request.method == 'POST': form = ChoiceForm(request.POST, instance=choice) if form.is_valid(): form.save() return HttpResponseRedirect(reverse('polls:administration:view', args=[poll.id])) else: form = ChoiceForm(initial={'poll':poll}) return render(request, 'polls/administration/create_choice.html', {'form': form, 'poll': poll, 'active_module': 'polls'})
def suggest_choice(request, poll_id): p = get_object_or_404(Poll, pk=poll_id) if request.method == 'POST': form = ChoiceForm(request.POST) # cand we loop validation (and creation) of the forms here if form.is_valid(): # TODO: default votes to 0 in model p.choice_set.create(choice_text=form.cleaned_data['choice_text'], votes=0) # can we loop saving of the forms here? return HttpResponseRedirect(reverse('poll_detail', args=(p.id,))) else: # cand we loop creation of the forms here form = ChoiceForm() return render_to_response('polls/suggest_choice.html', {'form': form, 'poll': p}, context_instance=RequestContext(request))
def analysis(request): form = ChoiceForm(request.POST or None) if request.POST: query_url = request.POST["query_url"] query = feedparser.parse(query_url + str('.rss')) entry_count = len(query['entries']) all_entries = query['entries'] choice = request.POST["example-select"] if request.user: new_query = Queries() new_query.url = query_url new_query.which_user = request.user new_query.save() c = {"request": request, "all_entries": all_entries, "choice": choice, "entry_count": entry_count} c.update(csrf(request)) return render_to_response("results/utopian-io-reddit-feed.html", c) c = {"request": request, "form": form} c.update(csrf(request)) return render_to_response("forms/make-choice.html", c)
def edit_choice(request, choice_id): choice = get_object_or_404(Choice, id=choice_id) if request.method == 'POST': form = ChoiceForm(request.POST, instance=choice) if form.is_valid(): form.save() # choice.choice = form.cleaned_data['choice'] # choice.votes = form.cleaned_data['votes'] # choice.save() return HttpResponseRedirect(reverse('polls:administration:view', args=[choice.poll.id])) else: form = ChoiceForm(model_to_dict(choice)) return render(request, 'polls/administration/edit_choice.html', {'form': form, 'choice': choice, 'active_module': 'polls'})
def add_choice(request, quiz_id, question_id): """Add the new choice that superuser created to the database.""" if (request.user.is_superuser): if request.method == 'POST': form = ChoiceForm(request.POST) if form.is_valid(): new_choice = form.save() new_choice.save() return HttpResponseRedirect( reverse('create_choice', kwargs={ 'question_id': question_id, 'quiz_id': quiz_id })) else: return render(request, 'invalidAttempt.html', {'message': 'Invalid input!'}) else: return render(request, 'invalidAttempt.html', {'message': 'You are not a super user!'})
def sign(request): form = ChoiceForm(request.POST) if form.is_valid(): if telephoneExpression.match(form.clean_data['phone']): fre = {'fname': form.clean_data['first_name'], 'lname': form.clean_data['last_name'], 'email': form.clean_data ['email'], 'phone': form.clean_data['phone'], 'pref_book': form.clean_data['pref_book'], 'osys': form.clean_data['osys']} post = Post(first_name=form.clean_data['first_name'], last_name=form.clean_data['last_name'], email=form.clean_data['email'], phone=form.clean_data['phone'], pref_book=form.clean_data['pref_book'], osys=form.clean_data['osys']) post.put() return render_to_response('thanks.html', fre ) else: return render_to_response('err.html') else: return render_to_response('err.html')
def extra_word(): global data, extra_word, col_name, theme_name if request.method == 'GET': col_name, theme_name, data, extra_word = getData0() form = ChoiceForm() form.choose.choices = data.items() if form.validate_on_submit(): if data[form.choose.data] == extra_word: answer = 1 message = 'Вы правильно определили лишнее слово.' else: answer = 0 message = 'Вы ошиблись в выборе лишнего слова. Лишнее слово - ' + extra_word.encode('utf-8') putData(col_name, theme_name, 0, answer) return redirect(url_for('.interpreted', message=message)) message = request.args['message'] return render_template('extra_word.html', title='Research', form=form, message=message)
def create_choice(request, poll_id): poll = get_object_or_404(Poll, id=poll_id) choice = Choice(poll=poll) if request.method == 'POST': form = ChoiceForm(request.POST, request.FILES, instance=choice) if form.is_valid(): form.save() return HttpResponseRedirect(reverse('polls:administration:view', args=[poll.id])) else: form = ChoiceForm(initial={'poll':poll}) return render(request, 'polls/administration/create_choice.html', {'form': form, 'poll': poll, 'active_module': 'polls'})
def create_choice(request, quiz_id, question_id): """For superuser to create new choices.""" if (request.user.is_superuser): question = get_object_or_404(Question, id=question_id) form = ChoiceForm(initial={'question': question}) choices = Choice.objects.order_by('text') return render( request, 'createChoice.html', { 'form': form, 'quizID': int(quiz_id), 'questionID': int(question_id), 'question_name': question, 'lists': choices }) else: return render(request, 'invalidAttempt.html', {'message': 'You are not a super user!'})
def edit_choice(request, choice_id): choice = get_object_or_404(Choice, id=choice_id) if request.method == 'POST': form = ChoiceForm(request.POST, request.FILES, instance=choice) if form.is_valid(): form.save() # choice.choice = form.cleaned_data['choice'] # choice.votes = form.cleaned_data['votes'] # choice.save() return HttpResponseRedirect(reverse('polls:administration:view', args=[choice.poll.id])) else: form = ChoiceForm(instance=choice) return render(request, 'polls/administration/edit_choice.html', {'form': form, 'choice': choice, 'active_module': 'polls'})
def edit_poll(request, slug): poll = get_object_or_404(Poll, slug=slug) if request.user != poll.user: raise PermissionDenied("You must own a poll in order to edit it.") choices = Choice.objects.get_choices_and_votes_for_poll(poll.id) poll_form = PollForm(request, instance=poll, prefix='poll') choice_form = ChoiceForm(request, poll, prefix='choice') if request.method == 'POST': if 'poll' in request.POST: poll_form = PollForm(request, request.POST, instance=poll, prefix='poll') if poll_form.is_valid(): p = poll_form.save() return HttpResponseRedirect(reverse('molnet-polls-edit-poll', kwargs={'slug': p.slug})) elif 'choice' in request.POST: choice_form = ChoiceForm(request, poll, request.POST, prefix='choice') if choice_form.is_valid(): choice, created = Choice.objects \ .get_or_create(poll=poll, choice=choice_form.cleaned_data['choice'], defaults={'user': request.user}) return HttpResponseRedirect(reverse('molnet-polls-edit-poll', kwargs={'slug': poll.slug})) elif 'delete-choice' in request.POST and 'choice-id' in request.POST: try: choice = Choice.objects.get(id=request.POST['choice-id'], poll=poll) \ .delete() return HttpResponseRedirect(reverse('molnet-polls-edit-poll', kwargs={'slug': poll.slug})) except Choice.DoesNotExist: raise Http404 elif 'delete' in request.POST: poll.delete() return HttpResponseRedirect(reverse('molnet-polls-startpage')) elif 'close' in request.POST: poll.status="CLOSED" poll.save() return HttpResponseRedirect(reverse('molnet-polls-edit-poll', kwargs={'slug': poll.slug})) elif 're-open' in request.POST: poll.status="PUBLISHED" poll.save() return HttpResponseRedirect(reverse('molnet-polls-edit-poll', kwargs={'slug': poll.slug})) elif 'unpublish' in request.POST: poll.status="DRAFT" poll.save() return HttpResponseRedirect(reverse('molnet-polls-edit-poll', kwargs={'slug': poll.slug})) elif 'publish' in request.POST: poll.status="PUBLISHED" poll.published_at = datetime.datetime.now() poll.save() return HttpResponseRedirect(reverse('molnet-polls-edit-poll', kwargs={'slug': poll.slug})) else: raise Http404 related_polls = None sidebar_polls = get_sidebar_polls(request.user) t = loader.get_template('polls-edit-poll.html') c = RequestContext(request, {'poll': poll, 'choices': choices, 'choice_form': choice_form, 'poll_form': poll_form, 'related_polls': related_polls, 'sidebar_polls': sidebar_polls}) return HttpResponse(t.render(c))