def show_poll_votes_stats(self): poll_options = DbInteractions.show_poll_options(self.poll_id) stats = DbInteractions.show_vote_stats(self.poll_id) poll_option_list = [[option[0], option[1]] for option in poll_options] stats_dict = {stat[0]: [stat[1], stat[2]] for stat in stats} for poll_option in poll_option_list: if poll_option[0] not in stats_dict.keys(): stats_dict[poll_option[0]] = [0, 0] stats_dict[poll_option[0]].append(poll_option[1]) return stats_dict
def select_by_poll_id(cls, poll_id) -> List["Option"]: options_tuple = DbInteractions.select_options_by_poll_id(poll_id) options_list = [ Option(options_params[2], options_params[1], options_params[0]) for options_params in options_tuple ] return options_list
def select_random_winner(cls, poll_id, start_date_naive, end_date_naive, timezone_): users_timezone = pytz.timezone(timezone_) start_date_aware = users_timezone.localize(start_date_naive) end_date_aware = users_timezone.localize(end_date_naive) start_date_as_utc = start_date_aware.astimezone(pytz.utc) end_date_as_utc = end_date_aware.astimezone(pytz.utc) start_utc_timestamp = start_date_as_utc.timestamp() end_utc_timestamp = end_date_as_utc.timestamp() random_winner = DbInteractions.draw_random_winner(poll_id, start_utc_timestamp, end_utc_timestamp) option_id, username, utc_timestamp, _, option_text, poll_id, _, poll_question, owner = random_winner utc_time_of_vote_naive = datetime.datetime.utcfromtimestamp(utc_timestamp) utc_timezone = pytz.timezone("UTC") utc_time_of_vote_aware = utc_timezone.localize(utc_time_of_vote_naive) selectors_time_of_vote = utc_time_of_vote_aware.astimezone(users_timezone) return poll_id, poll_question, username, option_text, selectors_time_of_vote
def show_poll_ids(cls): return DbInteractions.show_poll_ids()
def select_by_id(cls, poll_id) -> "Poll": poll_question_tuple = DbInteractions.select_poll_by_id(poll_id) poll = Poll(poll_question_tuple[1], poll_question_tuple[2], poll_question_tuple[0]) return poll
def print_all_polls(cls) -> List["Poll"]: all_polls_tuple = DbInteractions.select_all_polls() all_polls_list = [Poll(poll_params[1], poll_params[2], poll_params[0]) for poll_params in all_polls_tuple] return all_polls_list
def save_to_db(self): try: self.poll_id = DbInteractions.save_poll_to_db(self.poll_question, self.owner) return self.poll_id except Exception as e: print(e)
def save_to_db(self): try: DbInteractions.save_option_to_db(self.option_text, self.poll_id) except Exception as e: print(e)
def save_to_db(self): DbInteractions.save_vote_to_db(self.users_vote, self.users_name, self.timestamp_)