diff --git a/migrations/versions/6c34576847ab_.py b/migrations/versions/6c34576847ab_.py new file mode 100644 index 0000000..ebd1baf --- /dev/null +++ b/migrations/versions/6c34576847ab_.py @@ -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') diff --git a/pb/__init__.py b/pb/__init__.py index ccc6a14..80a123e 100644 --- a/pb/__init__.py +++ b/pb/__init__.py @@ -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/', 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) diff --git a/pb/models.py b/pb/models.py index 5347bb1..b6fffb8 100644 --- a/pb/models.py +++ b/pb/models.py @@ -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") + diff --git a/templates/index.html b/templates/index.html index bde2bea..c7e8546 100644 --- a/templates/index.html +++ b/templates/index.html @@ -17,7 +17,7 @@ New Study

- +