Ejemplo n.º 1
0
def _delete_web_user_membership(domain_name):
    from corehq.apps.users.models import WebUser
    active_web_users = WebUser.by_domain(domain_name)
    inactive_web_users = WebUser.by_domain(domain_name, is_active=False)
    for web_user in list(active_web_users) + list(inactive_web_users):
        web_user.delete_domain_membership(domain_name)
        web_user.save()
Ejemplo n.º 2
0
def _delete_web_user_membership(domain_name):
    from corehq.apps.users.models import WebUser
    active_web_users = WebUser.by_domain(domain_name)
    inactive_web_users = WebUser.by_domain(domain_name, is_active=False)
    for web_user in list(active_web_users) + list(inactive_web_users):
        web_user.delete_domain_membership(domain_name)
        web_user.save()
Ejemplo n.º 3
0
def _delete_web_user_membership(domain_name):
    from corehq.apps.users.models import WebUser
    active_web_users = WebUser.by_domain(domain_name)
    inactive_web_users = WebUser.by_domain(domain_name, is_active=False)
    for web_user in list(active_web_users) + list(inactive_web_users):
        web_user.delete_domain_membership(domain_name)
        if settings.UNIT_TESTING and not web_user.domain_memberships:
            web_user.delete(deleted_by=None)
        else:
            web_user.save()
Ejemplo n.º 4
0
    def test_create_facility_manager(self):
        with open(os.path.join(self.datapath, 'sample_webusers.json')) as f:
            webuser = EWSUser(json.loads(f.read())[1])

        self.assertEqual(0, len(WebUser.by_domain(TEST_DOMAIN)))
        self.api_object.web_user_sync(webuser)
        web_users = list(WebUser.by_domain(TEST_DOMAIN))
        self.assertEqual(1, len(web_users))
        self.assertEqual(0, len(CommCareUser.by_domain(TEST_DOMAIN)))
        facility_manager_role = UserRole.by_domain_and_name(TEST_DOMAIN, 'Facility manager')[0]
        dm = web_users[0].get_domain_membership(TEST_DOMAIN)
        self.assertEqual(facility_manager_role.get_id, dm.role_id)
Ejemplo n.º 5
0
    def test_edit_webuser_email(self):
        with open(os.path.join(self.datapath, 'sample_webusers.json')) as f:
            webuser = ILSUser(json.loads(f.read())[0])
        self.assertEqual(len(WebUser.by_domain(TEST_DOMAIN)), 0)

        ils_webuser = self.api_object.web_user_sync(webuser)
        self.assertEqual(len(WebUser.by_domain(TEST_DOMAIN)), 1)

        webuser.email = '*****@*****.**'
        ils_webuser2 = self.api_object.web_user_sync(webuser)
        ils_webuser = WebUser.get(docid=ils_webuser.get_id)

        self.assertEqual(len(WebUser.by_domain(TEST_DOMAIN)), 1)
        self.assertIsNone(ils_webuser.get_domain_membership(TEST_DOMAIN))
        self.assertEqual(ils_webuser2.username, '*****@*****.**')
Ejemplo n.º 6
0
 def handle(self, *args, **options):
     users = CommCareUser.by_domain('icds-cas')
     web_users = WebUser.by_domain('icds-cas')
     users = users + web_users
     user_details = self._get_details(users)
     usernames_list = list(user_details.keys())
     chunk_size = 100
     headers = ["username", "last_access_time", "created_on", "role"]
     rows = [headers]
     usernames_usage = set()
     for user_chunk in chunked(usernames_list, chunk_size):
         usage_data = ICDSAuditEntryRecord.objects.filter(
             username__in=list(user_chunk)).values('username').annotate(
                 time=Max('time_of_use'))
         for usage in usage_data:
             row_data = [
                 usage['username'],
                 self.convert_to_ist(usage['time']),
                 self.convert_to_ist(user_details[usage['username']][0]),
                 user_details[usage['username']][1]
             ]
             usernames_usage.add(usage['username'])
             rows.append(row_data)
     users_not_logged_in = set(usernames_list) - usernames_usage
     for user in users_not_logged_in:
         rows.extend([
             user, 'N/A',
             self.convert_to_ist(user_details[usage['username']][0]),
             user_details[usage['username']][1]
         ])
     fout = open('/home/cchq/National_users_usage_data.csv', 'w')
     writer = csv.writer(fout)
     writer.writerows(rows)
Ejemplo n.º 7
0
 def obj_get_list(self, bundle, **kwargs):
     domain = kwargs['domain']
     username = bundle.request.GET.get('username')
     if username:
         user = WebUser.get_by_username(username)
         return [user] if user else []
     return list(WebUser.by_domain(domain))
Ejemplo n.º 8
0
    def save(self, request, domain):
        try:
            if self.can_use_custom_logo:
                logo = self.cleaned_data['logo']
                if logo:

                    input_image = Image.open(io.BytesIO(logo.read()))
                    input_image.load()
                    input_image.thumbnail(LOGO_SIZE)
                    # had issues trying to use a BytesIO instead
                    tmpfilename = "/tmp/%s_%s" % (uuid.uuid4(), logo.name)
                    input_image.save(tmpfilename, 'PNG')

                    with open(tmpfilename) as tmpfile:
                        domain.put_attachment(tmpfile, name=LOGO_ATTACHMENT)
                elif self.cleaned_data['delete_logo']:
                    domain.delete_attachment(LOGO_ATTACHMENT)

            global_tz = self.cleaned_data['default_timezone']
            domain.default_timezone = global_tz
            users = WebUser.by_domain(domain.name)
            for user in users:
                dm = user.get_domain_membership(domain.name)
                if not dm.override_global_tz:
                    dm.timezone = global_tz
                    user.save()
            domain.save()
            return True
        except Exception:
            return False
