Пример #1
0
def _update_group_membership(request, domain, group_id):
    group = Group.get(group_id)
    if group.domain != domain:
        return HttpResponseForbidden()

    selected_users = request.POST.getlist('selected_ids')

    # check to make sure no users were deleted at time of making group
    users = iter_docs(CouchUser.get_db(), selected_users)
    safe_users = [
        CouchUser.wrap_correctly(user) for user in users
        if user['doc_type'] == 'CommCareUser' and user.get('domain') == domain
    ]
    safe_ids = [user.user_id for user in safe_users]
    users_added_ids, users_removed_ids = group.set_user_ids(safe_ids)
    _ensure_case_sharing_privilege(request, group)

    group.save()

    # re-fetch users to get fresh groups
    for updated_user_doc in iter_docs(CouchUser.get_db(), set.union(users_added_ids, users_removed_ids)):
        updated_user = CouchUser.wrap_correctly(updated_user_doc)
        log_user_groups_change(domain, request, updated_user)

    messages.success(request, _("Group %s updated!") % group.name)
    return HttpResponseRedirect(reverse("group_members", args=[domain, group_id]))
Пример #2
0
    def handle(self, *args, **options):
        self.stdout.write("Population location_id field...\n")

        relevant_ids = set([
            r['id'] for r in CouchUser.get_db().view(
                'users/by_username',
                reduce=False,
            ).all()
        ])

        to_save = []

        domain_cache = {}

        exclude = (
            "drewpsi",
            "psi",
            "psi-ors",
            "psi-test",
            "psi-test2",
            "psi-test3",
            "psi-unicef",
            "psi-unicef-wb",
        )

        def _is_location_domain(domain):
            if domain in domain_cache:
                return domain_cache[domain]
            else:
                domain_obj = Domain.get_by_name(domain)
                val = domain_obj.uses_locations
                domain_cache[domain] = val
                return val

        for user_doc in iter_docs(CommCareUser.get_db(), relevant_ids):
            if user_doc['doc_type'] == 'WebUser':
                continue

            if user_doc['domain'] in exclude:
                continue

            if not _is_location_domain(user_doc['domain']):
                continue

            user = CommCareUser.get(user_doc['_id'])

            if user._locations:
                user_doc['location_id'] = user._locations[0]._id
                to_save.append(user_doc)

            if len(to_save) > 500:
                self.stdout.write("Saving 500")
                CouchUser.get_db().bulk_save(to_save)
                to_save = []

        if to_save:
            CouchUser.get_db().bulk_save(to_save)
Пример #3
0
def get_users_assigned_to_locations(domain):
    from corehq.apps.users.models import CouchUser
    ids = [res['id'] for res in CouchUser.get_db().view(
        'users_extra/users_by_location_id',
        startkey=[domain],
        endkey=[domain, {}],
        include_docs=False,
        reduce=False,
    )]
    return map(CouchUser.wrap_correctly, iter_docs(CouchUser.get_db(), ids))
Пример #4
0
def get_users_assigned_to_locations(domain):
    from corehq.apps.users.models import CouchUser
    ids = [res['id'] for res in CouchUser.get_db().view(
        'users_extra/users_by_location_id',
        startkey=[domain],
        endkey=[domain, {}],
        include_docs=False,
        reduce=False,
    )]
    return imap(CouchUser.wrap_correctly, iter_docs(CouchUser.get_db(), ids))
