# Path: Server/jbox/models.py
# class Developer(UserMixin, db.Model):
#     __tablename__ = 'developers'
#     id = db.Column(db.Integer, primary_key=True)
#     dev_key = db.Column(db.String(40), unique=True, index=True)
#     platform = db.Column(db.String(50))
#     platform_id = db.Column(db.String(40), unique=True)
#     username = db.Column(db.String(150), index=True, default='')
#     email = db.Column(db.String(50), default='')
#     confirmed = db.Column(db.Boolean, default=False)
#     description = db.Column(db.String(150), default='')
#     avatar = db.Column(db.String(150))
#     integrations = db.relationship('Integration', backref='developer')
#     channels = db.relationship('Channel', primaryjoin='Developer.id==Channel.developer_id', backref='developer')
#     authorizations = db.relationship('Authorization', backref='developer')
# 
#     def __repr__(self):
#         return '<Developer %r>' % self.dev_key
# 
#     def verify_developer(self, platform_id):
#         return pwd_context.verify(platform_id, self.platform_id)
# 
#     def generate_confirmation_token(self, expiration=3600):
#         s = Serializer(current_app.config['SECRET_KEY'], expiration)
#         return s.dumps({'confirm': self.id})
# 
#     @staticmethod
#     def confirm(token):
#         s = Serializer(current_app.config['SECRET_KEY'])
#         try:
#             data = s.loads(token)
#         except SignatureExpired:
#             return None
#         except BadSignature:
#             return None
#         developer = Developer.query.get(data['confirm'])
#         return developer
# 
#     def insert_to_db(self):
#         db.session.add(self)
#         try:
#             db.session.commit()
#         except IntegrityError:
#             db.session.rollback()
#             b = False
#             while b == False:
#                 user = Developer.query.filter_by(dev_key=self.dev_key).first()
#                 if user is None:
#                     user = Developer.query.filter_by(platform_id=self.platform_id).first()
#                     if user is not None:
#                         self.platform_id = generate_platform_id()
#                         db.session.add(self)
#                         try:
#                             db.session.commit()
#                             b = True
#                         except IntegrityError:
#                             db.session.rollback()
#                             b = False
#                 else:
#                     self.dev_key = generate_dev_key()
#                     db.session.add(self)
#                     try:
#                         db.session.commit()
#                         b = True
#                     except IntegrityError:
#                         db.session.rollback()
#                         b = False
#         return True
# 
# class Integration(db.Model):
#     __tablename__ = 'integrations'
#     id = db.Column(db.Integer, primary_key=True)
#     integration_id = db.Column(db.String(40), unique=True)
#     name = db.Column(db.String(100), default='')
#     description = db.Column(db.String(150), default='')
#     icon = db.Column(db.String(150))
#     token = db.Column(db.String(150))
#     type = db.Column(db.String(50), default='custom')
#     owner = db.Column(db.String(100))
#     developer_id = db.Column(db.Integer, db.ForeignKey('developers.id'))
#     channel_id = db.Column(db.Integer, db.ForeignKey('channels.id'))
#     githubs = db.relationship('GitHub', backref='integration')
# 
#     def __repr__(self):
#         return '<Integration %r>' % self.integration_id
# 
#     def insert_to_db(self):
#         db.session.add(self)
#         try:
#             db.session.commit()
#         except IntegrityError:
#             db.session.rollback()
#             flag = False
#             while flag == False:
#                 integration = Integration.query.filter_by(integration_id=self.integration_id).first()
#                 if integration is not None:
#                     self.integration_id = generate_integration_id()
#                     db.session.add(self)
#                     try:
#                         db.session.commit()
#                         flag = True
#                     except IntegrityError:
#                         db.session.rollback()
#                         flag = False
#                 else:
#                     flag = True
#         return True
# 
# def generate_dev_key():
#     dev_key = ''.join([(string.ascii_letters+string.digits)[x] for x in random.sample(range(0, 62), 20)])
#     return dev_key


import os
from flask import render_template, session, redirect, url_for, flash, request, Flask, jsonify, Markup
from flask_oauthlib.client import OAuth
from . import main
from ..models import Developer, Integration, generate_dev_key

@main.route('/guide', methods=['GET'])
def guide():
    developer = get_developer()
    if developer is None:
        return render_template('guide.html', developer=developer)
    else:
        username = developer.username
        if username is None or username == '':
            return redirect(url_for('auth.setting'))
        return render_template('guide.html', developer=developer)


@main.route('/application', methods=['GET'])
def application():
    developer = get_developer()
    if developer is None:
        return render_template('application.html', developer=developer)
    else:
        username = developer.username
        if username is None or username == '':
            return redirect(url_for('auth.setting'))
        return render_template('application.html', developer=developer)



def get_developer():
    if 'qq_token' in session:
        respMe = qq.get('/oauth2.0/me', {'access_token': session['qq_token'][0]})
        openid = json_to_dict(respMe.data)['openid']

