Skip to content

Commit

Permalink
Merge pull request #41 from renatoalmeidaoliveira/patch-1
Browse files Browse the repository at this point in the history
Text from Jinja2 Template
  • Loading branch information
k01ek authored Feb 8, 2023
2 parents edd9717 + 36dab23 commit a97ba43
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"`.
Expand Down
54 changes: 30 additions & 24 deletions netbox_qrcode/template_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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'))

Expand Down

0 comments on commit a97ba43

Please sign in to comment.