Task: add pad_url to task
This commit is contained in:
parent
4974f97f6b
commit
b2800252cf
@ -1,18 +1,53 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import requests
|
||||||
|
from result import Err, Ok, Result
|
||||||
from models import Task
|
from models import Task
|
||||||
import argparse
|
import argparse
|
||||||
from sqlmodel import Session, SQLModel, create_engine, select
|
from sqlmodel import Session, SQLModel, create_engine
|
||||||
import datetime
|
|
||||||
import sys
|
import sys
|
||||||
import dateparser
|
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__":
|
if __name__ == "__main__":
|
||||||
parser = argparse.ArgumentParser(description='Create a task')
|
parser = argparse.ArgumentParser(description='Create a task')
|
||||||
parser.add_argument('name', type=str, help='Name of the 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('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('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('--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()
|
args = parser.parse_args()
|
||||||
|
|
||||||
@ -30,6 +65,15 @@ if __name__ == "__main__":
|
|||||||
due=args.due,
|
due=args.due,
|
||||||
timeout=args.timeout_seconds)
|
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.add(task)
|
||||||
session.commit()
|
session.commit()
|
||||||
session.refresh(task)
|
session.refresh(task)
|
||||||
|
7
main.py
7
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
|
# Create a group for the task if it does not exist
|
||||||
if chatgroup is None:
|
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):
|
if is_err(create_result):
|
||||||
return Err(create_result.unwrap_err())
|
return Err(create_result.unwrap_err())
|
||||||
|
@ -85,6 +85,7 @@ class Task(SQLModel, table=True):
|
|||||||
due: datetime.datetime
|
due: datetime.datetime
|
||||||
timeout: int # in seconds
|
timeout: int # in seconds
|
||||||
chatgroup: str | None = None # None = to be created
|
chatgroup: str | None = None # None = to be created
|
||||||
|
pad_url: str | None = None # optional pad url
|
||||||
|
|
||||||
participation_requests: list["ParticipationRequest"] = Relationship(back_populates="task")
|
participation_requests: list["ParticipationRequest"] = Relationship(back_populates="task")
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user