Ejemplo n.º 9
0
def invite_web_user(request, domain, template="users/invite_web_user.html"):
    role_choices = UserRole.role_choices(domain)
    if request.method == "POST":
        current_users = [user.username for user in WebUser.by_domain(domain)]
        pending_invites = [di.email for di in DomainInvitation.by_domain(domain)]
        form = AdminInvitesUserForm(request.POST,
            excluded_emails= current_users + pending_invites,
            role_choices=role_choices
        )
        if form.is_valid():
            data = form.cleaned_data
            # create invitation record
            data["invited_by"] = request.couch_user.user_id
            data["invited_on"] = datetime.utcnow()
            data["domain"] = domain
            invite = DomainInvitation(**data)
            invite.save()
            invite.send_activation_email()
            messages.success(request, "Invitation sent to %s" % invite.email)
            return HttpResponseRedirect(reverse("web_users", args=[domain]))
    else:
        form = AdminInvitesUserForm(role_choices=role_choices)
    context = _users_context(request, domain)
    context.update(
        registration_form=form
    )
    return render(request, template, context)
Ejemplo n.º 10
0
 def test_migration(self):
     bootstrap_domain(ILSGatewayAPI(TEST_DOMAIN, MockEndpoint('http://test-api.com/', 'dummy', 'dummy')))
     self.assertEqual(6, len(list(Product.by_domain(TEST_DOMAIN))))
     self.assertEqual(5, len(list(Location.by_domain(TEST_DOMAIN))))
     self.assertEqual(6, len(list(CommCareUser.by_domain(TEST_DOMAIN))))
     self.assertEqual(5, len(list(WebUser.by_domain(TEST_DOMAIN))))
     self.assertEqual(ILSMigrationStats.objects.filter(domain=TEST_DOMAIN).count(), 1)
Ejemplo n.º 11
0
    def main_context(self):
        try:
            facilities = Location.filter_by_type_count(self.domain, 'FACILITY')
        except TypeError:
            facilities = 0

        contacts = CommCareUser.by_domain(self.domain, reduce=True)
        web_users = WebUser.by_domain(self.domain, reduce=True)

        try:
            products = len(Product.by_domain(self.domain))
        except ResourceNotFound:
            products = 0

        main_context = super(GlobalStats, self).main_context
        context = {
            'supply_points':  len(list(Location.by_domain(self.domain))),
            'facilities': facilities,
            'contacts':  contacts[0]['value'] if contacts else 0,
            'web_users': web_users[0]['value'] if web_users else 0,
            'products':  products,
            #TODO add next after the enlargement ILS migration
            'product_stocks':  0,
            'stock_transactions':  0,
            'inbound_messages':  0,
            'outbound_messages':  0
        }
        main_context.update(context)
        return main_context
Ejemplo n.º 12
0
    def save(self, request, domain):
        try:
            if self.can_use_custom_logo:
                logo = self.cleaned_data['logo']
                if logo:

                    input_image = Image.open(io.BytesIO(logo.read()))
                    input_image.load()
                    input_image.thumbnail(LOGO_SIZE)
                    # had issues trying to use a BytesIO instead
                    tmpfilename = "/tmp/%s_%s" % (uuid.uuid4(), logo.name)
                    input_image.save(tmpfilename, 'PNG')

                    with open(tmpfilename) as tmpfile:
                        domain.put_attachment(tmpfile, name=LOGO_ATTACHMENT)
                elif self.cleaned_data['delete_logo']:
                    domain.delete_attachment(LOGO_ATTACHMENT)

            global_tz = self.cleaned_data['default_timezone']
            domain.default_timezone = global_tz
            users = WebUser.by_domain(domain.name)
            for user in users:
                dm = user.get_domain_membership(domain.name)
                if not dm.override_global_tz:
                    dm.timezone = global_tz
                    user.save()
            domain.save()
            return True
        except Exception:
            return False
Ejemplo n.º 13
0
def invite_web_user(request, domain, template="users/invite_web_user.html"):
    role_choices = UserRole.role_choices(domain)
    if request.method == "POST":
        current_users = [user.username for user in WebUser.by_domain(domain)]
        pending_invites = [di.email for di in DomainInvitation.by_domain(domain)]
        form = AdminInvitesUserForm(request.POST,
            excluded_emails= current_users + pending_invites,
            role_choices=role_choices
        )
        if form.is_valid():
            data = form.cleaned_data
            # create invitation record
            data["invited_by"] = request.couch_user.user_id
            data["invited_on"] = datetime.utcnow()
            data["domain"] = domain
            invite = DomainInvitation(**data)
            invite.save()
            invite.send_activation_email()
            messages.success(request, "Invitation sent to %s" % invite.email)
            return HttpResponseRedirect(reverse("web_users", args=[domain]))
    else:
        form = AdminInvitesUserForm(role_choices=role_choices)
    context = _users_context(request, domain)
    context.update(
        registration_form=form
    )
    return render(request, template, context)
