Exemple #1
0
def login_oauth():
    if "access_token" not in session:
        return redirect(url_for("index"))

    params = {
        "site": "ru.stackoverflow",
        "order": "desc",
        "sort": "reputation",
        "key": STACKEXCHANGE_CLIENT_KEY, 
        "access_token": session['access_token']
    }

    r = requests.get(STACKEXCHANGE_OAUTH_ME_ENDPOINT, data=params)
    data = json.loads(r.text)
    account_id = -1
    user_id = -1
    display_name = ""
    role = ""
    reputation = -1
    profile_image = ""
    link = ""
    if data.get("items", None) is not None:
        for item in data["items"]:
            if item.get("account_id", None) is not None:
                account_id = item["account_id"]
            if item.get("user_id", None) is not None:
                user_id = item["user_id"]
            if item.get("display_name", None) is not None:    
                display_name = item["display_name"]
            if item.get("user_type", None) is not None:    
                role = item["user_type"]
            if item.get("reputation", None) is not None:    
                reputation = item["reputation"]
            if item.get("profile_image", None) is not None:    
                profile_image = item["profile_image"]
            if item.get("link", None) is not None:    
                link = item["link"]
    
    if account_id < 0 or user_id < 0 or display_name == "":
        logging.error("OAuth response: %s. Url: %s" % (r.text, r.url))
        logging.error("account_id: %s, user_id: %s, display_name: %s" % (str(account_id), str(user_id), display_name))
        return redirect(url_for("no_way"))

    adder = DBModelAdder()
    adder.start()
    user = User.get_by_account_id(account_id)
    if user is None:
        adder.add(User(account_id, 
            user_id, 
            display_name, 
            reputation, 
            profile_image,
            link,
            role))
    else:
        if user.username != display_name:
            user.username = display_name
        if user.role != role:
            user.role = role
        if user.reputation != reputation:
            user.reputation = reputation
        if user.profile_image != profile_image:
            user.profile_image = profile_image
        if user.profile_link != link:
            user.profile_link = link
        adder.add(user)
    adder.done()

    session["account_id"] = account_id
    return redirect(url_for("index"))
def before_request():
    g.user = None
    if 'account_id' in session:
        g.user = User.get_by_account_id(int(session['account_id']))