def place_vote(data: dict): if not 'choice_id' in data: return Response.wrong_format( {'message': 'choiceid is required for voting'}) if not 'election_round_id' in data: return Response.wrong_format( {'message': 'election_round_id required for voting'}) if not 'person_id' in data: return Response.wrong_format( {'message': 'person_id required for voting'}) try: election_round_id = int(data['election_round_id']) person_id = int(data['person_id']) choice_id = int(data['choice_id']) except: return Response.wrong_format( {"message": "ids have to be an int base 10"}) elec_round = _get_electionround_by_id(election_round_id) if not elec_round: return Response.ressource_not_found( {"message": "No electionround for this id."}) person = get_person_by_id(person_id) if person in elec_round.persons_voted: return Response.server_error({"message": "Person already voted"}) try: choice = session.query(Choice).filter_by(id=choice_id).first() session.commit() if choice is None: return Response.ressource_not_found( {"message": "No Choice with this id."}) except: print("no choice") return Response.database_error() if choice not in elec_round.choices: return Response.server_error( {"message": "Electionround has no Choice with that ID"}) try: choice.counter = choice.counter + 1 session.commit() except: return Response.database_error() try: elec_round.persons_voted.append(person) session.commit() except: return Response.database_error() return Response.ok(model_as_dict(choice))
def updatePicture(choiceid: int, data: dict) -> str: if not 'picture' in data: return Response.wrong_format({ 'updated': False, 'message': 'picture missing' }) if not data['picture'].endswith('=='): return Response.wrong_format({ 'updated': False, 'message': 'not a base64 string' }) try: choice = session.query(Choice).get(choiceid) except: return Response.database_error() if not choice: return Response.ressource_not_found({ 'id': choiceid, 'message': 'choiceid not found!' }) choice.picture = data['picture'] try: session.commit() except: return Response.database_error() return Response.ok({'id': choiceid, 'updated': True})
def update_votes(choiceId: int, data: dict) -> str: if not 'votes' in data: return Response.wrong_format({ 'updated': False, 'message': 'votes missing' }) if not type(data['votes']) == int: return Response.wrong_format({ 'id': 'error', 'message': 'Not a number' }) try: choice = session.query(Choice).get(choiceId) except: return Response.database_error() if choice: choice.counter += int(data['votes'] or 0) try: session.commit() except: return Response.database_error() return Response.ok({'id': choiceId, 'success': True}) else: return Response.ressource_not_found({ 'id': choiceId, 'message': 'choice not found' })
def delete_choice_proxy(id: int) -> bool: '''Deletes an choice proxy.''' #choice = session.query(Person).join( # has_choice_proxy_table).join(Person).filter( # has_choice_proxy_table.sender_id == id).first() try: choice = session.query(has_choice_proxy_table).filter( has_choice_proxy_table.c.sender_id == id).first() if not choice: return Response.ressource_not_found( {'message': 'senderid not found'}) receiver = session.query(Person).filter( Person.id == choice.receiver_id).first() sender = session.query(Person).filter( Person.id == choice.sender_id).first() receiver.received_proxy_vote.remove(sender) session.commit() except: return Response.database_error() return Response.ok({ "success": True, 'message': 'senderid {} deleted vote from receiverid {}'.format( sender.id, receiver.id) })
def get_all_persons_who_have_not_voted(elec_round_id: int) -> dict: '''Get all persons who have not voted Warning: This is only accurate at the time of the election round, since people can leave (altering the is_present) after the election round. ''' try: elec_round = _get_electionround_by_id(elec_round_id) if not elec_round: return Response.ressource_not_found( {"message": "No election round for this id."}) except: return Response.database_error() persons_voted = elec_round.persons_voted # Get present people try: persons_present = session.query(Person).filter( Person.is_present == True).all() session.commit() except: return Response.database_error() # Get all persons who didn't vote but are present persons_not_voted = [] for person in persons_present: if person not in persons_voted: persons_not_voted.append(person) # Create response ret = [] for person in persons_not_voted: ret.append({"id": person.id, "name": person.name}) return Response.ok(json.dumps(ret))
def create_choice_proxy(data: dict) -> bool: '''Create an proxy for an voter.''' if not 'senderid' in data: return Response.wrong_format({'message': 'senderid missing'}) if not type(data['senderid']) == int: return Response.wrong_format({'message': 'senderid: not a number'}) if not 'receiverid' in data: return Response.wrong_format({'message': 'receiverid missing'}) if not type(data['receiverid']) == int: return Response.wrong_format({'message': 'receiverid: not a number'}) if data['receiverid'] == data['senderid']: return Response.wrong_format({'message': 'receiverid equals senderid'}) try: choice = session.query(has_choice_proxy_table).filter( has_choice_proxy_table.c.sender_id == data["senderid"]).first() if choice: return Response.wrong_format({ 'message': 'senderid {} already gave their vote'.format(data['senderid']) }) receiver = session.query(Person).filter( Person.id == data['receiverid']).first() if not receiver: return Response.ressource_not_found( {'message': 'receiverid: not found'}) sender = session.query(Person).filter( Person.id == data['senderid']).first() if not sender: return Response.ressource_not_found( {'message': 'senderid: not found'}) print('line 40') receiver.received_proxy_vote.append(sender) session.commit() except Exception as e: print(e) return Response.database_error() return Response.ok({ "success": True, 'message': 'senderid {} gave vote to receiverid {}'.format( sender.id, receiver.id) })
def get_person_by_id(personid: int) -> Person: try: person = session.query(Person).filter_by(id=personid).first() session.commit() except: return Response.database_error() if person is None: return Response.ressource_not_found( {"message": "No persion for this id."}) return person
def set_vote(data: dict): '''Add a person to the as has voted to the election_round''' if not 'elec_round_id' in data: return Response.wrong_format({'message': 'elec_round_id required'}) if not 'person_id' in data: return Response.wrong_format({'message': 'person_id required'}) try: elec_round_id = int(data['elec_round_id']) person_id = int(data['person_id']) except ValueError: return Response.wrong_format( {"message": "ids have to be an int (base 10)."}) # Get election_round try: elec_round = _get_electionround_by_id(elec_round_id) if not elec_round: return Response.ressource_not_found( {"message": "No electionround for this id."}) except: return Response.database_error() # Get person try: person = session.query(Person).filter_by(id=person_id).first() session.commit() if person is None: return Response.ressource_not_found( {"message": "No persion for this id."}) # Add person to election_round elec_round.persons_voted.append(person) session.commit() except: return Response.database_error() return Response.ok({"message": "OK"})
def delete_choice(choiceid: int) -> str: try: choice = session.query(Choice).get(choiceid) except: return Response.database_error() if not choice: return Response.ressource_not_found({ 'id': choiceid, 'message': 'choiceid not found!' }) session.delete(choice) try: session.commit() except: return Response.database_error() return Response.ok(json.dumps({'id': choiceid, 'deleted': True}))
def get_all_persons_who_voted(elec_round_id: int): '''Return all persons as dict who have already participated in an election round.''' try: elec_round = _get_electionround_by_id(elec_round_id) if not elec_round: return Response.ressource_not_found( {"message": "No election round for this id."}) except: return Response.database_error() # Build and return dict ret = [] for person in elec_round.persons_voted: ret.append({"id": person.id, "name": person.name}) return Response.ok(json.dumps(ret))
def _get_electionround_by_id(elec_round_id: int) -> ElectionRound: '''get election round by id and handle errors''' # Elec_round_id should be int try: elec_round_id = int(elec_round_id) except ValueError: return Response.wrong_format( {"message": "elec_round_id has to be an int (base 10)."}) # Get ElectionRound object from the DB. try: elec_round = session.query(ElectionRound).filter_by( id=elec_round_id).first() session.commit() except: return Response.database_error() # Handle invalid election round if elec_round is None: return Response.ressource_not_found( {"message": "No electionround for this id."}) return elec_round
def delete_person(userid: int): print(type(userid)) if not userid: return Response.wrong_format({ 'updated': False, 'message': 'userid missing' }) try: person = session.query(Person).filter_by(id=userid).first() except: return Response.database_error() if not person: return Response.ressource_not_found({"message": "userid not found!"}) session.delete(person) try: session.commit() except: Response.database_error() return Response.ok({"userid": userid, "message": "deleted"})