Github integration with admin

This commit is contained in:
Carlos Lopez 2020-07-15 07:00:25 -06:00
parent e82532aad8
commit a10ef9066d
3 changed files with 77 additions and 5 deletions

View File

@ -44,6 +44,7 @@ webtest = "*"
werkzeug = "*" werkzeug = "*"
xlrd = "*" xlrd = "*"
xlsxwriter = "*" xlsxwriter = "*"
pygithub = "*"
[requires] [requires]
python_version = "3.7" python_version = "3.7"

23
Pipfile.lock generated
View File

@ -1,7 +1,7 @@
{ {
"_meta": { "_meta": {
"hash": { "hash": {
"sha256": "97a15c4ade88db2b384d52436633889a4d9b0bdcaeea86b8a679ebda6f73fb59" "sha256": "a4a720761a082a0ca31d2be17c2ea137e1d487ba2de538db334c8dc396770665"
}, },
"pipfile-spec": 6, "pipfile-spec": 6,
"requires": { "requires": {
@ -235,6 +235,13 @@
"index": "pypi", "index": "pypi",
"version": "==5.2" "version": "==5.2"
}, },
"deprecated": {
"hashes": [
"sha256:525ba66fb5f90b07169fdd48b6373c18f1ee12728ca277ca44567a367d9d7f74",
"sha256:a766c1dccb30c5f6eb2b203f87edd1d8588847709c78589e1521d769addc8218"
],
"version": "==1.2.10"
},
"docutils": { "docutils": {
"hashes": [ "hashes": [
"sha256:0c5b78adfbf7762415433f5515cd5c9e762339e23369dbe8000d84a4bf4ab3af", "sha256:0c5b78adfbf7762415433f5515cd5c9e762339e23369dbe8000d84a4bf4ab3af",
@ -669,6 +676,14 @@
], ],
"version": "==2.20" "version": "==2.20"
}, },
"pygithub": {
"hashes": [
"sha256:8375a058ec651cc0774244a3bc7395cf93617298735934cdd59e5bcd9a1df96e",
"sha256:d2d17d1e3f4474e070353f201164685a95b5a92f5ee0897442504e399c7bc249"
],
"index": "pypi",
"version": "==1.51"
},
"pygments": { "pygments": {
"hashes": [ "hashes": [
"sha256:647344a061c249a3b74e230c739f434d7ea4d8b1d5f3721bc0f3558049b38f44", "sha256:647344a061c249a3b74e230c739f434d7ea4d8b1d5f3721bc0f3558049b38f44",
@ -988,6 +1003,12 @@
"index": "pypi", "index": "pypi",
"version": "==1.0.1" "version": "==1.0.1"
}, },
"wrapt": {
"hashes": [
"sha256:b62ffa81fb85f4332a4f609cab4ac40709470da05643a082ec1eb88e6d9b97d7"
],
"version": "==1.12.1"
},
"wtforms": { "wtforms": {
"hashes": [ "hashes": [
"sha256:6ff8635f4caeed9f38641d48cfe019d0d3896f41910ab04494143fc027866e1b", "sha256:6ff8635f4caeed9f38641d48cfe019d0d3896f41910ab04494143fc027866e1b",

View File

@ -3,16 +3,18 @@ import json
from flask import url_for from flask import url_for
from flask_admin import Admin from flask_admin import Admin
from flask_admin.actions import action
from flask_admin.contrib import sqla from flask_admin.contrib import sqla
from flask_admin.contrib.sqla import ModelView from flask_admin.contrib.sqla import ModelView
from github import Github, UnknownObjectException
from werkzeug.utils import redirect from werkzeug.utils import redirect
from jinja2 import Markup from jinja2 import Markup
from crc import db, app from crc import db, app
from crc.api.user import verify_token, verify_token_admin from crc.api.user import verify_token, verify_token_admin
from crc.models.approval import ApprovalModel from crc.models.approval import ApprovalModel
from crc.models.file import FileModel from crc.models.file import FileModel, FileDataModel
from crc.models.task_event import TaskEventModel from crc.models.stats import TaskEventModel
from crc.models.study import StudyModel from crc.models.study import StudyModel
from crc.models.user import UserModel from crc.models.user import UserModel
from crc.models.workflow import WorkflowModel from crc.models.workflow import WorkflowModel
@ -34,26 +36,73 @@ class AdminModelView(sqla.ModelView):
# redirect to login page if user doesn't have access # redirect to login page if user doesn't have access
return redirect(url_for('home')) return redirect(url_for('home'))
class UserView(AdminModelView): class UserView(AdminModelView):
column_filters = ['uid'] column_filters = ['uid']
class StudyView(AdminModelView): class StudyView(AdminModelView):
column_filters = ['id', 'primary_investigator_id'] column_filters = ['id', 'primary_investigator_id']
column_searchable_list = ['title'] column_searchable_list = ['title']
class ApprovalView(AdminModelView): class ApprovalView(AdminModelView):
column_filters = ['study_id', 'approver_uid'] column_filters = ['study_id', 'approver_uid']
class WorkflowView(AdminModelView): class WorkflowView(AdminModelView):
column_filters = ['study_id', 'id'] column_filters = ['study_id', 'id']
class FileView(AdminModelView): class FileView(AdminModelView):
column_filters = ['workflow_id'] column_filters = ['workflow_id', 'type']
@action('publish', 'Publish', 'Are you sure you want to publish this file(s)?')
def action_publish(self, ids):
# TODO: Move token to settings and replace docs repo
_github = Github('d082288d6192b45b2f8cefcefc1a0a2806554c9e')
repo = _github.get_user().get_repo('crispy-fiesta')
for file_id in ids:
file_data_model = FileDataModel.query.filter_by(file_model_id=file_id).first()
try:
repo_file = repo.get_contents(file_data_model.file_model.name)
except UnknownObjectException:
repo.create_file(
path=file_data_model.file_model.name,
message=f'Creating {file_data_model.file_model.name}',
content=file_data_model.data
)
else:
updated = repo.update_file(
path=repo_file.path,
message=f'Updating {file_data_model.file_model.name}',
content=file_data_model.data,
sha=repo_file.sha
)
@action('update', 'Update', 'Are you sure you want to update this file(s)?')
def action_update(self, ids):
_github = Github('d082288d6192b45b2f8cefcefc1a0a2806554c9e')
repo = _github.get_user().get_repo('crispy-fiesta')
for file_id in ids:
file_data_model = FileDataModel.query.filter_by(file_model_id=file_id).first()
try:
repo_file = repo.get_contents(file_data_model.file_model.name)
except UnknownObjectException:
# Add message indicating file is not in the repo
pass
else:
file_data_model.data = repo_file.content
db.session.add(file_data_model)
db.session.commit()
def json_formatter(view, context, model, name): def json_formatter(view, context, model, name):
value = getattr(model, name) value = getattr(model, name)
json_value = json.dumps(value, ensure_ascii=False, indent=2) json_value = json.dumps(value, ensure_ascii=False, indent=2)
return Markup('<pre>{}</pre>'.format(json_value)) return Markup(f'<pre>{json_value}</pre>')
class TaskEventView(AdminModelView): class TaskEventView(AdminModelView):
column_filters = ['workflow_id', 'action'] column_filters = ['workflow_id', 'action']
@ -62,6 +111,7 @@ class TaskEventView(AdminModelView):
'form_data': json_formatter, 'form_data': json_formatter,
} }
admin = Admin(app) admin = Admin(app)
admin.add_view(StudyView(StudyModel, db.session)) admin.add_view(StudyView(StudyModel, db.session))