def get(electionsList, firstName, secondName): # get the candidate with given id session = Session1() candidate = session.query(Candidate) \ .filter_by(electionList_id=electionsList, firstName=firstName, secondName=secondName).scalar() session.close() return candidate
def create(electedOrgan, votesToUse): # create a new election with given electedOrgan, votesToUse and state "inEdition" session = Session1() new_election = Elections(electedOrgan, votesToUse) session.add(new_election) session.commit() session.close()
def register(id, elections_id): # add election_id and add now() as registrationDate session = Session1() election = session.query(ElectionList).filter_by(id=id) election.update({'elections_id': elections_id, 'registrationDate': datetime.datetime.now()}) session.commit() session.close()
def get_candidates(id): # get all candidates from lists registered to given election id session = Session1() num_votes = session.query(Elections.votesToUse).filter(Elections.id == id).scalar() candidates = session.query(ElectionList.id, ElectionList.listName, Candidate.id, Candidate.firstName, Candidate.secondName)\ .join(Candidate).filter(ElectionList.elections_id == id).order_by(ElectionList.listName).all() session.close() return num_votes, candidates
def verify(code): # check if given codeToVote is correct and haven't been used already session = Session1() default = "USEDCODE" query = session.query(VotingCode).filter_by(codeToVote=code).scalar() if query is None or query.used or query.codeToVote == default: return 0 else: return 1
def close(id): # set elections_state to "closed" and return candidates with number of votes session = Session1() votes = count_votes(id) election = session.query(Elections).filter_by(id=id) election.update({'electionsState': 'closed'}) session.commit() session.close() return votes
def use(code): # mark given code as used session = Session1() default = "USEDCODE" codeToVote = session.query(VotingCode).filter_by(codeToVote=code) if codeToVote.scalar() is None: return 0 else: codeToVote.update({'used': True, 'codeToVote': default}) session.commit() return 1
def add(electionsList, firstName, secondName): # add a candidate with given electionList id, first name, second name and 0 votes session = Session1() if get(electionsList, firstName, secondName) is None: new_candidate = Candidate(electionsList, firstName, secondName) session.add(new_candidate) session.commit() session.close() return 1 else: session.close() return 0
def create(listName): # create an electionList with given name session = Session1() exists = session.query(session.query(ElectionList).filter_by(listName=listName).exists()).scalar() if (exists): session.close() return 0 else: new_list = ElectionList(listName) session.add(new_list) session.commit() session.close() return 1
def register(id, startTime, endTime): # check if any electionLists are linked to this election # add startTime, endTime and set status to "registered" # datetime format 'YYYY-MM-DD hh:mm:ss' session = Session1() election = session.query(Elections).filter_by(id=id) if (election.scalar().lists == []): session.close() return 0 else: election.update({'startTime': startTime, 'endTime': endTime, 'electionsState': 'registered'}) session.commit() session.close() return 1
def create(election_id, pesel): # create a new voting code with given election_id, pesel and unique codeToVote # doesn't check if entry exists, use get() first session = Session1() chars = string.ascii_letters + string.digits N = 8 while (True): code = ''.join(random.choice(chars) for _ in range(N)) if session.query(VotingCode).filter_by( codeToVote=code).scalar() is None: break new_code = VotingCode(election_id, pesel, code) session.add(new_code) session.commit() return new_code
def vote(electionsList, id, firstName, secondName): # add 1 vote to the candidate with given id , firstName and secondName from given election list session = Session1() candidate = session.query(Candidate) \ .filter_by(electionList_id=electionsList, id=id, firstName=firstName, secondName=secondName) if candidate is not None: if candidate.scalar().numberOfVotes is None: candidate.update({'numberOfVotes': 1}) else: candidate.update({'numberOfVotes': Candidate.numberOfVotes + 1}) session.commit() session.close() return 1 else: session.close() return 0
def get_all(): # get all candidates session = Session1() candidates = session.query(Candidate).all() session.close() return candidates
def delete(election_id, pesel): # delete the voting code with given election_id and pesel session = Session1() session.query(VotingCode).filter_by(election_id=election_id, pesel=pesel).delete() session.commit()
def count_votes(id): # get all candidates and theirs number of votes from lists registered to given election id session = Session1() votes = session.query(ElectionList.listName, Candidate.firstName, Candidate.secondName, Candidate.numberOfVotes) \ .join(Candidate).filter(ElectionList.elections_id == id).all() return votes
def delete(id): # delete the election code with given id session = Session1() session.query(Elections).filter_by(id=id).delete() session.commit() session.close()
def get(election_id, pesel): # get the voting code with given election_id and pesel session = Session1() code = session.query(VotingCode).filter_by(elections_id=election_id, pesel=pesel).scalar() return code
def get_all(): # get all voting_codes session = Session1() codes = session.query(VotingCode).all() return codes
def get_all(): # get all election Lists session = Session1() electionLists = session.query(ElectionList).all() session.close() return electionLists
def delete(id): # delete the candidate with given id session = Session1() session.query(Candidate).filter_by(id=id).delete() session.commit() session.close()
def get(elections_id, listName): # get the election List with given election_id and listName session = Session1() electionList = session.query(ElectionList).filter_by(elections_id=elections_id, listName=listName).scalar() session.close() return electionList
def delete(id): # get the election List with given id session = Session1() session.query(ElectionList).filter_by(id=id).delete() session.commit() session.close()
def get(id): # get the election with given id session = Session1() election = session.query(Elections).filter_by(id=id).scalar() session.close() return election