def test_pillow(self): from corehq.apps.change_feed.topics import get_topic_offset from corehq.pillows.synclog import get_user_sync_history_pillow consumer = get_test_kafka_consumer(topics.SYNCLOG_SQL) # get the seq id before the change is published kafka_seq = get_topic_offset(topics.SYNCLOG_SQL) # make sure user has empty reporting-metadata before a sync ccuser = CommCareUser.get(self.ccuser._id) self.assertEqual(ccuser.reporting_metadata.last_syncs, []) # do a sync synclog = SimplifiedSyncLog(domain=self.domain.name, user_id=self.ccuser._id, date=datetime.datetime(2015, 7, 1, 0, 0), app_id='123') synclog.save() # make sure kafka change updates the user with latest sync info message = next(consumer) change_meta = change_meta_from_kafka_message(message.value) synclog = self._get_latest_synclog() self.assertEqual(change_meta.document_id, synclog._id) self.assertEqual(change_meta.domain, self.domain.name) # make sure processor updates the user correctly pillow = get_user_sync_history_pillow() pillow.process_changes(since=kafka_seq) process_reporting_metadata_staging() ccuser = CommCareUser.get(self.ccuser._id) self.assertEqual(len(ccuser.reporting_metadata.last_syncs), 1) self.assertEqual(ccuser.reporting_metadata.last_syncs[0].sync_date, synclog.date) self.assertEqual(ccuser.reporting_metadata.last_sync_for_user.sync_date, synclog.date)
def test_last_modified_bulk(self): lm = self.user.last_modified CommCareUser.save_docs([self.user]) user = CommCareUser.get(self.user._id) self.assertGreater(user.last_modified, lm) lm = self.user.last_modified CommCareUser.bulk_save([self.user]) user = CommCareUser.get(self.user._id) self.assertGreater(user.last_modified, lm)
def obj_create(self, bundle, request=None, **kwargs): if not Group.by_name(kwargs["domain"], bundle.data.get("name")): bundle.obj = Group(bundle.data) bundle.obj.domain = kwargs["domain"] bundle.obj.save() for user in bundle.obj.users: CommCareUser.get(user).set_groups([bundle.obj._id]) else: raise Exception("A group with this name already exists") return bundle
def obj_create(self, bundle, request=None, **kwargs): if not Group.by_name(kwargs['domain'], bundle.data.get("name")): bundle.obj = Group(bundle.data) bundle.obj.name = bundle.obj.name or '' bundle.obj.domain = kwargs['domain'] bundle.obj.save() for user in bundle.obj.users: CommCareUser.get(user).set_groups([bundle.obj._id]) else: raise AssertionError("A group with name %s already exists" % bundle.data.get("name")) return bundle
def _unarchive_single_location(self): """ Unarchive a single location, caller is expected to handle unarchiving children as well. This is just used to prevent having to do recursive couch queries in `unarchive()`. """ self.is_archived = False self.save() # reopen supply point case if needed sp = self.linked_supply_point() # sanity check that the supply point exists and is not open. # this is important because if you unarchive a child, then try # to unarchive the parent, we don't want to try to open again if sp and sp.closed: for action in sp.actions: if action.action_type == 'close': action.xform.archive(user_id=COMMTRACK_USERNAME) break if self.user_id: from corehq.apps.users.models import CommCareUser user = CommCareUser.get(self.user_id) user.active = True user.save()
def get_context_data(self, **kwargs): context = super(BaseRemindersTester, self).get_context_data(**kwargs) context['phone_number'] = kwargs.get('phone_number') verified_number = VerifiedNumber.by_phone(context['phone_number']) context['phone_user'] = CommCareUser.get( verified_number.owner_id) if verified_number else None return context
def obj_delete(self, bundle, **kwargs): user = CommCareUser.get(kwargs['pk']) if user: user.retire(bundle.request.domain, deleted_by=bundle.request.couch_user, deleted_via=USER_CHANGE_VIA_API) return ImmediateHttpResponse(response=http.HttpAccepted())
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)
def prime_restore(user_ids, version=V1, cache_timeout=None, overwrite_cache=False): from corehq.apps.ota.views import get_restore_response total = len(user_ids) DownloadBase.set_progress(prime_restore, 0, total) ret = {'messages': []} for i, user_id in enumerate(user_ids): try: couch_user = CommCareUser.get(user_id) except ResourceNotFound: ret['messages'].append('User not found: {}'.format(user_id)) continue try: get_restore_response( couch_user.domain, couch_user, since=None, version=version, force_cache=True, cache_timeout=cache_timeout, overwrite_cache=overwrite_cache ) except Exception as e: ret['messages'].append('Error processing user: {}'.format(str(e))) DownloadBase.set_progress(prime_restore, i + 1, total) return ret
def reset_demo_user_restore(commcare_user, domain): """ Updates demo restore for the demo commcare_user """ assert commcare_user.domain == domain # if there is a restore already, delete it delete_demo_restore_for_user(commcare_user) # get latest restore restore = RestoreConfig( project=Domain.get_by_name(domain), restore_user=commcare_user.to_ota_restore_user(), params=RestoreParams(version=V2), ).get_payload().as_file() demo_restore = DemoUserRestore.create(commcare_user._id, restore, domain) # Set reference to new restore try: commcare_user.demo_restore_id = demo_restore.id commcare_user.save() except ResourceConflict: # If CommCareUser.report_metadata gets updated by sync log pillow, after a restore is # generated or for any other reason, there will be a DocumentUpdate conflict commcare_user = CommCareUser.get(commcare_user.get_id) commcare_user.is_demo_user = True commcare_user.demo_restore_id = demo_restore.id commcare_user.save()
def can_edit_form_location(domain, web_user, form): # Domain admins can always edit locations. If the user isn't an admin and # the location restriction is enabled, they can only edit forms that are # explicitly at or below them in the location tree. domain_obj = Domain.get_by_name(domain) if (not toggles.RESTRICT_FORM_EDIT_BY_LOCATION.enabled(domain) or user_can_edit_any_location(web_user, domain_obj)): return True if domain_obj.supports_multiple_locations_per_user: user_id = form.user_id if not user_id: return False form_user = CommCareUser.get(user_id) for location in form_user.locations: if user_can_edit_location(web_user, location.sql_location, domain_obj): return True return False else: form_location = get_xform_location(form) if not form_location: return False return user_can_edit_location(web_user, form_location, domain_obj)
def process_verification(phone_number, msg, backend_id=None): v = VerifiedNumber.by_phone(phone_number, True) if not v: return if not verification_response_ok(msg.text): return msg.domain = v.domain msg.couch_recipient_doc_type = v.owner_doc_type msg.couch_recipient = v.owner_id msg.save() if backend_id: backend = MobileBackend.load(backend_id) else: backend = MobileBackend.auto_load(phone_number, v.domain) # i don't know how to dynamically instantiate this object, which may be any number of doc types... #owner = CommCareMobileContactMixin.get(v.owner_id) assert v.owner_doc_type == 'CommCareUser' owner = CommCareUser.get(v.owner_id) v = owner.save_verified_number(v.domain, phone_number, True, backend.name) with localize(owner.language): send_sms_to_verified_number(v, _(CONFIRM))
def test_pillow(self): from corehq.apps.change_feed.topics import get_topic_offset from corehq.pillows.synclog import get_user_sync_history_pillow consumer = get_test_kafka_consumer(topics.SYNCLOG_SQL) # get the seq id before the change is published kafka_seq = get_topic_offset(topics.SYNCLOG_SQL) # make sure user has empty reporting-metadata before a sync self.assertEqual(self.ccuser.reporting_metadata.last_syncs, []) # do a sync synclog = SyncLog(domain=self.domain.name, user_id=self.ccuser._id, date=datetime.datetime(2015, 7, 1, 0, 0)) synclog.save() # make sure kafka change updates the user with latest sync info message = next(consumer) change_meta = change_meta_from_kafka_message(message.value) synclog = self._get_latest_synclog() self.assertEqual(change_meta.document_id, synclog._id) self.assertEqual(change_meta.domain, self.domain.name) # make sure processor updates the user correctly pillow = get_user_sync_history_pillow() pillow.process_changes(since=kafka_seq, forever=False) ccuser = CommCareUser.get(self.ccuser._id) self.assertEqual(len(ccuser.reporting_metadata.last_syncs), 1) self.assertEqual(ccuser.reporting_metadata.last_syncs[0].sync_date, synclog.date) self.assertEqual(ccuser.reporting_metadata.last_sync_for_user.sync_date, synclog.date)
def test_updates(self): # saving a record multiple times shouldn't retrigger user = self.make_user(self.private_location) self.assertEqual(1, len(self.repeat_records().all())) user.save() self.assertEqual(1, len(self.repeat_records().all())) # modifying the record and saving it shouldn't retrigger if the record hasn't sent user = self.make_user(self.private_location, username="******") self.assertEqual(2, len(self.repeat_records().all())) record = list(self.repeat_records())[-1] user.first_name = "daenerys" user.save() self.assertEqual(2, len(self.repeat_records().all())) _mock_fire_record(record) # record gets taken out of queue self.assertEqual(1, len(self.repeat_records().all())) user = CommCareUser.get(user.user_id) self.assertEqual(user.user_data.get('BETS_user_repeat_record_ids'), record._id) # updating the user after a successful send, should add this user to the queue user.first_name = 'dani' user.save() self.assertEqual(2, len(self.repeat_records().all()))
def _sync_location_user(self): if not self.location.location_id: return if self.location.location_type.has_user and not self.location.user_id: # make sure there's a location user res = list(UserES() .domain(self.domain) .show_inactive() .term('user_location_id', self.location.location_id) .values_list('_id', flat=True)) user_id = res[0] if res else None if user_id: user = CommCareUser.get(user_id) else: user = make_location_user(self.location) user.is_active = True user.user_location_id = self.location.location_id user.set_location(self.location, commit=False) user.save() self.location.user_id = user._id self.location.save() elif self.location.user_id and not self.location.location_type.has_user: # archive the location user user = CommCareUser.get_by_user_id(self.location.user_id, self.domain) if user: user.is_active = False user.save() self.location.user_id = '' self.location.save()
def obj_update(self, bundle, **kwargs): bundle.obj = CommCareUser.get(kwargs['pk']) assert bundle.obj.domain == kwargs['domain'] if self._update(bundle): assert bundle.obj.domain == kwargs['domain'] bundle.obj.save() return bundle
def get_and_assert_practice_user_in_domain(practice_user_id, domain): # raises PracticeUserException if CommCareUser with practice_user_id is not a practice mode user # or if user doesn't belong to domain try: user = CommCareUser.get(practice_user_id) if not user.domain == domain: raise ResourceNotFound except ResourceNotFound: raise PracticeUserException( _("Practice User with id {id} not found, please make sure you have not deleted this user").format( id=practice_user_id) ) if not user.is_demo_user: raise PracticeUserException( _("User {username} is not a practice user, please turn on practice mode for this user").format( username=user.username) ) if user.is_deleted(): raise PracticeUserException( _("User {username} has been deleted, you can't use that user as practice user").format( username=user.username) ) if not user.is_active: raise PracticeUserException( _("User {username} has been deactivated, you can't use that user as practice user").format( username=user.username) ) return user
def report_subtitles(self): with localize(self.lang): sf = self.report_config.get('sf') selected_af = self.request.GET.get('hierarchy_af') selected_block = self.request.GET.get('hierarchy_block') selected_district = self.request.GET.get('hierarchy_district') subtitles = [ _("Report: {0}").format(self.report.name), _("District: {0}").format(selected_district), ] if not sf or sf in ['sf2', 'sf3', 'sf4']: subtitles.extend([ _("Block: {0}").format(selected_block), ]) if sf != 'sf4' and selected_af: user = CommCareUser.get(selected_af) subtitles.append(_("AF: {0} {1}").format(user.first_name, user.last_name)) if sf in ['sf5', 'sf4', 'sf3']: subtitles.append( _("Last Reporting Month of the Quarter: {0} {1}").format( calendar.month_name[int(self.request.GET.get('month'))], self.request.GET.get('year') ) ) else: subtitles.append( _("For Date: {0} to {1}").format(self.datespan.startdate.strftime("%Y-%m-%d"), self.datespan.enddate.strftime("%Y-%m-%d")) ) return subtitles
def _sync_location_user(self): if not self.location.location_id: return if self.location.location_type.has_user and not self.location.user_id: # make sure there's a location user res = list(UserES().domain(self.domain).show_inactive().term( 'user_location_id', self.location.location_id).values_list('_id', flat=True)) user_id = res[0] if res else None if user_id: user = CommCareUser.get(user_id) else: user = make_location_user(self.location) user.is_active = True user.user_location_id = self.location.location_id user.set_location(self.location, commit=False) user.save() self.location.user_id = user._id self.location.save() elif self.location.user_id and not self.location.location_type.has_user: # archive the location user user = CommCareUser.get_by_user_id(self.location.user_id, self.domain) if user: user.is_active = False user.save() self.location.user_id = '' self.location.save()
def test_update(self): self.client.login(username=self.username, password=self.password) user = CommCareUser.create(domain=self.domain.name, username="******", password="******") group = Group({"name": "test"}) group.save() user_json = { "first_name": "test", "last_name": "last", "email": "*****@*****.**", "language": "pol", "phone_numbers": ["+50253311399", "50253314588"], "groups": [group._id], "user_data": {"chw_id": "13/43/DFA"}, } backend_id = user._id response = self.client.put( self.single_endpoint(backend_id), json.dumps(user_json), content_type="application/json" ) self.assertEqual(response.status_code, 200, response.content) self.assertEqual(1, len(CommCareUser.by_domain(self.domain.name))) modified = CommCareUser.get(backend_id) self.assertEqual(modified.username, "test") self.assertEqual(modified.first_name, "test") self.assertEqual(modified.last_name, "last") self.assertEqual(modified.email, "*****@*****.**") self.assertEqual(modified.language, "pol") self.assertEqual(modified.get_group_ids()[0], group._id) self.assertEqual(modified.user_data["chw_id"], "13/43/DFA") self.assertEqual(modified.default_phone_number, "50253311399") modified.delete() group.delete()
def test_heartbeat(self): self._do_request( self.user, device_id='123123', app_version=1, last_sync=datetime.utcnow().isoformat(), unsent_forms=2, quarantined_forms=3 ) device = CommCareUser.get(self.user.get_id).get_device('123123') self.assertEqual(device.device_id, '123123') self.assertIsNotNone(device.last_used) self.assertEqual(device.commcare_version, '2.39') self.assertEqual(1, len(device.app_meta)) app_meta = device.app_meta[0] self.assertEqual(app_meta.app_id, self.app.get_id) self.assertEqual(app_meta.build_id, self.build.get_id) self.assertEqual(app_meta.build_id, self.build.get_id) self.assertEqual(app_meta.build_version, 1) self.assertIsNotNone(app_meta.last_heartbeat) self.assertEqual(app_meta.last_request, app_meta.last_heartbeat) self.assertEqual(app_meta.num_unsent_forms, 2) self.assertEqual(app_meta.num_quarantined_forms, 3) self.assertIsNotNone(app_meta.last_sync)
def by_user(cls, user, wrap=True, domain=None): group_ids = Group.by_user(user, wrap=False) if isinstance(user, dict): # Added 2015-07-31, feel free to remove eventually. _assert = soft_assert('@'.join(['esoergel', 'dimagi.com'])) _assert(False, "This apparently IS called with a user dict. How?") user_id = user.get('user_id') user_domain = domain location = CommCareUser.get(user_id).sql_location else: user_id = user.user_id user_domain = user.domain location = user.sql_location loc_ids = location.path if location else [] def make_keys(owner_type, ids): return [[user_domain, 'data_item by {}'.format(owner_type), id_] for id_ in ids] fixture_ids = set(FixtureOwnership.get_db().view( 'fixtures/ownership', keys=(make_keys('user', [user_id]) + make_keys('group', group_ids) + make_keys('location', loc_ids)), reduce=False, wrapper=lambda r: r['value'], )) if wrap: results = cls.get_db().view('_all_docs', keys=list(fixture_ids), include_docs=True) # sort the results into those corresponding to real documents # and those corresponding to deleted or non-existent documents docs = [] deleted_fixture_ids = set() for result in results: if result.get('doc'): docs.append(cls.wrap(result['doc'])) elif result.get('error'): assert result['error'] == 'not_found' deleted_fixture_ids.add(result['key']) else: assert result['value']['deleted'] is True deleted_fixture_ids.add(result['id']) # fetch and delete ownership documents pointing # to deleted or non-existent fixture documents # this cleanup is necessary since we used to not do this bad_ownerships = FixtureOwnership.for_all_item_ids( deleted_fixture_ids, user_domain) FixtureOwnership.get_db().bulk_delete(bad_ownerships) return docs else: return fixture_ids
def _verify_users_are_accessible(domain, request_user, user_ids): # This function would be very slow if called with many user ids for user_id in user_ids: other_user = CommCareUser.get(user_id) if not user_can_access_other_user(domain, request_user, other_user): raise PermissionDenied("One or more users are not accessible")
def report_subtitles(self): with localize(self.lang): sf = self.report_config.get('sf') selected_af = self.request.GET.get('hierarchy_af') selected_block = self.request.GET.get('hierarchy_block') selected_district = self.request.GET.get('hierarchy_district') subtitles = [ _("Report: {0}").format(self.report.name), _("District: {0}").format(selected_district), ] if not sf or sf in ['sf2', 'sf3', 'sf4']: subtitles.extend([ _("Block: {0}").format(selected_block), ]) if sf != 'sf4' and selected_af: user = CommCareUser.get(selected_af) subtitles.append( _("AF: {0} {1}").format(user.first_name, user.last_name)) if sf in ['sf5', 'sf4', 'sf3']: subtitles.append( _("Last Reporting Month of the Quarter: {0} {1}").format( calendar.month_name[int( self.request.GET.get('month'))], self.request.GET.get('year'))) else: subtitles.append( _("For Date: {0} to {1}").format( self.datespan.startdate.strftime("%Y-%m-%d"), self.datespan.enddate.strftime("%Y-%m-%d"))) return subtitles
def process_verification(phone_number, msg, backend_id=None): v = VerifiedNumber.by_phone(phone_number, True) if not v: return if not verification_response_ok(msg.text): return msg.domain = v.domain msg.couch_recipient_doc_type = v.owner_doc_type msg.couch_recipient = v.owner_id msg.save() if not domain_has_privilege(msg.domain, privileges.INBOUND_SMS): return if backend_id: backend = MobileBackend.load(backend_id) else: backend = MobileBackend.auto_load(phone_number, v.domain) # i don't know how to dynamically instantiate this object, which may be any number of doc types... #owner = CommCareMobileContactMixin.get(v.owner_id) assert v.owner_doc_type == 'CommCareUser' owner = CommCareUser.get(v.owner_id) v = owner.save_verified_number(v.domain, phone_number, True, backend.name) with localize(owner.language): send_sms_to_verified_number(v, _(CONFIRM))
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 raw_value(self, **kwargs): user_id = kwargs.get('user_id') try: user = CommCareUser.get(user_id) return user.user_data.get(self.user_data_key) except Exception: pass return None
def test_blank_last_sync(self): self._do_request( self.user, device_id='2', last_sync='', ) device = CommCareUser.get(self.user.get_id).get_device('2') self.assertIsNone(device.get_meta_for_app(self.app.get_id).last_sync)
def is_user_in_CCT_by_case(case): if is_valid_user_by_case(case): try: user = CommCareUser.get(case.user_id) or None return (user is not None and hasattr(user, "user_data") and user.user_data.get("CCT", None) == "true") except ResourceNotFound: return False return False
def test_stop(self): a = """ 5551234 > stop 5551234 < {} """.format(unicode(STOP_MESSAGE)) self.run_script(a) user = CommCareUser.get(self.user1.get_id) self.assertEqual(user.user_data['needs_reminders'], "False")
def by_user(cls, user, wrap=True, domain=None): group_ids = Group.by_user(user, wrap=False) if isinstance(user, dict): # Added 2015-07-31, feel free to remove eventually. _assert = soft_assert('@'.join(['esoergel', 'dimagi.com'])) _assert(False, "This apparently IS called with a user dict. How?") user_id = user.get('user_id') user_domain = domain location = CommCareUser.get(user_id).sql_location else: user_id = user.user_id user_domain = user.domain location = user.sql_location loc_ids = location.path if location else [] def make_keys(owner_type, ids): return [[user_domain, 'data_item by {}'.format(owner_type), id_] for id_ in ids] fixture_ids = set( FixtureOwnership.get_db().view('fixtures/ownership', keys=(make_keys('user', [user_id]) + make_keys('group', group_ids) + make_keys('location', loc_ids)), reduce=False, wrapper=lambda r: r['value'], ) ) if wrap: results = cls.get_db().view('_all_docs', keys=list(fixture_ids), include_docs=True) # sort the results into those corresponding to real documents # and those corresponding to deleted or non-existent documents docs = [] deleted_fixture_ids = set() for result in results: if result.get('doc'): docs.append(cls.wrap(result['doc'])) elif result.get('error'): assert result['error'] == 'not_found' deleted_fixture_ids.add(result['key']) else: assert result['value']['deleted'] is True deleted_fixture_ids.add(result['id']) # fetch and delete ownership documents pointing # to deleted or non-existent fixture documents # this cleanup is necessary since we used to not do this bad_ownerships = FixtureOwnership.for_all_item_ids(deleted_fixture_ids, user_domain) FixtureOwnership.get_db().bulk_delete(bad_ownerships) return docs else: return fixture_ids
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)
def get_user(self, object_id): try: user = CommCareUser.get(object_id) if user.doc_type == 'CommCareUser' and user.domain == self._cchq_domain: return user except ResourceNotFound: pass return None
def obj_update(self, bundle, **kwargs): bundle.obj = CommCareUser.get(kwargs['pk']) assert bundle.obj.domain == kwargs['domain'] if self._update(bundle): assert bundle.obj.domain == kwargs['domain'] bundle.obj.save() return bundle else: raise BadRequest(''.join(chain.from_iterable(bundle.obj.errors)))
def report_subtitles(self): if self.report_config.get('sf') == 'sf2': selected_af = self.request.GET.get('hierarchy_af') user = CommCareUser.get(selected_af) return [ "Selected AF: {0} {1}".format(user.first_name, user.last_name), "For Date: {0} to {1}".format(self.datespan.startdate.strftime("%Y-%m-%d"), self.datespan.enddate.strftime("%Y-%m-%d")) ]
def user(self): user_id = self.user_id if user_id: try: return CommCareUser.get(user_id) except ResourceNotFound: return None else: return None
def get_user(self, object_id): try: user = CommCareUser.get(object_id) if user.doc_type == 'CommCareUser' and user.domain == self.domain: return user except ResourceNotFound: pass return None
def update_user_data(request, domain, couch_user_id): updated_data = json.loads(request.POST["user-data"]) user = CommCareUser.get(couch_user_id) assert user.doc_type == "CommCareUser" assert user.domain == domain user.user_data = updated_data user.save() messages.success(request, "User data updated!") return HttpResponseRedirect(reverse('user_account', args=[domain, couch_user_id]))
def get_user(username_or_id, domain): if '@' in username_or_id: return get_user_from_username(username_or_id) else: try: couch_user = CommCareUser.get(username_or_id) return couch_user except ResourceNotFound: username_or_id = '{}@{}.commcarehq.org'.format(username_or_id, domain) return get_user_from_username(username_or_id)
def turn_on_demo_mode_task(commcare_user_id, domain): from corehq.apps.ota.utils import turn_on_demo_mode from corehq.apps.users.models import CommCareUser user = CommCareUser.get(commcare_user_id) DownloadBase.set_progress(turn_on_demo_mode_task, 0, 100) results = turn_on_demo_mode(user, domain) DownloadBase.set_progress(turn_on_demo_mode_task, 100, 100) return {'messages': results}
def _expand_user_key(self, key): # given a formatted key, expand it by including all the # owner ids index = self.couch_key.index("<user_id>") user_id = key[index] def _repl(k, index, id): ret = copy(k) ret[index] = id return ret return [_repl(key, index, id) for id in CommCareUser.get(user_id).get_owner_ids()]
def test_bad_app_id(self): url = reverse('phone_heartbeat', args=[self.domain_obj.name, 'bad_id']) self._do_request(self.user, device_id='test_bad_app_id', app_id='no-app', url=url, response_code=404) device = CommCareUser.get( self.user.get_id).get_device('test_bad_app_id') self.assertIsNone(device)
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)
def update_user_data(request, domain, couch_user_id): user_data = request.POST["user-data"] if user_data: updated_data = json.loads(user_data) user = CommCareUser.get(couch_user_id) assert user.doc_type == "CommCareUser" assert user.domain == domain user.user_data = updated_data user.save(spawn_task=True) messages.success(request, "User data updated!") return HttpResponseRedirect(reverse(EditCommCareUserView.urlname, args=[domain, couch_user_id]))
def _remove_user(self): """ Unassigns the users assigned to that location. Used by both archive and delete methods """ if self.user_id: from corehq.apps.users.models import CommCareUser user = CommCareUser.get(self.user_id) user.active = False user.save()
def __init__(self, name, user_ids, month, year, curval, curname): self.name = name self.curval = curval self.curname = curname users = [CommCareUser.get(user_id) for user_id in user_ids] groups = [] for user in users: tmp = get_db().view('case/by_owner', key=[user._id, False], include_docs=True, reduce=False).all() for t in tmp: groups.append(LendingGroup(CommCareCase.get(t['doc']['_id']), month, year)) self.groups = groups if groups else []
def owner(self): if self.owner_doc_type == "CommCareCase": # Circular import from corehq.apps.sms.models import CommConnectCase return CommConnectCase.get(self.owner_id) elif self.owner_doc_type == "CommCareUser": # Circular import from corehq.apps.users.models import CommCareUser return CommCareUser.get(self.owner_id) else: return None