Пример #1
0
    def handle(self, *args, **options):
        for domain in Domain.get_all_names():
            fields_definition = cdm.CustomDataFieldsDefinition.get_or_create(
                domain, 'UserFields')
            had_fields = bool(fields_definition.fields)

            user_ids = (CommCareUser.ids_by_domain(domain) +
                        CommCareUser.ids_by_domain(domain, is_active=False))

            existing_field_slugs = set(
                [field.slug for field in fields_definition.fields])
            for user in iter_docs(CommCareUser.get_db(), user_ids):
                user_data = user.get('user_data', {})
                for key in user_data.keys():
                    if (key and key not in existing_field_slugs
                            and not cdm.is_system_key(key)):
                        existing_field_slugs.add(key)
                        fields_definition.fields.append(
                            cdm.CustomDataField(
                                slug=key,
                                label=key,
                                is_required=False,
                            ))

            for field in fields_definition.fields:
                if cdm.is_system_key(field.slug):
                    fields_definition.fields.remove(field)
            # Only save a definition for domains which use custom user data
            if fields_definition.fields or had_fields:
                fields_definition.save()
            print 'finished domain "{}"'.format(domain)
    def handle(self, *args, **options):
        for domain in Domain.get_all_names():
            fields_definition = cdm.CustomDataFieldsDefinition.get_or_create(
                domain,
                'UserFields'
            )
            had_fields = bool(fields_definition.fields)

            user_ids = (CommCareUser.ids_by_domain(domain) +
                        CommCareUser.ids_by_domain(domain, is_active=False))

            existing_field_slugs = set([field.slug for field in fields_definition.fields])
            for user in iter_docs(CommCareUser.get_db(), user_ids):
                user_data = user.get('user_data', {})
                for key in user_data.keys():
                    if (key and key not in existing_field_slugs
                        and not cdm.is_system_key(key)):
                        existing_field_slugs.add(key)
                        fields_definition.fields.append(cdm.CustomDataField(
                            slug=key,
                            label=key,
                            is_required=False,
                        ))

            for field in fields_definition.fields:
                if cdm.is_system_key(field.slug):
                    fields_definition.fields.remove(field)
            # Only save a definition for domains which use custom user data
            if fields_definition.fields or had_fields:
                fields_definition.save()
            print 'finished domain "{}"'.format(domain.name)
    def handle(self, *args, **options):
        for domain in Domain.get_all_names():
            fields_definition = CustomDataFieldsDefinition.get_or_create(
                domain,
                'UserFields'
            )

            user_ids = (CommCareUser.ids_by_domain(domain) +
                        CommCareUser.ids_by_domain(domain, is_active=False))

            existing_field_slugs = set([field.slug for field in fields_definition.fields])
            for user in iter_docs(CommCareUser.get_db(), user_ids):
                user_data = user.get('user_data', {})
                for key in user_data.keys():
                    if key and key not in existing_field_slugs:
                        existing_field_slugs.add(key)
                        fields_definition.fields.append(CustomDataField(
                            slug=key,
                            label=key,
                            is_required=False
                        ))

            # Only save a definition for domains which use custom user data
            if fields_definition.fields:
                fields_definition.save()
 def handle(self, domain, **options):
     ids = (CommCareUser.ids_by_domain(domain, is_active=True) +
            CommCareUser.ids_by_domain(domain, is_active=False))
     for doc in iter_docs(CommCareUser.get_db(), ids):
         user = CommCareUser.wrap(doc)
         try:
             self.process_user(user)
         except Exception as e:
             print("Error processing user %s: %s" % (user._id, e))
 def handle(self, domain, **options):
     ids = (
         CommCareUser.ids_by_domain(domain, is_active=True) +
         CommCareUser.ids_by_domain(domain, is_active=False)
     )
     for doc in iter_docs(CommCareUser.get_db(), ids):
         user = CommCareUser.wrap(doc)
         try:
             self.process_user(user)
         except Exception as e:
             print("Error processing user %s: %s" % (user._id, e))
Пример #6
0
    def handle(self, **kwargs):
        domains = kwargs['domains'].split(',')

        for domain in domains:
            print("Resync all contacts' phone numbers for project %s  " %
                  domain)
            print("Synching for phone numbers")
            commcare_user_ids = (
                CommCareUser.ids_by_domain(domain, is_active=True) +
                CommCareUser.ids_by_domain(domain, is_active=False))
            for user_id in with_progress_bar(commcare_user_ids):
                sms_sync_user_phone_numbers.delay(user_id)
            self.sync_cases(domain)
