diff --git a/pocwww/__init__.py b/pocwww/__init__.py index 30eb4bb..b64b882 100644 --- a/pocwww/__init__.py +++ b/pocwww/__init__.py @@ -125,19 +125,18 @@ def main(global_config, **settings): # SHARED ROUTES config.add_route('home', '/') - config.add_route('json_snapshot', '/json') - config.add_route("json_users", "/json/users", factory='pocwww.security.UserLoginFactory') + config.add_route('home_json', '/json') # Cards config.add_route('cards_page', '/cards/{cards_date}/{page_num}') - config.add_route('json_cards_page', '/json/cards/{cards_date}/{page_num}') + config.add_route('cards_page_json', '/json/cards/{cards_date}/{page_num}') config.add_route('cards_date', '/cards/{cards_date}') - config.add_route('json_cards_date', '/json/cards/{cards_date}') + config.add_route('cards_date_json', '/json/cards/{cards_date}') config.add_route('cards', '/cards') - config.add_route('json_cards', '/json/cards') - config.add_route('card_single', "/card/view/{stroke_number}") - config.add_route('json_card_single', "/json/card/view/{stroke_number}") - config.add_route('json_lastcard', "/json/lastcard") + config.add_route('cards_json', '/json/cards') + config.add_route('cards_singlecard', "/card/view/{stroke_number}") + config.add_route('cards_singlecard_json', "/json/card/view/{stroke_number}") + config.add_route('cards_lastcard_json', "/json/lastcard") # Measurements config.add_route('measurements_all', "/values") @@ -152,41 +151,43 @@ def main(global_config, **settings): # Gauge Off config.add_route('gaugeoff_all', '/gaugeoff') - config.add_route('json_gaugeoff_all', '/json/gaugeoff') + config.add_route('gaugeoff_all_json', '/json/gaugeoff') # Fluid Shots config.add_route('fluidshots_all', '/fluidshots') - config.add_route('json_fluidshots_all', '/json/fluidshots') + config.add_route('fluidshots_all_json', '/json/fluidshots') # Well Tests config.add_route('welltests_all', '/welltests') - config.add_route('json_welltests_all', '/json/welltests') + config.add_route('welltests_all_json', '/json/welltests') # Run Status config.add_route('runstatus', '/runstatus') - config.add_route('json_runstatus_page', '/json/runstatus/{page_num}') - config.add_route('json_runstatus', '/json/runstatus') - config.add_route('json_runstatusnow', "/json/runstatusnow") + config.add_route('runstatus_page_json', '/json/runstatus/{page_num}') + config.add_route('runstatus_json', '/json/runstatus') + config.add_route('runstatus_now_json', "/json/runstatusnow") # Configuration - config.add_route('json_config', '/json/config', factory='pocwww.security.UserLoginFactory') + config.add_route('config_json', '/json/config', factory='pocwww.security.UserLoginFactory') config.add_route('config', '/config', factory='pocwww.security.UserLoginFactory') - # Setpoints - config.add_route('json_setpoints', '/json/setpoints', factory='pocwww.security.UserLoginFactory') - config.add_route('json_mode', '/json/mode', factory='pocwww.security.UserLoginFactory') - config.add_route('setpoints', '/setpoints', factory='pocwww.security.UserLoginFactory') - # Administration config.add_route('admin', '/admin', factory='pocwww.security.UserLoginFactory') - config.add_route('auth', '/sign/{action}') - config.add_route('register', '/register', factory='pocwww.security.UserLoginFactory') + + # Users + config.add_route('users_auth', '/sign/{action}') + config.add_route('users_register', '/register', factory='pocwww.security.UserLoginFactory') + config.add_route("users_json", "/json/users", factory='pocwww.security.UserLoginFactory') + # POC Interface - config.add_route("json_updateconfig", "/json/updateconfig", factory='pocwww.security.UserLoginFactory') - config.add_route("json_shake", '/json/cmd/shake', factory='pocwww.security.UserLoginFactory') # Shake command is separate for allowing all access - config.add_route("json_cmd", '/json/cmd/{action}', factory='pocwww.security.UserLoginFactory') - config.add_route("json_update_poc_address", "/json/updatepocaddress", factory='pocwww.security.UserLoginFactory') + config.add_route("poc_updateconfig_json", "/json/updateconfig", factory='pocwww.security.UserLoginFactory') + config.add_route("poc_shake_json", '/json/cmd/shake', factory='pocwww.security.UserLoginFactory') # Shake command is separate for allowing all access + config.add_route("poc_cmd_json", '/json/cmd/{action}', factory='pocwww.security.UserLoginFactory') + config.add_route("poc_updatepocaddress_json", "/json/updatepocaddress", factory='pocwww.security.UserLoginFactory') + config.add_route('poc_setpoints_json', '/json/setpoints', factory='pocwww.security.UserLoginFactory') + config.add_route('poc_mode_json', '/json/mode', factory='pocwww.security.UserLoginFactory') + config.add_route('poc_setpoints', '/setpoints', factory='pocwww.security.UserLoginFactory') diff --git a/pocwww/admin.py b/pocwww/admin.py new file mode 100644 index 0000000..5d0a76d --- /dev/null +++ b/pocwww/admin.py @@ -0,0 +1,7 @@ +from pyramid.view import view_config + + +@view_config(route_name="admin", renderer="templates/admin.jinja2", permission="edit") +def admin_view(request): + address = get_poc_address(request) or 'localhost' + return {'navgroup': 'admin', 'pocIPAddress': address} diff --git a/pocwww/cards.py b/pocwww/cards.py index 183c202..e488917 100644 --- a/pocwww/cards.py +++ b/pocwww/cards.py @@ -1,4 +1,7 @@ from pyramid.view import view_config +from datetime import datetime +from datetime import timedelta +from math import ceil from .pagination import Pagination def get_all_dates_with_cards(request): @@ -20,21 +23,21 @@ def get_all_dates_with_cards(request): datelist.append({'count': d['count'], 'date': datetime.strptime(d['_id'], "%Y-%m-%d").date()}) return list(datelist) -@view_config(route_name="json_lastcard", renderer="prettyjson") +@view_config(route_name="cards_lastcard_json", renderer="prettyjson") def json_lastcard(request): return get_latest_card(request) @view_config(route_name='cards', renderer='templates/datelist.jinja2') -@view_config(route_name='json_cards', renderer='prettyjson') +@view_config(route_name='cards_json', renderer='prettyjson') def cards(request): return {'datelist': get_all_dates_with_cards(request), 'navgroup': 'cards'} @view_config(route_name='cards_page', renderer='templates/cardlist.jinja2') @view_config(route_name='cards_date', renderer='templates/cardlist.jinja2') -@view_config(route_name='json_cards_date', renderer='prettyjson') -@view_config(route_name='json_cards_page', renderer='prettyjson') +@view_config(route_name='cards_date_json', renderer='prettyjson') +@view_config(route_name='cards_page_json', renderer='prettyjson') def cards_page(request): page_num = 1 try: @@ -64,8 +67,8 @@ def cards_page(request): } -@view_config(route_name='card_single', renderer='templates/cardsingle.jinja2') -@view_config(route_name='json_card_single', renderer='prettyjson') +@view_config(route_name='cards_singlecard', renderer='templates/cardsingle.jinja2') +@view_config(route_name='cards_singlecard_json', renderer='prettyjson') def card_single(request): card = {} try: diff --git a/pocwww/config.py b/pocwww/config.py new file mode 100644 index 0000000..ff07722 --- /dev/null +++ b/pocwww/config.py @@ -0,0 +1,7 @@ +from pyramid.view import view_config + +@view_config(route_name="config", renderer="templates/config.jinja2", permission="edit") +@view_config(route_name="config_json", renderer="prettyjson", permission="edit") +def well_config(request): + current_configuration = list(request.db['wellConfiguration'].find().sort("timestamp", -1).limit(1))[0] + return {'navgroup': 'config', 'config': current_configuration} diff --git a/pocwww/fluid_shots.py b/pocwww/fluid_shots.py index 3698990..704dcd7 100644 --- a/pocwww/fluid_shots.py +++ b/pocwww/fluid_shots.py @@ -2,7 +2,7 @@ from pyramid.view import view_config @view_config(route_name="fluidshots_all", renderer="templates/fluidshots.jinja2") -@view_config(route_name="json_fluidshots_all", renderer="prettyjson") +@view_config(route_name="fluidshots_all_json", renderer="prettyjson") def fluidshots_all(request): fluidshots = list(request.db['fluidShots'].find()) return {'navgroup': 'fluidshots', 'data': fluidshots} diff --git a/pocwww/gauge_off.py b/pocwww/gauge_off.py index a5a19b1..b5d1633 100644 --- a/pocwww/gauge_off.py +++ b/pocwww/gauge_off.py @@ -1,7 +1,7 @@ from pyramid.view import view_config @view_config(route_name="gaugeoff_all", renderer="templates/gaugeoff_all.jinja2") -@view_config(route_name="json_gaugeoff_all", renderer="prettyjson") +@view_config(route_name="gaugeoff_all_json", renderer="prettyjson") def gaugeoff_all(request): gaugeoff_list = [] dateagg = list(request.db['gaugeOff'].aggregate( diff --git a/pocwww/home.py b/pocwww/home.py new file mode 100644 index 0000000..13affe7 --- /dev/null +++ b/pocwww/home.py @@ -0,0 +1,33 @@ +from pyramid.view import view_config + + +@view_config(route_name='home', renderer='templates/dashboard.jinja2') +@view_config(route_name="home_json", renderer="prettyjson") +def my_view(request): + latest_tag_values = [] + latesttag_agg = request.db['measurements'].aggregate([ + {'$sort': {'dateStored': 1}}, + { + '$group': { + '_id': '$tagname', + 'currentValue': {'$last': '$currentValue'}, + 'units': {'$last': '$units'}, + 'maxValue': {'$last': '$maxValue'}, + 'minValue': {'$last': '$minValue'}, + 'totalValue': {'$last': '$totalValue'}, + 'averageValue': {'$last': '$averageValue'}, + 'useTotal': {'$last': '$useTotal'}, + 'useAverage': {'$last': '$useAverage'} + } + } + ]) + for t in latesttag_agg: + latest_tag_values.append(t) + + latest_card = [] + try: + latest_card = list(request.db['cards'].find().sort("timestamp", -1).limit(1))[0] + except IndexError: + latest_card = [] + + return {'project': 'POC Web Interface', 'navgroup': 'dashboard', 'tag_values': latest_tag_values, 'card': latest_card} diff --git a/pocwww/poc_interface.py b/pocwww/poc_interface.py index 2f3da7c..d645870 100644 --- a/pocwww/poc_interface.py +++ b/pocwww/poc_interface.py @@ -10,7 +10,7 @@ def get_poc_address(request): return address -@view_config(route_name="json_updateconfig", renderer="prettyjson", request_method='POST', permission="edit") +@view_config(route_name="poc_updateconfig_json", renderer="prettyjson", request_method='POST', permission="edit") def json_updateconfig(request): conv_to_float = [ 'deltaT', @@ -54,7 +54,7 @@ def json_updateconfig(request): return {'new_config': request.json_body, 'stored_result': result, 'updated': pocCmdSts} -@view_config(route_name="json_cmd", renderer="prettyjson", permission="edit") +@view_config(route_name="poc_cmd_json", renderer="prettyjson", permission="edit") def json_cmd(request): print("got here") action = request.matchdict['action'] @@ -69,7 +69,7 @@ def json_cmd(request): return r.text if r.status_code == 200 else {"status": "failure sending command"} -@view_config(route_name="json_shake", renderer="prettyjson", permission="view") +@view_config(route_name="poc_shake_json", renderer="prettyjson", permission="view") def json_shake(request): address = get_poc_address(request) or 'localhost' url = "http://{}:8000/shake".format(address) @@ -78,7 +78,7 @@ def json_shake(request): return r.text if r.status_code == 200 else {"status": "failure sending command"} -@view_config(route_name="json_update_poc_address", renderer="prettyjson", request_method='POST', permission="edit") +@view_config(route_name="poc_updatepocaddress_json", renderer="prettyjson", request_method='POST', permission="edit") def json_update_poc_address(request): try: new_addr = request.json_body['pocIPAddress'] @@ -88,17 +88,17 @@ def json_update_poc_address(request): return {"status": "failure"} -@view_config(route_name='setpoints', renderer='templates/setpoints.jinja2', permission='edit') +@view_config(route_name='poc_setpoints', renderer='templates/setpoints.jinja2', permission='edit') def setpoints(request): return {"navgroup": "setpoints"} -@view_config(route_name="json_setpoints", renderer="prettyjson", request_method='GET', permission='edit') +@view_config(route_name="poc_setpoints_json", renderer="prettyjson", request_method='GET', permission='edit') def json_setpoints(request): return {'setpoints': list(request.db['setpoints'].find())} -@view_config(route_name="json_setpoints", renderer="prettyjson", request_method='POST', permission='edit') +@view_config(route_name="poc_setpoints_json", renderer="prettyjson", request_method='POST', permission='edit') def json_setpoints_post(request): jsb = request.json_body try: @@ -120,7 +120,7 @@ def json_setpoints_post(request): runModes = {0: 'poc', 1: 'manual', 2: 'timer'} -@view_config(route_name="json_mode", renderer="prettyjson", request_method='POST', permission='edit') +@view_config(route_name="poc_mode_json", renderer="prettyjson", request_method='POST', permission='edit') def json_mode_post(request): jsb = request.json_body try: diff --git a/pocwww/run_status.py b/pocwww/run_status.py index cd35e6e..f6d0fb5 100644 --- a/pocwww/run_status.py +++ b/pocwww/run_status.py @@ -3,8 +3,8 @@ from math import ceil from .pagination import Pagination @view_config(route_name="runstatus", renderer="templates/runstatus.jinja2") -@view_config(route_name="json_runstatus", renderer="prettyjson") -@view_config(route_name="json_runstatus_page", renderer="prettyjson") +@view_config(route_name="runstatus_json", renderer="prettyjson") +@view_config(route_name="runstatus_page_json", renderer="prettyjson") def run_status(request): page_num = 1 try: @@ -19,7 +19,7 @@ def run_status(request): return {'navgroup': 'runstatus', 'data': runStatuses, 'pagination': Pagination(page_num, num_per_page, num_cards)} -@view_config(route_name="json_runstatusnow", renderer="prettyjson") +@view_config(route_name="runstatus_now_json", renderer="prettyjson") def json_runstatusnow(request): status = False try: diff --git a/pocwww/security.py b/pocwww/security.py index 3c2e501..c3d3434 100644 --- a/pocwww/security.py +++ b/pocwww/security.py @@ -1,4 +1,5 @@ from pyramid.security import Allow, Everyone, Authenticated +from passlib.apps import custom_app_context as poc_pwd_context class UserLoginFactory(object): @@ -8,3 +9,20 @@ class UserLoginFactory(object): def __init__(self, request): pass + +def set_password(request, username, password): + password_hash = poc_pwd_context.encrypt(password) + request.db['users'].update_one({'username': username}, {"$set":{"username": username, "password": password_hash}}, upsert=True) + + +def check_password(request, username, password): + users = list(request.db['users'].find({'username': username})) + if len(users) > 0: + this_user = users[0] + + # is it cleartext? + if password == this_user['password']: + set_password(request, username, password) + return check_password(request, username, password) + + return poc_pwd_context.verify(password, this_user['password']) diff --git a/pocwww/templates/layout.jinja2 b/pocwww/templates/layout.jinja2 index c76c11b..a2d952d 100644 --- a/pocwww/templates/layout.jinja2 +++ b/pocwww/templates/layout.jinja2 @@ -67,9 +67,9 @@