Пример #5
0
    def handle(self, *args, **options):
        self.stdout.write("Population location_id field...\n")

        relevant_ids = set([r['id'] for r in CouchUser.get_db().view(
            'users/by_username',
            reduce=False,
        ).all()])

        to_save = []

        domain_cache = {}

        exclude = (
            "drewpsi",
            "psi",
            "psi-ors",
            "psi-test",
            "psi-test2",
            "psi-test3",
            "psi-unicef",
            "psi-unicef-wb",
        )

        def _is_location_domain(domain):
            if domain in domain_cache:
                return domain_cache[domain]
            else:
                domain_obj = Domain.get_by_name(domain)
                val = domain_obj.uses_locations
                domain_cache[domain] = val
                return val

        for user_doc in iter_docs(CommCareUser.get_db(), relevant_ids):
            if user_doc['doc_type'] == 'WebUser':
                continue

            if user_doc['domain'] in exclude:
                continue

            if not _is_location_domain(user_doc['domain']):
                continue

            user = CommCareUser.get(user_doc['_id'])

            if user._locations:
                user_doc['location_id'] = user._locations[0]._id
                to_save.append(user_doc)

            if len(to_save) > 500:
                self.stdout.write("Saving 500")
                CouchUser.get_db().bulk_save(to_save)
                to_save = []

        if to_save:
            CouchUser.get_db().bulk_save(to_save)
Пример #6
0
def deactivate_users_at_location(location_id):
    from corehq.apps.locations.dbaccessors import mobile_user_ids_at_locations
    user_ids = mobile_user_ids_at_locations([location_id])
    for doc in iter_docs(CouchUser.get_db(), user_ids):
        user = CouchUser.wrap_correctly(doc)
        user.is_active = False
        user.save(spawn_task=True)
 def handle(self, **options):
     self.options = options
     user_ids = with_progress_bar(self.get_user_ids())
     iter_update(CouchUser.get_db(),
                 self.migrate_user,
                 user_ids,
                 verbose=True)
Пример #8
0
    def handle(self, *args, **options):
        all_usernames = set(
            map(
                lambda x: x['key'],
                CouchUser.get_db().view(
                    'users/by_username',
                    include_docs=False,
                    reduce=False,
                ).all()
            )
        )
        uppercase_usernames = filter(
            lambda username: any(char.isupper() for char in username),
            all_usernames
        )

        print 'Number of uppercase usernames: %d' % len(uppercase_usernames)

        for username in uppercase_usernames:
            print 'Making %s lowercase' % username
            if username.lower() not in all_usernames:
                user = CouchUser.get_by_username(username)
                user.username = username.lower()
                user.save()
            else:
                print '%s already exists' % username.lower()
Пример #9
0
def update_users_at_locations(domain, location_ids, supply_point_ids, ancestor_ids):
    """
    Update location fixtures for users given locations
    """
    from corehq.apps.users.models import CouchUser, update_fixture_status_for_users
    from corehq.apps.locations.dbaccessors import user_ids_at_locations
    from corehq.apps.fixtures.models import UserFixtureType
    from dimagi.utils.couch.database import iter_docs

    # close supply point cases
    for supply_point_id in supply_point_ids:
        close_supply_point_case(domain, supply_point_id)

    # unassign users from locations
    unassign_user_ids = user_ids_at_locations(location_ids)
    for doc in iter_docs(CouchUser.get_db(), unassign_user_ids):
        user = CouchUser.wrap_correctly(doc)
        for location_id in location_ids:
            if location_id not in user.get_location_ids(domain):
                continue
            if user.is_web_user():
                user.unset_location_by_id(domain, location_id, fall_back_to_next=True)
            elif user.is_commcare_user():
                user.unset_location_by_id(location_id, fall_back_to_next=True)

    # update fixtures for users at ancestor locations
    user_ids = user_ids_at_locations(ancestor_ids)
    update_fixture_status_for_users(user_ids, UserFixtureType.LOCATION)
Пример #10
0
def _get_user_results_by_username(usernames, include_docs=True):
    return CouchUser.get_db().view(
        'users/by_username',
        keys=list(usernames),
        reduce=False,
        include_docs=include_docs,
    ).all()
Пример #11
0
def _get_deleted_user_results_by_username(usernames):
    return CouchUser.get_db().view(
        'deleted_users_by_username/view',
        keys=list(usernames),
        reduce=False,
        include_docs=False,
    ).all()
