Skip to content

Commit 3898d10

Browse files
committed
Allow counter to start from an offset
Signed-off-by: DL6ER <[email protected]>
1 parent 0d31fd3 commit 3898d10

6 files changed

Lines changed: 14 additions & 12 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ Additional printer support comes from [matmair/brother_ql-inventree](https://git
9898

9999
### Supported templates
100100

101-
- `{{counter}}` — Inserts the current counter value (automatically increments when printing multiple labels at the same time).
101+
- `{{counter[:start]}}` — Inserts the current counter value (automatically increments when printing multiple labels at the same time).
102102
- `{{datetime:<format>}}` — Inserts the current date and time, e.g. `%H:%M:%S %d.%m.%Y` (see [strftime](https://strftime.org/)).
103103
- `{{uuid}}` — Inserts a random UUID (Universally Unique Identifier).
104104
- `{{short-uuid}}` — Inserts a shortened version of a UUID.

app/labeldesigner/label.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def __init__(
9292
border_distance=(0, 0),
9393
border_color=(0, 0, 0),
9494
timestamp=0,
95-
counter=1,
95+
counter=0,
9696
red_support=False):
9797
self._width = width
9898
self._height = height
@@ -128,7 +128,7 @@ def want_text(self, img):
128128
# We always want to draw text (even when empty) when no image is
129129
# provided to avoid an error 500 because we created no image at all
130130
return img is None or self._label_content not in (LabelContent.QRCODE_ONLY,) and len(self.text) > 0 and len(self.text[0]['text']) > 0
131-
131+
132132
@property
133133
def need_image_text_distance(self):
134134
return self._label_content in (LabelContent.TEXT_QRCODE,
@@ -173,16 +173,17 @@ def label_type(self, value):
173173
self._label_type = value
174174

175175
def process_templates(self):
176-
# Loop over text lines and replace
177-
# {{datetime:x}} by current datetime in specified format x
178-
# {{counter}} by an incrementing counter
176+
# Loop over text lines and replace templates
179177
self.text = self.input_text.copy()
180178
for line in self.text:
181179
if len(line['text']) > 500:
182180
logger.warning("Text line is very long (> 500 characters), this may lead to long processing times.")
183181

184-
# Replace {{counter}} with current counter value
185-
line['text'] = line['text'].replace("{{counter}}", str(self._counter))
182+
# Replace {{counter[:offset]}} with current label counter (x is an optional offset defaulting to 1)
183+
def counter_replacer(match):
184+
offset = int(match.group(1)) if match.group(1) else 1
185+
return str(self._counter + offset)
186+
line['text'] = re.sub(r"\{\{counter(?:\:(\d+))?\}\}", counter_replacer, line['text'])
186187

187188
# Replace {{datetime:x}} with current datetime formatted as x
188189
def datetime_replacer(match):

app/labeldesigner/routes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def print_label():
128128

129129
try:
130130
for i in range(print_count):
131-
label = create_label_from_request(request, i + 1)
131+
label = create_label_from_request(request, i)
132132
# Cut only if we
133133
# - always cut, or
134134
# - we cut only once and this is the last label to be generated
@@ -164,7 +164,7 @@ def parse_text_form(input):
164164
return []
165165
return json.loads(input)
166166

167-
def create_label_from_request(request, counter: int = 1):
167+
def create_label_from_request(request, counter: int = 0):
168168
d=request.values
169169
label_size = d.get('label_size', "62")
170170
kind = [label.form_factor for label in ALL_LABELS if label.identifier == label_size][0]

app/labeldesigner/templates/labeldesigner.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,8 @@ <h6>Additional Font Options:</h6>
356356
<li><code>{{'{{datetime:%H:%M:%S %d.%m.%Y'}}}}</code> — Inserts the current date and time when printing.
357357
You can customize the format using <a href="https://strftime.org/" target="_blank"
358358
rel="noopener">strftime</a> options.</li>
359-
<li><code>{{'{{counter'}}}}</code> — Inserts an up-counting number, increasing for every printed label.
359+
<li><code>{{'{{counter[:start]}}'}}</code> — Inserts an up-counting number. You can optionally specify a <code>start</code> value (defaulting to 1) to start counting from a different number.
360+
When printing multiple labels at once, the counter automatically increments for each label.
360361
</li>
361362
<li><code>{{'{{uuid'}}}}</code> — Inserts a random UUID (Universally Unique Identifier).</li>
362363
<li><code>{{'{{short-uuid'}}}}</code> — Inserts a shortened version of a UUID.</li>

tests/template.png

78 Bytes
Loading

tests/test_labeldesigner_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ def test_generate_template(client):
392392
{
393393
'family': 'DejaVu Sans',
394394
'style': 'Book',
395-
'text': '{{datetime:%d.%m.%Y %H:%M:%S}} COUNTER: {{counter}}',
395+
'text': '{{datetime:%d.%m.%Y %H:%M:%S}} COUNTER: {{counter}} {{counter:5}}',
396396
'size': '30',
397397
'align': 'left'
398398
},

0 commit comments

Comments
 (0)