# Path: gitmostwanted/app.py

# 
# Path: gitmostwanted/models/user.py
# class User(db.Model):
#     __tablename__ = 'users'
# 
#     id = db.Column(db.Integer, primary_key=True)
#     email = db.Column(db.String(120), unique=True)
#     username = db.Column(db.String(80))
#     github_id = db.Column(db.BigInteger, unique=True)
# 
# Path: gitmostwanted/services/oauth.py
# def instance(app):


from flask import Blueprint, g, redirect, request, session, url_for
from gitmostwanted.app import app, db
from gitmostwanted.models.user import User
from gitmostwanted.services import oauth as service_oauth


user_oauth = Blueprint('user_oauth', __name__)

oauth = service_oauth.instance(app)
oauth.register(
    'github', **app.config['GITHUB_OAUTH'],
    fetch_token=lambda: session.get('oauth_access_token')
)


# @todo #1:15min move before_request method to a general place or a middleware
@app.before_request
def load_user_from_session():
    if str(request.url_rule) in ['/logout']:
        return None

# Path: gitmostwanted/app.py

# 
# Path: gitmostwanted/models/user.py
# class User(db.Model):
#     __tablename__ = 'users'
# 
#     id = db.Column(db.Integer, primary_key=True)
#     email = db.Column(db.String(120), unique=True)
#     username = db.Column(db.String(80))
#     github_id = db.Column(db.BigInteger, unique=True)
# 
# Path: gitmostwanted/services/oauth.py
# def instance(app):


# Path: gitmostwanted/blueprints/user_oauth.py
from flask import Blueprint, g, redirect, request, session, url_for
from gitmostwanted.app import app, db
from gitmostwanted.models.user import User
from gitmostwanted.services import oauth as service_oauth



user_oauth = Blueprint('user_oauth', __name__)

oauth = service_oauth.instance(app)
oauth.register(
    'github', **app.config['GITHUB_OAUTH'],
    fetch_token=lambda: session.get('oauth_access_token')
)


# @todo #1:15min move before_request method to a general place or a middleware
@app.before_request
def load_user_from_session():
    if str(request.url_rule) in ['/logout']:
        return None

    g.user = User.query.get(session['user_id']) if 'user_id' in session else None