Пример #12
0
def get_all_user_ids():
    return [
        res['id'] for res in CouchUser.get_db().view(
            'users/by_username',
            reduce=False,
        ).all()
    ]
Пример #13
0
def update_users_at_locations(domain, location_ids, supply_point_ids,
                              ancestor_ids):
    """
    Update location fixtures for users given locations
    """
    from corehq.apps.users.models import CouchUser, update_fixture_status_for_users
    from corehq.apps.locations.dbaccessors import mobile_user_ids_at_locations
    from corehq.apps.fixtures.models import UserLookupTableType
    from dimagi.utils.couch.database import iter_docs

    # close supply point cases
    for supply_point_id in supply_point_ids:
        close_supply_point_case(domain, supply_point_id)

    # unassign users from locations
    unassign_user_ids = mobile_user_ids_at_locations(location_ids)
    for doc in iter_docs(CouchUser.get_db(), unassign_user_ids):
        user = CouchUser.wrap_correctly(doc)
        for location_id in location_ids:
            if location_id not in user.get_location_ids(domain):
                continue
            if user.is_web_user():
                user.unset_location_by_id(domain,
                                          location_id,
                                          fall_back_to_next=True)
            elif user.is_commcare_user():
                user.unset_location_by_id(location_id, fall_back_to_next=True)

    # update fixtures for users at ancestor locations
    user_ids = mobile_user_ids_at_locations(ancestor_ids)
    update_fixture_status_for_users(user_ids, UserLookupTableType.LOCATION)
Пример #14
0
def get_user_docs_by_username(usernames):
    from corehq.apps.users.models import CouchUser
    return [res['doc'] for res in CouchUser.get_db().view(
        'users/by_username',
        keys=list(usernames),
        reduce=False,
        include_docs=True,
    ).all()]
Пример #15
0
def get_user_docs_by_username(usernames):
    from corehq.apps.users.models import CouchUser
    return [res['doc'] for res in CouchUser.get_db().view(
        'users/by_username',
        keys=list(usernames),
        reduce=False,
        include_docs=True,
    ).all()]
def _get_user_results_by_username(usernames, include_docs=True):
    from corehq.apps.users.models import CouchUser
    return CouchUser.get_db().view(
        'users/by_username',
        keys=list(usernames),
        reduce=False,
        include_docs=include_docs,
    ).all()
def get_all_user_ids():
    from corehq.apps.users.models import CouchUser
    return [
        res['id'] for res in CouchUser.get_db().view(
            'users/by_username',
            reduce=False,
        ).all()
    ]
Пример #18
0
def django_user_from_couch_id(id):
    """
    From a couch id of a profile object, get the django user
    """
    # get the couch doc
    from corehq.apps.users.models import CouchUser
    couch_rep = CouchUser.get_db().get(id)
    django_id = couch_rep["django_user"]["id"]
    return User.objects.get(id=django_id)
Пример #19
0
def refresh_es_for_profile_users(domain, profile_id):
    try:
        profile = CustomDataFieldsProfile.objects.get(
            id=profile_id, definition__domain=domain)
    except CustomDataFieldsProfile.DoesNotExist:
        return

    for user_doc in iter_docs(CouchUser.get_db(), profile.user_ids_assigned()):
        update_user_in_es(None, CouchUser.wrap_correctly(user_doc))
Пример #20
0
def django_user_from_couch_id(id):
    """
    From a couch id of a profile object, get the django user
    """
    # get the couch doc
    from corehq.apps.users.models import CouchUser
    couch_rep = CouchUser.get_db().get(id)
    django_id = couch_rep["django_user"]["id"]
    return User.objects.get(id=django_id)
Пример #21
0
def users_have_locations(domain):
    from corehq.apps.users.models import CouchUser
    return bool(CouchUser.get_db().view(
        'users_extra/users_by_location_id',
        startkey=[domain],
        endkey=[domain, {}],
        reduce=True,
        stale=stale_ok(),
    ).one())
