Merge pull request #55 from sartography/persist-selected-user-319

Persist selected user #319
This commit is contained in:
Dan Funk 2021-05-14 14:18:18 -04:00 committed by GitHub
commit 2770125025
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 102 additions and 14 deletions

View File

@ -0,0 +1,28 @@
"""empty message
Revision ID: 6c34576847ab
Revises: c14ddab2e6ca
Create Date: 2021-05-11 11:10:14.725089
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '6c34576847ab'
down_revision = 'c14ddab2e6ca'
branch_labels = None
depends_on = None
def upgrade():
op.create_table('selected_user',
sa.Column('user_id', sa.String(), nullable=False),
sa.Column('selected_user', sa.String(), nullable=True),
sa.PrimaryKeyConstraint('user_id')
)
def downgrade():
op.drop_table('selected_user')

View File

@ -5,6 +5,7 @@ import yaml
from io import TextIOWrapper
import connexion
from flask import g
from flask_cors import CORS
from flask import url_for, json, redirect, render_template, request, flash
from flask_assets import Environment, Bundle
@ -135,7 +136,7 @@ from pb.forms import StudyForm, StudyTable, InvestigatorForm, StudyDetailsForm,
IRBInfoForm
from pb.models import Study, RequiredDocument, Investigator, StudySchema, RequiredDocumentSchema, InvestigatorSchema, \
StudyDetails, StudyDetailsSchema, StudySponsor, Sponsor, SponsorSchema, StudySponsorSchema, IRBStatus, \
IRBStatusSchema, IRBInfo, IRBInfoSchema
IRBStatusSchema, IRBInfo, IRBInfoSchema, SelectedUser
from pb.ldap.ldap_service import LdapService
@ -151,19 +152,72 @@ def render_study_template(studies):
)
def _is_production():
return 'PRODUCTION' in app.config and app.config['PRODUCTION']
def _get_request_uid(req):
uid = None
if _is_production():
if 'user' in g and g.user is not None:
return g.user.uid
uid = req.headers.get("Uid")
if not uid:
uid = req.headers.get("X-Remote-Uid")
else:
uid = 'current_user'
return uid
def get_current_user(request):
current_user = _get_request_uid(request)
return current_user
def get_selected_user(current_user):
result = db.session.query(SelectedUser).filter(SelectedUser.user_id == current_user).first()
if result:
selected_user = result.selected_user
return selected_user
def update_selected_user(user, selected_user):
model = SelectedUser(user_id=user, selected_user=selected_user)
db_selected_user = db.session.query(SelectedUser).filter(SelectedUser.user_id==user).first()
if db_selected_user:
db_selected_user.selected_user = selected_user
else:
db_selected_user = model
db.session.add(db_selected_user)
db.session.commit()
return db_selected_user
@app.route('/', methods=['GET', 'POST'])
def index():
# display results
studies = db.session.query(Study).order_by(Study.DATE_MODIFIED.desc()).all()
return render_study_template(studies)
# If they have a selected_user,
# redirect to /user_studies/{selected_user}
# Otherwise, redirect to /user_studies/all
redirect_url = BASE_HREF + "/user_studies/all"
current_user = get_current_user(request)
if current_user:
selected_user = get_selected_user(current_user)
if selected_user:
redirect_url = BASE_HREF + "/user_studies/" + selected_user
return redirect(redirect_url)
@app.route('/user_studies/', defaults={'uva_id': 'all'})
@app.route('/user_studies/<uva_id>', methods=['GET'])
def user_studies(uva_id):
if uva_id == 'all':
return redirect(BASE_HREF + "/")
studies = db.session.query(Study).filter(Study.NETBADGEID == uva_id).order_by(Study.DATE_MODIFIED.desc()).all()
# return redirect(BASE_HREF + "/")
studies = db.session.query(Study).order_by(Study.DATE_MODIFIED.desc()).all()
else:
studies = db.session.query(Study).filter(Study.NETBADGEID == uva_id).order_by(Study.DATE_MODIFIED.desc()).all()
current_user = get_current_user(request)
update_selected_user(current_user, uva_id)
return render_study_template(studies)

View File

@ -281,9 +281,19 @@ class StudyDetails(db.Model):
REVIEWTYPENAME = db.Column(db.String, nullable=True)
class StudyDetailsSchema(ma.SQLAlchemyAutoSchema):
class Meta:
model = StudyDetails
load_instance = True
include_relationships = False
class SelectedUser(db.Model):
user_id = db.Column(db.String(), primary_key=True)
selected_user = db.Column(db.String(), nullable=True)
class SelectedUserSchema(ma.Schema):
class Meta:
fields = ("user_id", "selected_user")

View File

@ -17,7 +17,7 @@
<a class="btn btn-primary" href="{{ url_for('.new_study') }}"> New Study </a>
</p>
<p>
<label>User Studies</label>
<label>Only Show Studies for: </label>
<select name="uva_id" id="uva_id" onchange="selectStudies()">
<option value="">Select User</option>
<option value="all">All Users</option>
@ -58,11 +58,7 @@
<script>
function selectStudies() {
let uva_id = document.getElementById('uva_id').value;
if (uva_id == 'all') {
window.location.href = "{{base_href}}/";
} else {
window.location.href = "{{base_href}}/user_studies/" + uva_id;
}
window.location.href = "{{base_href}}/user_studies/" + uva_id;
}
</script>
</body>

View File

@ -205,7 +205,7 @@ class Sanity_Check_Test(unittest.TestCase):
if header[0] == 'Location':
self.assertIn('user_studies', header[1])
studies2 = self.app.get(f'/user_studies/')
self.assertEqual('302 FOUND', studies2.status)
self.assertEqual('200 OK', studies2.status)
for header in studies2.headers:
if header[0] == 'Location':
self.assertNotIn('user_studies', header[1])