Skip to content

Implement url_template allowing custom url for objects #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ QR Code. The following values are available:
is.
* `qr_border`: Integer (default 4), controls how many boxes thick the border should be
(the default is 4, which is the minimum according to the specs).
* `url_template`: Jinja2 template applied to url with {{ obj }} as context

### Per object options

Expand Down
10 changes: 8 additions & 2 deletions netbox_qrcode/template_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@ def x_page(self):
config = self.context['config']
obj = self.context['object']
request = self.context['request']
url = request.build_absolute_uri(obj.get_absolute_url())
# get object settings
obj_cfg = config.get(self.model.replace('dcim.', ''))
if obj_cfg is None:
return ''
# and ovverride default
config.update(obj_cfg)


if config.get('url_template'):
django_engine = engines["django"]
template = django_engine.from_string(config.get('url_template'))
url = template.render({'obj': obj})
else:
url = request.build_absolute_uri(obj.get_absolute_url())

qr_args = {}
for k, v in config.items():
if k.startswith('qr_'):
Expand Down