Пример #22
0
def num_mobile_users(domain, *args):
    startkey = ['active', domain, 'CommCareUser']
    endkey = startkey + [{}]
    result = CouchUser.get_db().view('users/by_domain',
                                     startkey=startkey,
                                     endkey=endkey,
                                     include_docs=False,
                                     reduce=True).one()
    return result['value'] if result else 0
Пример #23
0
def get_all_users_by_location(domain, location_id):
    from corehq.apps.users.models import CouchUser
    results = CouchUser.get_db().view(
        'users_extra/users_by_location_id',
        startkey=[domain, location_id],
        endkey=[domain, location_id, {}],
        include_docs=True,
        reduce=False,
    )
    return (CouchUser.wrap_correctly(res['doc']) for res in results)
Пример #24
0
def get_all_users_by_location(domain, location_id):
    from corehq.apps.users.models import CouchUser
    results = CouchUser.get_db().view(
        'users_extra/users_by_location_id',
        startkey=[domain, location_id],
        endkey=[domain, location_id, {}],
        include_docs=True,
        reduce=False,
    )
    return (CouchUser.wrap_correctly(res['doc']) for res in results)
Пример #25
0
def user_id_to_username(user_id):
    from corehq.apps.users.models import CouchUser
    if not user_id:
        return user_id
    elif user_id == "demo_user":
        return "demo_user"
    try:
        login = CouchUser.get_db().get(user_id)
    except ResourceNotFound:
        return None
    return raw_username(login['username']) if "username" in login else None
Пример #26
0
def user_id_to_username(user_id):
    from corehq.apps.users.models import CouchUser
    if not user_id:
        return user_id
    elif user_id == DEMO_USER_ID:
        return DEMO_USER_ID
    try:
        login = CouchUser.get_db().get(user_id)
    except ResourceNotFound:
        return None
    return raw_username(login['username']) if "username" in login else None
Пример #27
0
def user_id_to_username(user_id):
    from corehq.apps.users.models import CouchUser

    if not user_id:
        return user_id
    elif user_id == DEMO_USER_ID:
        return DEMO_USER_ID
    try:
        login = CouchUser.get_db().get(user_id)
    except ResourceNotFound:
        return None
    return raw_username(login["username"]) if "username" in login else None
    def handle(self, *args, **options):
        self.stdout.write("...\n")

        relevant_ids = set([r['id'] for r in CouchUser.get_db().view(
            'users/by_username',
            reduce=False,
        ).all()])

        to_save = []

        for user_doc in iter_docs(CouchUser.get_db(), relevant_ids):
            if 'commtrack_location' in user_doc:
                user = CommCareUser.get(user_doc['_id'])

                try:
                    original_location_object = Location.get(user['commtrack_location'])
                except ResourceNotFound:
                    # if there was bad data in there before, we can ignore it
                    continue
                user.set_locations([original_location_object])

                del user_doc['commtrack_location']

                to_save.append(user_doc)

                if len(to_save) > 500:
                    CouchUser.get_db().bulk_save(to_save)
                    to_save = []

        if to_save:
            CouchUser.get_db().bulk_save(to_save)
    def handle(self, *args, **options):
        self.stdout.write("...\n")

        relevant_ids = set([
            r['id'] for r in CouchUser.get_db().view(
                'users/by_username',
                reduce=False,
            ).all()
        ])

        to_save = []

        for user_doc in iter_docs(CouchUser.get_db(), relevant_ids):
            if 'commtrack_location' in user_doc:
                user = CommCareUser.get(user_doc['_id'])

                try:
                    original_location_object = Location.get(
                        user['commtrack_location'])
                except ResourceNotFound:
                    # if there was bad data in there before, we can ignore it
                    continue
                user.set_location(original_location_object)

                del user_doc['commtrack_location']

                to_save.append(user_doc)

                if len(to_save) > 500:
                    CouchUser.get_db().bulk_save(to_save)
                    to_save = []

        if to_save:
            CouchUser.get_db().bulk_save(to_save)
