forked from MISP/misp-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
give_honors_to_org.py
executable file
·219 lines (185 loc) · 7.48 KB
/
give_honors_to_org.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env python3.5
import configparser
import datetime
import json
import os
import sys
import time
import redis
import util
from helpers import contributor_helper
ONE_DAY = 60*60*24
configfile = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'config/config.cfg')
cfg = configparser.ConfigParser()
cfg.read(configfile)
serv_log = redis.StrictRedis(
host=cfg.get('RedisGlobal', 'host'),
port=cfg.getint('RedisGlobal', 'port'),
db=cfg.getint('RedisLog', 'db'))
serv_redis_db = redis.StrictRedis(
host=cfg.get('RedisGlobal', 'host'),
port=cfg.getint('RedisGlobal', 'port'),
db=cfg.getint('RedisDB', 'db'))
CHANNEL_LASTAWARDS = cfg.get('RedisLog', 'channelLastAwards')
chelper = contributor_helper.Contributor_helper(serv_redis_db, cfg)
def publish_log(zmq_name, name, content, channel):
to_send = { 'name': name, 'data': json.dumps(content), 'zmqName': zmq_name }
serv_log.publish(channel, json.dumps(to_send))
def printOrgInfo(org):
org_pnts = chelper.getOrgContributionTotalPoints(org)
org_c_rank = chelper.getOrgContributionRank(org)
org_c_status = chelper.getCurrentContributionStatus(org)
org_honor_badge = chelper.getOrgHonorBadges(org)
org_trophy = chelper.getOrgTrophies(org)
os.system('clear')
print()
print("Organisation points: {}".format(org_pnts))
print("Organisation contribution rank: {}".format(org_c_status['rank']))
print('''
Organisation contribution rank:
-------------------------------''')
for rank in range(1, chelper.org_rank_maxLevel+1):
acq = 'x' if org_c_status['status'][rank] == 1 else ' '
print("{}.\t[{}]\t{}\t{}".format(rank, acq, chelper.org_rank_requirement_pnts[rank], chelper.org_rank_requirement_text[rank]))
print()
print('''
Organisation honor badges:
--------------------------''')
for badgeNum, text in chelper.org_honor_badge_title.items():
acq = 'x' if badgeNum in org_honor_badge else ' '
print("{}.\t[{}]\t{}".format(badgeNum, acq, text))
print()
print('''
Organisation trophy:
--------------------------''')
for dic in org_trophy:
categ = dic['categ']
trophyRank = dic['trophy_true_rank']
trophyPnts = dic['trophy_points']
print("{}\t{} [{}]".format(categ, trophyRank, trophyPnts))
print()
def main():
if len(sys.argv) > 1:
org = sys.argv[1]
else:
org = input('Enter the organisation name: ')
printOrgInfo(org)
ContributionStatus = chelper.getCurrentContributionStatus(org)
OLD_org_c_status = ContributionStatus['status']
OLD_org_honor_badge = chelper.getOrgHonorBadges(org)
OLD_org_trophy = chelper.getOrgTrophies(org)
# ranks
while True:
org_pnts = chelper.getOrgContributionTotalPoints(org)
org_c_rank = chelper.getOrgContributionRank(org)
org_c_status = chelper.getCurrentContributionStatus(org)
org_honor_badge = chelper.getOrgHonorBadges(org)
org_trophy = chelper.getOrgTrophies(org)
userRep = input("Enter the organisation RANK to give/remove to {} (<ENTER> to finish): ".format(org))
if userRep == '':
break
else:
# validate input
try: #not int
rankNum = int(userRep)
except:
print('Not an integer')
continue
if rankNum < 1 or rankNum > chelper.org_rank_maxLevel:
print('Not a valid rank')
continue
if org_c_status['status'][rankNum] == 1: #remove rank
chelper.removeContribRankFromOrg(org, rankNum)
else:
chelper.giveContribRankToOrg(org, rankNum)
printOrgInfo(org)
# badges
while True:
org_pnts = chelper.getOrgContributionTotalPoints(org)
org_c_rank = chelper.getOrgContributionRank(org)
org_c_status = chelper.getCurrentContributionStatus(org)
org_honor_badge = chelper.getOrgHonorBadges(org)
org_trophy = chelper.getOrgTrophies(org)
userRep = input("Enter the organisation BADGE to give/remove to {} (<ENTER> to finish): ".format(org))
if userRep == '':
break
else:
# validate input
try: #not int
badgeNum = int(userRep)
except:
print('Not an integer')
continue
if badgeNum < 1 and badgeNum > chelper.honorBadgeNum:
print('Not a valid rank')
continue
if badgeNum in org_honor_badge: #remove badge
chelper.removeBadgeFromOrg(org, badgeNum)
else:
chelper.giveBadgeToOrg(org, badgeNum)
printOrgInfo(org)
# trophy
while True:
org_pnts = chelper.getOrgContributionTotalPoints(org)
org_c_rank = chelper.getOrgContributionRank(org)
org_c_status = chelper.getCurrentContributionStatus(org)
org_honor_badge = chelper.getOrgHonorBadges(org)
org_trophy = chelper.getOrgTrophies(org)
print()
for i, categ in enumerate(chelper.categories_in_trophy):
print("{}. {}".format(i, categ))
userCateg = input("Enter the CATEGORY in which to add/remove trophy points: ")
if userCateg == '':
break
try: #not int
userCateg = int(userCateg)
except:
print('Not an integer')
continue
if userCateg < 1 and userCateg > len(chelper.categories_in_trophy):
print('Not a valid rank')
continue
categ = chelper.categories_in_trophy[userCateg]
userRep = input("Enter the TROPHY POINTS to give/remove to {} (<ENTER> to finish) in {}: ".format(org, categ))
if userRep == '':
break
else:
# validate input
try: #not int
trophyPnts = int(userRep)
except:
print('Not an integer')
continue
chelper.giveTrophyPointsToOrg(org, categ, trophyPnts)
printOrgInfo(org)
now = datetime.datetime.now()
nowSec = int(time.time())
ContributionStatus = chelper.getCurrentContributionStatus(org)
NEW_org_c_status = ContributionStatus['status']
NEW_org_honor_badge = chelper.getOrgHonorBadges(org)
NEW_org_trophy = chelper.getOrgTrophies(org)
awards_given = []
for i in NEW_org_c_status.keys():
if OLD_org_c_status[i] < NEW_org_c_status[i] and i != ContributionStatus['rank']:
awards_given.append(['contribution_status', ContributionStatus['rank']])
for badgeNum in NEW_org_honor_badge:
if badgeNum not in OLD_org_honor_badge:
awards_given.append(['badge', badgeNum])
temp = {}
for item in OLD_org_trophy:
categ = item['categ']
rank = item['trophy_true_rank']
temp[categ] = rank
for item in NEW_org_trophy:
categ = item['categ']
rank = item['trophy_true_rank']
if rank > temp[categ]:
awards_given.append(['trophy', [categ, rank]])
for award in awards_given:
# update awards given
serv_redis_db.zadd('CONTRIB_LAST_AWARDS:'+util.getDateStrFormat(now), {json.dumps({'org': org, 'award': award, 'epoch': nowSec }): nowSec})
serv_redis_db.expire('CONTRIB_LAST_AWARDS:'+util.getDateStrFormat(now), ONE_DAY*7) #expire after 7 day
# publish
publish_log('GIVE_HONOR_ZMQ', 'CONTRIBUTION', {'org': org, 'award': award, 'epoch': nowSec }, CHANNEL_LASTAWARDS)
if __name__ == '__main__':
main()