Skip to content

Commit

Permalink
Merge pull request #214 from biocore/st_dh_issue_192
Browse files Browse the repository at this point in the history
Admin Panel For Systemwide Messaging
  • Loading branch information
wasade committed Jun 15, 2020
2 parents 06503d1 + 9dd5ed7 commit 6253f95
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 1 deletion.
36 changes: 36 additions & 0 deletions microsetta_private_api/example/client.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,41 @@ paths:
schema:
type: string

'/admin/system_message':
get:
operationId: microsetta_private_api.example.client_impl.get_system_message
tags:
- Admin
responses:
'200':
description: System Message Builder Webpage
content:
text/html:
schema:
type: string
post:
operationId: microsetta_private_api.example.client_impl.post_system_message
tags:
- Admin
requestBody:
content:
application/x-www-form-urlencoded:
schema:
type: object
properties:
system_msg:
type: string
nullable: true
msg_style:
type: string
enum: [null, "primary", "secondary", "success", "danger", "warning", "info", "light", "dark"]
responses:
'200':
description: System Message Builder Webpage
content:
text/html:
schema:
type: string

components:
parameters:
Expand Down Expand Up @@ -579,6 +614,7 @@ components:
description: Error message provided by front-end
schema:
$ref: '#/components/schemas/error_msg'

schemas:
# account section
account_id:
Expand Down
65 changes: 64 additions & 1 deletion microsetta_private_api/example/client_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# config somewhere and reload

# Python is dumb, don't put spaces anywhere in this string.
from werkzeug.exceptions import BadRequest
from werkzeug.exceptions import BadRequest, Unauthorized

from microsetta_private_api.config_manager import SERVER_CONFIG
from microsetta_private_api.model.source import Source
Expand Down Expand Up @@ -59,6 +59,13 @@
# in some way, as well as any special handling for external surveys.
VIOSCREEN_ID = 10001

# TODO FIXME HACK: In the future, we will want to be able to persist these
# messages, or tie them to dates of specific events like system downtime.
# Placing them in memory here is a stopgap until the minimal interface can be
# properly separated out.
SYSTEM_MSG = None
SYSTEM_MSG_STYLE = None


def _get_req_survey_templates_by_source_type(source_type):
if source_type == Source.SOURCE_TYPE_HUMAN:
Expand Down Expand Up @@ -332,6 +339,8 @@ def get_show_error_page(error_msg):

