from django.utils.translation import ugettext as _
from collections import defaultdict

from corehq.apps.users.models import (
    DomainMembershipError,
    StaticRole,
    SQLUserRole,
)


def get_editable_role_choices(domain, couch_user, allow_admin_role, use_qualified_id=True):
    """
    :param domain: roles for domain
    :param couch_user: user accessing the roles
    :param allow_admin_role: to include admin role, in case user is admin
    :param use_qualified_id: use role's qualified id as the id for the choice else the db id
    """
    def role_to_choice(role):
        return (role.get_qualified_id() if use_qualified_id else role.get_id,
                role.name or _('(No Name)'))

    roles = SQLUserRole.objects.get_by_domain(domain)
    if not couch_user.is_domain_admin(domain):
        try:
            user_role = couch_user.get_role(domain)
        except DomainMembershipError:
            user_role = None
        user_role_id = user_role.get_id if user_role else None
        roles = [
            role for role in roles
            if role.accessible_by_non_admin_role(user_role_id)
        ]
    elif allow_admin_role:
        roles = [StaticRole.domain_admin(domain)] + roles
    return [role_to_choice(role) for role in roles]


class BulkUploadResponseWrapper(object):

    def __init__(self, context):
        results = context.get('result') or defaultdict(lambda: [])
        self.response_rows = results['rows']
        self.response_errors = results['errors']
        if context['user_type'] == 'web users':
            self.problem_rows = [r for r in self.response_rows if r['flag'] not in ('updated', 'invited')]
        else:
            self.problem_rows = [r for r in self.response_rows if r['flag'] not in ('updated', 'created')]

    def success_count(self):
        return len(self.response_rows) - len(self.problem_rows)

    def has_errors(self):
        return bool(self.response_errors or self.problem_rows)

    def errors(self):
        errors = []
        for row in self.problem_rows:
            if row['flag'] == 'missing-data':
                errors.append(_('A row with no username was skipped'))
            else:
                errors.append('{username}: {flag}'.format(**row))
        errors.extend(self.response_errors)
        return errors

@@ -254,34 +254,11 @@
 role
-, use_qualified_id=True
 ):%0A
+
     
@@ -427,258 +427,11 @@
     
-:param use_qualified_id: use role's qualified id as the id for the choice else the db id%0A    %22%22%22%0A    def role_to_choice(role):%0A        return (role.get_qualified_id() if use_qualified_id else role.get_id,%0A                role.name or _('(No Name)'))%0A
+%22%22%22
 %0A   
@@ -950,28 +950,79 @@
 rn %5B
-role_to_choice(role)
+%0A        (role.get_qualified_id(), role.name or _('(No Name)'))%0A       
  for
@@ -1035,16 +1035,21 @@
 in roles
+%0A    
 %5D%0A%0A%0Aclas

remove unused 'use_qualified_id' arg