Example #1
0
    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")
Example #2
0
    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()
Example #3
0
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)
Example #4
0
 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
Example #5
0
    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")
Example #6
0
 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")
Example #7
0
    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
Example #8
0
#     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'
Example #9
0
from projects.models import Project
from voting.models import Candidate

Project.objects.bulk_create([
    Project(title='My First Project',
            description='A web development project.',
            technology='Django',
            image='img/iohannis.png'),
    Project(title='My Second Project',
            description='Another web development project.',
            technology='Flask',
            image='img/dancila.png')
])

Candidate.objects.bulk_create([
    Candidate(name='Klaus Werner Iohannis',
              description='Leader of PNL, '
              'Mayor of Siubiu.',
              image='img/iohannis.png'),
    Candidate(name='Vasilica Viorica Dăncilă',
              description='President of the PSD',
              image='img/dancila.png')
])
Example #10
0
 def setUp(self):
     p = Party(abreviatura="PC")
     p.save()
     c = Candidate(name="pepe")
     c.save()
     super().setUp()
Example #11
0
class VotingToString(BaseTestCase):
    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 tearDown(self):
        super().tearDown()
        self.v = None

    def test_Voting_toString(self):
        v = Voting.objects.get(name='Votacion')
        v1 = ReadonlyVoting.objects.get(name='VotacionRO')
        v2 = MultipleVoting.objects.get(name='VotacionM')
        candidate1 = Candidate.objects.get(name='test')
        self.assertEquals(str(v), "Votacion")
        self.assertEquals(str(v.question), "Descripcion")
        self.assertEquals(str(v.question.options.all()[0]), "option1 (2)")
        self.assertEquals(str(v.question.prefer_options.all()[0]),
                          "option1 (4)")
        self.assertEquals(str(v.question.options_ordering.all()[0]),
                          "prueba de ordenacion (5)")
        self.assertEquals(str(candidate1), "test (21) - AN - H - PC")
        self.assertEquals(str(v1), "VotacionRO")
        self.assertEquals(str(v2), "VotacionM")