Ejemplo n.º 14
0
 def obj_get_list(self, bundle, **kwargs):
     domain = kwargs['domain']
     username = bundle.request.GET.get('username')
     if username:
         user = WebUser.get_by_username(username)
         return [user] if user else []
     return list(WebUser.by_domain(domain))
Ejemplo n.º 15
0
    def main_context(self):
        try:
            facilities = Location.filter_by_type_count(self.domain, 'FACILITY')
        except TypeError:
            facilities = 0

        contacts = CommCareUser.by_domain(self.domain, reduce=True)
        web_users = WebUser.by_domain(self.domain, reduce=True)

        try:
            products = len(Product.by_domain(self.domain))
        except ResourceNotFound:
            products = 0

        main_context = super(GlobalStats, self).main_context
        context = {
            'supply_points': len(list(Location.by_domain(self.domain))),
            'facilities': facilities,
            'contacts': contacts[0]['value'] if contacts else 0,
            'web_users': web_users[0]['value'] if web_users else 0,
            'products': products,
            #TODO add next after the enlargement ILS migration
            'product_stocks': 0,
            'stock_transactions': 0,
            'inbound_messages': 0,
            'outbound_messages': 0
        }
        main_context.update(context)
        return main_context
Ejemplo n.º 16
0
def audit_logs(request, domain):
    from auditcare.models import NavigationEventAudit

    usernames = [user.username for user in WebUser.by_domain(domain)]
    data = {}
    for username in usernames:
        data[username] = []
        for doc in (
            get_db()
            .view(
                "auditcare/urlpath_by_user_date",
                startkey=[username],
                endkey=[username, {}],
                include_docs=True,
                wrapper=lambda r: r["doc"],
            )
            .all()
        ):
            try:
                (d,) = re.search(r"^/a/([\w\-_\.]+)/", doc["request_path"]).groups()
                if d == domain:
                    data[username].append(doc)
            except Exception:
                pass
    return json_response(data)
Ejemplo n.º 17
0
 def invite_web_user_form(self):
     role_choices = _get_editable_role_choices(self.domain,
                                               self.request.couch_user,
                                               allow_admin_role=True)
     loc = None
     domain_request = DomainRequest.objects.get(
         id=self.request_id) if self.request_id else None
     initial = {
         'email': domain_request.email if domain_request else None,
     }
     if 'location_id' in self.request.GET:
         from corehq.apps.locations.models import SQLLocation
         loc = SQLLocation.objects.get(
             location_id=self.request.GET.get('location_id'))
     if self.request.method == 'POST':
         current_users = [
             user.username for user in WebUser.by_domain(self.domain)
         ]
         pending_invites = [
             di.email for di in SQLInvitation.by_domain(self.domain)
         ]
         return AdminInvitesUserForm(self.request.POST,
                                     excluded_emails=current_users +
                                     pending_invites,
                                     role_choices=role_choices,
                                     domain=self.domain)
     return AdminInvitesUserForm(initial=initial,
                                 role_choices=role_choices,
                                 domain=self.domain,
                                 location=loc)
Ejemplo n.º 18
0
    def balance_email_reports(self):
        EWSMigrationProblem.objects.filter(domain=self.domain).delete()
        reports = set()
        reports_count = 0
        for web_user in WebUser.by_domain(self.domain):
            notifications = ReportNotification.by_domain_and_owner(self.domain, web_user.get_id)
            for notification in notifications:
                config_id = notification.config_ids[0] if notification.config_ids else None

                if not config_id:
                    continue

                config = ReportConfig.get(config_id)
                location_id = config.filters.get('location_id')
                if not location_id:
                    # report is not migrated from ews
                    continue
                reports_count += 1
                report_slug = config.report_slug
                code = SQLLocation.objects.get(location_id=location_id).site_code
                report_tuple = (
                    web_user.username, notification.day, notification.hour,
                    code, report_slug, notification.interval
                )
                external_id = '{}-{}-{}-{}-{}-{}'.format(*report_tuple)
                if not notification.send_to_owner and not notification.recipient_emails:
                    migration_problem, _ = EWSMigrationProblem.objects.get_or_create(
                        domain=self.domain,
                        object_id=web_user.username,
                        object_type='email_report_send_to_owner',
                        external_id=external_id
                    )
                    migration_problem.description = 'send_to_owner not set to true'
                    migration_problem.save()

                reports.add(report_tuple)

        total_count = 0

        for report in self.endpoint.get_daily_reports(limit=1000)[1]:
            if self._check_report(report, reports, 1, 'daily'):
                total_count += 1

        for report in self.endpoint.get_weekly_reports(limit=1000)[1]:
            if self._check_report(report, reports, report.day_of_week, 'weekly'):
                total_count += 1

        for report in self.endpoint.get_monthly_reports(limit=1000)[1]:
            if self._check_report(report, reports, report.day_of_month, 'monthly'):
                total_count += 1

        if total_count != reports_count:
            migration_problem, _ = EWSMigrationProblem.objects.get_or_create(
                domain=self.domain,
                object_id=None,
                object_type='email_report',
                external_id='email-report-count'
            )
            migration_problem.description = '{} / {}'.format(reports_count, total_count)
            migration_problem.save()