Пример #7
0
    def handle(self, **kwargs):
        domains = kwargs['domains'].split(',')

        for domain in domains:
            print("Resync all contacts' phone numbers for project %s  " % domain)
            print("Synching for phone numbers")
            commcare_user_ids = (
                CommCareUser.ids_by_domain(domain, is_active=True) +
                CommCareUser.ids_by_domain(domain, is_active=False)
            )
            for user_id in with_progress_bar(commcare_user_ids):
                sms_sync_user_phone_numbers.delay(user_id)
            self.sync_cases(domain)
Пример #8
0
    def handle(self, *args, **options):
        if len(args) == 0:
            raise CommandError(
                "Usage: python manage.py resync_location_user_data %s" %
                self.args)

        domain = args[0]
        ids = (CommCareUser.ids_by_domain(domain, is_active=True) +
               CommCareUser.ids_by_domain(domain, is_active=False))
        for doc in iter_docs(CommCareUser.get_db(), ids):
            user = CommCareUser.wrap(doc)
            try:
                self.process_user(user)
            except Exception as e:
                print "Error processing user %s: %s" % (user._id, e)
Пример #9
0
    def handle(self, *args, **options):
        if len(args) == 0:
            raise CommandError("Usage: python manage.py resync_location_user_data %s" % self.args)

        domain = args[0]
        ids = (
            CommCareUser.ids_by_domain(domain, is_active=True) +
            CommCareUser.ids_by_domain(domain, is_active=False)
        )
        for doc in iter_docs(CommCareUser.get_db(), ids):
            user = CommCareUser.wrap(doc)
            try:
                self.process_user(user)
            except Exception as e:
                print "Error processing user %s: %s" % (user._id, e)
Пример #10
0
    def handle(self, month_year, file_path, **options):
        month_year_parsed = dateutil.parser.parse('1-' + month_year)
        start_date = month_year_parsed.replace(day=1)
        end_date = start_date + relativedelta(
            day=1, months=+1, microseconds=-1)

        with open(file_path, 'wb') as file_object:
            writer = csv.writer(file_object)
            writer.writerow([
                'domain name', 'user id',
                'total number of forms submitted in a month',
                'used management case?', 'multiple_form_types?'
            ])

            for domain_object in Domain.get_all():
                domain_name = domain_object.name
                user_ids = CommCareUser.ids_by_domain(domain=domain_name)
                buckets_dict = get_forms_for_users(domain_object.name,
                                                   user_ids, start_date,
                                                   end_date)
                for user_id, bucket in buckets_dict.iteritems():
                    has_case = bool(
                        filter(lambda h: h.get('form', {}).get('case'),
                               bucket.top_hits_user_submissions.hits))
                    has_two_or_more_different_forms_submitted = len([
                        hit.get('form', {}).get('@xmlns')
                        for hit in bucket.top_hits_user_submissions.hits
                        if hit.get('form', {}).get('@xmlns')
                    ]) >= 2
                    writer.writerow([
                        domain_name, user_id, bucket.doc_count, has_case,
                        has_two_or_more_different_forms_submitted
                    ])
Пример #11
0
    def _get_all_active_and_submitted_users(self):
        """
            Active users are:
            1) All CommCareUser objects who have the is_active flag set to True at the time of this bill generation.
            2) All CommCareUser objects who have the is_active flag set to False at the time of this bill generation
                but have submitted things to CommCare HQ during the span of the billing period.
        """
        from corehq.apps.users.models import CommCareUser
        active_user_ids = set(CommCareUser.ids_by_domain(self.domain))
        inactive_user_ids = CommCareUser.ids_by_domain(self.domain, is_active=False)

        inactive_submitted_user_ids = set(FormData.object
            .filter(domain=self.domain)
            .filter(user_id__in=inactive_user_ids)
            .distinct('user_id')
            .values_list('user_id', flat=True)
        )

        self.active_users = active_user_ids | inactive_submitted_user_ids
Пример #12
0
def delete_connections_field_task(domain):
    to_save = []
    for user in iter_docs(CommCareUser.get_db(), CommCareUser.ids_by_domain(domain)):
        if 'connections' in user['user_data']:
            del user['user_data']['connections']
            to_save.append(user)
            if len(to_save) > 500:
                CommCareUser.get_db().bulk_save(to_save)
                to_save = []

    if to_save:
        CommCareUser.get_db().bulk_save(to_save)
