add svg qr codes
All checks were successful
ci/woodpecker/push/tests Pipeline was successful

Signed-off-by: Ivan Schaller <ivan@schaller.sh>
This commit is contained in:
Ivan Schaller 2023-06-26 11:55:18 +02:00
parent 32d043a2d5
commit 3249de4609
Signed by: olofvndrhr
GPG key ID: 2A6BE07D99C8C205
7 changed files with 42 additions and 29 deletions

8
.gitignore vendored
View file

@ -1,18 +1,16 @@
test.ipynb test.ipynb
test.py test.py
mangadexdlp/__pycache__/ test.html
test.sh
test/ test/
downloads/ downloads/
.vscode/ .vscode/
.pytest_cache& .pytest_cache
.vscode/ .vscode/
__pycache__/ __pycache__/
.pytest_cache/ .pytest_cache/
chaps.txt
mangas.txt
.idea/ .idea/
venv venv
test.sh
.ruff_cache/ .ruff_cache/
development/db-data/ development/db-data/
development/redis-data/ development/redis-data/

View file

@ -2,6 +2,8 @@
NetBox plugin to generate QR codes for assets NetBox plugin to generate QR codes for assets
> Based on/inspired by: [k01ek/netbox-qrcode](https://github.com/k01ek/netbox-qrcode)
CI/CD CI/CD
[![status-badge](https://img.shields.io/drone/build/olofvndrhr/netbox-qrgen?label=ci&server=https%3A%2F%2Fci.44net.ch)](https://ci.44net.ch/olofvndrhr/netbox-qrgen) [![status-badge](https://img.shields.io/drone/build/olofvndrhr/netbox-qrgen?label=ci&server=https%3A%2F%2Fci.44net.ch)](https://ci.44net.ch/olofvndrhr/netbox-qrgen)
@ -30,7 +32,9 @@ test
## Features (not complete) ## Features (not complete)
- test - Generate QR Codes for your assets
- Download them either as PNG or SVG
- Directly print them via the Web interface
## Compatibility ## Compatibility
@ -52,7 +56,7 @@ Review the [official Netbox plugin documentation](https://docs.netbox.dev/en/sta
/opt/netbox/venv/bin/pip install --no-warn-script-location netbox-qrgen /opt/netbox/venv/bin/pip install --no-warn-script-location netbox-qrgen
``` ```
### In the netbox docker container ### In the Netbox docker container
For adding to a NetBox Docker setup see For adding to a NetBox Docker setup see
[the general instructions for using netbox-docker with plugins](https://github.com/netbox-community/netbox-docker/wiki/Using-Netbox-Plugins). [the general instructions for using netbox-docker with plugins](https://github.com/netbox-community/netbox-docker/wiki/Using-Netbox-Plugins).
@ -78,6 +82,7 @@ PLUGINS_CONFIG = {
"qr_with_text": True, "qr_with_text": True,
"qr_text_fields": ["name", "serial"], "qr_text_fields": ["name", "serial"],
"qr_font": "Tahoma", "qr_font": "Tahoma",
"qr_width": "200px",
"qr_custom_text": None, "qr_custom_text": None,
"qr_text_location": "right", "qr_text_location": "right",
"qr_version": 2, "qr_version": 2,
@ -112,7 +117,8 @@ PLUGINS_CONFIG = {
| --------------------- | ----------- | -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | --------------------- | ----------- | -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `qr_with_text` | `bool` | `True` | Generate a text with the specified infos besides the QR code. | | `qr_with_text` | `bool` | `True` | Generate a text with the specified infos besides the QR code. |
| `qr_text_fields` | `list[str]` | `["name", "serial"]` | Fields to add as a text to the QR code. All object properties can be used. | | `qr_text_fields` | `list[str]` | `["name", "serial"]` | Fields to add as a text to the QR code. All object properties can be used. |
| `qr_font` | `str` | `'Tahoma'` | Font to use to generate the text. Included fonts: `ArialBlack`,`ArialMT`,`JetBrainsMono`,`JetBrainsMonoBold`,`Tahoma`,`TahomaBold`. | | `qr_font` | `str` | `Tahoma` | Font to use to generate the text. Included fonts: `ArialBlack`,`ArialMT`,`JetBrainsMono`,`JetBrainsMonoBold`,`Tahoma`,`TahomaBold`. |
| `qr_width` | `str` | `200px` | Size for the SVG image to render. Can be any HTML valid size. |
| `qr_custom_text` | `str` | `None` | Custom text to be added to every QR code. | | `qr_custom_text` | `str` | `None` | Custom text to be added to every QR code. |
| `qr_text_location` | `str` | `right` | Where the text fields are rendered relative to the QR code | | `qr_text_location` | `str` | `right` | Where the text fields are rendered relative to the QR code |
| `qr_version` | `int` | `2` | An integer from 1 to 40 that controls the size of the QR Code (the smallest, version 1, is a 21x21 matrix). More details [here](https://www.qrcode.com/en/about/version.html) | | `qr_version` | `int` | `2` | An integer from 1 to 40 that controls the size of the QR Code (the smallest, version 1, is a 21x21 matrix). More details [here](https://www.qrcode.com/en/about/version.html) |
@ -129,7 +135,3 @@ If you encounter any bugs, also just open an issue with a description of the pro
## TODO's ## TODO's
- test - test
```
```

View file

@ -21,6 +21,7 @@ PLUGINS_CONFIG = {
"qr_with_text": True, "qr_with_text": True,
"qr_text_fields": ["name", "serial"], "qr_text_fields": ["name", "serial"],
"qr_font": "TahomaBold", "qr_font": "TahomaBold",
"qr_width": "200px",
"qr_custom_text": None, "qr_custom_text": None,
"qr_text_location": "bottom", "qr_text_location": "bottom",
"qr_version": 2, "qr_version": 2,

View file

@ -14,6 +14,7 @@ class NetBoxQRGenConfig(PluginConfig):
"qr_with_text": True, "qr_with_text": True,
"qr_text_fields": ["name", "serial"], "qr_text_fields": ["name", "serial"],
"qr_font": "Tahoma", "qr_font": "Tahoma",
"qr_width": "200px",
"qr_custom_text": None, "qr_custom_text": None,
"qr_text_location": "right", "qr_text_location": "right",
"qr_version": 2, "qr_version": 2,

View file

@ -7,50 +7,50 @@ plugin_settings = settings.PLUGINS_CONFIG.get("netbox_qrgen", {})
class QRGen(PluginTemplateExtension): class QRGen(PluginTemplateExtension):
def get_url(self) -> str: def _get_url(self) -> str:
obj = self.context["object"] obj = self.context["object"]
request = self.context["request"] request = self.context["request"]
url: str = request.build_absolute_uri(obj.get_absolute_url()) url: str = request.build_absolute_uri(obj.get_absolute_url())
return url return url
def get_qrcode(self, url: str) -> str: def _get_qrcode(self, url: str) -> tuple[str, str]:
img = generate_qrcode(url=url, **plugin_settings) img_png, img_svg = generate_qrcode(url=url, **plugin_settings)
b64 = get_base64(img) b64_png = get_base64(img_png)
b64_svg = get_base64(img_svg)
return b64 return b64_png, b64_svg
def right_page(self): def right_page(self):
qr_img = self.get_qrcode(self.get_url()) b64_png, b64_svg = self._get_qrcode(self._get_url())
return self.render( return self.render(
"netbox_qrgen/qrgen.html", "netbox_qrgen/qrgen.html",
extra_context={"image": qr_img}, extra_context={
"image_width": plugin_settings["qr_width"],
"image_png": b64_png,
"image_svg": b64_svg,
},
) )
class QRGenDevice(QRGen): class QRGenDevice(QRGen):
model = "dcim.device" model = "dcim.device"
# kind = "device"
class QRGenRack(QRGen): class QRGenRack(QRGen):
model = "dcim.rack" model = "dcim.rack"
# kind = "rack"
class QRGenCable(QRGen): class QRGenCable(QRGen):
model = "dcim.cable" model = "dcim.cable"
# kind = "cable"
class QRGenInventoryItem(QRGen): class QRGenInventoryItem(QRGen):
model = "dcim.inventoryitem" model = "dcim.inventoryitem"
# kind = "inventoryitem"
class QRGenCircuit(QRGen): class QRGenCircuit(QRGen):
model = "circuits.circuit" model = "circuits.circuit"
# kind = "circuit"
template_extensions = [ template_extensions = [

View file

@ -12,11 +12,21 @@
<div class="card"> <div class="card">
<h5 class="card-header">QR Code</h5> <h5 class="card-header">QR Code</h5>
<div class="card-body text-right noprint"> <div class="card-body text-right noprint">
<img src="data:image/png;base64,{{image}}" /> <img
width="{{image_width}}"
src="data:image/svg+xml;base64,{{image_svg}}"
/>
</div> </div>
<div class="card-footer text-end noprint"> <div class="card-footer text-end noprint">
<button <button
onclick="printImg('data:image/png;base64,{{image}}')" onclick="window.open('data:image/png;base64,{{image_png}}', '_blank').focus();"
;
class="btn btn-xs btn-primary"
>
<span class="mdi mdi-download" aria-hidden="true"></span> Get PNG
</button>
<button
onclick="printImg('data:image/svg+xml;base64,{{image_svg}}')"
; ;
class="btn btn-xs btn-primary" class="btn btn-xs btn-primary"
> >

View file

@ -7,7 +7,7 @@ from qrcode.image.pure import PyPNGImage
from qrcode.image.svg import SvgPathFillImage from qrcode.image.svg import SvgPathFillImage
def generate_qrcode(url: str, **kwargs) -> PyPNGImage: def generate_qrcode(url: str, **kwargs) -> tuple[PyPNGImage, SvgPathFillImage]:
qr = qrcode.QRCode( qr = qrcode.QRCode(
version=kwargs["qr_version"], version=kwargs["qr_version"],
error_correction=kwargs["qr_error_correction"], error_correction=kwargs["qr_error_correction"],
@ -18,9 +18,10 @@ def generate_qrcode(url: str, **kwargs) -> PyPNGImage:
qr.add_data(url) qr.add_data(url)
qr.make(fit=True) qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white") img_png = qr.make_image(image_factory=PyPNGImage, fill_color="black", back_color="white")
img_svg = qr.make_image(image_factory=SvgPathFillImage, fill_color="black", back_color="white")
return img return img_png, img_svg
def get_base64(img: Union[SvgPathFillImage, PyPNGImage]) -> str: def get_base64(img: Union[SvgPathFillImage, PyPNGImage]) -> str: