[tasks] Fix updating and stopping tasks from Emacs
This commit is contained in:
@ -500,6 +500,55 @@ def todoist_scrobble_task(
|
||||
return scrobble
|
||||
|
||||
|
||||
def emacs_scrobble_update_task(
|
||||
emacs_id: str, emacs_notes: dict, user_id: int
|
||||
) -> Optional[Scrobble]:
|
||||
scrobble = Scrobble.objects.filter(
|
||||
in_progress=True,
|
||||
user_id=user_id,
|
||||
log__source_id=emacs_id,
|
||||
log__source="emacs",
|
||||
).first()
|
||||
|
||||
if not scrobble:
|
||||
logger.info(
|
||||
"[emacs_scrobble_update_task] no task found",
|
||||
extra={
|
||||
"emacs_notes": emacs_notes,
|
||||
"user_id": user_id,
|
||||
"media_type": Scrobble.MediaType.TASK,
|
||||
},
|
||||
)
|
||||
return
|
||||
|
||||
notes_updated = False
|
||||
for note in emacs_notes:
|
||||
existing_note_ts = [
|
||||
n.get("timestamp") for n in scrobble.log.get("notes", [])
|
||||
]
|
||||
if not scrobble.log.get('notes"'):
|
||||
scrobble.log["notes"] = []
|
||||
if note.get("timestamp") not in existing_note_ts:
|
||||
scrobble.log["notes"].append(
|
||||
{note.get("timestamp"): note.get("content")}
|
||||
)
|
||||
notes_updated = True
|
||||
|
||||
if notes_updated:
|
||||
scrobble.save(update_fields=["log"])
|
||||
|
||||
logger.info(
|
||||
"[emacs_scrobble_update_task] emacs note added",
|
||||
extra={
|
||||
"emacs_note": emacs_notes,
|
||||
"user_id": user_id,
|
||||
"media_type": Scrobble.MediaType.TASK,
|
||||
},
|
||||
)
|
||||
|
||||
return scrobble
|
||||
|
||||
|
||||
def emacs_scrobble_task(
|
||||
task_data: dict,
|
||||
user_id: int,
|
||||
@ -509,6 +558,7 @@ def emacs_scrobble_task(
|
||||
|
||||
prefix = ""
|
||||
suffix = ""
|
||||
|
||||
source_id = task_data.get("source_id")
|
||||
for label in task_data.get("labels"):
|
||||
if label in TODOIST_TITLE_PREFIX_LABELS:
|
||||
@ -516,14 +566,13 @@ def emacs_scrobble_task(
|
||||
if label in TODOIST_TITLE_SUFFIX_LABELS:
|
||||
suffix = label
|
||||
|
||||
if not prefix and suffix:
|
||||
if not prefix and not suffix:
|
||||
logger.warning(
|
||||
"Missing a prefix and suffix tag for task",
|
||||
extra={"emacs_scrobble_task": task_data},
|
||||
)
|
||||
|
||||
title = " ".join([prefix.capitalize(), suffix.capitalize()])
|
||||
|
||||
task = Task.find_or_create(title)
|
||||
|
||||
timestamp = pendulum.parse(task_data.get("updated_at", timezone.now()))
|
||||
@ -531,10 +580,9 @@ def emacs_scrobble_task(
|
||||
user_id=user_id,
|
||||
in_progress=True,
|
||||
log__source_id=source_id,
|
||||
log__source=task_data.get("source"),
|
||||
log__source="emacs",
|
||||
task=task,
|
||||
).last()
|
||||
print(in_progress_scrobble)
|
||||
|
||||
if not in_progress_scrobble and stopped:
|
||||
logger.info(
|
||||
@ -547,7 +595,7 @@ def emacs_scrobble_task(
|
||||
|
||||
if in_progress_scrobble and started:
|
||||
logger.info(
|
||||
"[todoist_scrobble_task] cannot start already started task",
|
||||
"[emacs_scrobble_task] cannot start already started task",
|
||||
extra={
|
||||
"emacs_id": source_id,
|
||||
},
|
||||
@ -559,7 +607,7 @@ def emacs_scrobble_task(
|
||||
logger.info(
|
||||
"[emacs_scrobble_task] finishing",
|
||||
extra={
|
||||
"emacs_id": emacs_task["emacs_id"],
|
||||
"emacs_id": source_id,
|
||||
},
|
||||
)
|
||||
in_progress_scrobble.stop(timestamp=timestamp, force_finish=True)
|
||||
@ -568,6 +616,14 @@ def emacs_scrobble_task(
|
||||
if in_progress_scrobble:
|
||||
return in_progress_scrobble
|
||||
|
||||
notes = task_data.pop("notes")
|
||||
if notes:
|
||||
task_data["notes"] = []
|
||||
for note in notes:
|
||||
task_data["notes"].append(
|
||||
{note.get("timestamp"): note.get("content")}
|
||||
)
|
||||
|
||||
scrobble_dict = {
|
||||
"user_id": user_id,
|
||||
"timestamp": timestamp,
|
||||
|
||||
Reference in New Issue
Block a user