output = render_template('error.jinja2',
admin_mode=session.get(ADMIN_MODE_KEY, False),
system_msg_text=SYSTEM_MSG,
system_msg_style=SYSTEM_MSG_STYLE,
mailto_url=mailto_url,
error_msg=error_msg,
endpoint=SERVER_CONFIG["endpoint"],
Expand All @@ -344,6 +353,8 @@ def get_show_error_page(error_msg):
def get_show_faq():
output = render_template('faq.jinja2',
admin_mode=session.get(ADMIN_MODE_KEY, False),
system_msg_text=SYSTEM_MSG,
system_msg_style=SYSTEM_MSG_STYLE,
authrocket_url=SERVER_CONFIG["authrocket_url"],
endpoint=SERVER_CONFIG["endpoint"])
return output
Expand Down Expand Up @@ -377,6 +388,8 @@ def get_home():
if session.get(ADMIN_MODE_KEY, False):
return render_template('admin_home.jinja2',
admin_mode=session.get(ADMIN_MODE_KEY, False),
system_msg_text=SYSTEM_MSG,
system_msg_style=SYSTEM_MSG_STYLE,
accounts=[],
endpoint=SERVER_CONFIG["endpoint"],
authrocket_url=SERVER_CONFIG["authrocket_url"])
Expand All @@ -385,6 +398,8 @@ def get_home():
# login if they aren't logged in yet.
return render_template('home.jinja2',
admin_mode=session.get(ADMIN_MODE_KEY, False),
system_msg_text=SYSTEM_MSG,
system_msg_style=SYSTEM_MSG_STYLE,
user=user,
email_verified=email_verified,
accounts=accts_output,
Expand Down Expand Up @@ -447,6 +462,8 @@ def get_create_account():
return render_template('account_details.jinja2',
CREATE_ACCT=True,
admin_mode=session.get(ADMIN_MODE_KEY, False),
system_msg_text=SYSTEM_MSG,
system_msg_style=SYSTEM_MSG_STYLE,
authorized_email=email,
account=default_account_values)

Expand Down Expand Up @@ -489,6 +506,8 @@ def get_update_email(account_id):

return render_template("update_email.jinja2",
admin_mode=session.get(ADMIN_MODE_KEY, False),
system_msg_text=SYSTEM_MSG,
system_msg_style=SYSTEM_MSG_STYLE,
account_id=account_id)


Expand Down Expand Up @@ -537,6 +556,8 @@ def get_account(account_id):

return render_template('account_overview.jinja2',
admin_mode=session.get(ADMIN_MODE_KEY, False),
system_msg_text=SYSTEM_MSG,
system_msg_style=SYSTEM_MSG_STYLE,
account=account,
sources=sources)

Expand All @@ -553,6 +574,8 @@ def get_account_details(account_id):
return render_template('account_details.jinja2',
CREATE_ACCT=False,
admin_mode=session.get(ADMIN_MODE_KEY, False),
system_msg_text=SYSTEM_MSG,
system_msg_style=SYSTEM_MSG_STYLE,
account=account)


Expand Down Expand Up @@ -652,6 +675,8 @@ def get_fill_local_source_survey(account_id, source_id, survey_template_id):

return render_template("survey.jinja2",
admin_mode=session.get(ADMIN_MODE_KEY, False),
system_msg_text=SYSTEM_MSG,
system_msg_style=SYSTEM_MSG_STYLE,
endpoint=SERVER_CONFIG["endpoint"],
account_id=account_id,
source_id=source_id,
Expand Down Expand Up @@ -834,6 +859,8 @@ def get_source(account_id, source_id):
is_human = source_output['source_type'] == Source.SOURCE_TYPE_HUMAN
return render_template('source.jinja2',
admin_mode=session.get(ADMIN_MODE_KEY, False),
system_msg_text=SYSTEM_MSG,
system_msg_style=SYSTEM_MSG_STYLE,
account_id=account_id,
source_id=source_id,
is_human=is_human,
Expand Down Expand Up @@ -890,6 +917,8 @@ def get_update_sample(account_id, source_id, sample_id):

return render_template('sample.jinja2',
admin_mode=session.get(ADMIN_MODE_KEY, False),
system_msg_text=SYSTEM_MSG,
system_msg_style=SYSTEM_MSG_STYLE,
account_id=account_id,
source_id=source_id,
source_name=source_output['source_name'],
Expand Down Expand Up @@ -989,6 +1018,9 @@ def post_claim_samples(account_id, source_id, body):

# Administrator Mode Functionality
def get_interactive_account_search(email_query):
if not session.get(ADMIN_MODE_KEY, False):
raise Unauthorized()

do_return, email_diagnostics, _ = ApiRequest.get(
'/admin/search/account/%s' % (email_query,))
if do_return:
Expand All @@ -998,11 +1030,42 @@ def get_interactive_account_search(email_query):
for acct in email_diagnostics['accounts']]
return render_template('admin_home.jinja2',
admin_mode=session.get(ADMIN_MODE_KEY, False),
system_msg_text=SYSTEM_MSG,
system_msg_style=SYSTEM_MSG_STYLE,
accounts=accounts,
endpoint=SERVER_CONFIG["endpoint"],
authrocket_url=SERVER_CONFIG["authrocket_url"])


def get_system_message():
if not session.get(ADMIN_MODE_KEY, False):
raise Unauthorized()

return render_template('admin_system_panel.jinja2',
admin_mode=session.get(ADMIN_MODE_KEY, False),
system_msg=SYSTEM_MSG,
msg_style=SYSTEM_MSG_STYLE)


def post_system_message(body):
if not session.get(ADMIN_MODE_KEY, False):
raise Unauthorized()

global SYSTEM_MSG
global SYSTEM_MSG_STYLE
SYSTEM_MSG = body.get("system_msg")
SYSTEM_MSG_STYLE = body.get("msg_style")

if not SYSTEM_MSG or len(SYSTEM_MSG) == 0:
SYSTEM_MSG = None
SYSTEM_MSG_STYLE = None

return render_template('admin_system_panel.jinja2',
admin_mode=session.get(ADMIN_MODE_KEY, False),
system_msg=SYSTEM_MSG,
msg_style=SYSTEM_MSG_STYLE)


class BearerAuth(AuthBase):
def __init__(self, token):
self.token = token
Expand Down
21 changes: 21 additions & 0 deletions microsetta_private_api/static/input_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,24 @@ function preclude_whitespace(selector)
});
});
}

