Changeset - 2ea1af3149c2
[Not reviewed]
default
0 3 0
Dennis Fink - 3 years ago 2022-03-03 17:40:15
dennis.fink@c3l.lu
Add more debug logging
3 files changed with 12 insertions and 0 deletions:
0 comments (0 inline, 0 general)
stockcli/cli.py
Show inline comments
 
import json
 
import logging
 
import logging.config
 
from operator import itemgetter
 

	
 
import click
 
import requests
 
from rich.panel import Panel
 
from rich.table import Table
 

	
 
from .console import DEFAULT_PADDING, console, int_prompt, prompt
 
from .stock import (
 
    add_by_barcode,
 
    get_info_by_barcode,
 
@@ -53,24 +54,25 @@ def stockcli(ctx: click.Context, configf
 
    menu.add_column(justify="left", style="green", no_wrap=True)
 
    menu.add_column(justify="left", style="cyan", no_wrap=True)
 

	
 
    for task_id, task in sorted(TASK_MAP.items(), key=itemgetter(0)):
 
        menu.add_row(task_id, task[0])
 

	
 
    while True:
 
        click.clear()
 
        console.print(Panel(menu, title="[green bold]Menu[/green bold]"))
 
        choice = prompt.ask(
 
            "Enter a number to select a task", choices=list(TASK_MAP.keys())
 
        )
 
        logging.debug(f"User selected task: {choice}")
 
        selected_task = TASK_MAP[choice][1]
 

	
 
        rerun = True
 
        while rerun:
 
            barcode = prompt.ask("Please scan the barcode")
 
            barcode = prepare_barcode(barcode)
 

	
 
            try:
 
                selected_task(barcode)
 
            except (
 
                requests.Timeout,
 
                requests.ConnectionError,
stockcli/stock.py
Show inline comments
 
import logging
 
from operator import itemgetter
 
from typing import Any
 

	
 
from rich.panel import Panel
 
from rich.table import Table
 

	
 
from . import utils
 
from .console import DEFAULT_PADDING, console, int_prompt, prompt
 
from .style import GreenBoldText
 

	
 

	
 
def get_info_by_barcode(barcode: str) -> None:
 
@@ -121,24 +122,25 @@ def transfer_by_barcode(barcode: str) ->
 
            title="[green bold]Product Info[/green bold]",
 
            subtitle="[blue]Transfer stock[/blue]",
 
        )
 
    )
 

	
 
    from_id = int_prompt.ask(
 
        "From (Enter 0 to abort)",
 
        choices=choices,
 
        default=int(choices[0]),
 
    )
 

	
 
    if not from_id:
 
        logging.debug("User aborted task!")
 
        return
 

	
 
    grid = Table.grid(padding=DEFAULT_PADDING)
 
    grid.add_column(justify="right", style="blue", no_wrap=True)
 
    grid.add_column(justify="left", style="cyan", no_wrap=True)
 

	
 
    choices = []
 
    for location in sorted(all_locations, key=itemgetter("id")):
 
        location_id = location["id"]
 
        if int(location_id) != from_id:
 
            choices.append(location_id)
 
            grid.add_row(location_id, location["name"])
 
@@ -150,29 +152,31 @@ def transfer_by_barcode(barcode: str) ->
 
            title="[green bold]Locations[/green bold]",
 
            subtitle="[blue]Transfer Stock[/blue]",
 
        )
 
    )
 

	
 
    to_id = int_prompt.ask(
 
        "To (Enter 0 to abort)",
 
        choices=choices,
 
        default=int(choices[0]),
 
    )
 

	
 
    if not to_id:
 
        logging.debug("User aborted task!")
 
        return
 

	
 
    amount = int_prompt.ask("Amount (Enter 0 to abort)")
 

	
 
    if not amount:
 
        logging.debug("User aborted task!")
 
        return
 

	
 
    data = {"amount": amount, "location_id_from": from_id, "location_id_to": to_id}
 

	
 
    response = utils.post_request(
 
        f"stock/products/by-barcode/{barcode}/transfer", data
 
    )  # type: dict[str, Any]
 
    console.print("Successfully transfered!")
 
    return
 

	
 

	
 
def add_by_barcode(barcode: str) -> None:
 
@@ -191,24 +195,25 @@ def add_by_barcode(barcode: str) -> None
 
    grid.add_row(GreenBoldText("Product Name:"), inner_product["name"])
 
    console.print(
 
        Panel(
 
            grid,
 
            title="[green bold]Product Info[/green bold]",
 
            subtitle="[blue]Add Stock[/blue]",
 
        )
 
    )
 

	
 
    amount = int_prompt.ask("Amount (Enter 0 to abort)")
 

	
 
    if not amount:
 
        logging.debug("User aborted task!")
 
        return
 

	
 
    data = {"amount": amount, "transaction_type": "purchase"}
 

	
 
    response = utils.post_request(
 
        f"stock/products/by-barcode/{barcode}/add", data
 
    )  # type: dict[str, Any]
 
    console.print("Successfully added!")
 
    return
 

	
 

	
 
def update_by_barcode(barcode: str) -> None:
 
@@ -229,23 +234,24 @@ def update_by_barcode(barcode: str) -> N
 
    grid.add_row(GreenBoldText("Overall Stock Amount:"), product["stock_amount"])
 

	
 
    console.print(
 
        Panel(
 
            grid,
 
            title="[green bold]Product Info[/green bold]",
 
            subtitle="[blue]Update stock[/blue]",
 
        )
 
    )
 
    amount = int_prompt.ask("Amount (Enter 0 to abort)")
 

	
 
    if not amount:
 
        logging.debug("User aborted task!")
 
        return
 

	
 
    data = {
 
        "new_amount": amount,
 
    }
 

	
 
    response = utils.post_request(
 
        f"stock/products/{product_id}/inventory", data
 
    )  # type: dict[str, Any]
 
    console.print("Successfully updated!")
 
    return
stockcli/utils.py
Show inline comments
 
@@ -12,26 +12,30 @@ UNALLOWED_CHARACTERS = str.maketrans(dic
 

	
 

	
 
def make_request(method: str, url_path: str, data: Optional[Any] = None) -> Any:
 
    obj = click.get_current_context().obj
 
    session = obj["request_session"]
 
    base_url = obj["base_url"]
 
    requested_url = base_url + url_path
 

	
 
    method_function = attrgetter(method)
 

	
 
    try:
 
        if data is not None:
 
            logging.debug(
 
                f"Making {method.upper()} request: {requested_url} with {data}"
 
            )
 
            response = method_function(session)(requested_url, json=data)
 
        else:
 
            logging.debug(f"Making {method.upper()} request: {requested_url}")
 
            response = method_function(session)(requested_url)
 
        response.raise_for_status()
 
    except requests.Timeout:
 
        logging.error(f"The connection to {requested_url} timed out!")
 
        error_console.print("Connection timed out!")
 
        raise
 
    except requests.ConnectionError:
 
        logging.error(f"Couldn't establish a connection to {requested_url}!")
 
        error_console.print("Couldn't establish a connection!")
 
        raise
 
    except requests.HTTPError:
 
        logging.error(f"{requested_url} sent back an HTTPError")
0 comments (0 inline, 0 general)