<commit_before>from django.db import IntegrityError
from django.contrib.auth.models import Group

from django_odesk.core.clients import DefaultClient
from django_odesk.conf import settings

def get_odesk_permissions(auth_token):
    """
    Gets oDesk team roles/permissions for the authenticated user
    """
    client = DefaultClient(auth_token)
    response = client.hr.get_user_role()
    permissions = []
    for record in response['userrole']:
        data = dict(team_id = record['team__id'], role=record['role']) 
        if record['permissions']:
            data['permissions'] = record['permissions']['permission']
        permissions.append(data)
    return permissions



def sync_odesk_permissions(user, auth_token, create_groups=True):
    """
    Syncs oDesk team roles/permissions with Django groups
    """
    permissions = get_odesk_permissions(auth_token)
    user.groups.through.objects.filter(group__name__contains = '@odesk.com').delete()
    
    def get_or_create_group(name):
        created = False
        group = None
        try:
            group = Group.objects.get(name=name)
        except Group.DoesNotExist:
            if create_groups:
                group = Group(name=name)
                group.save()
        if group:
            user.groups.add(group)
        return group, created 

    for perm in permissions:
        name = <EMAIL>' % perm['team_id']
        group, c = get_or_create_group(name=name)
        if settings.ODESK_CREATE_PSEUDO_GROUPS:
            #Adding special groups for admins
            #May later add other pseudo-roles
            if perm['role'] == 'admin':
                name = '%s:%s<EMAIL>' % (perm['team_id'], 'admins')
                group, c = get_or_create_group(name=name)
    return True

<commit_msg>Handle roles for users with single team.<commit_after>from django.db import IntegrityError
from django.contrib.auth.models import Group

from django_odesk.core.clients import DefaultClient
from django_odesk.conf import settings

def get_odesk_permissions(auth_token):
    """
    Gets oDesk team roles/permissions for the authenticated user
    """
    client = DefaultClient(auth_token)
    response = client.hr.get_user_role()
    permissions = []
    roles = response['userrole']
    # oDesk API returns dict if there is single role
    if isinstance(roles, dict):
        roles = [roles]
    for record in roles:
        data = dict(team_id = record['team__id'], role=record['role']) 
        if record['permissions']:
            data['permissions'] = record['permissions']['permission']
        permissions.append(data)
    return permissions



def sync_odesk_permissions(user, auth_token, create_groups=True):
    """
    Syncs oDesk team roles/permissions with Django groups
    """
    permissions = get_odesk_permissions(auth_token)
    user.groups.through.objects.filter(group__name__contains = '@odesk.com').delete()
    
    def get_or_create_group(name):
        created = False
        group = None
        try:
            group = Group.objects.get(name=name)
        except Group.DoesNotExist:
            if create_groups:
                group = Group(name=name)
                group.save()
        if group:
            user.groups.add(group)
        return group, created 

    for perm in permissions:
        name = <EMAIL>' % perm['team_id']
        group, c = get_or_create_group(name=name)
        if settings.ODESK_CREATE_PSEUDO_GROUPS:
            #Adding special groups for admins
            #May later add other pseudo-roles
            if perm['role'] == 'admin':
                name = '%s:%s@<EMAIL>' % (perm['team_id'], 'admins')
                group, c = get_or_create_group(name=name)
    return True


{'fraction_non_alphanumeric': 0.10002681684097613, 'fraction_numerical': 0.0, 'mean_word_length': 2.6390243902439026, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, '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': '6928760', 'n_tokens_mistral': 1091, 'n_tokens_neox': 1073, 'n_words': 256}