Ejemplo n.º 19
0
def set_send_to_owner_field_task(domain):
    for web_user in WebUser.by_domain(domain):
        notifications = ReportNotification.by_domain_and_owner(domain, web_user.get_id)
        for notification in notifications:
            if not notification.send_to_owner and not notification.recipient_emails:
                notification.send_to_owner = True
                notification.save()
    balance_migration_task.delay(domain)
Ejemplo n.º 20
0
 def test_migration(self):
     ils_bootstrap_domain_test_task(
         TEST_DOMAIN, MockEndpoint('http://test-api.com/', 'dummy',
                                   'dummy'))
     self.assertEqual(6, len(list(Product.by_domain(TEST_DOMAIN))))
     self.assertEqual(5, len(list(Location.by_domain(TEST_DOMAIN))))
     self.assertEqual(6, len(list(CommCareUser.by_domain(TEST_DOMAIN))))
     self.assertEqual(5, len(list(WebUser.by_domain(TEST_DOMAIN))))
Ejemplo n.º 21
0
    def test_create_facility_manager(self):
        with open(os.path.join(self.datapath, 'sample_webusers.json')) as f:
            webuser = EWSUser(json.loads(f.read())[1])

        self.assertEqual(0, len(WebUser.by_domain(TEST_DOMAIN)))
        ewsghana_webuser = self.api_object.web_user_sync(webuser)
        web_users = list(WebUser.by_domain(TEST_DOMAIN))
        self.assertEqual(1, len(web_users))
        self.assertEqual(0, len(CommCareUser.by_domain(TEST_DOMAIN)))
        facility_manager_role = UserRole.by_domain_and_name(
            TEST_DOMAIN, 'Facility manager')[0]
        dm = web_users[0].get_domain_membership(TEST_DOMAIN)
        self.assertEqual(facility_manager_role.get_id, dm.role_id)
        location = SQLLocation.objects.get(external_id=1, domain=TEST_DOMAIN)
        self.assertEqual(
            ewsghana_webuser.get_domain_membership(TEST_DOMAIN).location_id,
            location.location_id)
Ejemplo n.º 22
0
    def setUp(self):
        self.endpoint = MockEndpoint('http://test-api.com/', 'dummy', 'dummy')
        self.api_object = ILSGatewayAPI(TEST_DOMAIN, self.endpoint)
        self.datapath = os.path.join(os.path.dirname(__file__), 'data')
        initial_bootstrap(TEST_DOMAIN)

        for user in WebUser.by_domain(TEST_DOMAIN):
            user.delete()
Ejemplo n.º 23
0
    def setUp(self):
        self.endpoint = MockEndpoint('http://test-api.com/', 'dummy', 'dummy')
        self.api_object = ILSGatewayAPI(TEST_DOMAIN, self.endpoint)
        self.datapath = os.path.join(os.path.dirname(__file__), 'data')
        initial_bootstrap(TEST_DOMAIN)

        for user in WebUser.by_domain(TEST_DOMAIN):
            user.delete()
Ejemplo n.º 24
0
    def tearDownClass(cls):
        cls.sms_backend_mapping.delete()
        cls.sms_backend.delete()
        for user in WebUser.by_domain(TEST_DOMAIN):
            user.delete()

        for vn in VerifiedNumber.by_domain(TEST_DOMAIN):
            vn.delete()
Ejemplo n.º 25
0
def _users_context(request, domain):
    couch_user = request.couch_user
    web_users = WebUser.by_domain(domain)

    for user in [couch_user] + list(web_users):
        user.current_domain = domain

    return {"web_users": web_users, "domain": domain, "couch_user": couch_user}
Ejemplo n.º 26
0
 def billing_admins_response(self):
     all_web_users = WebUser.by_domain(domain=self.request.domain)
     admins = filter(lambda x: x.is_domain_admin and x.username != self.request.couch_user.username,
                     all_web_users)
     admins = filter(lambda x: x.username not in self.existing, admins)
     if self.search_string:
         admins = filter(lambda x: (x.username.lower().startswith(self.search_string.lower())
                                    or self.search_string in x.full_name), admins)
     return [(a.username, "%s (%s)" % (a.full_name, a.username)) for a in admins]
Ejemplo n.º 27
0
 def billing_admins_response(self):
     all_web_users = WebUser.by_domain(domain=self.request.domain)
     admins = filter(lambda x: x.is_domain_admin and x.username != self.request.couch_user.username,
                     all_web_users)
     admins = filter(lambda x: x.username not in self.existing, admins)
     if self.search_string:
         admins = filter(lambda x: (x.username.lower().startswith(self.search_string.lower())
                                    or self.search_string in x.full_name), admins)
     return [(a.username, "%s (%s)" % (a.full_name, a.username)) for a in admins]
Ejemplo n.º 28
0
 def setUp(self):
     self.endpoint = MockEndpoint('http://test-api.com/', 'dummy', 'dummy')
     self.api_object = EWSApi(TEST_DOMAIN, self.endpoint)
     self.datapath = os.path.join(os.path.dirname(__file__), 'data')
     initial_bootstrap(TEST_DOMAIN)
     self.api_object.prepare_commtrack_config()
     self.api_object.create_or_edit_roles()
     for user in WebUser.by_domain(TEST_DOMAIN):
         user.delete()
