/
updown.py
101 lines (88 loc) · 3.45 KB
/
updown.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import db
db.init()
import datetime
class User:
def __init__(self, email, user_ID, activity={}, votes={}, obselete=False):
self.email = email
self.user_ID = user_ID
self.activity = activity
self.votes = votes
self.obselete = obselete
def has_visited(self, target_path):
for this_date, activity_list in self.activity.items():
for activity_unit in activity_list:
if activity_unit[0] == target_path:
return True
return False
class Opinion:
def __init__(self, ID, text, activity, approved=None, scheduled=False, reserved_for=None, bill='', resolved=False):
self.ID = ID
self.text = text
self.activity = activity
self.approved = approved
self.scheduled = scheduled
self.reserved_for = reserved_for
self.bill = bill
self.resolved = resolved
def count_votes(self):
# prepare verified accounts set
verified_accounts = set()
for this_cookie, this_secure in db.cookie_database.items():
if this_secure[1] == 'verified':
verified_accounts.add(this_secure[0])
# count the votes
up_votes = 0
down_votes = 0
abstains = 0
for this_user_id in verified_accounts:
user = db.user_ids[this_user_id]
if str(self.ID) in user.votes:
this_vote = user.votes[str(self.ID)][-1][0]
if this_vote == 'up':
up_votes += 1
elif this_vote == 'down':
down_votes += 1
elif this_vote == 'abstain':
abstains += 1
else:
raise ValueError(f'Found a vote other than up, down, or abstain: {this_vote}')
return up_votes, down_votes, abstains
def care_agree_percent(self, return_all=False):
up, down, abstain = self.count_votes()
care = 0
agree = 0
if up + down + abstain > 0:
care = (up + down) / (up + down + abstain) * 100
if up + down > 0:
agree = up / (up + down) * 100
if return_all:
return (up, down, abstain), (care, agree)
else:
return care, agree
def is_after_voting(self):
today_date = datetime.date.today()
see_day = today_date - datetime.timedelta(days=today_date.weekday())
return self.scheduled and self.activity[2][0][0] < today_date and str(self.ID) not in db.opinions_calendar.get(str(see_day), set())
def rankings(self, return_all=False):
if return_all:
counts, percentages = self.care_agree_percent(return_all=True)
my_care, my_agree = percentages
else:
my_care, my_agree = self.care_agree_percent()
my_overall = my_care * my_agree
care_rank = 1
agree_rank = 1
overall_rank = 1
for opinion in db.opinions_database.values():
this_care, this_agree = opinion.care_agree_percent()
this_overall = this_care * this_agree
if this_care > my_care:
care_rank += 1
if this_agree > my_agree:
agree_rank += 1
if this_overall > my_overall:
overall_rank += 1
if return_all:
return counts, (my_care, my_agree), (care_rank, agree_rank, overall_rank)
else:
return care_rank, agree_rank, overall_rank