From 082a7e0b471380659be20e69f7837533590e6aa9 Mon Sep 17 00:00:00 2001 From: Niki Vihtola Date: Fri, 12 Dec 2025 15:07:40 +0200 Subject: [PATCH] added option to delete a daily through application at remo --- README.md | 5 ++++ jrnl/cli.py | 7 ++++++ jrnl/commands/daily.py | 48 ++++++++++++++++++++++++++++++++++++- jrnl/database/operations.py | 8 +++++++ 4 files changed, 67 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c3bf84a..736bca3 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,11 @@ jrnl daily --days 2 # Regenerate today's standup jrnl daily --regenerate + +# Delete a daily entry +jrnl daily --delete 2024-12-12 +jrnl daily --delete today +jrnl daily --delete latest ``` ### Configuration Management diff --git a/jrnl/cli.py b/jrnl/cli.py index ebb4dd9..5dcacf2 100644 --- a/jrnl/cli.py +++ b/jrnl/cli.py @@ -58,6 +58,11 @@ Examples: # Generate standup including last 2 days jrnl daily --days 2 + + # Delete a daily entry + jrnl daily --delete 2024-12-12 + jrnl daily --delete today + jrnl daily --delete latest ''', formatter_class=argparse.RawDescriptionHelpFormatter ) @@ -65,6 +70,8 @@ Examples: help='Number of days to include (default: 1)') daily_parser.add_argument('-r', '--regenerate', action='store_true', help='Regenerate today\'s standup with latest logs') + daily_parser.add_argument('--delete', metavar='DATE', + help='Delete a daily entry (DATE: YYYY-MM-DD, "today", or "latest")') # jrnl logs logs_parser = subparsers.add_parser( diff --git a/jrnl/commands/daily.py b/jrnl/commands/daily.py index 6908747..3f89e4e 100644 --- a/jrnl/commands/daily.py +++ b/jrnl/commands/daily.py @@ -5,7 +5,9 @@ from ..database.operations import ( get_logs_since, get_latest_daily, get_previous_daily_before, - insert_daily + get_daily_for_date, + insert_daily, + delete_daily ) from ..database.models import Daily from ..config import Config @@ -17,6 +19,10 @@ from ..utils.formatting import format_daily_header def handle(args): """Handle the 'daily' command.""" try: + # Handle deletion if requested + if hasattr(args, 'delete') and args.delete: + return handle_delete(args.delete) + config = Config.load() # Determine date range for logs @@ -77,6 +83,46 @@ def handle(args): return 1 +def handle_delete(date_arg: str) -> int: + """Handle deleting a daily entry.""" + try: + # Resolve date argument + if date_arg.lower() in ['today', 'latest']: + # Get today's date or latest daily date + if date_arg.lower() == 'today': + date = get_current_date() + else: + latest = get_latest_daily() + if not latest: + print("No daily entries found.") + return 1 + date = latest.daily_date + else: + # Assume it's a date in YYYY-MM-DD format + date = date_arg + + # Check if daily exists + daily = get_daily_for_date(date) + if not daily: + print(f"No daily found for date: {date}") + return 1 + + # Delete the daily + if delete_daily(date): + print(f"Deleted daily for {date}") + return 0 + else: + print(f"Failed to delete daily for {date}") + return 1 + + except sqlite3.Error as e: + print(f"Database error: {e}") + return 1 + except Exception as e: + print(f"Error deleting daily: {e}") + return 1 + + def get_normal_cutoff(): """Get cutoff timestamp for normal daily generation.""" latest_daily = get_latest_daily() diff --git a/jrnl/database/operations.py b/jrnl/database/operations.py index 944c354..293e4e7 100644 --- a/jrnl/database/operations.py +++ b/jrnl/database/operations.py @@ -158,3 +158,11 @@ def get_previous_daily_before(date: str) -> Optional[Daily]: daily_message=row['daily_message'] ) return None + + +def delete_daily(date: str) -> bool: + """Delete a daily entry by its date. Returns True if deleted, False if not found.""" + with get_connection() as conn: + cursor = conn.cursor() + cursor.execute('DELETE FROM dailies WHERE daily_date = ?', (date,)) + return cursor.rowcount > 0