Beispiel #1
0
def github_link_account_behavior():
    url = github_apis.auth_user()
    auth = OAuth2(client_id=client_id, token=session['oauth_user_token'])
    res = requests.get(url, auth=auth)
    if res.status_code != 200:
        msg = 'GitHub authorization failed'
        flash(msg, 'danger')
        return redirect(url_for('main.index'))

    github_user = res.json()
    username = github_user.get('login')
    email = github_user.get('email')
    github_url = github_user.get('html_url')
    github_avatar_url = github_user.get('avatar_url')

    if len(models.User(github_username=username)) > 0:
        msg = 'This GitHub account({0}) has been binded to another user'.format(
            username)
        flash(msg, 'danger')
        return redirect(url_for('main.index'))

    if not current_user.avatar_url:
        avatar_name = 'github_avatar_{0}.jpeg'.format(username)
        avatar_url = qiniu_fetch_img(github_avatar_url, avatar_name)
        current_user.avatar_url = avatar_url

    current_user.github_username = username
    current_user.github = github_url

    current_user.save()

    return redirect(url_for('main.index'))
Beispiel #2
0
def github_link_account_behavior():
    url = github_apis.auth_user()
    auth = OAuth2(client_id=client_id, token=session['oauth_user_token'])
    res = requests.get(url, auth=auth)
    if res.status_code != 200:
        msg = 'GitHub authorization failed'
        flash(msg, 'danger')
        return redirect(url_for('main.index'))

    github_user = res.json()
    username = github_user.get('login')
    email = github_user.get('email')
    github_url = github_user.get('html_url')
    github_avatar_url = github_user.get('avatar_url')

    if not current_user.avatar_url:
        avatar_name = 'github_avatar_{0}.jpeg'.format(username)
        avatar_url = qiniu_fetch_img(github_avatar_url, avatar_name)
        current_user.avatar_url = avatar_url

    current_user.github_username = username
    current_user.github = github_url
    
    current_user.save()

    return redirect(url_for('main.index'))
Beispiel #3
0
def github_register_behavior():
    url = github_apis.auth_user()
    auth = OAuth2(client_id=client_id, token=session['oauth_user_token'])
    res = requests.get(url, auth=auth)
    if res.status_code != 200:
        msg = 'GitHub authorization failed'
        url = url_for('accounts.register')
        # messages.add_message(request, messages.ERROR, msg)
        flash(msg, 'danger')
        return redirect(url)

    github_user = res.json()
    username = github_user.get('login')
    email = github_user.get('email')
    print('email: {0}'.format(email))
    github_url = github_user.get('html_url')
    github_avatar_url = github_user.get('avatar_url')

    users = models.User.objects.filter(github_username=username)
    if len(users) > 0:
        msg = 'You have registered with Github'
        # messages.add_message(request, messages.ERROR, msg)
        flash(msg, 'danger')
        url = url_for('accounts.login')
        return redirect(url)

    def get_unique_username(username):
        check_username = username
        while True:
            users = models.User.objects.filter(username=check_username)
            if len(users) == 0:
                return check_username
            check_username = check_username + str(randint(1, 1000))

    # def create_user(username):
    #     # try:
    #     #     user = models.User()
    #     #     user.username = username
    #     #     user.password = '******'

    #     #     user.save()

    #     #     return user

    #     # except:
    #     #     from random import random, randint
    #     #     username = username + str(randint(1, 1000))
    #     #     # return create_user(username)
    #     #     return None
    #     user = models.User()
    #     user.username = username
    #     user.password = '******'

    #     user.save()

    #     return user

    avatar_name = 'github_avatar_{0}.jpeg'.format(username)
    avatar_url = qiniu_fetch_img(github_avatar_url, avatar_name)
    checked_username = get_unique_username(username)

    user = models.User()
    user.username = checked_username
    user.password = str(datetime.datetime.now)
    user.email = email
    user.github_username = username
    user.github = github_url
    user.is_email_confirmed = True
    user.avatar_url = avatar_url
    user.save()

    login_user(user)
    user.last_login = datetime.datetime.now
    user.save()

    url = url_for('accounts.login')
    return redirect(url)
Beispiel #4
0
def github_register_behavior():
    url = github_apis.auth_user()
    auth = OAuth2(client_id=client_id, token=session['oauth_user_token'])
    res = requests.get(url, auth=auth)
    if res.status_code != 200:
        msg = 'GitHub authorization failed'
        url = url_for('accounts.register')
        # messages.add_message(request, messages.ERROR, msg)
        flash(msg, 'danger')
        return redirect(url)

    github_user = res.json()
    username = github_user.get('login')
    email = github_user.get('email')
    print 'email: {0}'.format(email)
    github_url = github_user.get('html_url')
    github_avatar_url = github_user.get('avatar_url')

    users = models.User.objects.filter(github_username=username)
    if len(users) > 0:
        msg = 'You have registered with Github'
        # messages.add_message(request, messages.ERROR, msg)
        flash(msg, 'danger')
        url = url_for('accounts.login')
        return redirect(url)

    def get_unique_username(username):
        check_username = username
        while True:
            users = models.User.objects.filter(username=check_username)
            if len(users) == 0:
                return check_username
            check_username = check_username + str(randint(1, 1000))

    # def create_user(username):
    #     # try:
    #     #     user = models.User()
    #     #     user.username = username
    #     #     user.password = '******'

    #     #     user.save()

    #     #     return user

    #     # except:
    #     #     from random import random, randint
    #     #     username = username + str(randint(1, 1000))
    #     #     # return create_user(username)
    #     #     return None
    #     user = models.User()
    #     user.username = username
    #     user.password = '******'

    #     user.save()

    #     return user

    avatar_name = 'github_avatar_{0}.jpeg'.format(username)
    avatar_url = qiniu_fetch_img(github_avatar_url, avatar_name)
    checked_username = get_unique_username(username)

    user = models.User()
    user.username = checked_username
    user.password = str(datetime.datetime.now)
    user.email = email
    user.github_username = username
    user.github = github_url
    user.is_email_confirmed = True
    user.avatar_url = avatar_url
    user.save()

    login_user(user)
    user.last_login = datetime.datetime.now
    user.save()

    url = url_for('accounts.login')
    return redirect(url)