# Path: Server/jbox/models.py
# class Developer(UserMixin, db.Model):
#     __tablename__ = 'developers'
#     id = db.Column(db.Integer, primary_key=True)
#     dev_key = db.Column(db.String(40), unique=True, index=True)
#     platform = db.Column(db.String(50))
#     platform_id = db.Column(db.String(40), unique=True)
#     username = db.Column(db.String(150), index=True, default='')
#     email = db.Column(db.String(50), default='')
#     confirmed = db.Column(db.Boolean, default=False)
#     description = db.Column(db.String(150), default='')
#     avatar = db.Column(db.String(150))
#     integrations = db.relationship('Integration', backref='developer')
#     channels = db.relationship('Channel', primaryjoin='Developer.id==Channel.developer_id', backref='developer')
#     authorizations = db.relationship('Authorization', backref='developer')
# 
#     def __repr__(self):
#         return '<Developer %r>' % self.dev_key
# 
#     def verify_developer(self, platform_id):
#         return pwd_context.verify(platform_id, self.platform_id)
# 
#     def generate_confirmation_token(self, expiration=3600):
#         s = Serializer(current_app.config['SECRET_KEY'], expiration)
#         return s.dumps({'confirm': self.id})
# 
#     @staticmethod
#     def confirm(token):
#         s = Serializer(current_app.config['SECRET_KEY'])
#         try:
#             data = s.loads(token)
#         except SignatureExpired:
#             return None
#         except BadSignature:
#             return None
#         developer = Developer.query.get(data['confirm'])
#         return developer
# 
#     def insert_to_db(self):
#         db.session.add(self)
#         try:
#             db.session.commit()
#         except IntegrityError:
#             db.session.rollback()
#             b = False
#             while b == False:
#                 user = Developer.query.filter_by(dev_key=self.dev_key).first()
#                 if user is None:
#                     user = Developer.query.filter_by(platform_id=self.platform_id).first()
#                     if user is not None:
#                         self.platform_id = generate_platform_id()
#                         db.session.add(self)
#                         try:
#                             db.session.commit()
#                             b = True
#                         except IntegrityError:
#                             db.session.rollback()
#                             b = False
#                 else:
#                     self.dev_key = generate_dev_key()
#                     db.session.add(self)
#                     try:
#                         db.session.commit()
#                         b = True
#                     except IntegrityError:
#                         db.session.rollback()
#                         b = False
#         return True
# 
# class Integration(db.Model):
#     __tablename__ = 'integrations'
#     id = db.Column(db.Integer, primary_key=True)
#     integration_id = db.Column(db.String(40), unique=True)
#     name = db.Column(db.String(100), default='')
#     description = db.Column(db.String(150), default='')
#     icon = db.Column(db.String(150))
#     token = db.Column(db.String(150))
#     type = db.Column(db.String(50), default='custom')
#     owner = db.Column(db.String(100))
#     developer_id = db.Column(db.Integer, db.ForeignKey('developers.id'))
#     channel_id = db.Column(db.Integer, db.ForeignKey('channels.id'))
#     githubs = db.relationship('GitHub', backref='integration')
# 
#     def __repr__(self):
#         return '<Integration %r>' % self.integration_id
# 
#     def insert_to_db(self):
#         db.session.add(self)
#         try:
#             db.session.commit()
#         except IntegrityError:
#             db.session.rollback()
#             flag = False
#             while flag == False:
#                 integration = Integration.query.filter_by(integration_id=self.integration_id).first()
#                 if integration is not None:
#                     self.integration_id = generate_integration_id()
#                     db.session.add(self)
#                     try:
#                         db.session.commit()
#                         flag = True
#                     except IntegrityError:
#                         db.session.rollback()
#                         flag = False
#                 else:
#                     flag = True
#         return True
# 
# def generate_dev_key():
#     dev_key = ''.join([(string.ascii_letters+string.digits)[x] for x in random.sample(range(0, 62), 20)])
#     return dev_key


# Path: Server/jbox/main/views.py
import os
from flask import render_template, session, redirect, url_for, flash, request, Flask, jsonify, Markup
from flask_oauthlib.client import OAuth
from . import main
from ..models import Developer, Integration, generate_dev_key


@main.route('/guide', methods=['GET'])
def guide():
    developer = get_developer()
    if developer is None:
        return render_template('guide.html', developer=developer)
    else:
        username = developer.username
        if username is None or username == '':
            return redirect(url_for('auth.setting'))
        return render_template('guide.html', developer=developer)


@main.route('/application', methods=['GET'])
def application():
    developer = get_developer()
    if developer is None:
        return render_template('application.html', developer=developer)
    else:
        username = developer.username
        if username is None or username == '':
            return redirect(url_for('auth.setting'))
        return render_template('application.html', developer=developer)



def get_developer():
    if 'qq_token' in session:
        respMe = qq.get('/oauth2.0/me', {'access_token': session['qq_token'][0]})
        openid = json_to_dict(respMe.data)['openid']

        developer = Developer.query.filter_by(platform_id=openid).first()