<commit_before>"""Bitbucket user page on Gittip.
"""
import decimal
import os
import re

import requests
from aspen import json, Response
from gittip import AMOUNTS, CARDINALS, db
from gittip.elsewhere import bitbucket
from gittip.models import Participant


# ========================================================================== ^L

# Try to load from Bitbucket.
# ===========================

user_info = bitbucket.get_user_info(path['username'])

# Try to load from Gittip.
# ========================
# We can only tip Users, not Organizations (or whatever else type can be).

username = user_info['username']
name = user_info.get('display_name')
if not name:
    name = username

is_team = user_info.get("is_team", None)
usertype = "unknown type of account"
if is_team is None:
    can_tip = False
    img_src = "/assets/-/avatar-default.gif"
elif not is_team:
    usertype = "user"
    account = bitbucket.BitbucketAccount(unicode(username), user_info)
    locked = account.is_locked
    lock_action = "unlock" if account.is_locked else "lock"
    if account.is_claimed:
        request.redirect('/%s/' % account.participant_id)

    participant = Participant.query.get(account.participant_id)

    if not user.ANON:
        my_tip = user.get_tip_to(participant.id)

    tip_or_pledge = "pledge"
    number = participant.get_number_of_backers()
    img_src = participant.get_img_src(128)

elif is_team:
    usertype = "organization"

    bb_url = "https://bitbucket.org/api/2.0/users/%s/members" % username
    members = []
    resp = requests.get(bb_url)
    if resp.status_code is not 200:
        raise Response(404, "Bitbucket is currently unavailable")
    else:
        bb_data = json.loads(resp.text)
        for m in bb_data['values']:
            members.append(m)

        while 'next' in bb_data:
            next_url = u"https://bitbucket.org%s" % bb_data['next']
            resp = requests.get(next_url)
            bb_data = json.loads(resp.text)
            for m in bb_data['values']:
                members.append(m)

    usernames = [member['username'] for member in members]

    ON_GITTIP = """\

        SELECT elsewhere.user_info -> 'username' AS login
          FROM elsewhere
          JOIN participants p
            ON p.id = elsewhere.participant_id
         WHERE elsewhere.platform = 'bitbucket'
           AND elsewhere.user_info -> 'username' = any(%s)
           AND p.claimed_time IS NOT NULL

    """
    _on_gittip = [rec['username'] for rec in db.fetchall(ON_GITTIP, (usernames,))]
    on_gittip = []
    not_on_gittip = []<commit_msg>Fix bug in Bb team pages; #149<commit_after>"""Bitbucket user page on Gittip.
"""
import decimal
import os
import re

import requests
from aspen import json, Response
from gittip import AMOUNTS, CARDINALS, db
from gittip.elsewhere import bitbucket
from gittip.models import Participant


# ========================================================================== ^L

# Try to load from Bitbucket.
# ===========================

user_info = bitbucket.get_user_info(path['username'])

# Try to load from Gittip.
# ========================
# We can only tip Users, not Organizations (or whatever else type can be).

username = user_info['username']
name = user_info.get('display_name')
if not name:
    name = username

is_team = user_info.get("is_team", None)
usertype = "unknown type of account"
if is_team is None:
    can_tip = False
    img_src = "/assets/-/avatar-default.gif"
elif not is_team:
    usertype = "user"
    account = bitbucket.BitbucketAccount(unicode(username), user_info)
    locked = account.is_locked
    lock_action = "unlock" if account.is_locked else "lock"
    if account.is_claimed:
        request.redirect('/%s/' % account.participant_id)

    participant = Participant.query.get(account.participant_id)

    if not user.ANON:
        my_tip = user.get_tip_to(participant.id)

    tip_or_pledge = "pledge"
    number = participant.get_number_of_backers()
    img_src = participant.get_img_src(128)

elif is_team:
    usertype = "organization"

    bb_url = "https://bitbucket.org/api/2.0/users/%s/members" % username
    members = []
    resp = requests.get(bb_url)
    if resp.status_code is not 200:
        raise Response(404, "Bitbucket is currently unavailable")
    else:
        bb_data = json.loads(resp.text)
        for m in bb_data['values']:
            members.append(m)

        while 'next' in bb_data:
            next_url = u"https://bitbucket.org%s" % bb_data['next']
            resp = requests.get(next_url)
            bb_data = json.loads(resp.text)
            for m in bb_data['values']:
                members.append(m)

    usernames = [member['username'] for member in members]

    ON_GITTIP = """\

        SELECT elsewhere.user_info -> 'username' AS username
          FROM elsewhere
          JOIN participants p
            ON p.id = elsewhere.participant_id
         WHERE elsewhere.platform = 'bitbucket'
           AND elsewhere.user_info -> 'username' = any(%s)
           AND p.claimed_time IS NOT NULL

    """
    _on_gittip = [rec['username'] for rec in db.fetchall(ON_GITTIP, (usernames,))]
    on_gittip = []
    not_on_gittip = []
{'fraction_non_alphanumeric': 0.14957594448727835, 'fraction_numerical': 0.00481881264456438, 'mean_word_length': 3.10197628458498, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2256166', 'n_tokens_mistral': 1686, 'n_tokens_neox': 1589, 'n_words': 410}