def delete_event(id): """ Delete event based on provided ID """ session = session_factory() event = session.query(Event).filter_by(id=id).first() if not event: raise click.UsageError(f"Event with ID {id} does't exists.") title = event.title session.delete(event) session.commit() session.close() print( f'{Style.BRIGHT}{Fore.GREEN}(\u2713){Style.RESET_ALL} Deleted event titled "{title}"' )
def send_mail(id): if not send_to: raise UsageError(WARN) if not (EMAIL_ADDRESS and EMAIL_PASSWORD): raise UsageError('`EMAIL` or `PASSWORD`' ' environment variables not found.' ' These are needed in order to send emails.') session = session_factory() content = session.query(Event).filter_by(id=id).first() if content is None: raise UsageError(f'No event with ID : {id}') _send_mail(content.title, content.html, send_to=send_to) session.close()
def edit( id: int, title: str, short_description: str, date_created: str, edit_reminder: bool, edit_markdown: bool, ): """ Edit an existing event """ session = session_factory() event = session.query(Event).filter_by(id=id).first() if not event: raise click.UsageError(f"Event with ID {id} does't exists.") if title: event.title = title if short_description: event.short_description = short_description if date_created: date_created = parse_date(date_created) if edit_reminder: base_date = parse_date(prompt("Base Date")) reminder_object_list = reminder(event, base_date) for old_reminder in event.reminder_dates: session.delete(old_reminder) for reminder_object in reminder_object_list: session.add(reminder_object) if edit_markdown: markdown_path = path_join(BASEDIR, "markdown", "description.md") user_choice = prompt( "Have you already edited the markdown file([y]/n)") if user_choice == "n": with open(markdown_path, "w") as file: file.write(event.long_description) print(f"{Style.BRIGHT}{Fore.GREEN}(\u2713){Style.RESET_ALL} " f"Markdown file written at {markdown_path}\n" "Please edit the file and call command\n" f"evem edit {id} --markdown") else: event.long_description = read_markdown() event.html = parse_markdown(event) print(f"{Style.BRIGHT}{Fore.GREEN}(\u2713){Style.RESET_ALL} " "Markdown updated") session.commit() print(f"{Style.BRIGHT}{Fore.GREEN}(\u2713){Style.RESET_ALL} " "Changes commited if any.")
def view_event_details(id): """ Print the description(markdown) of event to terminal. """ try: from rich.console import Console from rich.markdown import Markdown except ImportError: print("For this feature to work, you need to install rich") print("You could install it with:") print("pip install rich") raise click.Abort() session = session_factory() event: Event = session.query(Event).filter_by(id=id).first() if event is None: raise click.UsageError("Unable to find event") markdown = event.long_description console = Console() md = Markdown(markdown) console.print(md)
def remind(): """ Checks all events and set reminder mail. """ session = session_factory() events = session.query(Event).all() today = datetime.date.today() for event in events: reminders = event.reminder_dates if reminders is None: reminders = [] for reminder in reminders: if not reminder.repeat_forever and reminder.repeat <= 0: session.delete(reminder) session.commit() continue if reminder.date == today or reminder.date < today: rdelta = relativedelta( years=reminder.year_delta, months=reminder.month_delta, days=reminder.day_delta, ) if reminder.date < today: new_date = increment_date(rdelta, reminder.date, today) if not new_date == False: reminder.date = new_date session.commit() if reminder.date == today: send_mail(event.id) print( f"{Style.BRIGHT}{Fore.GREEN}(\u2713){Style.RESET_ALL} " f"Sent mail for event titled {event.title}") reminder.date += rdelta if not reminder.repeat_forever: reminder.repeat -= 1 print( f"Next reminder for '{event.title}' on {reminder.date}" ) session.commit()
def query_data(): session = session_factory() data = session.query(Event).order_by(Event.date_created).all() if data: return data return []
def new(commit): """ Create new event """ if not commit: print(f"{Fore.LIGHTYELLOW_EX}Date Format: dd-mm-yyyy{Style.RESET_ALL}") print( f"{Fore.MAGENTA}Leave any date field empty for today's date{Style.RESET_ALL}\n" ) while True: title = prompt("title") if not len(title) > 0: print(f"{Fore.RED}Title cannot be left blank{Style.RESET_ALL}") else: break short_description = prompt("short-description") date_created = prompt("date-of-event") base_date = prompt("base-date(Default: date-of-event)") if not short_description: short_description = title try: date_created = parse_date(date_created) base_date = parse_date(base_date) if date_created > datetime.date.today(): raise Exception( "date-of-event cannot be bigger than today's date.") except Exception as e: raise click.UsageError(f"{Fore.RED}{e}{Style.RESET_ALL}") if not base_date: base_date = date_created syntax = "period = (years:int, months:int, days:int), repeat = (int|*)" print( f"\n{Fore.MAGENTA}Date are calculated for time after `base-date`{Style.RESET_ALL}" ) print(Fore.LIGHTYELLOW_EX + "Syntax: ") print(syntax) print("\nExamples:") print( "period = (0,1,0), repeat = 10 # means to remind every month for 10 times" ) print( "period = (0,0,14), repeat = * # means to remind every 14 days (forever)" ) print( "period = (1,0,0), repeat = 1 # means remind after 1 year (1 time only)" ) print(Style.RESET_ALL) print( f"{Fore.RED}Enter q to exit (There should be atleast one reminder){Style.RESET_ALL}\n" ) event = Event(title, short_description, long_description="", date_created=date_created) model_objects = { "event": event, "reminder_dates": reminder(event, base_date) } create_dir_if_not_exists(BASEDIR, "markdown") markdown_file = path_join(BASEDIR, "markdown", "description.md") template_markdown = path_join(BASEDIR, "templates", "markdown.md") with open(markdown_file, "w") as file: # Read from template markdown with open(template_markdown) as template_file: # Write starter template to this file file.write(template_file.read()) pickle_object(model_objects) print(f"{Style.DIM}Created `description.md` file") print("Run following commands to complete event creation:") print(f"Edit the file with suitable description{Style.RESET_ALL}") print(f"\n>> {Fore.LIGHTBLUE_EX}nano {markdown_file}{Style.RESET_ALL}") print( f">> {Fore.LIGHTBLUE_EX}evem event new --commit{Style.RESET_ALL}") else: model_objects = unpickle_object() if not model_objects: raise click.BadOptionUsage( option_name="markdown", message=("Unable to find cached object." " Run command without `-c/--commit` flag first."), ) os.remove(path_join(BASEDIR, "cache", "__object__.cache")) event = model_objects["event"] event.long_description = read_markdown() event.html = parse_markdown(event) session = session_factory() session.add(event) for model in model_objects["reminder_dates"]: session.add(model) session.commit() session.close() print( f"{Style.BRIGHT}{Fore.GREEN}(\u2713){Style.RESET_ALL} Event successfully commited." )