Пример #13
0
    def form_valid(self):
        res = queue_prime_restore(self.domain,
                                  CommCareUser.ids_by_domain(self.domain),
                                  version=V2,
                                  cache_timeout_hours=24,
                                  overwrite_cache=True,
                                  check_cache_only=False)
        download = MultipleTaskDownload()
        download.set_task(res)
        download.save()

        return redirect('hq_soil_download', self.domain, download.download_id)
Пример #14
0
    def form_valid(self):
        download = DownloadBase()
        res = prime_restore.delay(
            self.domain,
            CommCareUser.ids_by_domain(self.domain),
            version=V2,
            cache_timeout_hours=24,
            overwrite_cache=True,
            check_cache_only=False
        )
        download.set_task(res)

        return redirect('hq_soil_download', self.domain, download.download_id)
Пример #15
0
    def form_valid(self):
        res = queue_prime_restore(
            self.domain,
            CommCareUser.ids_by_domain(self.domain),
            version=V2,
            cache_timeout_hours=24,
            overwrite_cache=True,
            check_cache_only=False
        )
        download = MultipleTaskDownload()
        download.set_task(res)
        download.save()

        return redirect('hq_soil_download', self.domain, download.download_id)
Пример #16
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
    all_users = CommCareUser.ids_by_domain(domain)
    safe_ids = [u for u in selected_users if u in all_users]

    group.users = safe_ids
    group.save()
    messages.success(request, _("Group %s updated!") % group.name)
    return HttpResponseRedirect(reverse("group_members", args=[domain, group_id]))
Пример #17
0
def update_group_membership(request, domain, group_id):
    group = Group.get(group_id)
    if group.domain != domain:
        return HttpResponseForbidden()

    form = MultipleSelectionForm(request.POST)
    form.fields['selected_ids'].choices = [(id, 'throwaway') for id in CommCareUser.ids_by_domain(domain)]
    if form.is_valid():
        group.users = form.cleaned_data['selected_ids']
        group.save()
        messages.success(request, _("Group %s updated!") % group.name)
    else:
        messages.error(request, _("Form not valid. A user may have been deleted while you were viewing this page"
                                  "Please try again."))
    return HttpResponseRedirect(reverse("group_members", args=[domain, group_id]))
Пример #18
0
def _update_group_membership(request, domain, group_id):
    group = Group.get(group_id)
    if group.domain != domain:
        return HttpResponseForbidden()

    selected_users = request.POST.get('selected_ids').split(',')

    # check to make sure no users were deleted at time of making group
    all_users = CommCareUser.ids_by_domain(domain)
    safe_ids = [u for u in selected_users if u in all_users]

    group.users = safe_ids
    group.save()
    messages.success(request, _("Group %s updated!") % group.name)
    return HttpResponseRedirect(reverse("group_members", args=[domain, group_id]))
Пример #19
0
def update_group_membership(request, domain, group_id):
    group = Group.get(group_id)
    if group.domain != domain:
        return HttpResponseForbidden()

    form = MultipleSelectionForm(request.POST)
    form.fields['selected_ids'].choices = [(id, 'throwaway') for id in CommCareUser.ids_by_domain(domain)]
    if form.is_valid():
        group.users = form.cleaned_data['selected_ids']
        group.save()
        messages.success(request, _("Group %s updated!") % group.name)
    else:
        messages.error(request, _("Form not valid. A user may have been deleted while you were viewing this page"
                                  "Please try again."))
    return HttpResponseRedirect(reverse("group_members", args=[domain, group_id]))
Пример #20
0
    def handle(self, month_year, file_path, **options):
        month_year_parsed = dateutil.parser.parse('1-' + month_year)
        start_date = month_year_parsed.replace(day=1)
        end_date = start_date + relativedelta(day=1, months=+1, microseconds=-1)

        with open(file_path, 'wb') as file_object:
            writer = csv.writer(file_object)
            writer.writerow([
                'domain name',
                'user id',
                'total number of forms submitted in a month',
                'used case management',
                'multiple form types'
            ])

            for domain in with_progress_bar(Domain.get_all(include_docs=False)):
                domain_name = domain['key']
                user_ids = CommCareUser.ids_by_domain(domain=domain_name)
                for users in chunked(user_ids, 100):
                    forms = get_forms_for_users(domain_name, users, start_date, end_date)
                    user_dict = defaultdict(list)
                    for form in forms:
                        user_id = form['form']['meta']['userID']
                        user_dict[user_id].append(form)
                    for user_id, forms in six.iteritems(user_dict):
                        has_two_forms_submitted = False
                        has_case = False
                        unique_forms = set()
                        for form in forms:
                            if has_case and has_two_forms_submitted:
                                break
                            if not has_case and form.get('form', {}).get('case'):
                                has_case = True
                            if not has_two_forms_submitted:
                                xmlns = form.get('form', {}).get('@xmlns')
                                if xmlns:
                                    unique_forms.add(xmlns)
                                    if len(unique_forms) >= 2:
                                        has_two_forms_submitted = True
                        writer.writerow([
                            domain_name,
                            user_id,
                            len(forms),
                            has_case,
                            has_two_forms_submitted
                        ])
