|
| 1 | +import json |
| 2 | +import requests |
| 3 | +import importlib |
| 4 | +import asynctasks |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +from flask import Flask, redirect, jsonify, session, request, url_for, render_template |
| 9 | +from flask.ext import login as login |
| 10 | +from frontendconfig import config as frontendconf |
| 11 | +from database import db |
| 12 | +import recastfrontend.models as dbmodels |
| 13 | + |
| 14 | +celeryapp = importlib.import_module(frontendconf['CELERYAPP']).app |
| 15 | + |
| 16 | +ORCID_APPID = frontendconf['ORCID_APPID'] |
| 17 | +ORCID_REDIRECT_URI = frontendconf['ORCID_REDIRECT_URI'] |
| 18 | +ORCID_SECRET = frontendconf['ORCID_SECRET'] |
| 19 | + |
| 20 | + |
| 21 | +class User(login.UserMixin): |
| 22 | + def __init__(self,**kwargs): |
| 23 | + self.orcid = kwargs.get('orcid','no-orcid') |
| 24 | + self.fullname = kwargs.get('fullname','no-name') |
| 25 | + def get_id(self): |
| 26 | + return self.orcid |
| 27 | + def name(self): |
| 28 | + return self.fullname |
| 29 | + |
| 30 | +def create_app(): |
| 31 | + app = Flask(__name__) |
| 32 | + app.config.from_object(frontendconf['FLASKCONFIG']) |
| 33 | + db.init_app(app) |
| 34 | + return app |
| 35 | + |
| 36 | +app = create_app() |
| 37 | + |
| 38 | +login_manager = login.LoginManager() |
| 39 | +login_manager.init_app(app) |
| 40 | + |
| 41 | +@app.route("/") |
| 42 | +def home(): |
| 43 | + all_users = dbmodels.User.query.all() |
| 44 | + celeryapp.set_current() |
| 45 | + asynctasks.hello_world.delay() |
| 46 | + return render_template('home.html', user_data = all_users) |
| 47 | + |
| 48 | +@app.route("/about") |
| 49 | +def about(): |
| 50 | + return render_template('about.html') |
| 51 | + |
| 52 | + |
| 53 | +@app.route('/login') |
| 54 | +def login_user(): |
| 55 | + if not request.args.has_key('code'): |
| 56 | + return redirect('https://orcid.org/oauth/authorize?client_id={}&response_type=code&scope=/authenticate&redirect_uri={}&show_login=true'.format( |
| 57 | + ORCID_APPID, |
| 58 | + ORCID_REDIRECT_URI |
| 59 | + )) |
| 60 | + |
| 61 | + auth_code = request.args.get('code') |
| 62 | + data = {'client_id':ORCID_APPID,'client_secret':ORCID_SECRET,'grant_type':'authorization_code','code':auth_code} |
| 63 | + |
| 64 | + r = requests.post('https://pub.orcid.org/oauth/token', data = data) |
| 65 | + login_details = json.loads(r.content) |
| 66 | + |
| 67 | + user = User(orcid = login_details['orcid'], fullname = login_details['name'], authenticated = True) |
| 68 | + login.login_user(user) |
| 69 | + |
| 70 | + return redirect(url_for('home')) |
| 71 | + |
| 72 | + |
| 73 | +@app.route("/logout") |
| 74 | +@login.login_required |
| 75 | +def logout(): |
| 76 | + login.logout_user() |
| 77 | + return redirect('/') |
| 78 | + |
| 79 | +@login_manager.user_loader |
| 80 | +def load_user(userid): |
| 81 | + r = requests.get('http://pub.orcid.org/v1.2/{}/orcid-profile'.format(userid), headers = {'Accept':'application/json'}) |
| 82 | + login_bio = json.loads(r.content)['orcid-profile']['orcid-bio'] |
| 83 | + return User(orcid = userid, fullname = '{} {}'.format(login_bio['personal-details']['given-names']['value'],login_bio['personal-details']['family-name']['value'])) |
| 84 | + |
| 85 | +@login_manager.unauthorized_handler |
| 86 | +def unauthorized(): |
| 87 | + return redirect(url_for('login_user')) |
| 88 | + |
| 89 | +@app.route("/profile") |
| 90 | +@login.login_required |
| 91 | +def profile(): |
| 92 | + return render_template('profile.html') |
0 commit comments