# Path: Server/jbox/models.py
# class Developer(UserMixin, db.Model):
# class Integration(db.Model):
# class GitHub(db.Model):
# class Channel(db.Model):
# class Authorization(db.Model):
#     def __repr__(self):
#     def verify_developer(self, platform_id):
#     def generate_confirmation_token(self, expiration=3600):
#     def confirm(token):
#     def insert_to_db(self):
# def developer_loader(platform, platform_id):
# def request_loader(request):
# def verify_developer(platform_or_token, platform_id):
#     def __repr__(self):
#     def insert_to_db(self):
#     def __repr__(self):
# def load_user(developer_id):
# def generate_dev_key():
# def generate_platform_id():
# def generate_integration_id():
# def generate_auth_token():
# 
# Path: Server/jbox/main/views.py
# def json_to_dict(x):
# def update_qq_api_request_data(data={}):
# def get_user_info():
# def login():
# def logout():
# def authorized():
# def get_qq_oauth_token():
# def index():
# def document():
# def guide():
# def application():
# def get_developer():


import os
import requests
import uuid
from flask import abort, Flask, json, jsonify, render_template, redirect, request, url_for, flash, session
from flask_httpauth import HTTPAuth
from flask_oauthlib.client import OAuth
from . import auth
from config import basedir
from ..models import db, Channel, Developer, Integration, Authorization, generate_integration_id, generate_auth_token
from ..main.views import update_qq_api_request_data, qq, json_to_dict
                            index = display.index('/')
                            if index > 0:
                                repo_list.append(display[index + 1:len(display)])
                        new_github = GitHub(integration_id=integration.integration_id, name=integration.name, icon=integration.icon,
                                            channel=integration.channel.channel, repositories=repo_list)
                        github_integrations.append(new_github)
                    else:
                        github_integrations.append(integration)
        return render_template('auth/github_integration.html', **locals())
    except Exception:
        # 重新授权
        return github.authorize(callback=url_for('auth.github_re_authorize', _external=True))


@auth.route('/github/re-authorize', methods=['GET'])
def github_re_authorize():
    developer = get_developer()
    if developer is None:
        return redirect(url_for('main.login'))
    dev_key = developer.dev_key
    resp = github.authorized_response()
    if resp is None:
        return 'Access denied: reason=%s error=%s' % (
            request.args['error_reason'],
            request.args['error_description']
        )
    session['github_token'] = (resp['access_token'], '')
    token = session['github_token'][0]
    authorization = Authorization.query.filter_by(developer_id=developer.id, type='github').first()
    if authorization is None:

# Path: Server/jbox/models.py
# class Developer(UserMixin, db.Model):
# class Integration(db.Model):
# class GitHub(db.Model):
# class Channel(db.Model):
# class Authorization(db.Model):
#     def __repr__(self):
#     def verify_developer(self, platform_id):
#     def generate_confirmation_token(self, expiration=3600):
#     def confirm(token):
#     def insert_to_db(self):
# def developer_loader(platform, platform_id):
# def request_loader(request):
# def verify_developer(platform_or_token, platform_id):
#     def __repr__(self):
#     def insert_to_db(self):
#     def __repr__(self):
# def load_user(developer_id):
# def generate_dev_key():
# def generate_platform_id():
# def generate_integration_id():
# def generate_auth_token():
# 
# Path: Server/jbox/main/views.py
# def json_to_dict(x):
# def update_qq_api_request_data(data={}):
# def get_user_info():
# def login():
# def logout():
# def authorized():
# def get_qq_oauth_token():
# def index():
# def document():
# def guide():
# def application():
# def get_developer():


# Path: Server/jbox/auth/views.py
import os
import requests
import uuid
from flask import abort, Flask, json, jsonify, render_template, redirect, request, url_for, flash, session
from flask_httpauth import HTTPAuth
from flask_oauthlib.client import OAuth
from . import auth
from config import basedir
from ..models import db, Channel, Developer, Integration, Authorization, generate_integration_id, generate_auth_token
from ..main.views import update_qq_api_request_data, qq, json_to_dict

                            index = display.index('/')
                            if index > 0:
                                repo_list.append(display[index + 1:len(display)])
                        new_github = GitHub(integration_id=integration.integration_id, name=integration.name, icon=integration.icon,
                                            channel=integration.channel.channel, repositories=repo_list)
                        github_integrations.append(new_github)
                    else:
                        github_integrations.append(integration)
        return render_template('auth/github_integration.html', **locals())
    except Exception:
        # 重新授权
        return github.authorize(callback=url_for('auth.github_re_authorize', _external=True))


@auth.route('/github/re-authorize', methods=['GET'])
def github_re_authorize():
    developer = get_developer()
    if developer is None:
        return redirect(url_for('main.login'))
    dev_key = developer.dev_key
    resp = github.authorized_response()
    if resp is None:
        return 'Access denied: reason=%s error=%s' % (
            request.args['error_reason'],
            request.args['error_description']
        )
    session['github_token'] = (resp['access_token'], '')
    token = session['github_token'][0]
    authorization = Authorization.query.filter_by(developer_id=developer.id, type='github').first()
    if authorization is None:

        authorization = Authorization(developer=developer, oauth_token=token, type='github')