Пример #30
0
def get_doc_info_by_id(domain, id):
    not_found_value = DocInfo(display=id, link=None, owner_type=None)
    if not id:
        return not_found_value
    try:
        doc = CouchUser.get_db().get(id)
    except ResourceNotFound:
        return not_found_value

    if doc.get("domain") != domain and domain not in doc.get("domains", ()):
        return not_found_value

    return get_doc_info(doc, domain_hint=domain)
Пример #31
0
def get_doc_info_by_id(domain, id):
    not_found_value = DocInfo(display=id, link=None, owner_type=None)
    if not id:
        return not_found_value
    try:
        doc = CouchUser.get_db().get(id)
    except ResourceNotFound:
        return not_found_value

    if doc.get('domain') != domain and domain not in doc.get('domains', ()):
        return not_found_value

    return get_doc_info(doc, domain_hint=domain)
Пример #32
0
def user_id_to_username(user_id):
    from corehq.apps.users.models import CouchUser
    if not user_id:
        return None
    if isinstance(user_id, numbers.Number):
        # couch chokes on numbers so just short-circuit this
        return None
    elif user_id == DEMO_USER_ID:
        return DEMO_USER_ID
    try:
        login = CouchUser.get_db().get(user_id)
    except ResourceNotFound:
        return None
    return raw_username(login['username']) if "username" in login else None
Пример #33
0
    def process_view(self, request, view_func, view_args, view_kwargs):
        if 'domain' in view_kwargs:
            request.domain = view_kwargs['domain']
        if 'org' in view_kwargs:
            request.org = view_kwargs['org']
        if request.user and hasattr(request.user, 'get_profile'):
            sessionid = request.COOKIES.get('sessionid', None)
            if sessionid:
                # roundabout way to keep doc_id based caching consistent.
                # get user doc_id from session_id
                MISSING = object()
                INTERRUPTED = object()
                try:
                    cached_user_doc_id = rcache.get(
                        SESSION_USER_KEY_PREFIX % sessionid, MISSING)
                except ConnectionInterrumped:
                    cached_user_doc_id = INTERRUPTED

                # disable session based couch user caching - to be enabled later.
                if cached_user_doc_id not in (MISSING, INTERRUPTED):
                    # cache hit
                    couch_user = CouchUser.wrap_correctly(
                        cache_core.cached_open_doc(CouchUser.get_db(),
                                                   cached_user_doc_id))
                else:
                    # cache miss, write to cache
                    couch_user = CouchUser.from_django_user(request.user)
                    if couch_user:
                        cache_core.do_cache_doc(couch_user.to_json())
                        if cached_user_doc_id is not INTERRUPTED:
                            rcache.set(SESSION_USER_KEY_PREFIX % sessionid,
                                       couch_user.get_id)
                request.couch_user = couch_user

            if 'domain' in view_kwargs:
                domain = request.domain
                if not request.couch_user:
                    couch_domain = Domain.view(
                        "domain/domains",
                        key=domain,
                        reduce=False,
                        include_docs=True,
                    ).one()
                    if couch_domain and couch_domain.is_public:
                        request.couch_user = PublicUser(domain)
                    else:
                        request.couch_user = InvalidUser()
                if request.couch_user:
                    request.couch_user.current_domain = domain
        return None
 def handle(self, *args, **options):
     run_fix = options.get(RUN_FIX, False)
     for user in CouchUser.all():
         doc_json = CouchUser.get_db().get(user.get_id)
         if doc_json.get("doc_type", None) == "WebUser" and (
             user.created_on is not None and user.created_on >= datetime.datetime(*[int(_) for _ in args[0:3]])
         ):
             if user.email_opt_out:
                 if run_fix:
                     user.email_opt_out = False
                     user.save()
                     print("fixed %s, created on %s" % (user.get_id, user.created_on))
                 else:
                     print("should fix %s, created on %s" % (user.get_id, user.created_on))
Пример #35
0
def get_doc_info_by_id(domain, id):
    not_found_value = DocInfo(display=id, link=None, owner_type=None)
    if not id:
        return not_found_value
    id = loc_group_id_or_none(id) or id  # strip prefix if it's a location group
    try:
        doc = CouchUser.get_db().get(id)
    except ResourceNotFound:
        return not_found_value

    if doc.get('domain') != domain and domain not in doc.get('domains', ()):
        return not_found_value

    return get_doc_info(doc, domain_hint=domain)
Пример #36
0
 def handle(self, *args, **options):
     db = CouchUser.get_db()
     # This view includes users with base_doc == CouchUser-Deleted
     for res in db.view("users/by_default_phone", include_docs=True, reduce=False):
         doc = res['doc']
         # if this condition is met, the doc can't be wrapped
         if doc['email'] and not doc['email'].islower():
             print doc['email']
             doc['email'] = doc['email'].lower()
             try:
                 user = CouchUser.wrap_correctly(doc)
                 user.save()
             except:
                 print doc['_id'], "failed to save"
Пример #37
0
    def process_view(self, request, view_func, view_args, view_kwargs):
        if 'domain' in view_kwargs:
            request.domain = view_kwargs['domain']
        if 'org' in view_kwargs:
            request.org = view_kwargs['org']
        if request.user and hasattr(request.user, 'get_profile'):
            sessionid = request.COOKIES.get('sessionid', None)
            if sessionid:
                # roundabout way to keep doc_id based caching consistent.
                # get user doc_id from session_id
                MISSING = object()
                INTERRUPTED = object()
                try:
                    cached_user_doc_id = rcache.get(SESSION_USER_KEY_PREFIX % sessionid, MISSING)
                except ConnectionInterrumped:
                    cached_user_doc_id = INTERRUPTED

                # disable session based couch user caching - to be enabled later.
                if cached_user_doc_id not in (MISSING, INTERRUPTED):
                    # cache hit
                    couch_user = CouchUser.wrap_correctly(
                        cache_core.cached_open_doc(
                            CouchUser.get_db(), cached_user_doc_id
                        )
                    )
                else:
                    # cache miss, write to cache
                    couch_user = CouchUser.from_django_user(request.user)
                    if couch_user:
                        cache_core.do_cache_doc(couch_user.to_json())
                        if cached_user_doc_id is not INTERRUPTED:
                            rcache.set(SESSION_USER_KEY_PREFIX % sessionid, couch_user.get_id)
                request.couch_user = couch_user

            if 'domain' in view_kwargs:
                domain = request.domain
                if not request.couch_user:
                    couch_domain = Domain.view("domain/domains",
                        key=domain,
                        reduce=False,
                        include_docs=True,
                    ).one()
                    if couch_domain and couch_domain.is_public:
                        request.couch_user = PublicUser(domain)
                    else:
                        request.couch_user = InvalidUser()
                if request.couch_user:
                    request.couch_user.current_domain = domain
        return None
Пример #38
0
def _unassign_users_from_location(domain, location_id):
    """
    Unset location for all users assigned to that location.
    """
    from corehq.apps.locations.dbaccessors import user_ids_at_locations
    from corehq.apps.users.models import CouchUser
    from dimagi.utils.couch.database import iter_docs

    user_ids = user_ids_at_locations([location_id])
    for doc in iter_docs(CouchUser.get_db(), user_ids):
        user = CouchUser.wrap_correctly(doc)
        if user.is_web_user():
            user.unset_location_by_id(domain, location_id, fall_back_to_next=True)
        elif user.is_commcare_user():
            user.unset_location_by_id(location_id, fall_back_to_next=True)
