/
explorer.py
212 lines (167 loc) · 5.93 KB
/
explorer.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
import json
import os
from datetime import datetime
from eospy.cleos import Cleos
from flask import Flask, render_template, url_for, redirect
from flask_cdn import CDN
app = Flask(__name__)
app.config['CDN_DOMAIN'] = 'static.waxbadges.com'
app.config['CDN_TIMESTAMP'] = False
CDN(app)
CONTRACT_ACCOUNT = 'waxbadgesftw'
CONTRACT_SCOPE = CONTRACT_ACCOUNT
ECOSYSTEMS_TABLE = 'ecosystems'
MYWAXBADGES_TABLE = 'mywaxbadges'
"""
Cleos quick reference
get_table(self, code, scope, table, index_position='',key_type='', lower_bound='', upper_bound='', limit=10, timeout=30)
"""
@app.route('/favicon.ico')
def favicon():
return redirect("https://explorer.waxbadges.com/static/image/favicon/favicon.ico", code=302)
@app.route('/', methods=['GET'])
def index():
cleos = Cleos(url='https://chain.wax.io')
ecosystems = cleos.get_table(
code=CONTRACT_ACCOUNT,
scope=CONTRACT_SCOPE,
table=ECOSYSTEMS_TABLE,
limit=100,
timeout=30
)
return render_template(
'index.html',
ecosystems=ecosystems.get('rows', [])
)
@app.route('/ecosys/<key>', methods=['GET'])
def get_ecosystem(key):
cleos = Cleos(url='https://chain.wax.io')
ecosystems = cleos.get_table(
code=CONTRACT_ACCOUNT,
scope=CONTRACT_SCOPE,
table=ECOSYSTEMS_TABLE,
lower_bound=key,
upper_bound=key,
limit=1,
timeout=30
)
ecosystem = ecosystems.get('rows', [])[0]
print(ecosystem)
return render_template(
'ecosys.html',
ecosystem=ecosystem
)
@app.route('/ach/<ecosys_key>/<cat_key>/<key>', methods=['GET'])
def get_achievement(ecosys_key, cat_key, key):
cleos = Cleos(url='https://chain.wax.io')
ecosystems = cleos.get_table(
code=CONTRACT_ACCOUNT,
scope=CONTRACT_SCOPE,
table=ECOSYSTEMS_TABLE,
lower_bound=ecosys_key,
upper_bound=ecosys_key,
limit=1,
timeout=30
)
ecosystem = ecosystems.get('rows', [])[0]
achievement = ecosystem.get('categories')[int(cat_key)].get('achievements')[int(key)]
achievement['key'] = int(key)
if achievement['maxquantity'] == 0:
achievement['num_available'] = 'unlimited'
achievement['maxquantity'] = 'unlimited'
else:
achievement['num_available'] = achievement['maxquantity'] - len(achievement.get('usergrants', []))
users = []
for usergrant in achievement.get('usergrants'):
user = ecosystem.get('users')[usergrant.get('user_id')]
if not user.get('avatarurl').startswith('http'):
user['avatarurl'] = ecosystem.get('baseasseturl') + "/" + user.get('avatarurl')
user['user_id'] = usergrant.get('user_id')
print(user)
users.append({
"user": user,
"timestamp": datetime.utcfromtimestamp(usergrant.get('timestamp')),
})
return render_template(
'ach.html',
ecosystem=ecosystem,
category_id=cat_key,
achievement=achievement,
usergrants=users
)
@app.route('/poa/<ecosys_key>/<cat_key>/<ach_key>/<user_key>', methods=['GET'])
def get_proofofachievement(ecosys_key, cat_key, ach_key, user_key):
cleos = Cleos(url='https://chain.wax.io')
ecosystems = cleos.get_table(
code=CONTRACT_ACCOUNT,
scope=CONTRACT_SCOPE,
table=ECOSYSTEMS_TABLE,
lower_bound=ecosys_key,
upper_bound=ecosys_key,
limit=1,
timeout=30
)
ecosystem = ecosystems.get('rows', [])[0]
achievement = ecosystem.get('categories')[int(cat_key)].get('achievements')[int(ach_key)]
achievement['key'] = int(ach_key)
if achievement['maxquantity'] == 0:
achievement['maxquantity'] = 'unlimited'
user = ecosystem.get('users')[int(user_key)]
user['key'] = int(user_key)
if not user.get('avatarurl').startswith('http'):
user['avatarurl'] = ecosystem.get('baseasseturl') + "/" + user.get('avatarurl')
for userachievement in user.get('userachievements'):
if userachievement.get('category_id') == int(cat_key) and userachievement.get('achievement_id') == int(ach_key):
timestamp = datetime.utcfromtimestamp(userachievement.get('timestamp'))
break
return render_template(
'poa.html',
ecosystem=ecosystem,
category_id=cat_key,
achievement=achievement,
user=user,
timestamp=timestamp
)
@app.route('/user/<ecosys_key>/<key>', methods=['GET'])
def get_user(ecosys_key, key):
cleos = Cleos(url='https://chain.wax.io')
ecosystems = cleos.get_table(
code=CONTRACT_ACCOUNT,
scope=CONTRACT_SCOPE,
table=ECOSYSTEMS_TABLE,
lower_bound=ecosys_key,
upper_bound=ecosys_key,
limit=1,
timeout=30
)
ecosystem = ecosystems.get('rows', [])[0]
user = ecosystem.get('users')[int(key)]
# Get all of this User's granted Achievements
achievements = []
grants_categories = user.bycategory
for (category_id, achievements_list) in grants_categories.iteritems():
userachievements = achievements_list.userachievements
for userachievement in userachievements:
achievements.append((ecosystem.get('categories')[category_id].get('achievements')[userachievement.get('achievement_id')], userachievement.get('timestamp')))
return render_template(
'user.html',
ecosystem=ecosystem,
achievements=achievements
)
@app.route('/find/user/<ecosys_key>/<userid>', methods=['GET'])
def find_user(ecosys_key, userid):
cleos = Cleos(url='https://chain.wax.io')
ecosystems = cleos.get_table(
code=CONTRACT_ACCOUNT,
scope=CONTRACT_SCOPE,
table=ECOSYSTEMS_TABLE,
lower_bound=ecosys_key,
upper_bound=ecosys_key,
limit=1,
timeout=30
)
ecosystem = ecosystems.get('rows', [])[0]
for (key, user) in enumerate(ecosystem.users):
if user.userid == userid:
return key
return None