Пример #21
0
    def form_valid(self, form):
        domain = form.cleaned_data['domain']
        if form.cleaned_data['all_users']:
            user_ids = CommCareUser.ids_by_domain(domain)
        else:
            user_ids = form.user_ids

        download = DownloadBase()
        res = prime_restore.delay(
            user_ids,
            version=form.cleaned_data['version'],
            cache_timeout=form.cleaned_data['cache_timeout'],
            overwrite_cache=form.cleaned_data['overwrite_cache']
        )
        download.set_task(res)

        return redirect('hq_soil_download', domain, download.download_id)
Пример #22
0
    def handle(self, month_year, file_path, **options):
        month_year_parsed = dateutil.parser.parse('1-' + month_year)
        start_date = month_year_parsed.replace(day=1)
        end_date = start_date + relativedelta(day=1, months=+1, microseconds=-1)

        with open(file_path, 'wb') as file_object:
            writer = csv.writer(file_object)
            writer.writerow([
                'domain name',
                'user id',
                'total number of forms submitted in a month',
                'used case management',
                'multiple form types'
            ])

            for domain in with_progress_bar(Domain.get_all(include_docs=False)):
                domain_name = domain['key']
                user_ids = CommCareUser.ids_by_domain(domain=domain_name)
                for users in chunked(user_ids, 100):
                    forms = get_forms_for_users(domain_name, users, start_date, end_date)
                    user_dict = defaultdict(list)
                    for form in forms:
                        user_id = form['form']['meta']['userID']
                        user_dict[user_id].append(form)
                    for user_id, forms in six.iteritems(user_dict):
                        has_two_forms_submitted = False
                        has_case = False
                        unique_forms = set()
                        for form in forms:
                            if has_case and has_two_forms_submitted:
                                break
                            if not has_case and form.get('form', {}).get('case'):
                                has_case = True
                            if not has_two_forms_submitted:
                                xmlns = form.get('form', {}).get('@xmlns')
                                if xmlns:
                                    unique_forms.add(xmlns)
                                    if len(unique_forms) >= 2:
                                        has_two_forms_submitted = True
                        writer.writerow([
                            domain_name,
                            user_id,
                            len(forms),
                            has_case,
                            has_two_forms_submitted
                        ])
Пример #23
0
    def form_valid(self):
        if self.form.cleaned_data['all_users']:
            user_ids = CommCareUser.ids_by_domain(self.domain)
        else:
            user_ids = self.form.user_ids

        res = queue_prime_restore(
            self.domain,
            user_ids,
            version=V2,
            cache_timeout_hours=24,
            overwrite_cache=self.form.cleaned_data['overwrite_cache'],
            check_cache_only=self.form.cleaned_data['check_cache_only'])
        download = MultipleTaskDownload()
        download.set_task(res)
        download.save()

        return redirect('hq_soil_download', self.domain, download.download_id)
Пример #24
0
    def form_valid(self):
        if self.form.cleaned_data['all_users']:
            user_ids = CommCareUser.ids_by_domain(self.domain)
        else:
            user_ids = self.form.user_ids

        download = DownloadBase()
        res = prime_restore.delay(
            self.domain,
            user_ids,
            version=V2,
            cache_timeout_hours=24,
            overwrite_cache=self.form.cleaned_data['overwrite_cache'],
            check_cache_only=self.form.cleaned_data['check_cache_only']
        )
        download.set_task(res)

        return redirect('hq_soil_download', self.domain, download.download_id)
Пример #25
0
 def get_users(self):
     user_ids = CommCareUser.ids_by_domain(self.domain)
     for user_doc in iter_docs(CommCareUser.get_db(), user_ids):
         yield CommCareUser.wrap(user_doc)
Пример #26
0
 def get_users(self):
     user_ids = CommCareUser.ids_by_domain(self.domain)
     for user_doc in iter_docs(CommCareUser.get_db(), user_ids):
         yield CommCareUser.wrap(user_doc)