def get_question(request): # if this is a POST request we need to process the form data if request.method == 'POST': # create a form instance and populate it with data from the request: form = QuestionForm(request.POST) # check whether it's valid: if form.is_valid(): # process the data in form.cleaned_data as required new_question = Question() new_question.body = Gymkhana_body.objects.get( nameofbody=form.cleaned_data['body']) new_question.question_text = form.cleaned_data['question'] new_question.pub_date = timezone.now() new_question.save() choice1 = Choice() choice1.question = new_question choice1.choice_text = form.cleaned_data['choice1'] choice1.votes = 0 choice1.save() if form.cleaned_data['choice2']: choice2 = Choice() choice2.question = new_question choice2.choice_text = form.cleaned_data['choice2'] choice2.votes = 0 choice2.save() if form.cleaned_data['choice3']: choice3 = Choice() choice3.question = new_question choice3.choice_text = form.cleaned_data['choice3'] choice3.votes = 0 choice3.save() if form.cleaned_data['choice4']: choice4 = Choice() choice4.question = new_question choice4.choice_text = form.cleaned_data['choice4'] choice4.votes = 0 choice4.save() # redirect to a new URL: messages.add_message(request, messages.SUCCESS, 'Thanks for your suggestion!') return HttpResponseRedirect('/') # if a GET (or any other method) we'll create a blank form else: form = QuestionForm() return render(request, 'organisation/addquestion.html', {'form': form})
def test_choice_str(self): time = timezone.now() question = Question(pub_date=time) choice = Choice() choice.question = question choice.choice_text = 'Choice1' self.assertEqual(str(choice), 'Choice1')
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_text = "doin' fine..." # Need change # 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_text, "doin' fine...") # Need change self.assertEquals(choice_from_db.votes, 3)
def submit_options(request): options = dict(request.POST)['option'] poll_id = request.POST['poll_id'] p = get_object_or_404(Poll, pk=int(poll_id)) if isinstance(options, basestring): opt = Choice() opt.poll = p opt.choice_text = options opt.votes = 0 opt.save() else: for opt in options: c = Choice() c.poll = p c.choice_text = opt c.votes = 0 c.save() return HttpResponseRedirect(reverse('polls:index'))
def submit_options(request): options = dict(request.POST)['option'] poll_id = request.POST['poll_id'] p = get_object_or_404(Poll, pk=int(poll_id)) if isinstance(options, basestring): opt = Choice() opt.poll=p opt.choice_text=options opt.votes=0 opt.save() else: for opt in options: c = Choice() c.poll = p c.choice_text=opt c.votes=0 c.save() return HttpResponseRedirect(reverse('polls:index'))
def test_saving_and_retrieving_choices(self): choice = Choice() choice.choice_text = '1. Demi Moore 2. Julia Roberts' choice.votes = 1000 choice.save() saved_choices = Choice.objects.all() self.assertEqual(saved_choices.count(), 1) saved_choice = saved_choices[0] self.assertIn('1. Demi Moore 2. Julia Roberts', repr(saved_choice), ) self.assertEqual(saved_choice.choice_text, '1. Demi Moore 2. Julia Roberts') self.assertEqual(saved_choice.votes, 1000)
def i_have_a_list_of_polls(step): from polls.models import Question, Choice from django.utils import timezone #create 5 poll question for i in range(0, 5): question = Question() question.question_text = "Question text %s" % i question.pub_date = timezone.now() question.save() # create 5 choice for each question for j in range(0, 5): choice = Choice() choice.choice_text = "Choice text %s" % j choice.question = question choice.save()
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 seed(request): """Seeds the database with sample polls.""" samples_path = path.join(path.dirname(__file__), 'samples.json') with open(samples_path, 'r') as samples_file: samples_polls = json.load(samples_file) for sample_poll in samples_polls: poll = Question() poll.question_text = sample_poll['text'] poll.pub_date = timezone.now() poll.save() for sample_choice in sample_poll['choices']: choice = Choice() choice.question = poll choice.choice_text = sample_choice choice.votes = 0 choice.save() return HttpResponseRedirect(reverse('polls:question'))
# -*- coding: utf-8 -*- from django.contrib.auth.models import User from polls.models import Question ,Choice from django.utils import timezone import random for q in range(0 ,11): question =Question() question.question_text ='qst_' +str(q) question.pub_date =timezone.now() question.save() for c in range(0 ,random.randint(1 ,10)): choice =Choice() choice.choice_text ='chi_' +str(q) +'_' +str(c) choice.question =question choice.votes =random.randint(3 ,8) choice.save() #>>> from func.filldb import createphoto #>>> createphoto()
def import_data(): # Initial Imports # Processing model: polls.models.Question from polls.models import Question polls_question_1 = Question() polls_question_1.question_text = 'How are you?' polls_question_1.pub_date = dateutil.parser.parse( "2018-06-11T07:57:12.927873+00:00") polls_question_1 = importer.save_or_locate(polls_question_1) polls_question_2 = Question() polls_question_2.question_text = 'How mad are you?' polls_question_2.pub_date = dateutil.parser.parse( "2018-06-11T07:57:34.837612+00:00") polls_question_2 = importer.save_or_locate(polls_question_2) polls_question_3 = Question() polls_question_3.question_text = 'How wrong are you?' polls_question_3.pub_date = dateutil.parser.parse( "2018-06-11T07:57:55.751242+00:00") polls_question_3 = importer.save_or_locate(polls_question_3) polls_question_4 = Question() polls_question_4.question_text = 'How happy are you??' polls_question_4.pub_date = dateutil.parser.parse( "2018-06-11T08:33:45+00:00") polls_question_4 = importer.save_or_locate(polls_question_4) # Processing model: polls.models.Choice from polls.models import Choice polls_choice_1 = Choice() polls_choice_1.question = polls_question_1 polls_choice_1.choice_text = 'You are the man!' polls_choice_1.votes = 1 polls_choice_1 = importer.save_or_locate(polls_choice_1) polls_choice_2 = Choice() polls_choice_2.question = polls_question_1 polls_choice_2.choice_text = 'You are the best!' polls_choice_2.votes = 2 polls_choice_2 = importer.save_or_locate(polls_choice_2) polls_choice_3 = Choice() polls_choice_3.question = polls_question_2 polls_choice_3.choice_text = 'You are the man!' polls_choice_3.votes = 4 polls_choice_3 = importer.save_or_locate(polls_choice_3) polls_choice_4 = Choice() polls_choice_4.question = polls_question_3 polls_choice_4.choice_text = 'You are the one!' polls_choice_4.votes = 7 polls_choice_4 = importer.save_or_locate(polls_choice_4) polls_choice_5 = Choice() polls_choice_5.question = polls_question_2 polls_choice_5.choice_text = 'You are mad!' polls_choice_5.votes = 4 polls_choice_5 = importer.save_or_locate(polls_choice_5) polls_choice_6 = Choice() polls_choice_6.question = polls_question_4 polls_choice_6.choice_text = 'Very Happy' polls_choice_6.votes = 2 polls_choice_6 = importer.save_or_locate(polls_choice_6) polls_choice_7 = Choice() polls_choice_7.question = polls_question_4 polls_choice_7.choice_text = 'Damn Happy' polls_choice_7.votes = 3 polls_choice_7 = importer.save_or_locate(polls_choice_7) polls_choice_8 = Choice() polls_choice_8.question = polls_question_4 polls_choice_8.choice_text = 'Super duper Happy' polls_choice_8.votes = 3 polls_choice_8 = importer.save_or_locate(polls_choice_8) # Processing model: polls.models.mModel from polls.models import mModel polls_mmodel_1 = mModel() polls_mmodel_1.name = 'Ahobaka' polls_mmodel_1.gender = 'M' polls_mmodel_1.age = 12 polls_mmodel_1.email = '*****@*****.**' polls_mmodel_1.dob = dateutil.parser.parse("2018-06-11") polls_mmodel_1.job = 'Student' polls_mmodel_1 = importer.save_or_locate(polls_mmodel_1) polls_mmodel_2 = mModel() polls_mmodel_2.name = 'Ultraman' polls_mmodel_2.gender = 'F' polls_mmodel_2.age = 23 polls_mmodel_2.email = '*****@*****.**' polls_mmodel_2.dob = dateutil.parser.parse("2018-06-11") polls_mmodel_2.job = 'Superhero' polls_mmodel_2 = importer.save_or_locate(polls_mmodel_2) polls_mmodel_3 = mModel() polls_mmodel_3.name = 'Supergirl' polls_mmodel_3.gender = 'F' polls_mmodel_3.age = 26 polls_mmodel_3.email = '*****@*****.**' polls_mmodel_3.dob = dateutil.parser.parse("2018-06-12") polls_mmodel_3.job = 'Superheroin' polls_mmodel_3 = importer.save_or_locate(polls_mmodel_3) polls_mmodel_4 = mModel() polls_mmodel_4.name = 'Batgirl' polls_mmodel_4.gender = 'F' polls_mmodel_4.age = 21 polls_mmodel_4.email = '*****@*****.**' polls_mmodel_4.dob = dateutil.parser.parse("2018-06-12") polls_mmodel_4.job = 'Superheroin' polls_mmodel_4 = importer.save_or_locate(polls_mmodel_4) polls_mmodel_5 = mModel() polls_mmodel_5.name = 'benghenglian' polls_mmodel_5.gender = 'M' polls_mmodel_5.age = 33 polls_mmodel_5.email = '*****@*****.**' polls_mmodel_5.dob = dateutil.parser.parse("2018-06-12") polls_mmodel_5.job = 'BengHengLIanKnight' polls_mmodel_5 = importer.save_or_locate(polls_mmodel_5) polls_mmodel_6 = mModel() polls_mmodel_6.name = 'benghenglian' polls_mmodel_6.gender = 'M' polls_mmodel_6.age = 33 polls_mmodel_6.email = '*****@*****.**' polls_mmodel_6.dob = dateutil.parser.parse("2018-06-12") polls_mmodel_6.job = 'BengHengLIanKnight' polls_mmodel_6 = importer.save_or_locate(polls_mmodel_6) polls_mmodel_7 = mModel() polls_mmodel_7.name = 'Superman' polls_mmodel_7.gender = 'm' polls_mmodel_7.age = 23 polls_mmodel_7.email = '*****@*****.**' polls_mmodel_7.dob = dateutil.parser.parse("2018-06-12") polls_mmodel_7.job = 'Knight' polls_mmodel_7 = importer.save_or_locate(polls_mmodel_7) polls_mmodel_8 = mModel() polls_mmodel_8.name = 'Superman' polls_mmodel_8.gender = 'm' polls_mmodel_8.age = 23 polls_mmodel_8.email = '*****@*****.**' polls_mmodel_8.dob = dateutil.parser.parse("2018-06-12") polls_mmodel_8.job = 'Knight' polls_mmodel_8 = importer.save_or_locate(polls_mmodel_8) polls_mmodel_9 = mModel() polls_mmodel_9.name = 'Superman' polls_mmodel_9.gender = 'm' polls_mmodel_9.age = 23 polls_mmodel_9.email = '*****@*****.**' polls_mmodel_9.dob = dateutil.parser.parse("2018-06-12") polls_mmodel_9.job = 'Knight' polls_mmodel_9 = importer.save_or_locate(polls_mmodel_9) polls_mmodel_10 = mModel() polls_mmodel_10.name = 'Superman' polls_mmodel_10.gender = 'm' polls_mmodel_10.age = 23 polls_mmodel_10.email = '*****@*****.**' polls_mmodel_10.dob = dateutil.parser.parse("2018-06-12") polls_mmodel_10.job = 'Knight' polls_mmodel_10 = importer.save_or_locate(polls_mmodel_10) polls_mmodel_11 = mModel() polls_mmodel_11.name = 'Superman' polls_mmodel_11.gender = 'm' polls_mmodel_11.age = 23 polls_mmodel_11.email = '*****@*****.**' polls_mmodel_11.dob = dateutil.parser.parse("2018-06-12") polls_mmodel_11.job = 'Knight' polls_mmodel_11 = importer.save_or_locate(polls_mmodel_11) polls_mmodel_12 = mModel() polls_mmodel_12.name = 'Superman' polls_mmodel_12.gender = 'm' polls_mmodel_12.age = 23 polls_mmodel_12.email = '*****@*****.**' polls_mmodel_12.dob = dateutil.parser.parse("2018-06-12") polls_mmodel_12.job = 'Knight' polls_mmodel_12 = importer.save_or_locate(polls_mmodel_12) polls_mmodel_13 = mModel() polls_mmodel_13.name = 'Superman' polls_mmodel_13.gender = 'm' polls_mmodel_13.age = 23 polls_mmodel_13.email = '*****@*****.**' polls_mmodel_13.dob = dateutil.parser.parse("2018-06-12") polls_mmodel_13.job = 'Knight' polls_mmodel_13 = importer.save_or_locate(polls_mmodel_13) polls_mmodel_14 = mModel() polls_mmodel_14.name = 'Superman' polls_mmodel_14.gender = 'm' polls_mmodel_14.age = 23 polls_mmodel_14.email = '*****@*****.**' polls_mmodel_14.dob = dateutil.parser.parse("2018-06-12") polls_mmodel_14.job = 'Knight' polls_mmodel_14 = importer.save_or_locate(polls_mmodel_14) polls_mmodel_15 = mModel() polls_mmodel_15.name = 'Superman' polls_mmodel_15.gender = 'd' polls_mmodel_15.age = 2 polls_mmodel_15.email = '*****@*****.**' polls_mmodel_15.dob = dateutil.parser.parse("2018-06-12") polls_mmodel_15.job = 'Knight' polls_mmodel_15 = importer.save_or_locate(polls_mmodel_15) polls_mmodel_16 = mModel() polls_mmodel_16.name = 'Superman' polls_mmodel_16.gender = 'd' polls_mmodel_16.age = 2 polls_mmodel_16.email = '*****@*****.**' polls_mmodel_16.dob = dateutil.parser.parse("2018-06-12") polls_mmodel_16.job = 'Knight' polls_mmodel_16 = importer.save_or_locate(polls_mmodel_16) polls_mmodel_17 = mModel() polls_mmodel_17.name = 'Superman' polls_mmodel_17.gender = 'd' polls_mmodel_17.age = 2 polls_mmodel_17.email = '*****@*****.**' polls_mmodel_17.dob = dateutil.parser.parse("2018-06-12") polls_mmodel_17.job = 'Knight' polls_mmodel_17 = importer.save_or_locate(polls_mmodel_17) polls_mmodel_18 = mModel() polls_mmodel_18.name = 'Superman' polls_mmodel_18.gender = 'd' polls_mmodel_18.age = 2 polls_mmodel_18.email = '*****@*****.**' polls_mmodel_18.dob = dateutil.parser.parse("2018-06-12") polls_mmodel_18.job = 'Knight' polls_mmodel_18 = importer.save_or_locate(polls_mmodel_18) polls_mmodel_19 = mModel() polls_mmodel_19.name = 'Superman' polls_mmodel_19.gender = 'd' polls_mmodel_19.age = 2 polls_mmodel_19.email = '*****@*****.**' polls_mmodel_19.dob = dateutil.parser.parse("2018-06-12") polls_mmodel_19.job = 'Knight' polls_mmodel_19 = importer.save_or_locate(polls_mmodel_19) polls_mmodel_20 = mModel() polls_mmodel_20.name = 'Superman' polls_mmodel_20.gender = 'd' polls_mmodel_20.age = 2 polls_mmodel_20.email = '*****@*****.**' polls_mmodel_20.dob = dateutil.parser.parse("2018-06-12") polls_mmodel_20.job = 'Knight' polls_mmodel_20 = importer.save_or_locate(polls_mmodel_20) polls_mmodel_21 = mModel() polls_mmodel_21.name = 'Superman' polls_mmodel_21.gender = 'd' polls_mmodel_21.age = 2 polls_mmodel_21.email = '*****@*****.**' polls_mmodel_21.dob = dateutil.parser.parse("2018-06-12") polls_mmodel_21.job = 'Knight' polls_mmodel_21 = importer.save_or_locate(polls_mmodel_21) polls_mmodel_22 = mModel() polls_mmodel_22.name = 'Superman' polls_mmodel_22.gender = 'd' polls_mmodel_22.age = 2 polls_mmodel_22.email = '*****@*****.**' polls_mmodel_22.dob = dateutil.parser.parse("2018-06-12") polls_mmodel_22.job = 'Knight' polls_mmodel_22 = importer.save_or_locate(polls_mmodel_22) polls_mmodel_23 = mModel() polls_mmodel_23.name = 'Superman' polls_mmodel_23.gender = 'c' polls_mmodel_23.age = 2 polls_mmodel_23.email = '*****@*****.**' polls_mmodel_23.dob = dateutil.parser.parse("2018-06-12") polls_mmodel_23.job = 'Knight' polls_mmodel_23 = importer.save_or_locate(polls_mmodel_23) polls_mmodel_24 = mModel() polls_mmodel_24.name = 'Superman' polls_mmodel_24.gender = 'c' polls_mmodel_24.age = 2 polls_mmodel_24.email = '*****@*****.**' polls_mmodel_24.dob = dateutil.parser.parse("2018-06-12") polls_mmodel_24.job = 'Knight' polls_mmodel_24 = importer.save_or_locate(polls_mmodel_24) polls_mmodel_25 = mModel() polls_mmodel_25.name = 'Superman' polls_mmodel_25.gender = 's' polls_mmodel_25.age = 2 polls_mmodel_25.email = '*****@*****.**' polls_mmodel_25.dob = dateutil.parser.parse("2018-06-12") polls_mmodel_25.job = 'Knight' polls_mmodel_25 = importer.save_or_locate(polls_mmodel_25) polls_mmodel_26 = mModel() polls_mmodel_26.name = 'Superman' polls_mmodel_26.gender = 's' polls_mmodel_26.age = 2 polls_mmodel_26.email = '*****@*****.**' polls_mmodel_26.dob = dateutil.parser.parse("2018-06-12") polls_mmodel_26.job = 'Knight' polls_mmodel_26 = importer.save_or_locate(polls_mmodel_26) polls_mmodel_27 = mModel() polls_mmodel_27.name = 'Superman' polls_mmodel_27.gender = 's' polls_mmodel_27.age = 2 polls_mmodel_27.email = '*****@*****.**' polls_mmodel_27.dob = dateutil.parser.parse("2018-06-12") polls_mmodel_27.job = 'Knight' polls_mmodel_27 = importer.save_or_locate(polls_mmodel_27) polls_mmodel_28 = mModel() polls_mmodel_28.name = 'Superman' polls_mmodel_28.gender = 's' polls_mmodel_28.age = 2 polls_mmodel_28.email = '*****@*****.**' polls_mmodel_28.dob = dateutil.parser.parse("2018-06-12") polls_mmodel_28.job = 'Knight' polls_mmodel_28 = importer.save_or_locate(polls_mmodel_28) polls_mmodel_29 = mModel() polls_mmodel_29.name = 'Superman' polls_mmodel_29.gender = 'g' polls_mmodel_29.age = 2 polls_mmodel_29.email = '*****@*****.**' polls_mmodel_29.dob = dateutil.parser.parse("2018-06-12") polls_mmodel_29.job = 'Knight' polls_mmodel_29 = importer.save_or_locate(polls_mmodel_29) polls_mmodel_30 = mModel() polls_mmodel_30.name = 'Superman' polls_mmodel_30.gender = 'f' polls_mmodel_30.age = 2 polls_mmodel_30.email = '*****@*****.**' polls_mmodel_30.dob = dateutil.parser.parse("2018-06-12") polls_mmodel_30.job = 'Knight' polls_mmodel_30 = importer.save_or_locate(polls_mmodel_30) polls_mmodel_31 = mModel() polls_mmodel_31.name = 'HOHOH' polls_mmodel_31.gender = 'M' polls_mmodel_31.age = 2 polls_mmodel_31.email = '*****@*****.**' polls_mmodel_31.dob = dateutil.parser.parse("2018-06-12") polls_mmodel_31.job = 'DarkKinght' polls_mmodel_31 = importer.save_or_locate(polls_mmodel_31) polls_mmodel_32 = mModel() polls_mmodel_32.name = 'HOHOH' polls_mmodel_32.gender = 'M' polls_mmodel_32.age = 2 polls_mmodel_32.email = '*****@*****.**' polls_mmodel_32.dob = dateutil.parser.parse("2018-06-12") polls_mmodel_32.job = 'DarkKinght' polls_mmodel_32 = importer.save_or_locate(polls_mmodel_32) polls_mmodel_33 = mModel() polls_mmodel_33.name = 'PowerRangers' polls_mmodel_33.gender = 'M' polls_mmodel_33.age = 24 polls_mmodel_33.email = '*****@*****.**' polls_mmodel_33.dob = dateutil.parser.parse("2018-05-12") polls_mmodel_33.job = 'Super Junior Heroes' polls_mmodel_33 = importer.save_or_locate(polls_mmodel_33) polls_mmodel_34 = mModel() polls_mmodel_34.name = 'Sherlock Holmes' polls_mmodel_34.gender = 'M' polls_mmodel_34.age = 44 polls_mmodel_34.email = '*****@*****.**' polls_mmodel_34.dob = dateutil.parser.parse("1901-03-14") polls_mmodel_34.job = 'Detective' polls_mmodel_34 = importer.save_or_locate(polls_mmodel_34) polls_mmodel_35 = mModel() polls_mmodel_35.name = 'Meng' polls_mmodel_35.gender = 'M' polls_mmodel_35.age = 31 polls_mmodel_35.email = '*****@*****.**' polls_mmodel_35.dob = dateutil.parser.parse("1987-04-01") polls_mmodel_35.job = 'Software Dev' polls_mmodel_35 = importer.save_or_locate(polls_mmodel_35) polls_mmodel_36 = mModel() polls_mmodel_36.name = 'ABCDE' polls_mmodel_36.gender = 'M' polls_mmodel_36.age = 25 polls_mmodel_36.email = '*****@*****.**' polls_mmodel_36.dob = dateutil.parser.parse("2018-06-20") polls_mmodel_36.job = 'CSS Dev' polls_mmodel_36 = importer.save_or_locate(polls_mmodel_36) polls_mmodel_37 = mModel() polls_mmodel_37.name = 'Ali' polls_mmodel_37.gender = 'M' polls_mmodel_37.age = 24 polls_mmodel_37.email = '*****@*****.**' polls_mmodel_37.dob = dateutil.parser.parse("2018-06-20") polls_mmodel_37.job = 'Manager' polls_mmodel_37 = importer.save_or_locate(polls_mmodel_37)