Complete CRUD UserView

This commit is contained in:
sfigato 2022-02-01 23:30:49 +01:00
parent 297b31ab90
commit 7201d9a529
Signed by: blallo
GPG Key ID: 0CBE577C9B72DC3F

View File

@ -1,27 +1,131 @@
from aiohttp.web import json_response, View from aiohttp.web import json_response, View
from aiohttp.web import HTTPNotFound, HTTPUnprocessableEntity from aiohttp.web import (
HTTPNotFound,
HTTPUnprocessableEntity,
HTTPServerError,
HTTPNoContent,
HTTPBadRequest,
HTTPCreated,
)
from phi.logging import get_logger from phi.logging import get_logger
from phi.api.utils import serialize from phi.api.utils import serialize
from phi.async_ldap.model import Hackers from phi.async_ldap.model import Hackers, User
from phi.exceptions import (
PhiEntryDoesNotExist,
PhiUnexpectedRuntimeValue,
PhiCannotExecute,
)
log = get_logger(__name__) log = get_logger(__name__)
class UserView(View): class UserView(View):
def get_log(self):
return self.request.app["log"]
def get_uid(self):
try:
return self.request.match_info["uid"]
except KeyError:
raise HTTPUnprocessableEntity
async def find_user(self, uid):
log = self.get_log()
try:
user = await self.request.app["users"].search(uid)
log.info("Found user %s", user)
except PhiEntryDoesNotExist as e:
log.info(e)
raise HTTPNotFound
return user
async def get(self): async def get(self):
uid = self.request.match_info.get("uid", None) log = self.get_log()
log.debug(f"{self.__class__.__name__}.get")
uid = self.get_uid()
if uid is None: user = await self.find_user(uid)
return HTTPUnprocessableEntity()
user = await self.request.app["users"].get_by_uid(uid)
self.request.app["alog"].info("Found user %s", user)
if not user:
return HTTPNotFound()
result = await user.describe() result = await user.describe()
self.request.app["alog"].debug("Returning result %s", result) log.debug("Returning result %s", result)
return json_response(result) return json_response(result)
async def post(self):
log = self.get_log()
log.debug(f"{self.__class__.__name__}.post")
uid = self.get_uid()
user = await self.find_user(uid)
try:
user = await self.request.app["users"].search(uid)
log.info("Found user %s", user)
except PhiEntryDoesNotExist as e:
log.debug(e)
raise HTTPNotFound(text=str(e))
except PhiUnexpectedRuntimeValue as e:
log.error("Exception: %s", e)
raise HTTPServerError
body = await self.request.json()
for k, v in body.items():
try:
user[k] = v
except PhiCannotExecute as e:
err = str(e)
log.error(err)
raise HTTPBadRequest(text=err)
try:
await user.modify()
except Exception as e:
log.error("Exception: %s", e)
raise HTTPNoContent
async def put(self):
log = self.get_log()
log.debug(f"{self.__class__.__name__}.put")
uid = self.get_uid()
user = User(self.request.app["ldap_client"], uid)
body = await self.request.json()
try:
for k, v in body.items():
user[k] = v
log.debug(f"Saving: {user}")
await user.save()
except PhiCannotExecute as e:
err = str(e)
log.error(err)
raise HTTPBadRequest(text=err)
log.info(f"New user saved: {user}")
raise HTTPCreated
async def delete(self):
log = self.get_log()
log.debug(f"{self.__class__.__name__}.delete")
uid = self.get_uid()
try:
user = await self.request.app["users"].search(uid)
log.info("Found user %s", user)
await user.delete()
except PhiEntryDoesNotExist as e:
log.debug(e)
raise HTTPNotFound(text=str(e))
except PhiUnexpectedRuntimeValue as e:
log.error("Exception: %s", e)
raise HTTPServerError
raise HTTPNoContent