def import_func(file: str) -> None: session = get_session() click.get_text_stream('stdin') all_items = [] with click.open_file(file, 'r') as f: for item_str in json.load(f): all_items.append(Item.from_dict(item_str)) session = get_session() session.add_all(all_items) session.commit() click.echo(f'Added {len(all_items)} items')
def add(description: str, state: str, signifier: str, parent: str, time: str) -> None: # Parses the parent. if parent != NONE_STR: parent = int(parent) state = parse_state(state) signifier = parse_signifier(signifier) # Parses the time. if time != NONE_STR: time = dateparser.parse(time) else: time = None # Creates the item to insert. item = Item(description=description, state=state, signifier=signifier, time=time, parent_id=parent) click.echo(item) if click.confirm('Do you want to add this item?', abort=True): session = get_session() session.add(item) session.commit() click.echo('Added item')
def query(substring: str, show_complete: bool) -> None: session = get_session() query = session.query(Item).filter(Item.description.contains(substring)) if not show_complete: query = query.filter(Item.state != ItemState.COMPLETE) items = query.order_by(Item.time_updated.desc()) render_items(items, 'Matching Items', 'No matching items found')
def delete(id: int) -> None: session = get_session() item = session.query(Item).get(id) if item is None: raise RuntimeError(f'Item {id} not found') click.echo(item) if click.confirm('Do you want to delete this item?', abort=True): session.delete(item) session.commit() click.echo('Deleted item')
def complete() -> None: session = get_session() items = session.query(Item) \ .filter(Item.time < datetime.now()) \ .filter(Item.state != ItemState.COMPLETE) num_items = items.count() if num_items: if click.confirm(f'Mark {num_items} items as complete?', abort=True): items.update({'state': ItemState.COMPLETE}) session.commit() click.echo(f'Completed {num_items} items') else: click.echo('All past items are complete')
def mark(state: str, id: int) -> None: session = get_session() item = session.query(Item).get(id) if item is None: raise RuntimeError(f'Item {id} not found') state = parse_choice(state) if isinstance(state, ItemState): item.state = state ostr = f'Marked item {id} as {state.value}' elif isinstance(state, ItemSignifier): item.signifier = state ostr = f'Marked item {id} as {state.value}' else: item.signifier = None ostr = f'Cleared signifier for item {id}' session.commit() click.echo(item) click.echo(ostr)
def index(state: str = ItemState.MIGRATED.value) -> str: session = get_session() items = session.query(Item).order_by(Item.id.desc()) state = parse_choice(state) if isinstance(state, ItemState): items = items.filter(Item.state == state) strs = (f'All {state.value.capitalize()}', f'No {state.value.capitalize()}') elif isinstance(state, ItemSignifier): items = items.filter(Item.signifier == state) strs = (f'All {state.value.capitalize()}', f'No {state.value.capitalize()}') else: strs = ('All Items', 'No Items') title = strs[0] if items.count() else strs[1] links = [l for l in ALL_CHOICES if len(l) > 3] session.close() return render_template('index.html', items=items, title=title, links=links)
def export_func(file: str) -> None: session = get_session() all_items = [item.as_dict() for item in session.query(Item)] with click.open_file(file, 'w') as f: json.dump(all_items, f, indent=2)