Ejemplo n.º 29
0
    def tearDown(self):
        delete_all_locations()

        for user in WebUser.by_domain(self.TEST_DOMAIN):
            user.delete()

        delete_domain_phone_numbers(self.TEST_DOMAIN)

        super(SMSNotificationTestCase, self).tearDown()
Ejemplo n.º 30
0
 def page_context(self):
     return {
         'stats': get_object_or_404(ILSMigrationStats, domain=self.domain),
         'products_count': SQLProduct.objects.filter(domain=self.domain).count(),
         'locations_count': SQLLocation.objects.filter(domain=self.domain).exclude(is_archived=True).count(),
         'web_users_count': WebUser.by_domain(self.domain, reduce=True)[0]['value'],
         'sms_users_count': CommCareUser.by_domain(self.domain, reduce=True)[0]['value'],
         'problems': ILSMigrationProblem.objects.filter(domain=self.domain)
     }
Ejemplo n.º 31
0
 def obj_get_list(self, bundle, **kwargs):
     domain = kwargs['domain']
     username = bundle.request.GET.get('web_username')
     if username:
         user = WebUser.get_by_username(username)
         if not (user and user.is_member_of(domain)):
             user = None
         return [user] if user else []
     return list(WebUser.by_domain(domain))
Ejemplo n.º 32
0
    def test_create_facility_manager(self):
        with open(os.path.join(self.datapath, 'sample_webusers.json')) as f:
            webuser = EWSUser(json.loads(f.read())[1])

        self.assertEqual(0, len(WebUser.by_domain(TEST_DOMAIN)))
        ewsghana_webuser = self.api_object.web_user_sync(webuser)
        web_users = list(WebUser.by_domain(TEST_DOMAIN))
        self.assertEqual(1, len(web_users))
        facility_manager_role = UserRole.by_domain_and_name(TEST_DOMAIN, 'Facility manager')[0]
        dm = web_users[0].get_domain_membership(TEST_DOMAIN)
        self.assertEqual(facility_manager_role.get_id, dm.role_id)
        location = SQLLocation.objects.get(external_id=1, domain=TEST_DOMAIN)
        self.assertEqual(ewsghana_webuser.get_domain_membership(TEST_DOMAIN).location_id, location.location_id)

        extension = EWSExtension.objects.get(user_id=ewsghana_webuser.get_id, domain=TEST_DOMAIN)
        self.assertEqual(SQLLocation.objects.get(location_id=extension.location_id).site_code, 'rsp2')
        self.assertListEqual(ewsghana_webuser.phone_numbers, ['1233232'])
        self.assertEqual(ewsghana_webuser.default_phone_number, '1233232')
Ejemplo n.º 33
0
    def tearDown(self):
        for location in Location.by_domain(self.TEST_DOMAIN):
            location.delete()

        for user in WebUser.by_domain(self.TEST_DOMAIN):
            user.delete()

        for vn in VerifiedNumber.by_domain(self.TEST_DOMAIN):
            vn.delete()
Ejemplo n.º 34
0
def get_users_to_export(username, domain):
    if username:
        users = [username]
        super_users = []
    else:
        users = {u.username for u in WebUser.by_domain(domain)}
        super_users = {u['username'] for u in User.objects.filter(is_superuser=True).values('username')}
        super_users = super_users - users

    return users, super_users
Ejemplo n.º 35
0
def get_admins_emails():
    def get_role_or_none(user):
        role = user.get_role(DOMAIN, False, False)
        if role:
            return role.name
        return None

    return map(lambda user: user.get_email(),
               filter(lambda user: get_role_or_none(user) == 'Succeed Admin',
               WebUser.by_domain(DOMAIN)))
Ejemplo n.º 36
0
 def test_webusers_migration(self):
     checkpoint = MigrationCheckpoint(
         domain=TEST_DOMAIN, start_date=datetime.utcnow(), date=datetime.utcnow(), api="product", limit=100, offset=0
     )
     location_api = ApiSyncObject("webuser", self.endpoint.get_webusers, self.api_object.web_user_sync)
     synchronization(location_api, checkpoint, None, 100, 0)
     self.assertEqual("webuser", checkpoint.api)
     self.assertEqual(100, checkpoint.limit)
     self.assertEqual(0, checkpoint.offset)
     self.assertEqual(5, len(list(WebUser.by_domain(TEST_DOMAIN))))
Ejemplo n.º 37
0
    def tearDown(self):
        for location in Location.by_domain(self.TEST_DOMAIN):
            location.delete()

        for user in WebUser.by_domain(self.TEST_DOMAIN):
            user.delete()

        delete_domain_phone_numbers(self.TEST_DOMAIN)

        super(SMSNotificationTestCase, self).tearDown()
Ejemplo n.º 38
0
def get_admins_emails():
    def get_role_or_none(user):
        role = user.get_role(DOMAIN, False, False)
        if role:
            return role.name
        return None

    return map(
        lambda user: user.get_email(),
        filter(lambda user: get_role_or_none(user) == 'Succeed Admin',
               WebUser.by_domain(DOMAIN)))
