diff --git a/create_task.py b/create_task.py index 1cac967..f52f006 100644 --- a/create_task.py +++ b/create_task.py @@ -1,18 +1,53 @@ #!/usr/bin/env python +import requests +from result import Err, Ok, Result from models import Task import argparse -from sqlmodel import Session, SQLModel, create_engine, select -import datetime +from sqlmodel import Session, SQLModel, create_engine + import sys import dateparser +def create_hedgedoc_copy(old_url: str) -> Result[str, str]: + # remove query params and # + old_url = old_url.split("?")[0].split("#")[0] + + # remove trailing slash + if old_url[-1] == "/": + old_url = old_url[:-1] + + download_url = old_url + "/download" + response = requests.get(download_url) + + if response.status_code != 200: + return Err("Failed to download the document.") + + old_content = response.text + content = old_content + "\n\n---\n\nThe template for this pad can be found [here](" + old_url + "). If there is something in the tasks that should be changed, you can alter the template carefully." + + pad_base_url = "/".join(old_url.split("/")[0:3]) + + # do not follow redirects + response = requests.post( + pad_base_url + "/new", + data=content, + headers={"Content-Type": "text/markdown"}, + allow_redirects=False) + + if response.status_code != 302: + return Err(response.text) + + return Ok(response.headers['Location']) + + if __name__ == "__main__": parser = argparse.ArgumentParser(description='Create a task') parser.add_argument('name', type=str, help='Name of the task') parser.add_argument('number_of_participants', type=int, help='Number of participants') parser.add_argument('due', type=dateparser.parse, help='Due date of the task') parser.add_argument('--timeout_seconds', type=int, help='Timeout in seconds (default = 1 day)', default=24*3600) + parser.add_argument('--pad-template-url', type=str, help='URL to pad that contains the task description (will be copied)') args = parser.parse_args() @@ -30,6 +65,15 @@ if __name__ == "__main__": due=args.due, timeout=args.timeout_seconds) + if args.pad_template_url is not None: + result = create_hedgedoc_copy(args.pad_template_url) + + if result.is_err(): + print("Error: " + result.unwrap_err(), file=sys.stderr) + exit(1) + + task.pad_url = result.unwrap() + session.add(task) session.commit() session.refresh(task) diff --git a/main.py b/main.py index 05ce808..874a8ec 100644 --- a/main.py +++ b/main.py @@ -416,7 +416,12 @@ If due to any reason you can not participate, please just change your "👍" rea # Create a group for the task if it does not exist if chatgroup is None: - create_result = self.api.create_group(CreateGroupRequest(name=task.name, members=[self.api.number])) + group_creation_request = CreateGroupRequest(name=task.name, members=[self.api.number]) + + if task.pad_url is not None: + group_creation_request.description = task.pad_url + + create_result = self.api.create_group(group_creation_request) if is_err(create_result): return Err(create_result.unwrap_err()) diff --git a/models.py b/models.py index 54477ad..4058c68 100644 --- a/models.py +++ b/models.py @@ -85,6 +85,7 @@ class Task(SQLModel, table=True): due: datetime.datetime timeout: int # in seconds chatgroup: str | None = None # None = to be created + pad_url: str | None = None # optional pad url participation_requests: list["ParticipationRequest"] = Relationship(back_populates="task")