Ejemplo n.º 1
0
 def get(self):
     projects = Project.objects().all()
     return res(
         "All projects returned",
         "success",
         projects=convert_query(projects, list=True),
     )
Ejemplo n.º 2
0
 def get(self, id):
     caller = get_bearer(request)
     if caller["role"] != "manager":
         return res("⛔️ Must be a manager to view a project", "error"), 400
     try:
         project = Project.objects(id=id)[0]
         return res("Retrieved Successfully",
                    "success",
                    project=convert_query(project))
     except:
         return res("Project doesn't exist", "error"), 400
Ejemplo n.º 3
0
 def delete(self, id):
     caller = get_bearer(request)
     if caller["role"] != "admin":
         return res("⛔️ Must be an admin to delete a project", "error"), 400
     try:
         project = Project.objects(id=id)
         project.delete()
         return res("Project deleted 💀",
                    "success",
                    project=convert_query(project))
     except:
         return res("Project doesn't exist", "error"), 400
Ejemplo n.º 4
0
    def put(self, id):
        caller = get_caller(request)
        if caller["role"] != "admin":
            return res("⛔️ Must be an admin to update a project", "error"), 400

        req = parse(request)
        errors = ProjectSchema().validate(req)
        if errors:
            return res("Errors in request", "alert", errors=errors), 400
        try:
            project = Project.objects(id=id)[0]
        except:
            return res("Project doesn't exist", "error"), 400
        for i in req:
            project[i] = req[i]

        project.save()

        return res("Project Modified",
                   "success",
                   project=convert_query(project))
Ejemplo n.º 5
0
    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))