Ejemplo n.º 39
0
 def web_users(self):
     web_users = WebUser.by_domain(self.domain)
     teams = Team.get_by_domain(self.domain)
     for team in teams:
         for user in team.get_members():
             if user.get_id not in [web_user.get_id for web_user in web_users]:
                 user.from_team = True
                 web_users.append(user)
     for user in web_users:
         user.current_domain = self.domain
     return web_users
Ejemplo n.º 40
0
 def test_create_web_reporter(self):
     with open(os.path.join(self.datapath, 'sample_webusers.json')) as f:
         webuser = EWSUser(json.loads(f.read())[2])
     ewsghana_webuser = self.api_object.web_user_sync(webuser)
     web_users = list(WebUser.by_domain(TEST_DOMAIN))
     self.assertEqual(1, len(web_users))
     self.assertEqual(0, len(CommCareUser.by_domain(TEST_DOMAIN)))
     web_reporter_role = UserRole.by_domain_and_name(TEST_DOMAIN, 'Web Reporter')[0]
     dm = web_users[0].get_domain_membership(TEST_DOMAIN)
     self.assertEqual(web_reporter_role.get_id, dm.role_id)
     location = SQLLocation.objects.get(external_id=620, domain=TEST_DOMAIN)
     self.assertEqual(location.location_id, ewsghana_webuser.get_domain_membership(TEST_DOMAIN).location_id)
Ejemplo n.º 41
0
    def tearDown(self):
        delete_all_locations()

        for user in WebUser.by_domain(self.TEST_DOMAIN):
            user.delete()

        delete_domain_phone_numbers(self.TEST_DOMAIN)

        for product in Product.by_domain(self.TEST_DOMAIN):
            product.delete()

        super(MissingReportNotificationTestCase, self).tearDown()
Ejemplo n.º 42
0
 def invite_web_user_form(self):
     role_choices = UserRole.role_choices(self.domain)
     if self.request.method == 'POST':
         current_users = [user.username for user in WebUser.by_domain(self.domain)]
         pending_invites = [di.email for di in DomainInvitation.by_domain(self.domain)]
         return AdminInvitesUserForm(
             self.request.POST,
             excluded_emails=current_users + pending_invites,
             role_choices=role_choices,
             domain=self.domain
         )
     return AdminInvitesUserForm(role_choices=role_choices, domain=self.domain)
Ejemplo n.º 43
0
 def web_users(self):
     web_users = WebUser.by_domain(self.domain)
     teams = Team.get_by_domain(self.domain)
     for team in teams:
         for user in team.get_members():
             if user.get_id not in [web_user.get_id for web_user in web_users]:
                 user.from_team = True
                 web_users.append(user)
     for user in web_users:
         user.current_domain = self.domain
     web_users.sort(key=lambda x: (x.role_label(), x.email))
     return web_users
Ejemplo n.º 44
0
 def invite_web_user_form(self):
     role_choices = UserRole.role_choices(self.domain)
     if self.request.method == 'POST':
         current_users = [user.username for user in WebUser.by_domain(self.domain)]
         pending_invites = [di.email for di in DomainInvitation.by_domain(self.domain)]
         return AdminInvitesUserForm(
             self.request.POST,
             excluded_emails=current_users + pending_invites,
             role_choices=role_choices,
             domain=self.domain
         )
     return AdminInvitesUserForm(role_choices=role_choices, domain=self.domain)
Ejemplo n.º 45
0
def _users_context(request, domain):
    couch_user = request.couch_user
    web_users = WebUser.by_domain(domain)

    for user in [couch_user] + list(web_users):
        user.current_domain = domain

    return {
        'web_users': web_users,
        'domain': domain,
        'couch_user': couch_user,
    }
Ejemplo n.º 46
0
def _users_context(request, domain):
    couch_user = request.couch_user
    web_users = WebUser.by_domain(domain)

    for user in [couch_user] + list(web_users):
        user.current_domain = domain

    return {
        'web_users': web_users,
        'domain': domain,
        'couch_user': couch_user,
    }
Ejemplo n.º 47
0
 def web_users(self):
     web_users = WebUser.by_domain(self.domain)
     teams = Team.get_by_domain(self.domain)
     for team in teams:
         for user in team.get_members():
             if user.get_id not in [web_user.get_id for web_user in web_users]:
                 user.from_team = True
                 web_users.append(user)
     for user in web_users:
         user.current_domain = self.domain
     web_users.sort(key=lambda x: (x.role_label(), x.email))
     return web_users
