def validate_dto(v): """Validate data for writing in DB. Returns (True/False, "Error message")""" result = True errorMessage = "Data validated" if result: if user.load_user_by_id(v.promoter_user.user_id) == None: result = False errorMessage = "Promoter user id not valid" if result: if len(v.votation_description.strip()) == 0: result = False errorMessage = "Description is mandatory" if result: if not validate_string_date(v.begin_date): result = False errorMessage = "Begin date not valid" if result: if not validate_string_date(v.end_date): result = False errorMessage = "End date not valid" if result: if v.end_date < v.begin_date: result = False errorMessage = "Begin and End dates are not in sequence" if result: if v.votation_type != TYPE_RANDOM and v.votation_type != TYPE_MAJORITY: result = False errorMessage = "Votation Type not valid" if result: if v.votation_status < STATUS_WAIT_FOR_CAND_AND_GUAR or v.votation_status > STATUS_FAILED: result = False errorMessage = "Status not valid: {}".format(v.votation_status) return (result, errorMessage)
def load_guarantor(votation_id, user_id): """Returns a guarantor_dto""" conn = dbmanager.get_connection() c = conn.cursor() c.execute("select * from guarantor c where c.votation_id = ? and user_id = ?", (votation_id,user_id) ) row = c.fetchone() g = guarantor_dto() g.votation_id = row['votation_id'] g.hash_ok = row['hash_ok'] g.passphrase_ok = row['passphrase_ok'] g.order_n = row['order_n'] g.u = user.load_user_by_id(row['user_id']) c.close() conn.close() return g
def load_candidate(votation_id, user_id): """Returns a candidate_dto """ conn = dbmanager.get_connection() c = conn.cursor() c.execute( "select * from candidate c where c.votation_id = ? and c.user_id = ?", (votation_id, user_id)) row = c.fetchone() o = candidate_dto() o.votation_id = row['votation_id'] o.passphrase_ok = row['passphrase_ok'] o.order_n = row['order_n'] o.u = user.load_user_by_id(row['user_id']) c.close() conn.close() return o
def load_guarantor_by_votation(votation_id): """Returns a guarantor_dto array""" ar = [] conn = dbmanager.get_connection() c = conn.cursor() c.execute("select * from guarantor c where c.votation_id = ? order by order_n", (votation_id,) ) row = c.fetchone() while row: g = guarantor_dto() g.votation_id = row['votation_id'] g.hash_ok = row['hash_ok'] g.passphrase_ok = row['passphrase_ok'] g.order_n = row['order_n'] g.u = user.load_user_by_id(row['user_id']) ar.append(g) row = c.fetchone() c.close() conn.close() return ar
def load_votation_by_id(votation_id): """Returns a votation_dto object or None""" v = None conn = dbmanager.get_connection() c = conn.cursor() c.execute("select * from votation where votation_id = ?", (votation_id,)) row = c.fetchone() if row: v = votation_dto() v.votation_id = row['votation_id'] v.promoter_user = user.load_user_by_id( row['promoter_user_id'] ) v.votation_description = row['votation_description'] v.begin_date = row['begin_date'] v.end_date = row['end_date'] v.votation_type = row['votation_type'] v.votation_status = row['votation_status'] c.close() conn.close() return v
def load_candidate_by_votation(votation_id): """Returns a candidate_dto array""" ar = [] conn = dbmanager.get_connection() c = conn.cursor() c.execute( "select * from candidate c where c.votation_id = ? order by order_n", (votation_id, )) row = c.fetchone() while row: o = candidate_dto() o.votation_id = row['votation_id'] o.passphrase_ok = row['passphrase_ok'] o.order_n = row['order_n'] o.u = user.load_user_by_id(row['user_id']) ar.append(o) row = c.fetchone() c.close() conn.close() return ar
def validate_dto(o): """Validate data for writing in DB. Returns error code, 0 on success""" result = 0 if result == 0: if o.u.user_id == None: result = 1 if result == 0: if o.votation_id == None: result = 2 if result == 0: u = user.load_user_by_id(o.u.user_id) if u == None: result = 3 if result == 0: u = votation.load_votation_by_id(o.votation_id) if u == None: result = 4 if result == 0: if check_for_duplicate(o): result = 5 return result
def load_votations(): """Returns a votation_dto array""" ar = [] conn = dbmanager.get_connection() c = conn.cursor() c.execute("select * from votation") row = c.fetchone() while row: v = votation_dto() v.votation_id = row['votation_id'] v.promoter_user = user.load_user_by_id( row['promoter_user_id'] ) v.votation_description = row['votation_description'] v.begin_date = row['begin_date'] v.end_date = row['end_date'] v.votation_type = row['votation_type'] v.votation_status = row['votation_status'] ar.append(v) row = c.fetchone() c.close() conn.close() return ar
def save_votes(user_id, vote_key, votation_id, vote_array): """ Save a vote, one rank number per option id. The vote in vote_array[0] is for the first option. The vote in vote_array[1] is for the second option, and so on. Every option need a vote. Also, the voter is set as voted. """ u = user.load_user_by_id(user_id) if not u: return False if voter_dao.has_voted(user_id, votation_id): ar = vote_dao.load_vote_by_key(vote_key) if len(ar) == 0: return False vote_dao.delete_votes_by_key(vote_key) if vote_dao.save_vote(votation_id=votation_id, vote_key=vote_key, array_judgements=vote_array): if voter_dao.set_voted(user_id, votation_id): db.session.commit() return True db.session.rollback() return False