Custom formatters
A list of validators and formatters that can be overridden in Salesman.
Price formatter
Displaying prices in Salesman is controlled using a price formatter function.
The default formatter function is set to salesman.core.utils.format_price()
and returns
a value formated with two decimal places.
Tip
Price formatter can be used (among other things) to display a price with a symbol or even a
converted price to another currency based on the request
that’s received in the context
.
You can change the price formatter by providing a dotted path in SALESMAN_PRICE_FORMATTER
setting
that points to your custom formatter function.
def format_price(value: Decimal, context: dict[str, Any] = {}) -> str:
"""
Default price format function. Can be overriden by providing a
dotted path to a function in ``SALESMAN_PRICE_FORMATTER`` setting.
Args:
value (Decimal): Number value to be formatted
context (dict, optional): Format context data. Defaults to {}.
Returns:
str: Formatted price as a string
"""
return f"{value:.2f}"
Your custom function should accept a value
argument of type Decimal
and a context
dictionary that contains additional render data like request
and either the basket
or
order
object. The function should return a formatted price as a string.
Admin JSON formatter
When displaying JSON data in admin, a formatter function is used. The default function
salesman.admin.utils.format_json()
uses the Pygments
library to create the
default JSON display. You can override the JSON formatter by providing a dotted path
to a function in SALESMAN_ADMIN_JSON_FORMATTER
setting.
def format_json(value: dict[str, Any], context: dict[str, Any] = {}) -> str:
"""
Format json and add color using pygments with fallback.
Args:
value (dict): Dict to be formated to json
context (dict, optional): Format context data. Defaults to {}.
Returns:
str: JSON formated html string
"""
output = json.dumps(value, indent=2)
output = pygments_highlight(output, "json", "tango")
style = pygments_css("tango")
styled = context.get("styled", True) # Used for testing.
if styled and style:
html = (
f"<style>{style}</style>"
f'<pre class="highlight" style="margin: 0; padding: 1em;">{output}</pre>'
)
else:
html = f'<pre style="margin: 0;">{output}</pre>'
return format_html("<div>{}</div>", mark_safe(html))
Your custom formatter should accept a dictionary value
and return the HTML string.
It also receives a context
dictionary with additional context. Either an order
or
order_item
boolean will be passed in depending on the formatting location.