def rendered_content(self):
        from corehq.apps.users.views.mobile.users import EditCommCareUserView
        users = CommCareUser.view(
            'locations/users_by_location_id',
            startkey=[self.config['location_id']],
            endkey=[self.config['location_id'], {}],
            include_docs=True
        ).all()

        user_to_dict = lambda sms_user: {
            'id': sms_user.get_id,
            'full_name': sms_user.full_name,
            'phone_numbers': sms_user.phone_numbers,
            'in_charge': user.user_data.get('role') == 'In Charge',
            'url': reverse(EditCommCareUserView.urlname, args=[self.config['domain'], sms_user.get_id])
        }

        web_users = [
            {
                'id': web_user['_id'],
                'first_name': web_user['first_name'],
                'last_name': web_user['last_name'],
                'email': web_user['email']
            }
            for web_user in UserES().web_users().domain(self.config['domain']).term(
                "domain_memberships.location_id", self.config['location_id']
            ).run().hits
        ]
        return render_to_string('ewsghana/partials/users_tables.html', {
            'users': [user_to_dict(user) for user in users],
            'domain': self.domain,
            'location_id': self.location_id,
            'web_users': web_users
        })
예제 #2
0
def _get_commcare_users_in_domain(active_flag, domain, start_at, limit):
    extra_args = {}
    if start_at != 0:
        extra_args['skip'] = start_at
    if limit is not None:
        extra_args['limit'] = limit
    return CommCareUser.view("users/by_domain",
                             reduce=False,
                             include_docs=True,
                             startkey=[active_flag, domain, 'CommCareUser'],
                             endkey=[active_flag, domain, 'CommCareUser', {}],
                             stale=stale_ok(),
                             **extra_args).all()
예제 #3
0
    def rows(self):
        users = CommCareUser.view(
            'locations/users_by_location_id',
            startkey=[self.config['location_id']],
            endkey=[self.config['location_id'], {}],
            include_docs=True
        ).all()

        for user in users:
            if user.user_data.get('role') == 'In Charge' and user.full_name:
                yield [user.full_name]
        yield ['<button id="in-charge-button" class="btn" data-target="#configureInCharge" data-toggle="modal">'
               'Configure In Charge</button>']
예제 #4
0
 def get_users(self, wrap=True, include_groups=False):
     user_ids = set(get_owner_ids_by_type(self.domain, 'user', self.get_id))
     if include_groups:
         group_ids = self.get_groups(wrap=False)
     else:
         group_ids = set()
     users_in_groups = [group.get_users(only_commcare=True) for group in Group.view('_all_docs',
         keys=list(group_ids),
         include_docs=True
     )]
     if wrap:
         return set(CommCareUser.view('_all_docs', keys=list(user_ids), include_docs=True)).union(*users_in_groups)
     else:
         return user_ids | set([user.get_id for user in users_in_groups])
예제 #5
0
 def get_users(self, wrap=True, include_groups=False):
     user_ids = set(get_owner_ids_by_type(self.domain, 'user', self.get_id))
     if include_groups:
         group_ids = self.get_groups(wrap=False)
     else:
         group_ids = set()
     users_in_groups = [group.get_users(only_commcare=True) for group in Group.view('_all_docs',
         keys=list(group_ids),
         include_docs=True
     )]
     if wrap:
         return set(CommCareUser.view('_all_docs', keys=list(user_ids), include_docs=True)).union(*users_in_groups)
     else:
         return user_ids | set([user.get_id for user in users_in_groups])
예제 #6
0
def _get_commcare_users_in_domain(active_flag, domain, start_at, limit):
    extra_args = {}
    if start_at != 0:
        extra_args['skip'] = start_at
    if limit is not None:
        extra_args['limit'] = limit
    return CommCareUser.view("users/by_domain",
        reduce=False,
        include_docs=True,
        startkey=[active_flag, domain, 'CommCareUser'],
        endkey=[active_flag, domain, 'CommCareUser', {}],
        stale=stale_ok(),
        **extra_args
    ).all()
예제 #7
0
def send_message_to_admins(user, message):
    users = CommCareUser.view(
        'locations/users_by_location_id',
        startkey=[user.location.get_id],
        endkey=[user.location.get_id, {}],
        include_docs=True
    ).all()
    in_charge_users = [
        u
        for u in users
        if u.get_verified_number() and u.user_data.get('role') == "In Charge"
    ]
    for in_charge_user in in_charge_users:
        send_sms_to_verified_number(in_charge_user.get_verified_number(),
                                    message % (in_charge_user.username, in_charge_user.location.name))
예제 #8
0
def configure_in_charge(request, domain):
    in_charge_ids = request.POST.getlist('users[]')
    location_id = request.POST.get('location_id')
    all_users = CommCareUser.view(
        'locations/users_by_location_id',
        startkey=[location_id],
        endkey=[location_id, {}],
        include_docs=True
    ).all()

    for u in all_users:
        if (u.user_data.get('role') == 'In Charge') != (u._id in in_charge_ids):
            u.user_data['role'] = 'In Charge' if u._id in in_charge_ids else 'Other'
            u.save()
    return HttpResponse('OK')
예제 #9
0
    def rows(self):
        from corehq.apps.users.views.mobile import CreateCommCareUserView

        users = CommCareUser.view(
            'locations/users_by_location_id',
            startkey=[self.config['location_id']],
            endkey=[self.config['location_id'], {}],
            include_docs=True
        ).all()

        for user in users:
            if user.full_name and user.phone_numbers:
                yield ['<div val="%s" sel=%s>%s</div>' % (
                    user._id, 'true' if user.user_data.get('role') == 'In Charge' else 'false',
                    user.full_name), user.phone_numbers[0]]

        yield [get_url_with_location(CreateCommCareUserView.urlname, 'Create new Mobile Worker',
                                     self.config['location_id'], self.config['domain'])]
예제 #10
0
 def get_users(self, wrap=True, include_groups=False):
     user_ids = set(
         get_db().view('fixtures/ownership',
             key=[self.domain, 'user by data_item', self.get_id],
             reduce=False,
             wrapper=lambda r: r['value']
         )
     )
     if include_groups:
         group_ids = self.get_groups(wrap=False)
     else:
         group_ids = set()
     users_in_groups = [group.get_users(only_commcare=True) for group in Group.view('_all_docs',
         keys=list(group_ids),
         include_docs=True
     )]
     if wrap:
         return set(CommCareUser.view('_all_docs', keys=list(user_ids), include_docs=True)).union(*users_in_groups)
     else:
         return user_ids | set([user.get_id for user in users_in_groups])