async def delete(self, _id): try: result = await self._coll.delete_one(filter={'id': _id}) except pymongo.errors.ConnectionFailure as err: raise MongoConnError(err) if result.deleted_count is 0: raise ResourceNotFound(_id) return
async def get_all(self, request): user = request.match_info['user'] if user == '_self': user = await self.aa.get_user(request) else: await self.aa.require_admin(request) if not await self.users.resource_exists(user): raise ResourceNotFound(user) return json_response(await self.credentials.get_all(user))
async def get(self, _id, fields=None): try: result = await self._coll.find_one( filter={ 'id': _id, }, projection=self._projection(fields)) except pymongo.errors.ConnectionFailure as err: raise MongoConnError(err) if result is None: raise ResourceNotFound(_id) return self._format(result)
async def update(self, _id, payload): update = {'$set': {}} for k, v in payload.items(): update['$set'][k] = v try: result = await self._coll.find_one_and_update( filter={'id': _id}, update=update, projection=self._projection(), return_document=pymongo.ReturnDocument.AFTER) except pymongo.errors.ConnectionFailure as err: raise MongoConnError(err) if result is None: raise ResourceNotFound(_id) return self._format(result)
async def post(self, request): user = request.match_info['user'] if user == '_self': user = await self.aa.get_user(request) else: await self.aa.require_admin(request) payload = await request.json() jsonschema.validate(payload, CREDENTIALS_CREATE, format_checker=jsonschema.draft4_format_checker) payload = payload.get('data') if not await self.users.resource_exists(user): raise ResourceNotFound(user) return json_response(await self.credentials.create(user, payload), status=201)
async def get(self, _id, owner): try: result = await self._coll.find_one(filter={ 'id': self._str_uuid_2_bin(_id), 'owner': owner }, projection=self._projection()) if result is None: raise ResourceNotFound(_id) if 'created' in result: result['created'] = str(result['created']) if 'id' in result: result['id'] = str(result['id']) return self._format(result) except pymongo.errors.ConnectionFailure as err: raise MongoConnError(err)
async def set_ipa_otp(self, _id, ipa_otp): if not ipa_otp: ipa_otp = "" update = {'$set': {}} update['$set']['ipa_otp'] = ipa_otp try: result = await self._coll.find_one_and_update( filter={'id': _id}, update=update, projection=self._projection(), return_document=pymongo.ReturnDocument.AFTER) except pymongo.errors.ConnectionFailure as err: raise MongoConnError(err) if result is None: raise ResourceNotFound(_id) return self._format(result)