def add_from_cli(session): # Read description for the task desc = input(f"Enter description: {Color.RED}") print(Color.END, end='', flush=True) # Read relevant scopes requested_scopes = sorted(_read_validated_scopes()) if not len(requested_scopes): requested_scopes = [TimeScope(datetime.now().strftime("%G-ww%V.%u"))] print(f" => defaulting to {requested_scopes}") if len(requested_scopes) > 1: print(f" => parsed as {requested_scopes}") # Create a Task, now that we have all required fields t = Task(desc=desc, first_scope=requested_scopes[0], created_at=datetime.now()) # And a category category = input(f"Enter category: {Color.RED}") if category is not None and category != "": t.category = category print(Color.END, end='', flush=True) # And a time_estimate time_estimate = input(f"Enter time_estimate: {Color.RED}") if time_estimate is not None and time_estimate != "": if not re.fullmatch(r"\d+\.\d", time_estimate): print(Color.END, end='') print( "time_estimate must be in format like `12.0` (\"\\d+\\.\\d\"), exiting" ) print() return t.time_estimate = time_estimate print(Color.END, end='', flush=True) # Try committing the Task try: session.add(t) session.commit() except StatementError as e: print() print("Hit exception when parsing:") print(json.dumps(t.to_json_dict(), indent=4)) session.rollback() return # Try creating the TaskTimeScopes for requested_scope in requested_scopes: session.add( TaskTimeScope(task_id=t.task_id, time_scope_id=requested_scope)) session.commit() print() print(f"Created task {t.task_id}") print(json.dumps(t.to_json_dict(), indent=4))
def test_to_json_legacy(): TEST_DESC = "sample descr aheou;lq" TEST_CATEGORY = "test cat g'l.',.p" TEST_SCOPE = "2011-ww38.3" t = Task(desc=TEST_DESC, category=TEST_CATEGORY, first_scope=TEST_SCOPE) json_dict = t.to_json_dict() assert json_dict['desc'] == TEST_DESC assert json_dict['category'] == TEST_CATEGORY assert json_dict['first_scope'] == TEST_SCOPE assert 'created_at' not in json_dict