def get(self,listName): """Return a todo list --- tags: - Todo API parameters: - in: path name: listName description: The todo list name required: true type: string - in : header name: token description : user authentication token type: string required : true responses: 200: description: Success 401: description: Missing or invalid token 404: description: Invalid list id""" token=request.headers.get("token") user=decodeToken(token) if user is None: return ({"status":3,"message":"Bad authentication token"},401) try: res={"name":listName,"todos":BDD.extract(f"app/resources/bdd/{user}/{listName}.json")} return ({"status":0,"message":"Todo list returned successully","data":res}) except Exception as e: print(e) return({"status":4,"message":"List doesn't exist"},404)
def get(self): """Return all todo lists --- tags: - Todo API parameters: - in : header name: token description : user authentication token type: string required : true responses: 200: description: Success 401: description: Missing or invalid token""" token = request.headers.get("token") user = decodeToken(token) if user is None: return ({"status": 3, "message": "Bad authentication token"}, 401) res = [] for fichier in os.listdir(f"app/resources/bdd/{user}"): res.append({ "name": fichier[:-5], "todos": BDD.extract(f"app/resources/bdd/{user}/{fichier}") }) return ({ "status": 0, "message": "Todo lists returned successully", "data": res })
def patch(self,listName): """Modify a todo list's name --- tags: - Todo API parameters: - in: path name: listName description: The todo list name required: true type: string - in: body name: attributes description: The new name and description of the todo schema: type: object properties: newName: type: string - in : header name: token description : user authentication token type: string required : true responses: 200: description: Success 401: description: Missing or invalid token 404: description: Invalid list id 409: description: List with new name already exists""" token=request.headers.get("token") parser=reqparse.RequestParser() parser.add_argument("newName",type=str,required=True,help="Missing new list name") args=parser.parse_args(strict=True) newName=args["newName"] user=decodeToken(token) if user is None: return ({"status":3,"message":"Bad authentication token"},401) if f"{newName}.json" in os.listdir(f"app/resources/bdd/{user}"): return ({"status":2,"message":"List with new name already exists"},409) try: os.rename(f"app/resources/bdd/{user}/{listName}.json",f"app/resources/bdd/{user}/{newName}.json") return ({"status":0,"message":"Todo list renamed successully"}) except Exception as e: print(e) return({"status":4,"message":"List doesn't exist"},404)
def delete(self, listName, todoName): """Delete a todo --- tags: - Todo API parameters: - in: path name: listName description: The todo list name required: true type: string - in: path name: todoName description: The todo name required: true type: string - in : header name: token description : user authentication token type: string required : true responses: 200: description: Success 401: description: Missing or invalid token 404: description: Invalid list id or todo id""" token = request.headers.get("token") user = decodeToken(token) if user is None: return ({"status": 3, "message": "Bad authentication token"}, 401) try: liste = BDD.extract(f"app/resources/bdd/{user}/{listName}.json") except Exception as e: print(e) return ({"status": 4, "message": "List doesn't exist"}, 404) todo = None for t in liste: if t["name"] == todoName: liste.remove(t) BDD.dump(f"app/resources/bdd/{user}/{listName}.json", liste) return ({"status": 0, "message": "Todo deleted successully"}) return ({"status": 4, "message": "Todo doesn't exist"}, 404)
def put(self): """Create an empty todo list --- tags: - Todo API parameters: - in: body name: attributes description: The name of the list schema: type: object properties: nom: type: string - in : header name: token description : user authentication token type: string required : true responses: 200: description: Success 401: description: Missing or invalid token 409: description: List already exists""" token = request.headers.get("token") user = decodeToken(token) if user is None: return ({"status": 3, "message": "Bad authentication token"}, 401) parser = reqparse.RequestParser() parser.add_argument("nom", type=str, required=True, help="Missing list name") args = parser.parse_args(strict=True) nom = args["nom"] if f"{nom}.json" in os.listdir(f"app/resources/bdd/{user}"): return ({"status": 2, "message": "List already exists"}, 409) BDD.dump(f"app/resources/bdd/{user}/{nom}.json", []) return ({"status": 0, "message": "Todo lists created successully"})
def patch(self, listName, todoName): """Modify a todo's name and content --- tags: - Todo API parameters: - in: path name: listName description: The todo list name required: true type: string - in: path name: todoName description: The todo name required: true type: string - in: body name: attributes description: The new name and description of the todo schema: type: object properties: newName: type: string newDesc: type: string - in : header name: token description : user authentication token type: string required : true responses: 200: description: Success 401: description: Missing or invalid token 404: description: Invalid list id or todo id 409: description: Todo with new name already exists in list""" token = request.headers.get("token") parser = reqparse.RequestParser() parser.add_argument("newName", type=str, required=True, help="Missing new todo name") parser.add_argument("newDesc", type=str, required=True, help="Missing new todo description") args = parser.parse_args(strict=True) newName = args["newName"] newDesc = args["newDesc"] user = decodeToken(token) if user is None: return ({"status": 3, "message": "Bad authentication token"}, 401) try: liste = BDD.extract(f"app/resources/bdd/{user}/{listName}.json") except Exception as e: print(e) return ({"status": 4, "message": "List doesn't exist"}, 404) todo = None if (todoName != newName) and any(t["name"] == newName for t in liste): return ({ "status": 2, "message": "Todo with this name already exists" }, 409) for t in liste: if t["name"] == todoName: t["name"] = newName t["description"] = newDesc t["lastModif"] = datetime.now().strftime("%d %b %Y, %H:%M:%S") BDD.dump(f"app/resources/bdd/{user}/{listName}.json", liste) return ({"status": 0, "message": "Todo modified successully"}) return ({"status": 4, "message": "Todo doesn't exist"}, 404)
def put(self, listName): """Add a todo in a todo list --- tags: - Todo API parameters: - in: path name: listName description: The todo list name required: true type: string - in: body name: attributes description: The name and description of the todo schema: type: object properties: nom: type: string description: type: string - in : header name: token description : user authentication token type: string required : true responses: 200: description: Success 401: description: Missing or invalid token 404: description: Invalid list id 409: description: Todo with new name already exists in list""" token = request.headers.get("token") user = decodeToken(token) if user is None: return ({"status": 3, "message": "Bad authentication token"}, 401) parser = reqparse.RequestParser() parser.add_argument("nom", type=str, required=True, help="Missing todo name") parser.add_argument("description", type=str, required=True, help="Missing todo description") args = parser.parse_args(strict=True) nom = args["nom"] description = args["description"] try: liste = BDD.extract(f"app/resources/bdd/{user}/{listName}.json") if any(t["name"] == nom for t in liste): return ({ "status": 2, "message": "Todo with this name already exists" }, 409) liste.append({ "name": nom, "description": description, "lastModif": datetime.now().strftime("%d %b %Y, %H:%M:%S") }) except Exception as e: print(e) return ({"status": 4, "message": "List doesn't exist"}, 404) BDD.dump(f"app/resources/bdd/{user}/{listName}.json", liste) return ({"status": 0, "message": "Todo created successully"})