def get_by_email(email): """ Get chino user object by email, null if it not exist NB: the mail attribute requires indexing! """ if chino_data.CHINO_DEBUG: return User._create_debug_user() _chino = models.get_chino_client() res = _chino.searches.users(settings.CHINO_USERS_SCHEMA, result_type="FULL_CONTENT", filters=[{ "field": "email", "type": "eq", "value": email }]) if res.paging.count == 1: return res.users[0] # for cu in get_all_chino_users(_chino): # if cu.attributes.email == email: # return cu return None
def get_by_username(username): """ Get chino user object by email, null if it not exist """ if chino_data.CHINO_DEBUG: return User._create_debug_user() _chino = models.get_chino_client() res = _chino.searches.users(settings.CHINO_USERS_SCHEMA, result_type="FULL_CONTENT", filters=[{ "field": "username", "type": "eq", "value": username }]) if res.paging.count == 1: return res.users[0] # for cu in get_all_chino_users(_chino): # if cu.username == username: # return cu return None
def fetch(django_related): """ Makes a chino call and returns a ChinoUser filled with all """ _chino = models.get_chino_client() chino_related = _chino.documents.detail(django_related.chino_id) user = Related._copy_remote(django_related, chino_related) return user
def update(self, django_related): """ Syncs the given instance of Related, associated with the given django_related, to chino """ _chino = models.get_chino_client() data = { 'content': self._get_attrs(), } # TODO _chino.documents.update(django_related.chino_id, **data) return
def fetch(django_user, indepth=True): """ Makes a chino call and returns a ChinoUser filled with all """ if chino_data.CHINO_DEBUG: return User._create_debug_user() _chino = models.get_chino_client() chino_user = _chino.users.detail(django_user.chino_id) user = User._copy_remote(django_user, chino_user, indepth=indepth) return user
def username_available(username): """ Checks whether the given username is taken or is available """ if chino_data.CHINO_DEBUG: return User._create_debug_user() _chino = models.get_chino_client() exists = _chino.searches.users(settings.CHINO_USERS_SCHEMA, result_type="USERNAME_EXISTS", filters=[{ "field": "username", "type": "eq", "value": username }]) return not exists
def check_password(username, password): if chino_data.CHINO_DEBUG: # Just don't do anything return True # TODO: There will soon be a search/filter feature _chino = models.get_chino_client() try: _chino.users.login(username, password) except CallError: # Unable to authenticate.. return False return True
def set_password(self, django_user, password): if chino_data.CHINO_DEBUG: # Just don't do anything return _chino = models.get_chino_client() # TODO: There will soon be a patch feature data = { 'password': password, } _chino.users.partial_update(django_user.chino_id, **data) return
def create(self, django_related_to): """ Creates the given related in the chino backend. Returns the created Related instance """ _chino = models.get_chino_client() attrs = self._get_attrs() chino_related = _chino.documents.create(settings.CHINO_RELATEDS_SCHEMA, attrs) rel = models.Related(patient=django_related_to, chino_id=chino_related.document_id) rel.save() return rel
def fetch_many(django_users, indepth=False): _chino = models.get_chino_client() chino_ids2user = dict((dj.chino_id, dj) for dj in django_users) chino_ids = [_id for _id in chino_ids2user.iterkeys()] chino_users = _chino.searches.users(settings.CHINO_USERS_SCHEMA, result_type="FULL_CONTENT", filters=[{ "field": "_id", "type": "in", "value": chino_ids }]) return [ User._copy_remote(chino_ids2user[cu.user_id], cu, indepth=indepth) for cu in chino_users.users ]
def email_available(email): """ Checks whether the given email is taken or is available NB: the mail attribute requires indexing! """ if chino_data.CHINO_DEBUG: # Just don't do anything return True _chino = models.get_chino_client() exists = _chino.searches.users(settings.CHINO_USERS_SCHEMA, result_type="EXISTS", filters=[{ "field": "email", "type": "eq", "value": email }]) return not exists
def update(self, django_user): """ Syncs the given instance of User, associated with the given django_user, to chino """ if chino_data.CHINO_DEBUG: # Just don't do anything return _chino = models.get_chino_client() data = { 'username': self.username, 'attributes': self._get_attrs(), } chino_user = _chino.users.update(django_user.chino_id, **data) return
def create(self, password): """ Creates the given user in the chino backend. Returns the created ChinoUser instance """ if chino_data.CHINO_DEBUG: user = models.ChinoUser(chino_id=chino_data.get_random_chino_id()) user.save() return user _chino = models.get_chino_client() attrs = self._get_attrs() chino_user = _chino.users.create(settings.CHINO_USERS_SCHEMA, username=self.username, password=password, attributes=attrs) user = models.ChinoUser(chino_id=chino_user.user_id) user.save() return user
def fetch_many(django_relateds, each=None): # TODO: Fix me: I need improvements: A search query over chino # DB instead of filtering afterwards would be probably the # best solution _chino = models.get_chino_client() chino_ids = dict((dj.chino_id, dj) for dj in django_relateds) res = [] for cr in _chino.documents.list(settings.CHINO_RELATEDS_SCHEMA, full_document=True).documents: if cr.document_id in chino_ids: dj_rel = chino_ids[cr.document_id] related = Related._copy_remote(dj_rel, cr) if each: each(dj_rel, related) res.append(related) pass pass return res
def list_users(): _chino = models.get_chino_client() for u in get_all_chino_users(_chino): print u.username, u.user_id, u.attributes.first_name, u.attributes.last_name
def delete(django_related): _chino = models.get_chino_client() _chino.documents.delete(django_related.chino_id, force=True) return