def create_a_poll(request,survey_id): # create a poll in a survey # get questions from input # redirect to craetPollPage logging.debug(request.POST) question = request.POST['Question0'] choice_list = request.POST.getlist('Field1') s = get_object_or_404(Survey,pk = survey_id) # check if user correct if not s.author == request.user.username: return p = Poll(survey=s, question = question) p.save() # get choice from form input logging.debug(choice_list) for c in choice_list: if c: logging.debug(c) choice = Choice(poll= p, choice = c, votes=0) choice.save() return HttpResponseRedirect('createPollPage')
def test_polls_page_shows_links_to_all_polls(self): #setup polls poll1 = Poll(question='6 times 7', pub_date=timezone.now()) poll1.save() poll2 = Poll(question='How is your day', pub_date=timezone.now()) poll2.save() #access polls view response = self.client.get(reverse('polls.views.show_all_pools')) #check if right template used self.assertTemplateUsed(response, 'all_polls.html') #check if polls transferred to template polls_in_context = response.context['polls'] self.assertEquals(list(polls_in_context), [poll1, poll2]) #check if polls displayed on the page self.assertIn(poll1.question, response.content) self.assertIn(poll2.question, response.content) #check if polls has links to individual poll self.assertIn(reverse('polls.views.poll', args=[poll1.id]), response.content) self.assertIn(reverse('polls.views.poll', args=[poll2.id]), response.content)
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_from_db = Poll.objects.all() self.assertEquals(len(all_polls_from_db), 1) first_poll_from_db = all_polls_from_db[0] self.assertEquals(first_poll_from_db, poll) self.assertEquals(first_poll_from_db.question, "What's up?") self.assertEquals(first_poll_from_db.pub_date, poll.pub_date)
def createPoll(request): if not request.POST: folders = utils.getDateList() if utils.getNowH() >= utils.dinnerH: meal = "dinner" else: meal = "lunch" return render_to_response("createpoll.html", { "folders": folders, "meal": meal }) codeLen = 8 codeChars = string.ascii_uppercase + string.ascii_lowercase + string.digits code = "" while True: code = "".join(random.choice(codeChars) for _ in range(codeLen)) if not Poll.objects.filter(code=code): break owner = utils.escapeHtml(request.POST["owner"]) if not owner: owner = utils.getMaskedIp(request) title = utils.escapeHtml(request.POST["title"]) if not title: title = "%s's poll" % owner open = "open" in request.POST p = Poll(owner=owner, title=title, open=open, parent=utils.getToday(), code=code, count=0) result = {} result["0_-1"] = [] # any result["9_-1"] = [] # 9 any result["22_-1"] = [] # 22 any lunch9, lunch22, dinner9, dinner22 = utils.getFileLists() if utils.getNowH() >= utils.dinnerH: dish9 = [int(x.split(".")[0]) for x in dinner9] dish22 = [int(x.split(".")[0]) for x in dinner22] else: dish9 = [int(x.split(".")[0]) for x in lunch9] dish22 = [int(x.split(".")[0]) for x in lunch22] for d in dish9: result["9_" + str(d)] = [] for d in dish22: result["22_" + str(d)] = [] p.result = json.dumps(result) p.save() return redirect("/poll/%s/" % code)
def addVote(self, request, page): user = auth.get_user(request) film = Film.objects.get(id=page) if not user.is_anonymous: user = CustomUser.objects.get(user=user) if self.is_valid(): poll = Poll() poll.user = user poll.film = film poll.mark = self.cleaned_data.get("vote") poll.save() return False else: mistake = u"Форма заполнена с ошибками" return mistake
def post(self, *args): logging.info(self.request.arguments()) name = self.request.get('name') start_time = parse(self.request.get('start_time')) duration = int(self.request.get('duration')) logging.info(name) party = politicalparties.all_parties[self.request.get('political_party')] poll = Poll(name=name, start_time=start_time, duration=duration) logging.info(poll.start_time) poll.start_time = poll.start_time.replace(tzinfo=gettz('Europe/London')) poll.save() poll.create_choice(party['id'], party['short_name'], party['colour']) self.redirect("/admin/poll/list")
def add(request): if not request.user.id: return redirect('/accounts/login?flag=1') if request.POST: poll = Poll(question=request.POST.get('question')) poll.save() for ch in request.POST.getlist('choice'): choice = Choice(poll=poll, choice=ch) choice.save() poll_list = Poll.objects.all() return render_to_response('index.html',dict(poll_list=poll_list, msg_success='Successfully submitted poll and choices')) else: return render(request,'add.html',{})
def post(self, *args): logging.info(self.request.arguments()) name = self.request.get('name') start_time = parse(self.request.get('start_time')) duration = int(self.request.get('duration')) logging.info(name) party = politicalparties.all_parties[self.request.get( 'political_party')] poll = Poll(name=name, start_time=start_time, duration=duration) logging.info(poll.start_time) poll.start_time = poll.start_time.replace( tzinfo=gettz('Europe/London')) poll.save() poll.create_choice(party['id'], party['short_name'], party['colour']) self.redirect("/admin/poll/list")
def get_poll_article(request, article_id): logged_user = request.user article = Article.objects.get(id=article_id) polls = logged_user.poll_set.all() articles = [] for poll in polls: articles.append(poll.article) if article in articles: url = urlparse.urljoin('/fire/', article_id) return redirect(url) else: article.poll_num += 1 article.save() poll = Poll(user=logged_user, article=article) poll.save() data = {} return redirect('/fire/')
def createPoll(request): if not request.POST: folders = utils.getDateList() if utils.getNowH() >= utils.dinnerH: meal = "dinner" else: meal = "lunch" return render_to_response("createpoll.html", {"folders":folders, "meal":meal}) codeLen = 8 codeChars = string.ascii_uppercase+string.ascii_lowercase+string.digits code = "" while True: code = "".join(random.choice(codeChars) for _ in range(codeLen)) if not Poll.objects.filter(code=code): break owner = utils.escapeHtml(request.POST["owner"]) if not owner: owner = utils.getMaskedIp(request) title = utils.escapeHtml(request.POST["title"]) if not title: title = "%s's poll" % owner open = "open" in request.POST p = Poll(owner=owner, title=title, open=open, parent=utils.getToday(), code=code, count=0) result = {} result["0_-1"] = [] # any result["9_-1"] = [] # 9 any result["22_-1"] = [] # 22 any lunch9, lunch22, dinner9, dinner22 = utils.getFileLists() if utils.getNowH() >= utils.dinnerH: dish9 = [int(x.split(".")[0]) for x in dinner9] dish22 = [int(x.split(".")[0]) for x in dinner22] else: dish9 = [int(x.split(".")[0]) for x in lunch9] dish22 = [int(x.split(".")[0]) for x in lunch22] for d in dish9: result["9_"+str(d)] = [] for d in dish22: result["22_"+str(d)] = [] p.result = json.dumps(result) p.save() return redirect("/poll/%s/" % code)
def create_a_poll(request, survey_id): # create a poll in a survey # get questions from input # redirect to craetPollPage logging.debug(request.POST) question = request.POST['Question0'] choice_list = request.POST.getlist('Field1') s = get_object_or_404(Survey, pk=survey_id) # check if user correct if not s.author == request.user.username: return p = Poll(survey=s, question=question) p.save() # get choice from form input logging.debug(choice_list) for c in choice_list: if c: logging.debug(c) choice = Choice(poll=p, choice=c, votes=0) choice.save() return HttpResponseRedirect('createPollPage')
# Create a new database with our models! # ./manage.py syncdb """ LEARNING THE BASICS OF THE ORM """ from polls.models import * from django.utils import timezone # Queryset with all Poll objects in the database Poll.objects.all() p = Poll(question="What up?", pub_date=timezone.now()) p.save() # Let's see the id p.id # There's also an alias p.pk # if you override the primary key name which by default is id # you can still access it by pk. from django.utils import timezone p = Poll(question="What up?", pub_date=timezone.now()) Poll(question="What's not?", pub_date=timezone.now()).save() Poll(question="How's it going?", pub_date=timezone.now()).save() Poll(question="Wasaaappp?", pub_date=timezone.now()).save()
# Lets add some sample data too u = User() u.displayname = "Testkonto 1" u.email = "*****@*****.**" u.password = "******" u.save() p1 = Poll() p1.name = "Testpoll 1" p1.number = "+46766862842" p1.allow_duplicate_answers = True p1.allow_multiple_choices = True p1.owner = u p1.save() c1 = Choice() c1.poll = p1 c1.name = "01" c1.description = "Bidrag nr. 1 - Bä bä vita lamm" c1.save() c2 = Choice() c2.poll = p1 c2.name = "02" c2.description = "Bidrag nr. 2 - Blinka lilla stjärna" c2.save() c3 = Choice() c3.poll = p1
from django.shortcuts import render from django.views.generic import TemplateView # # Create your views here. '''from django.http import HttpResponse def TestView(request, **kwargs): return HttpResponse("Hello Word")''' from mongoengine import * #connect("datascience") from models import Poll poll = Poll(question="What is your name", ) poll.save() #print poll.question class SplashView(TemplateView): template_name = "index.html"