function replicate_text(input_selector, destination_selector)
{
$(input_selector).bind('input', function(){
$(this).val(function(_, v){
$(destination_selector).html(v)
return v
});
});
}

function select_class(input_selector, destination_selector, input_to_class)
{
$(input_selector).change(function(){
$(this).val(function(_, v){
$(destination_selector).removeClass()
$(destination_selector).addClass(input_to_class(v))
return v
});
});
}
39 changes: 39 additions & 0 deletions microsetta_private_api/templates/admin_system_panel.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{% extends "sitebase.jinja2" %}
{% set page_title = "ADMINISTRATOR SYSTEM PANEL" %}
{% set show_breadcrumbs = False %}
{% set show_logout = True %}
{% block head %}
<script>
$(function(){
to_class = function(choice){ return ["alert", "alert-"+choice]; }
replicate_text("#system_msg", "#test_msg");
select_class("#msg_style", "#test_msg", to_class);
});
</script>
{% endblock %}
{% block content %}
<h4> Set System Message </h4>
<div id="test_msg" class="alert alert-{{msg_style|e}}" role="alert">
{{system_msg}}
</div>

<form method="post" id="system_msg_form" name="system_msg_form">
<div class="form-group row">
<label for="system_msg" name="system_msg_label" class="col-sm-2 col-form-label">System Message:</label>
<div class="col-sm-8">
<textarea class="form-control" id="system_msg" name="system_msg" rows="5">{{system_msg |e}}</textarea>
<select id="msg_style" name="msg_style" class="form-control" required>
<option value="primary" {% if msg_style == "primary" %} selected {% endif %}>primary</option>
<option value="secondary" {% if msg_style == "secondary" %} selected {% endif %}>secondary</option>
<option value="success" {% if msg_style == "success" %} selected {% endif %}>success</option>
<option value="danger" {% if msg_style == "danger" %} selected {% endif %}>danger</option>
<option value="warning" {% if msg_style == "warning" %} selected {% endif %}>warning</option>
<option value="info" {% if msg_style == "info" %} selected {% endif %}>info</option>
<option value="light" {% if msg_style == "light" %} selected {% endif %}>light</option>
<option value="dark" {% if msg_style == "dark" %} selected {% endif %}>dark</option>
</select>
</div>
</div>
<button type="submit" class="btn btn-primary">Set System Message</button>
</form>
{% endblock %}
7 changes: 7 additions & 0 deletions microsetta_private_api/templates/sitebase.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
{% if admin_mode %}
<nav class="navbar fixed-top navbar-light bg-warning">
Administrator Toolbar
<a class="btn btn-outline-success" href="/admin/system_message">System Panel</a>
<form class="form-inline" action="/admin/account_search" method="get">
<input id="email_query" name="email_query" class="form-control mr-sm-2" type="search" placeholder="User Email" aria-label="Search"/>
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Find Account</button>
Expand All @@ -46,6 +47,12 @@
<br />
{% endif %}

{% if system_msg_text and not admin_mode %}
<div id="system_message" class="alert alert-{{system_msg_style|e}}" role="alert">
{{system_msg_text}}
</div>
{% endif %}

<div class="content">

{% if show_breadcrumbs %}
Expand Down

0 comments on commit 6253f95

Please sign in to comment.