user_perms not hard coded

This commit is contained in:
iou1name 2019-09-26 13:33:06 -04:00
parent 83ba097d6e
commit 0cad501405
3 changed files with 16 additions and 4 deletions

View File

@ -35,7 +35,7 @@ async def index(request):
request['session']['id']) request['session']['id'])
if request['session']['admin']: if request['session']['admin']:
apps = await conn.fetch( apps = await conn.fetch(
"SELECT name FROM app_info") "SELECT id, name FROM app_info")
user_perms = await conn.fetch( user_perms = await conn.fetch(
"SELECT user_info.id, user_info.username, app_user.app_id " "SELECT user_info.id, user_info.username, app_user.app_id "
"FROM user_info LEFT JOIN app_user " "FROM user_info LEFT JOIN app_user "
@ -49,10 +49,11 @@ async def index(request):
request['session']['id']) request['session']['id'])
if request['session']['admin']: if request['session']['admin']:
apps = [app['name'] for app in apps]
users = defaultdict(lambda: [False]*len(apps)) users = defaultdict(lambda: [False]*len(apps))
for user_perm in user_perms: for user_perm in user_perms:
users[user_perm['username']][user_perm['app_id']-1] = True index = tools.find_dict(apps, 'id', user_perm['app_id'])
if index != -1:
users[user_perm['username']][index] = True
users_json = json.dumps(users) users_json = json.dumps(users)
return render_template("index.html", request, locals()) return render_template("index.html", request, locals())

View File

@ -38,7 +38,7 @@
<tr> <tr>
<th>User</th> <th>User</th>
{% for app in apps %} {% for app in apps %}
<th>{{ app }}</th> <th>{{ app['name'] }}</th>
{% endfor %} {% endfor %}
<th></th> <th></th>
</tr> </tr>

View File

@ -84,3 +84,14 @@ async def validate_register(request, form):
errors['email'].append("Email already in use.") errors['email'].append("Email already in use.")
return errors return errors
def find_dict(lst, key, value):
"""
Returns the index of the dictionary in the given `lst` which
has d[`key`] == `value`.
"""
for i, d in enumerate(lst):
if d[key] == value:
return i
return -1