def setUp(self): super(VotingTestCase, self).setUp() self.contest = Election(key_name=self.id(), title="Yet another contest") self.contest.put() candidates = [ "Favorite", "Middling", "Underdog", ] self.candidates = [] for title in candidates: candidate = Candidate(title=title, parent=self.contest) candidate.put() self.candidates.append(str(candidate.key().id())) candidates = db.GqlQuery( "SELECT * FROM Candidate WHERE ANCESTOR IS :1", self.contest) self.assertEqual(set(str(c.key().id()) for c in candidates), set(self.candidates)) self.user = self.login() self.app = TestApp(application) self.page = self.app.get("/" + self.contest.key().name() + "/vote")
def election(request,election_id): if request.method == 'POST': context = {} form = VotingForm(request.POST) if form.is_valid(): voter_id = form.cleaned_data['voter_id'] if (Ballot.objects.filter(voter_id=voter_id).count() != 0): return render(request,'voting/already_voted.html', context=context) for key in request.POST.keys(): if (key.startswith("post_")): candidate_id = request.POST[key] ballot = Ballot() ballot.voter = Voter(id=int(voter_id)) ballot.candidate = Candidate(id=int(candidate_id)) ballot.save()# candidate = Candidate.objects.get(id=int(candidate_id)) candidate.votes = F('votes') + 1 candidate.save()# return render(request,'voting/thank_you_for_voting.html', context=context) else: context["forms"] = form return render(request,'voting/candidates.html', context=context) else: if ('voter_id' in request.session): voter_id = request.session['voter_id'] del request.session['voter_id'] else: return HttpResponseRedirect(reverse('voting:index')) if ('voter_name' in request.session): voter_name = request.session['voter_name'] del request.session['voter_name'] election = Election.objects.get(id=election_id) context = { 'election': election, 'voter_name' : voter_name, 'election_name': election.desc_text } myform = VotingForm(initial={ 'election_id': election.id, 'voter_id': voter_id}) candidate_list = Candidate.objects.filter(election=election_id) post_list = Post.objects.all() post_dict = {} for post in post_list: post_dict[post.post_name] = post.id posts = {} for candidate in candidate_list: if not candidate.post.post_name in posts: posts[candidate.post.post_name] = {} posts[candidate.post.post_name][str(candidate.id)] = candidate.voter.voter_name myform.updateField(posts, post_dict) context["forms"] = myform return render(request,'voting/candidates.html', context=context)
def testExistCompleteCandidate(self): p1 = Party(abreviatura="PC") candidate_test = Candidate(name="test", age=21, number=1, auto_community="AN", sex="H", political_party=p1) self.assertEqual(candidate_test.name, "test") self.assertEqual(candidate_test.age, 21) self.assertEqual(candidate_test.number, 1) self.assertEqual(candidate_test.auto_community, "AN") self.assertEqual(candidate_test.sex, "H") self.assertEqual(candidate_test.political_party.abreviatura, "PC")
def setUp(self): q = Question(desc="Descripcion") q.save() que1 = Question(desc="Descripcion1") que1.save() que2 = Question(desc="Descripcion2") que2.save() opt1 = QuestionOption(question=q, option="option1") opt1.save() opt2 = QuestionOption(question=q, option="option2") opt2.save() q_prefer = QuestionPrefer(question=q, prefer="YES", number=4, option="option1") q_prefer.save() q_ordering = QuestionOrdering(question=q, number=5, option="prueba de ordenacion", ordering=1) q_ordering.save() party1 = Party(abreviatura="PC") party1.save() self.candidate1 = Candidate(name="test", age=21, number=1, auto_community="AN", sex="H", political_party=party1) self.candidate1.save() self.v1 = ReadonlyVoting(name="VotacionRO", question=que1, desc="example") self.v2 = MultipleVoting(name="VotacionM", desc="example") self.v2.save() self.v2.question.add(que1) self.v2.question.add(que2) self.v = Voting(name="Votacion", question=q) self.v.save() self.v1.save() self.v2.save() super().setUp()
def post(self): election = self.election() if not election: return try: candidate = Candidate(parent=election) candidate.title = self.request.get("title").strip() candidate.description = self.request.get("description").strip() candidate.put() self.redirect("/%s/candidate" % election.key().name()) except Exception, err: logging.exception("Failed to save candidate: %r", repr(locals())) self.render("candidate.html", election=election, candidates=[], defaults=candidate, error=err) raise
# description='A web development project.', # technology='Django', # image='img/iohannis.png' # ) # p2 = Project( # title='My Second Project', # description='Another web development project.', # technology='Flask', # image='img/dancila.png' # ) # p1.save() # p2.save() can1 = Candidate( name='Klaus Werner Iohannis', description='Leader of PNL, Mayor of Sibiu', image='img/iohannis.png' ) can2 = Candidate( name='Vasilica Viorica Dăncilă', description='President of the PSD', image='img/dancila.png' ) can1.save() can2.save() sesh1 = VotingSession( name="Presidential Elections", year=2019, country='Romania'
def setUp(self): p = Party(abreviatura="PC") p.save() c = Candidate(name="pepe") c.save() super().setUp()