Ejemplo n.º 48
0
    def main_context(self):
        contacts = CommCareUser.by_domain(self.domain, reduce=True)
        web_users = WebUser.by_domain(self.domain)
        web_users_admins = web_users_read_only = 0
        facilities = SQLLocation.objects.filter(domain=self.domain, location_type__name__iexact='FACILITY')
        admin_role_list = UserRole.by_domain_and_name(self.domain, 'Administrator')
        if admin_role_list:
            admin_role = admin_role_list[0]
        else:
            admin_role = None
        for web_user in web_users:
            dm = web_user.get_domain_membership(self.domain)
            if admin_role and dm.role_id == admin_role.get_id:
                web_users_admins += 1
            else:
                web_users_read_only += 1

        main_context = super(GlobalStats, self).main_context
        entities_reported_stock = SQLLocation.objects.filter(
            domain=self.domain,
            location_type__administrative=False
        ).count()

        context = {
            'root_name': self.root_name,
            'country': SQLLocation.objects.filter(domain=self.domain,
                                                  location_type__name__iexact=self.root_name).count(),
            'region': SQLLocation.objects.filter(domain=self.domain, location_type__name__iexact='region').count(),
            'district': SQLLocation.objects.filter(
                domain=self.domain,
                location_type__name__iexact='district'
            ).count(),
            'entities_reported_stock': entities_reported_stock,
            'facilities': len(facilities),
            'contacts': contacts[0]['value'] if contacts else 0,
            'web_users': len(web_users),
            'web_users_admins': web_users_admins,
            'web_users_read_only': web_users_read_only,
            'products': SQLProduct.objects.filter(domain=self.domain, is_archived=False).count(),
            'product_stocks': StockState.objects.filter(sql_product__domain=self.domain).count(),
            'stock_transactions': StockTransaction.objects.filter(report__domain=self.domain).count(),
            'inbound_messages': SMS.count_by_domain(self.domain, direction=INCOMING),
            'outbound_messages': SMS.count_by_domain(self.domain, direction=OUTGOING),
        }

        if self.show_supply_point_types:
            counts = SQLLocation.objects.values('location_type__name').filter(domain=self.domain).annotate(
                Count('location_type')
            ).order_by('location_type__name')
            context['location_types'] = counts
        main_context.update(context)
        return main_context
Ejemplo n.º 49
0
    def save(self, request, domain):
        try:
            if self.can_use_custom_logo:
                logo = self.cleaned_data['logo']
                if logo:

                    input_image = Image.open(io.BytesIO(logo.read()))
                    input_image.load()
                    input_image.thumbnail(LOGO_SIZE)
                    # had issues trying to use a BytesIO instead
                    tmpfilename = "/tmp/%s_%s" % (uuid.uuid4(), logo.name)
                    input_image.save(tmpfilename, 'PNG')

                    with open(tmpfilename) as tmpfile:
                        domain.put_attachment(tmpfile, name=LOGO_ATTACHMENT)
                elif self.cleaned_data['delete_logo']:
                    domain.delete_attachment(LOGO_ATTACHMENT)

            domain.call_center_config.enabled = self.cleaned_data.get('call_center_enabled', False)
            if domain.call_center_config.enabled:
                domain.internal.using_call_center = True
                domain.call_center_config.case_owner_id = self.cleaned_data.get('call_center_case_owner', None)
                domain.call_center_config.case_type = self.cleaned_data.get('call_center_case_type', None)

            global_tz = self.cleaned_data['default_timezone']
            if domain.default_timezone != global_tz:
                domain.default_timezone = global_tz
                users = WebUser.by_domain(domain.name)
                users_to_save = []
                for user in users:
                    dm = user.get_domain_membership(domain.name)
                    if not dm.override_global_tz and dm.timezone != global_tz:
                        dm.timezone = global_tz
                        users_to_save.append(user)
                if users_to_save:
                    WebUser.bulk_save(users_to_save)

            secure_submissions = self.cleaned_data.get(
                'secure_submissions', False)
            apps_to_save = []
            if secure_submissions != domain.secure_submissions:
                for app in get_apps_in_domain(domain.name):
                    if app.secure_submissions != secure_submissions:
                        app.secure_submissions = secure_submissions
                        apps_to_save.append(app)
            domain.secure_submissions = secure_submissions
            domain.save()
            if apps_to_save:
                ApplicationBase.bulk_save(apps_to_save)
            return True
        except Exception:
            return False
Ejemplo n.º 50
0
    def main_context(self):
        contacts = CommCareUser.by_domain(self.domain, reduce=True)
        web_users = WebUser.by_domain(self.domain)
        web_users_admins = web_users_read_only = 0
        facilities = SQLLocation.objects.filter(domain=self.domain, location_type__name__iexact='FACILITY')
        admin_role_list = UserRole.by_domain_and_name(self.domain, 'Administrator')
        if admin_role_list:
            admin_role = admin_role_list[0]
        else:
            admin_role = None
        for web_user in web_users:
            dm = web_user.get_domain_membership(self.domain)
            if admin_role and dm.role_id == admin_role.get_id:
                web_users_admins += 1
            else:
                web_users_read_only += 1

        main_context = super(GlobalStats, self).main_context
        entities_reported_stock = SQLLocation.objects.filter(
            domain=self.domain,
            location_type__administrative=False
        ).count()

        context = {
            'root_name': self.root_name,
            'country': SQLLocation.objects.filter(domain=self.domain,
                                                  location_type__name__iexact=self.root_name).count(),
            'region': SQLLocation.objects.filter(domain=self.domain, location_type__name__iexact='region').count(),
            'district': SQLLocation.objects.filter(
                domain=self.domain,
                location_type__name__iexact='district'
            ).count(),
            'entities_reported_stock': entities_reported_stock,
            'facilities': len(facilities),
            'contacts': contacts[0]['value'] if contacts else 0,
            'web_users': len(web_users),
            'web_users_admins': web_users_admins,
            'web_users_read_only': web_users_read_only,
            'products': SQLProduct.objects.filter(domain=self.domain, is_archived=False).count(),
            'product_stocks': StockState.objects.filter(sql_product__domain=self.domain).count(),
            'stock_transactions': StockTransaction.objects.filter(report__domain=self.domain).count(),
            'inbound_messages': SMSLog.count_incoming_by_domain(self.domain),
            'outbound_messages': SMSLog.count_outgoing_by_domain(self.domain)
        }

        if self.show_supply_point_types:
            counts = SQLLocation.objects.values('location_type__name').filter(domain=self.domain).annotate(
                Count('location_type')
            ).order_by('location_type__name')
            context['location_types'] = counts
        main_context.update(context)
        return main_context
