diff --git a/README.md b/README.md index f035e8f..ea3a187 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ Restart NetBox and add `netbox-qrcode` to your local_requirements.txt The following options are available: * `with_text`: Boolean (default True). Text label will be added to QR code image if enabled. +* `text_template`: Jinja2 template with {{ obj }} as context, using it ignores `text_fields` and `custom_text` * `text_fields`: List of String (default ['name']). Text fields of an object that will be added as text label to QR image. It's possible to use custom field values. * `font`: String (default TahomaBold) Font name for text label ( Some font include in package, see fonts dir). * `text_location`: Where to render the text, relative to the QR code. Valid values are `"right"` (default), `"left"`", `"up"`, and `"down"`. diff --git a/netbox_qrcode/template_content.py b/netbox_qrcode/template_content.py index d670fe6..d2e34db 100644 --- a/netbox_qrcode/template_content.py +++ b/netbox_qrcode/template_content.py @@ -2,6 +2,7 @@ from django.conf import settings from django.core.exceptions import ObjectDoesNotExist +from django.template import engines from extras.plugins import PluginTemplateExtension @@ -29,31 +30,36 @@ def x_page(self): qr_img = get_qr(url, **qr_args) if config.get('with_text'): - text = [] - for text_field in config.get('text_fields', []): - cfn = None - if '.' in text_field: - try: - text_field, cfn = text_field.split('.') - except ValueError: - cfn = None - if getattr(obj, text_field, None): - if cfn: + if config.get('text_template'): + django_engine = engines["django"] + template = django_engine.from_string(config.get('text_template')) + text = template.render({'obj': obj}) + else: + text = [] + for text_field in config.get('text_fields', []): + cfn = None + if '.' in text_field: try: - if getattr(obj, text_field).get(cfn): - text.append('{}'.format(getattr(obj, text_field).get(cfn))) - except AttributeError: - # fix for nb3.3: trying to get cable termination and device in same way as custom field - if type(getattr(obj, text_field)) is list: - first_element = next(iter(getattr(obj, text_field)), None) - if first_element and getattr(first_element, cfn, None): - text.append('{}'.format(getattr(first_element, cfn))) - else: - text.append('{}'.format(getattr(obj, text_field))) - custom_text = config.get('custom_text') - if custom_text: - text.append(custom_text) - text = '\n'.join(text) + text_field, cfn = text_field.split('.') + except ValueError: + cfn = None + if getattr(obj, text_field, None): + if cfn: + try: + if getattr(obj, text_field).get(cfn): + text.append('{}'.format(getattr(obj, text_field).get(cfn))) + except AttributeError: + # fix for nb3.3: trying to get cable termination and device in same way as custom field + if type(getattr(obj, text_field)) is list: + first_element = next(iter(getattr(obj, text_field)), None) + if first_element and getattr(first_element, cfn, None): + text.append('{}'.format(getattr(first_element, cfn))) + else: + text.append('{}'.format(getattr(obj, text_field))) + custom_text = config.get('custom_text') + if custom_text: + text.append(custom_text) + text = '\n'.join(text) text_img = get_qr_text(qr_img.size, text, config.get('font')) qr_with_text = get_concat(qr_img, text_img, config.get('text_location', 'right'))