Пример #39
0
def _unassign_users_from_location(domain, location_id):
    """
    Unset location for all users assigned to that location.
    """
    from corehq.apps.locations.dbaccessors import user_ids_at_locations
    from corehq.apps.users.models import CouchUser
    from dimagi.utils.couch.database import iter_docs

    user_ids = user_ids_at_locations([location_id])
    for doc in iter_docs(CouchUser.get_db(), user_ids):
        user = CouchUser.wrap_correctly(doc)
        if user.is_web_user():
            user.unset_location_by_id(domain, location_id, fall_back_to_next=True)
        elif user.is_commcare_user():
            user.unset_location_by_id(location_id, fall_back_to_next=True)
 def handle(self, *args, **options):
     run_fix = options.get(RUN_FIX, False)
     for user in CouchUser.all():
         doc_json = CouchUser.get_db().get(user.get_id)
         if (doc_json.get('doc_type', None) == 'WebUser'
                 and (user.created_on is not None and user.created_on >=
                      datetime.datetime(*[int(_) for _ in args[0:3]]))):
             if user.email_opt_out:
                 if run_fix:
                     user.email_opt_out = False
                     user.save()
                     print('fixed %s, created on %s' %
                           (user.get_id, user.created_on))
                 else:
                     print('should fix %s, created on %s' %
                           (user.get_id, user.created_on))
Пример #41
0
def all_domain_stats():
    webuser_counts = defaultdict(lambda: 0)
    commcare_counts = defaultdict(lambda: 0)

    for row in CouchUser.get_db().view('users/by_domain', startkey=["active"],
                             endkey=["active", {}], group_level=3).all():
        _, domain, doc_type = row['key']
        value = row['value']
        {
            'WebUser': webuser_counts,
            'CommCareUser': commcare_counts
        }[doc_type][domain] = value

    return {
        "web_users": webuser_counts,
        "commcare_users": commcare_counts,
    }
Пример #42
0
def all_domain_stats():
    webuser_counts = defaultdict(int)
    commcare_counts = defaultdict(int)

    for row in CouchUser.get_db().view('users/by_domain', startkey=["active"],
                                       endkey=["active", {}], group_level=3).all():
        _, domain, doc_type = row['key']
        value = row['value']
        {
            'WebUser': webuser_counts,
            'CommCareUser': commcare_counts
        }[doc_type][domain] = value

    return {
        "web_users": webuser_counts,
        "commcare_users": commcare_counts,
    }
    def handle(self, *args, **options):
        all_usernames = set(
            map(
                lambda x: x["key"], CouchUser.get_db().view("users/by_username", include_docs=False, reduce=False).all()
            )
        )
        uppercase_usernames = filter(lambda username: any(char.isupper() for char in username), all_usernames)

        print "Number of uppercase usernames: %d" % len(uppercase_usernames)

        for username in uppercase_usernames:
            print "Making %s lowercase" % username
            if username.lower() not in all_usernames:
                user = CouchUser.get_by_username(username)
                user.username = username.lower()
                user.save()
            else:
                print "%s already exists" % username.lower()
Пример #44
0
def user_id_to_username(user_id, use_name_if_available=False):
    from corehq.apps.users.models import CouchUser
    if not user_id:
        return None
    if isinstance(user_id, numbers.Number):
        # couch chokes on numbers so just short-circuit this
        return None
    elif user_id == DEMO_USER_ID:
        return DEMO_USER_ID
    try:
        user_object = CouchUser.get_db().get(user_id)
    except ResourceNotFound:
        return None

    if use_name_if_available and (user_object.get('first_name', '') or user_object.get('last_name', '')):
        return ' '.join([user_object.get('first_name', ''), user_object.get('last_name', '')]).strip()
    else:
        return raw_username(user_object['username']) if "username" in user_object else None
Пример #45
0
def _all_domain_stats():
    webuser_counts = defaultdict(lambda: 0)
    commcare_counts = defaultdict(lambda: 0)

    for row in CouchUser.get_db().view('users/by_domain',
                                       startkey=["active"],
                                       endkey=["active", {}],
                                       group_level=3).all():
        _, domain, doc_type = row['key']
        value = row['value']
        {
            'WebUser': webuser_counts,
            'CommCareUser': commcare_counts
        }[doc_type][domain] = value

    form_counts = get_number_of_forms_per_domain()
    case_counts = get_number_of_cases_per_domain()

    return {
        "web_users": webuser_counts,
        "commcare_users": commcare_counts,
        "forms": form_counts,
        "cases": case_counts
    }
