def createquestion(request): p=Poll() p.question=request.GET['question'] #p.pub_date=request.getparameter('pub_date') p.pub_date=timezone.now() p.save() return index(request)
def test_creating_some_choices_for_a_poll(self): # start by creating a new Poll object poll = Poll() poll.question = "What's up?" poll.pub_date = timezone.now() poll.save() # now create a Choice object choice = Choice() # link it with our Poll choice.poll = poll # give it some text choice.choice = "doin' fine..." # and let's say it's had some votes choice.votes = 3 # save it choice.save() # try retrieving it from the database, using the poll object's reverse # lookup poll_choices = poll.choice_set.all() self.assertEquals(poll_choices.count(), 1) # finally, check its attributes have been saved choice_from_db = poll_choices[0] self.assertEquals(choice_from_db, choice) self.assertEquals(choice_from_db.choice, "doin' fine...") self.assertEquals(choice_from_db.votes, 3)
def test_creating_some_choices_for_a_poll(self): # Start by creating a new Poll object poll = Poll() poll.question = "What's up?" poll.pub_date = timezone.now() poll.save() # Now we create a Choice object choice = Choice() # Link it to our poll choice.poll = poll choice.choice = 'Doing fine...' choice.votes = 3 choice.save() # Try retrieving from the database using the poll object's reverse lookup poll_choices = poll.choice_set.all() self.assertEquals(poll_choices.count(), 1) # Finally, check that its attributes have been saved choice_from_db = poll_choices[0] self.assertEquals(choice_from_db, choice) self.assertEquals(choice_from_db.choice, 'Doing fine...') self.assertEquals(choice_from_db.votes, 3)
def test_create_some_choices_for_a_poll(self): # Create new poll object poll = Poll() poll.question = "What's up?" poll.pub_date = timezone.now() poll.save() # Create Choice object choice = Choice() choice.poll = poll choice.choice = "doin' fine..." # Give it faux votes choice.votes = 3 choice.save() # Try to retrieve from DB using poll's reverse lookup. poll_choices = poll.choice_set.all() self.assertEquals(poll_choices.count(), 1) # Finally, check attrbs have been saved choice_from_db = poll_choices[0] self.assertEqual(choice_from_db, choice) self.assertEqual(choice_from_db.choice, "doin' fine...") self.assertEqual(choice_from_db.votes, 3)
def test_was_published_today(self): poll = Poll(question="Django is for the internets", pub_date=datetime.now()) poll.save() self.assertTrue(poll.was_published_today()) poll.pub_date = datetime.now() - timedelta(days=3) poll.save() self.assertFalse(poll.was_published_today())
def test_creating_a_new_poll_and_saving_it_to_database(self): poll = Poll() poll.question = self.question poll.pub_date = self.pub_date poll.save() all_polls_in_database = Poll.objects.all() self.assertEquals(len(all_polls_in_database), 1) only_poll_in_db = all_polls_in_database[0] self.assertEquals(only_poll_in_db, poll) self.assertEquals(only_poll_in_db.question, self.question) self.assertEquals(only_poll_in_db.pub_date, self.pub_date)
def test_creating_a_new_poll_and_saving_it_to_the_database(self): poll = Poll() poll.question = "What's up?" poll.pub_date = timezone.now() poll.save() all_polls_in_database = Poll.objects.all() self.assertEquals(len(all_polls_in_database), 1) only_poll_in_database = all_polls_in_database[0] self.assertEquals(only_poll_in_database, poll) self.assertEquals(only_poll_in_database.question, "What's up?") self.assertEquals(only_poll_in_database.pub_date, poll.pub_date)
def test_creating_a_new_poll_and_saving_it_to_the_database(self): # start by creating a new Poll object with its "question" set poll = Poll() poll.question = "What's up?" poll.pub_date = timezone.now() # check we can save it to the database poll.save() # now check we can find it in the database again all_polls_in_database = Poll.objects.all() self.assertEquals(len(all_polls_in_database), 1) only_poll_in_database = all_polls_in_database[0] self.assertEquals(only_poll_in_database, poll) # and check that it's saved its two attributes: question and pub_date self.assertEquals(only_poll_in_database.question, "What's up?") self.assertEquals(only_poll_in_database.pub_date, poll.pub_date)
def test_creating_some_choices_for_a_poll(self): poll = Poll() poll.question = "what's up?" poll.pub_date = timezone.now() poll.save() choice = Choice() choice.poll = poll choice.choice = 'alright then ...' choice.votes = 3 choice.save() # check the database poll_choices = poll.choice_set.all() self.assertEquals(poll_choices.count(), 1) self.assertEquals(poll_choices[0].choice, choice.choice)
def test_creating_a_new_poll_and_saving_it_to_the_database(self): poll = Poll() poll.question = "what's up?" poll.pub_date = timezone.now() # check we can save ite to the database poll.save() # now create a Choice object choice = Choice() # link it with our Poll choice.poll = poll # give it some text choice.choice = "doin' fine..." # and let's say it's had some votes choice.votes = 3 # save it choice.save() # try retrieving it from the database, using the poll # object's reverse lookup poll_choices = poll.choice_set.all() self.assertEquals(poll_choices.count(), 1) # finally, check its attributes have been saved choice_from_db = poll_choices[0] self.assertEquals(choice_from_db, choice) self.assertEquals(choice_from_db.choice, "doin' fine...") self.assertEquals(choice_from_db.votes, 3) # now check we can find it in the database again all_polls_in_database = Poll.objects.all() self.assertEquals(len(all_polls_in_database), 1) only_poll_in_database = all_polls_in_database[0] self.assertEquals(only_poll_in_database, poll) # and check that it's saved its two attributes: question and # pub_date self.assertEquals(only_poll_in_database.question, "what's up?") self.assertEquals(only_poll_in_database.pub_date, poll.pub_date)
def test_creating_a_new_poll_and_saving_it_to_the_database(self): # start by creating a new Poll object with its "Question" set poll = Poll() poll.question = "What's up?" poll.pub_date = timezone.now() # check we can save it to the database poll.save() # now check we can find it in the database again all_polls = Poll.objects.all() self.assertEquals(len(all_polls),1) only_poll = all_polls[0] self.assertEquals(only_poll, poll) # check the attributes are set correctly self.assertEquals(only_poll.question, "What's up?") self.assertEquals(only_poll.pub_date, poll.pub_date)
def test_creating_some_choices_for_a_poll(self): poll = Poll() poll.question = "What's up?" poll.pub_date = timezone.now() poll.save() choice = Choice() choice.poll = poll choice.choice = "doin' fine.." choice.votes = 3 choice.save() poll_choices = poll.choice_set.all() self.assertEquals(poll_choices.count(), 1) choice_from_db = poll_choices[0] self.assertEquals(choice_from_db, choice) self.assertEquals(choice_from_db.choice, "doin' fine..") self.assertEquals(choice_from_db.votes, 3)
def create_poll(request): current_user = get_current_user(request=request) if request.method == "POST": #Create poll new_poll = Poll() new_poll.pub_date = datetime.datetime.now() new_poll.question = request.POST["question"] new_poll.master = current_user new_poll.save() #Create answers answers = [request.POST["answer1"], request.POST["answer2"], request.POST["answer3"], request.POST["answer4"], request.POST["answer5"], request.POST["answer6"]] for answer in answers: if answer != "": new_choice = Choice() new_choice.poll = new_poll new_choice.choice_text = answer new_choice.save() return render(request, "add_poll.html", {"current_user": current_user})
def test_creating_a_new_poll_and_saving_it_to_the_database(self): # Create a new poll object wiht its question set poll = Poll() poll.question = "What's up?" poll.pub_date = timezone.now() # Check if we can save it to the db poll.save() # Now check if we can find it in the db again all_polls_in_db = Poll.objects.all() self.assertEqual(len(all_polls_in_db), 1) only_poll_in_db = all_polls_in_db[0] self.assertEqual(only_poll_in_db, poll) # And check that it has saved its two attrbs, question and pub_date self.assertEqual(only_poll_in_db.question, poll.question) self.assertEqual(only_poll_in_db.pub_date, poll.pub_date)
def create( self, request, id=None ): #called for a POST request #When making a POST request, data has to be sent with the request, same goes with PUT #eg - In jQuery ajax {"a":null} works but {} doesn't #print "GOT CREATE (POST) request on /api/poll/" if not id: #id should always be None here try: question = request.POST.get('question') pub_date = utils.dateFromString(request.POST.get('pubDate')) if question and pub_date: poll = Poll() poll.question = question poll.pub_date = pub_date poll.polling_ended = False poll.save() return poll except Exception, e: resp = rc.BAD_REQUEST resp.write("Error creating 'Poll' object") return resp
def save_poll_3(request): result = {} question = request.POST["question"] pub_date = request.POST["pub_date"] try: poll = Poll() poll.question = question poll.pub_date = pub_date poll.save() result["status"] = "SUCCESS" result["msg_status"] = "Su encuesta se ha creado satisfactoriamente" result['name'] = "Leandro" result['lastname'] = "Agudelo" except Exception as error: result["status"] = "FAILURE" result["msg_status"] = "Error en la creacion" return render_to_response('home3.html', result, context_instance=RequestContext(request))
def save_poll_3(request): result = {} question = request.POST["question"] pub_date = request.POST["pub_date"] try: poll = Poll() poll.question = question poll.pub_date = pub_date poll.save() result["status"] = "SUCCESS" result["msg_status"] = "Su encuesta se ha creado satisfactoriamente" result['name'] = "Leandro" result['lastname'] = "Agudelo" except Exception as error: result["status"] = "FAILURE" result["msg_status"] = "Error en la creacion" return render_to_response('home3.html',result ,context_instance=RequestContext(request))
def submit_poll(request): question = request.POST['question'] number = int(request.POST['num_choices']) end_date = request.POST['end_date'] errors = [] if not question or not number: if not question: errors.append("You didn't put a question") if not number: errors.append("You didn't specify a number of options") if not end_date: errors.append("You didn't specify an end date for the poll") return render(request, 'polls/poll_creation.html', { 'error_message': errors }) else: poll = Poll() poll.question=question poll.pub_date=timezone.now() poll.start_date=timezone.now() poll.end_date=end_date poll.save() return HttpResponseRedirect(reverse('polls:option_creation', args=(number, poll.id)))
def submit_poll(request): question = request.POST['question'] number = int(request.POST['num_choices']) end_date = request.POST['end_date'] errors = [] if not question or not number: if not question: errors.append("You didn't put a question") if not number: errors.append("You didn't specify a number of options") if not end_date: errors.append("You didn't specify an end date for the poll") return render(request, 'polls/poll_creation.html', {'error_message': errors}) else: poll = Poll() poll.question = question poll.pub_date = timezone.now() poll.start_date = timezone.now() poll.end_date = end_date poll.save() return HttpResponseRedirect( reverse('polls:option_creation', args=(number, poll.id)))
# No polls are in the system yet. print Poll.objects.all() # Create a new Poll. import datetime p = Poll(question="What's up?", pub_date=datetime.datetime.now()) print p # Save the object into the database. You have to call save() explicitly. print p.save() # Now it has an ID. Note that this might say "1L" instead of "1", depending # on which database you're using. That's no biggie; it just means your # database backend prefers to return integers as Python long integer # objects. print p.id # Access database columns via Python attributes. print p.question print p.pub_date # Change values by changing the attributes, then calling save(). p.pub_date = datetime.datetime(2007, 4, 1, 0, 0) print p.pub_date print p.save() # objects.all() displays all the polls in the database. print Poll.objects.all()