Ejemplo n.º 51
0
    def tearDown(self):
        for location in Location.by_domain(self.TEST_DOMAIN):
            location.delete()

        for user in WebUser.by_domain(self.TEST_DOMAIN):
            user.delete()

        delete_domain_phone_numbers(self.TEST_DOMAIN)

        for product in Product.by_domain(self.TEST_DOMAIN):
            product.delete()

        super(UrgentNonReportingNotificationTestCase, self).tearDown()
Ejemplo n.º 52
0
def get_users_for_domain(domain):
    users = {u.username for u in WebUser.by_domain(domain)}
    super_users = {
        u['username']
        for u in User.objects.filter(is_superuser=True).values('username')
    }
    users_who_accepted_invitations = set(
        Invitation.objects.filter(is_accepted=True,
                                  domain=domain).values_list('email',
                                                             flat=True))
    removed_users = users_who_accepted_invitations - users
    super_users = super_users - users
    return users, removed_users, super_users
Ejemplo n.º 53
0
 def web_users(self):
     web_users = WebUser.by_domain(self.domain)
     teams = Team.get_by_domain(self.domain)
     for team in teams:
         for user in team.get_members():
             if user.get_id not in [
                     web_user.get_id for web_user in web_users
             ]:
                 user.from_team = True
                 web_users.append(user)
     for user in web_users:
         user.current_domain = self.domain
     return web_users
Ejemplo n.º 54
0
    def apply(self, domain):
        if not self.has_migrated_permissions:
            logging.info("Applying permissions migration to domain %s" % domain.name)
            from corehq.apps.users.models import UserRole, WebUser
            UserRole.init_domain_with_presets(domain.name)
            for web_user in WebUser.by_domain(domain.name):
                try:
                    web_user.save()
                except ResourceConflict:
                    # web_user has already been saved by another thread in the last few seconds
                    pass

            self.has_migrated_permissions = True
            domain.save()
Ejemplo n.º 55
0
 def save(self, request, domain):
     try:
         global_tz = self.cleaned_data['default_timezone']
         domain.default_timezone = global_tz
         users = WebUser.by_domain(domain.name)
         for user in users:
             dm = user.get_domain_membership(domain.name)
             if not dm.override_global_tz:
                 dm.timezone = global_tz
                 user.save()
         domain.save()
         return True
     except Exception:
         return False
Ejemplo n.º 56
0
 def test_webusers_migration(self):
     checkpoint = MigrationCheckpoint(domain=TEST_DOMAIN,
                                      start_date=datetime.utcnow(),
                                      date=datetime.utcnow(),
                                      api='product',
                                      limit=100,
                                      offset=0)
     location_api = ApiSyncObject('webuser', self.endpoint.get_webusers,
                                  self.api_object.web_user_sync)
     synchronization(location_api, checkpoint, None, 100, 0)
     self.assertEqual('webuser', checkpoint.api)
     self.assertEqual(100, checkpoint.limit)
     self.assertEqual(0, checkpoint.offset)
     self.assertEqual(5, len(list(WebUser.by_domain(TEST_DOMAIN))))
Ejemplo n.º 57
0
def _notify_dimagi_users_on_domain(domain):
    from corehq.apps.users.models import WebUser
    from corehq.apps.hqwebapp.tasks import send_mail_async
    recipients = [
        user.get_email() for user in WebUser.by_domain(domain)
        if user.is_dimagi
    ]

    subject = 'CommCare HQ project migrated to the scale backend.'.format(
        domain)
    message = """
    The CommCare HQ project "{}" has been migrated to the scale backend.

    You should not notice anything different but if you do please report a bug.
    """.format(domain)
    send_mail_async.delay(subject, message, settings.DEFAULT_FROM_EMAIL,
                          recipients)
Ejemplo n.º 58
0
def audit_logs(request, domain):
    usernames = [user.username for user in WebUser.by_domain(domain)]
    data = {}
    for username in usernames:
        data[username] = []
        for doc in get_db().view('auditcare/urlpath_by_user_date',
                                 startkey=[username],
                                 endkey=[username, {}],
                                 include_docs=True,
                                 wrapper=lambda r: r['doc']).all():
            try:
                (d, ) = re.search(r'^/a/([\w\-_\.]+)/',
                                  doc['request_path']).groups()
                if d == domain:
                    data[username].append(doc)
            except Exception:
                pass
    return json_response(data)
Ejemplo n.º 59
0
def _users_context(request, domain):
    couch_user = request.couch_user
    web_users = WebUser.by_domain(domain)
    teams = Team.get_by_domain(domain)
    for team in teams:
        for user in team.get_members():
            if user.get_id not in [web_user.get_id for web_user in web_users]:
                user.from_team = True
                web_users.append(user)

    for user in [couch_user] + list(web_users):
        user.current_domain = domain

    return {
        'web_users': web_users,
        'domain': domain,
        'couch_user': couch_user,
    }