Skip to content

Commit

Permalink
Rename /users to /admin; properly index entries; show a program count.
Browse files Browse the repository at this point in the history
  • Loading branch information
fpereiro committed Feb 3, 2021
1 parent bb6063c commit 9aa7213
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def main_page(page):
lang = requested_lang()
effective_lang = lang

if page in ['signup', 'login', 'my-profile', 'recover', 'reset', 'users']:
if page in ['signup', 'login', 'my-profile', 'recover', 'reset', 'admin']:
return auth_templates(page, lang, render_main_menu(page), request)

if page == 'programs':
Expand Down
13 changes: 7 additions & 6 deletions auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import re
import urllib
from flask import request, make_response, jsonify, redirect, render_template
from utils import type_check, object_check, timems, times, db_get, db_set, db_del, db_del_many, db_scan
from utils import type_check, object_check, timems, times, db_get, db_set, db_del, db_del_many, db_scan, db_describe
import datetime
from functools import wraps
from config import config
Expand Down Expand Up @@ -417,7 +417,7 @@ def auth_templates (page, lang, menu, request):
return render_template ('profile.html', lang=lang, auth=TRANSLATIONS.data [lang] ['Auth'], menu=menu, username=current_user (request) ['username'], current_page='my-profile')
if page in ['signup', 'login', 'recover', 'reset']:
return render_template (page + '.html', lang=lang, auth=TRANSLATIONS.data [lang] ['Auth'], menu=menu, username=current_user (request) ['username'], current_page='login')
if page == 'users':
if page == 'admin':
user = current_user (request)
if user ['username'] != os.getenv ('ADMIN_USER') and user ['email'] != os.getenv ('ADMIN_USER'):
return 'unauthorized', 403
Expand All @@ -426,7 +426,6 @@ def auth_templates (page, lang, menu, request):
users = db_scan ('users')
userdata = []
fields = ['username', 'email', 'birth_year', 'country', 'gender', 'created', 'last_login', 'verification_pending']
counter = 1
for user in users:
data = {}
for field in fields:
Expand All @@ -438,10 +437,12 @@ def auth_templates (page, lang, menu, request):
data ['created'] = datetime.datetime.fromtimestamp (int (str (data ['created']) [:-3])).isoformat ()
if data ['last_login']:
data ['last_login'] = datetime.datetime.fromtimestamp (int (str (data ['last_login']) [:-3])).isoformat ()
data ['index'] = counter
counter = counter + 1
userdata.append (data)

userdata.sort(key=lambda user: user ['created'], reverse=True)
counter = 1
for user in userdata:
user ['index'] = counter
counter = counter + 1

return render_template ('users.html', users=userdata)
return render_template ('admin.html', users=userdata, program_count=db_describe ('programs') ['Table'] ['ItemCount'])
6 changes: 3 additions & 3 deletions doc/backend.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@
- If `email` is present and different from the existing email, the route will also send a verification email to the provided `email`.
- If successful, the route returns 200.

- `GET /users`
- This route allows the admin user to retrieve a list of all the users in the system.
- `GET /admin`
- This route allows the admin user to retrieve a list of all the users in the system, as well as a program count.
- If there's no session or the logged in user is not the admin user, it returns 403.
- If successful, the route will return a template containing a table with all the users in the system. The users will be sorted by creation date, last first.
- If successful, the route will return a template containing a table with all the users in the system and a total count of saved programs. The users will be sorted by creation date, last first.

### Programs

Expand Down
1 change: 1 addition & 0 deletions templates/users.html → templates/admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

{% block main %}
<div class="px-8">
<h2>Total programs: <strong>{{ program_count }}</strong></h2>
<h2>Users</h2>
<table class="users">
<thead>
Expand Down
3 changes: 3 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,6 @@ def db_scan (table):
for item in result ['Items']:
output.append (db_decode (item))
return output

def db_describe (table):
return db.describe_table (TableName = db_prefix + '-' + table)

0 comments on commit 9aa7213

Please sign in to comment.