Пример #46
0
    def record_iter(cls, start_datetime, end_datetime):
        user_ids = get_user_ids_by_last_modified(start_datetime, end_datetime)

        return iter_docs(CouchUser.get_db(), user_ids)
Пример #47
0
def user_db():
    return CouchUser.get_db()
 def handle(self, **options):
     self.options = options
     user_ids = with_progress_bar(self.get_user_ids())
     iter_update(CouchUser.get_db(), self.migrate_user, user_ids, verbose=True)
Пример #49
0
def get_all_user_ids():
    from corehq.apps.users.models import CouchUser
    return [res['id'] for res in CouchUser.get_db().view(
        'users/by_username',
        reduce=False,
    ).all()]
Пример #50
0
 def __init__(self, **kwargs):
     super(UnknownUsersPillow, self).__init__(**kwargs)
     self.couch_db = XFormInstance.get_db()
     self.user_db = CouchUser.get_db()
     self.es = get_es()
Пример #51
0
 def __init__(self):
     checkpoint = get_default_django_checkpoint_for_legacy_pillow_class(self.__class__)
     super(UnknownUsersPillow, self).__init__(checkpoint=checkpoint)
     self.user_db = CouchUser.get_db()
     self.es = get_es_new()
     self.es_type = ES_META['users'].type
Пример #52
0
def _user_exists_in_couch(user_id):
    return CouchUser.get_db().doc_exist(user_id)
Пример #53
0
def _user_exists(user_id):
    return CouchUser.get_db().doc_exist(user_id)
Пример #54
0
def num_mobile_users(domain, *args):
    row = CouchUser.get_db().view('users/by_domain', startkey=[domain], endkey=[domain, {}]).one()
    return row["value"] if row else 0
Пример #55
0
def num_web_users(domain, *args):
    key = ["active", domain, 'WebUser']
    row = CouchUser.get_db().view('users/by_domain', startkey=key, endkey=key+[{}]).one()
    return row["value"] if row else 0
Пример #56
0
 def validate_supply_points(self, date):
     for location in iterate_over_api_objects(
             self.endpoint.get_locations, filters={'is_active': True, 'date_updated__gte': date}
     ):
         for supply_point in location.supply_points:
             sp = get_supply_point_case_by_domain_external_id(self.domain, supply_point.id)
             if sp:
                 EWSMigrationProblem.objects.filter(
                     domain=self.domain, external_id=supply_point.id, object_type='supply_point'
                 ).delete()
                 sql_location = sp.sql_location
                 ids = sql_location.facilityincharge_set.all().values_list('user_id', flat=True)
                 usernames = [user['username'].split('@')[0] for user in iter_docs(CouchUser.get_db(), ids)]
                 if not all([self._check_username(usernames, incharge) for incharge in supply_point.incharges]):
                     migration_problem, _ = EWSMigrationProblem.objects.get_or_create(
                         domain=self.domain,
                         object_id=sql_location.location_id,
                         object_type='location'
                     )
                     migration_problem.object_type = 'location'
                     migration_problem.external_id = sql_location.external_id
                     migration_problem.description = 'Invalid in charges'
                     migration_problem.save()
                 else:
                     EWSMigrationProblem.objects.filter(
                         domain=self.domain,
                         external_id=sql_location.external_id,
                         object_type='location'
                     ).delete()
             elif supply_point.active and supply_point.last_reported:
                 migration_problem, _ = EWSMigrationProblem.objects.get_or_create(
                     domain=self.domain,
                     external_id=supply_point.id,
                 )
                 migration_problem.object_type = 'supply_point'
                 migration_problem.external_id = supply_point.id
                 migration_problem.description = 'Not exists'
                 migration_problem.save()