def get(self): clients = Client.objects().all() return res( "Returned list of clients", "success", clients=convert_query(clients, list=True), )
def get(self, id): try: client = Client.objects(id=id)[0] return res("Returned client", "success", client=convert_query(client)) except: return res("Client doesn't exist", "error"), 400
def delete(self, id): caller = get_bearer(request) if caller["role"] != "admin": return res("⛔️ Must be an admin to delete a client", "error"), 400 try: client = Client.objects(id=id) except: return res("Client doesn't exist", "error"), 400 client.delete() return res("Deleted client", "success")
def put(self, id): caller = get_caller(request) if caller["role"] != "admin": return res("⛔️ Must be an admin to modify a client", "error"), 400 req = parse(request) errors = ClientSchema().validate(req) if errors: return res("Errors in request", "alert", errors=errors), 400 try: client = Client.objects(id=id)[0] except: return res("Client doesn't exist", "error"), 400 for i in req: client[i] = req[i] client.save() return res("Modified client", "success", client=convert_query(client))
def put(self, id): caller = get_bearer(request) if caller["id"] == id: pass elif caller["role"] != "admin": return res("⛔️ Must be an admin to edit another user", "error"), 400 req = parse(request) errors = UserSchema().validate(req) if errors: return res("Errors in request", "alert", errors=errors), 400 try: user = User.objects(id=id)[0] except: return res("User doesn't exist", "error"), 400 for i in req: if i == "role" and caller["role"] != "admin": return res("⛔️ Cannot change your own role", "error"), 400 if i == "role": # If changing to an admin, remove fields they shouldn't have if req[i] in [ "admin", "pending", ]: # If we make them admin or pending remove all their fields user["employees"] = [] user["request_list"] = [] user["client"] = None user["project"] = None if req[i] == "employee": user["employees"] = [] user["client"] = None user["project"] = None if req[i] == "manager": user["request_list"] if i == "project": if user["role"] == "manager": try: project = Project.objects().get(id=req[i]) except: return res("Invalid project ID", "error") user["project"] = project else: return res(user["role"] + " cant have a project", "error"), 400 elif i == "client": if user["role"] == "manager": try: client = Client.objects().get(id=req[i]) except: return res("Invalid project ID", "error") user["client"] = client else: return res(user["role"] + " cant have a client", "error"), 400 else: user[i] = req[i] user.save() return res("User modified", "success", user=convert_query(user))
def get(self, *args, **kwargs): user = current_user.user() client_id = kwargs.get('client_id') client = Client.objects(client_id = client_id).first() return {"user":user, "client":client}