netbox-community / netbox

The premier source of truth powering network automation. Open source under Apache 2. Try NetBox Cloud free: https://netboxlabs.com/free-netbox-cloud/
http://netboxlabs.com/oss/netbox/
Apache License 2.0
16.05k stars 2.57k forks source link

Add function to trim whitespaces in export templates via jinja environment settings #17653

Open w1tht0uchp4d opened 3 weeks ago

w1tht0uchp4d commented 3 weeks ago

NetBox version

3.7.5

Feature type

Change to existing functionality

Proposed functionality

The goal is to achieve feature parity between the [Provisioning --> Config Templates edit] and [Customization --> Export Templates dialog]. In config templates it is possible to use jinja environment parameters, in export templates it is not. It's also the same behavior in Netbox v4.1.2 (checked via demo application).

Use case

I'm currently implementing a custom export template, which exports prefixes, their assigned VLANs and some custom fields. I'm aware that part of this, is also achievable via the csv export function, but I want to add some semantic sugar via the power of Jinja, like adding first host address, last host address, ... The messy white space handling with jinja, unfortunately destroys the formatting of the csv.

Database changes

None required?

External dependencies

None?

pl0xym0r commented 2 weeks ago

Both ConfigTemplate and ExportTemplate inherits from ExportTemplatesMixin

In ConfigTemplate there are params/methods like: environment_params def render() def _get_environment()

Idk if it's too much for export templates but would it be great to move such params/functions to ExportTemplatesMixin ? Or modify utilities.jinja2.py ? At least, use the same enviroment creation for both cases.