def init_app(): try: app.logger.debug("Adding users") load_users() app.logger.debug("Adding voters") for box in BOXES: app.logger.debug(f"Processing box: {box}") box_obj = Box.objects(number=box).first() if not box_obj: box_obj = Box(number=box) load_votes_matrix(box_obj) for i in range(1, 350 + 1): voter_obj = Voter.objects(order=i, box=box_obj).first() if not voter_obj: circuit_obj = Circuit.objects(name=CIRCUIT).first() if not circuit_obj: circuit_obj = Circuit(name=CIRCUIT).save() try: Voter( order=i, box=box_obj, circuit=circuit_obj, ).save() except mongoengine.errors.NotUniqueError: continue except Exception: app.logger.exception("There was something wrong with init.") app.logger.debug("Done init")
def pdf_to_db(filename): def handle_voting_place(electoral_zone_code, county_code, county_name, place_code, place_name): place_db_id = place_cache.get( f"{electoral_zone_code}#{county_code}#{place_code}") if not place_db_id: county_db_id = county_cache.get( f"{electoral_zone_code}#{county_code}") if not county_db_id: electoral_zone_db_id = electoral_zone_cache.get( electoral_zone_code) if not electoral_zone_db_id: electoral_zone = ElectoralZone.create( code=electoral_zone_code) electoral_zone_cache.update( {electoral_zone_code: electoral_zone.id}) electoral_zone_db_id = electoral_zone.id county = County.create(electoral_zone_id=electoral_zone_db_id, code=county_code, name=county_name) county_cache.update( {f"{electoral_zone_code}#{county_code}": county.id}) county_db_id = county.id place = VotingPlace.create(county_id=county_db_id, name=place_name) place_cache.update({ f"{electoral_zone_code}#{county_code}#{place_code}": place.id }) def handle_voter(name, birthdate, voter_id, section_code, place_code, county_code, electoral_zone_code): section_db_id = section_cache.get( f"{electoral_zone_code}#{county_code}#{place_code}#{section_code}") if not section_db_id: place_db_id = place_cache.get( f"{electoral_zone_code}#{county_code}#{place_code}") section = VotingSection.create(voting_place_id=place_db_id, code=section_code) section_cache.update({ f"{electoral_zone_code}#{county_code}#{place_code}#{section_code}": section.id }) section_db_id = section.id voters.append({ "voting_section_id": section_db_id, "name": name.upper(), "birthdate": birthdate, "voter_id": voter_id }) electoral_zone_cache = {} county_cache = {} place_cache = {} section_cache = {} voters = [] transform(filename, handle_voting_place, handle_voter) db_batch_size = 100 with db.atomic(): for idx in range(0, len(voters), db_batch_size): Voter.insert_many(voters[idx:idx + db_batch_size]).execute()
def insert_or_update_voter(voter_id: int, voter_name: str): try: voter = Voter.get_by_id(voter_id) except Voter.DoesNotExist: voter = Voter.create(id=voter_id, name=voter_name) return voter if voter.name != voter_name: voter.name = voter_name voter.save() return voter
def test_insert3(self): o1 = Voter(user_id=1, votation_id=self.__votation__.votation_id, voted=0) o2 = Voter(user_id=1, votation_id=self.__votation__.votation_id, voted=0) voter_dao.delete_dto(o1) self.assertTrue(voter_dao.insert_dto(o1)) #self.assertFalse(voter_dao.insert_dto(o2)) this don't thrown errors in sqlalchemy voter_dao.delete_dto(o1)
def test_update_dto1(self): o = Voter(user_id=1, votation_id=self.__votation__.votation_id, voted=0) voter_dao.delete_dto(o) voter_dao.insert_dto(o) self.assertFalse(voter_dao.has_voted(o.user_id, o.votation_id)) o.voted = 1 voter_dao.update_dto(o) self.assertTrue(voter_dao.has_voted(o.user_id, o.votation_id)) voter_dao.delete_dto(o)
def test_count_voters2(self): o = Voter(user_id=1, votation_id=self.__votation__.votation_id, voted=0) voter_dao.insert_dto(o) self.assertEqual(0, voter_dao.count_voters(o.votation_id)) voter_dao.delete_dto(o)
def save_state(order): app.logger.debug("Saving order: %s" % order) box = Box.objects(number=request.form.get("box")).first() voter = Voter.objects(order=order, box=box).first() voter.update(status=request.form.get("intencion"), last_updated=datetime.now()) return Response(status=200)
def test_is_voter(self): o = Voter(user_id=2, votation_id=self.__votation__.votation_id, voted=0) voter_dao.delete_dto(o) voter_dao.insert_dto(o) self.assertTrue(voter_dao.is_voter(o.votation_id, o.user_id)) voter_dao.delete_dto(o)
def test_set_voted_with_list(self): # insert a voter o = Voter(user_id=2, votation_id=self.__votation_list__.votation_id, voted=0) self.assertFalse(voter_dao.is_voter(o.votation_id, o.user_id)) voter_dao.insert_dto(o) self.assertTrue(voter_dao.is_voter(o.votation_id, o.user_id)) # run set_voted() self.assertFalse(voter_dao.has_voted(o.user_id, o.votation_id)) self.assertTrue(voter_dao.set_voted(o.user_id, o.votation_id)) self.assertTrue(voter_dao.has_voted(o.user_id, o.votation_id))
def set_voted(user_id, votation_id): """insert or update the voter record""" result = False if has_voted(user_id, votation_id): return True voter1 = db.session.query(Voter).filter(Voter.votation_id == votation_id, Voter.user_id == user_id).first() if voter1: voter1.voted = 1 result = True else: o = Voter(votation_id=votation_id, user_id=user_id, voted=1) result = insert_dto(o) return result
def export(): boxes = Box.objects() padron = [] for box in boxes: voters = Voter.objects(box=box) for voter in voters: padron.append([ voter.order, voter.status, box.number, ]) return render_template( 'export.html', padron=padron, )
def setUp(self): self.__votation__ = Votation( \ votation_description = 'Simple Votation with voters for vote test ' + str(random.randint(0,50000)) , \ description_url = "" , \ votation_type = votation_dao.TYPE_SIMPLE_MAJORITY , \ promoter_user_id = 1 , \ begin_date = datetime(2018,1,1) , \ end_date = datetime(2018,1,15) , \ votation_status = 2 , \ list_voters = 1) self.assertTrue(votation_dao.insert_votation_dto(self.__votation__)) o1 = Option(votation_id=self.__votation__.votation_id, \ option_name = 'test.option1') self.assertTrue(option_dao.insert_dto(o1)) o2 = Option(votation_id=self.__votation__.votation_id, \ option_name = 'test.option2') self.assertTrue(option_dao.insert_dto(o2)) o3 = Option(votation_id=self.__votation__.votation_id, \ option_name = 'test.option3') self.assertTrue(option_dao.insert_dto(o3)) self.__option1 = o1 self.__option2 = o2 self.__option3 = o3 self.assertIsNotNone(o1.option_id) self.assertIsNotNone(o2.option_id) self.assertIsNotNone(o3.option_id) # set juds j1 = Judgement(votation_id = self.__votation__.votation_id, \ jud_value = 0, jud_name = "bad") j2 = Judgement(votation_id = self.__votation__.votation_id, \ jud_value = 1, jud_name = "medium") j3 = Judgement(votation_id = self.__votation__.votation_id, \ jud_value = 2, jud_name = "good") judgement_dao.insert_dto(j1) judgement_dao.insert_dto(j2) judgement_dao.insert_dto(j3) db.session.commit() jud_array = judgement_dao.load_judgement_by_votation( self.__votation__.votation_id) self.__jud1 = jud_array[0] self.__jud2 = jud_array[1] self.__jud3 = jud_array[2] voter1 = Voter(user_id=1, votation_id=self.__votation__.votation_id, voted=0) voter_dao.insert_dto(voter1) db.session.commit() return super().setUp()
def insert_voters_array(votation_id, ar): """returns number of inserted rows""" count = 0 for user_name in ar: u = user.load_user_by_username(user_name) if u: n = db.session.query(Voter).filter( Voter.user_id == u.user_id, Voter.votation_id == votation_id).count() if n == 0: o = Voter(votation_id=votation_id, user_id=u.user_id, voted=0) if voter_dao.insert_dto(o): count += 1 if count > 0: db.session.commit() return count
def padron_box(box_number): box_permission = Permission(RoleNeed(str(box_number))) app.logger.info("Entered Padron for box: %s" % box_number) _roles = get_current_user_roles(current_user) app.logger.debug("Roles: %s" % _roles) app.logger.debug("box_permission: %s" % box_permission) pager = Pager(int(box_number), [int(role) for role in _roles]) pages = pager.get_pages() box = Box.objects(number=box_number).first() _padron = Voter.objects(box=box) if box_permission.can(): return render_template( 'padron.html', padron=_padron, pages=pages, box_number=box_number, ) else: abort(403)
def summary(): _filter = get_current_user_roles(current_user) boxes = Box.objects(number__in=_filter).order_by("-number") custom_boxes = [] for box in boxes: custom_box = { 'number': box.number, "recurrido": Voter.objects(box=box, status=2).count(), "voto": Voter.objects(box=box, status=3).count(), "ausentes": Voter.objects(box=box, status=4).count() } custom_boxes.append(custom_box) votos_recurrido = Voter.objects(status=2, box__in=boxes).count() votos_ns_nc = Voter.objects(status=3, box__in=boxes).count() votos_ausentes = Voter.objects(status=4, box__in=boxes).count() data = [ { "intention": "Recurrido", "count": votos_recurrido }, { "intention": "Voto", "count": votos_ns_nc }, { "intention": "Ausentes", "count": votos_ausentes }, ] values = [votos_recurrido, votos_ns_nc, votos_ausentes] labels = ["Recurrido", "Voto", "Ausentes"] return render_template( 'summary.html', values=values, labels=labels, colors=COLORS, boxes=custom_boxes, data=data, )