Skip to content

Commit

Permalink
update for #122
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Aug 2, 2024
1 parent fdf4501 commit f8fc404
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 34 deletions.
9 changes: 5 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ RUN pip install --upgrade pip
RUN pip install --no-cache /wheels/*
RUN pip install mysql-connector-python

# this is a hack to overwrite Django's broken TZ stuff that causes errors (500 page) to fail. See https://code.djangoproject.com/ticket/33674
COPY fix_tz_hack.sh /tmp/fix_tz_hack.sh
RUN chmod +x /tmp/fix_tz_hack.sh
RUN /tmp/fix_tz_hack.sh
# Sometimes we need customizations made to python packages
# List changes in the .sh script, making sure it fails gracefully
COPY python_file_hack.sh /tmp/python_file_hack.sh
RUN chmod +x /tmp/python_file_hack.sh
RUN /tmp/python_file_hack.sh

# volume instead
#COPY . $APP_HOME
Expand Down
18 changes: 18 additions & 0 deletions auctions/migrations/0137_alter_userlabelprefs_preset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.7 on 2024-08-02 19:52

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('auctions', '0136_alter_lot_banned_alter_lot_partial_refund_percent'),
]

operations = [
migrations.AlterField(
model_name='userlabelprefs',
name='preset',
field=models.CharField(choices=[('sm', 'Small (Avery 5160)'), ('lg', 'Large (Avery 18262)'), ('thermal_sm', 'Thermal 3"x2"'), ('custom', 'Custom')], default='lg', max_length=20, verbose_name='Label size'),
),
]
1 change: 1 addition & 0 deletions auctions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2808,6 +2808,7 @@ class UserLabelPrefs(models.Model):
PRESETS = (
('sm', 'Small (Avery 5160)'),
('lg', 'Large (Avery 18262)'),
('thermal_sm', 'Thermal 3"x2"'),
('custom', 'Custom'),
)
preset = models.CharField(
Expand Down
56 changes: 40 additions & 16 deletions auctions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3543,6 +3543,7 @@ def get_context_data(self, **kwargs):
user_label_prefs, created = UserLabelPrefs.objects.get_or_create(user=self.request.user)
context = {}
context['empty_labels'] = user_label_prefs.empty_labels
context['print_qr']=True
if user_label_prefs.preset == "sm":
# Avery 5160 labels
context['page_width'] = 8.5
Expand Down Expand Up @@ -3571,6 +3572,21 @@ def get_context_data(self, **kwargs):
context['page_margin_right'] = 0.1
context['font_size'] = 14
context['unit'] = 'in'
elif user_label_prefs.preset == "thermal_sm":
# thermal label printer 3x2
context['page_width'] = 3
context['page_height'] = 2
context['label_width'] = 2.6
context['label_height'] = 1.4
context['label_margin_right'] = .1
context['label_margin_bottom'] = .1
context['page_margin_top'] = .1
context['page_margin_bottom'] = .1
context['page_margin_left'] = .1
context['page_margin_right'] = .1
context['font_size'] = 14
context['unit'] = 'in'
context['print_qr'] = False
else:
context.update({f'{field.name}': getattr(user_label_prefs, field.name) for field in UserLabelPrefs._meta.get_fields()})
return context
Expand Down Expand Up @@ -3605,14 +3621,16 @@ def create_labels(self, request, *args, **kwargs):
page_width = page_width*unit - page_margin_left*unit - page_margin_right*unit
# each label is broken into 3 parts, with a seperate cell for each:
# first cell
qr_code_width = label_width*unit/4

if qr_code_width > label_height*unit:
qr_code_width = label_height*unit
if label_height*unit > qr_code_width:
qr_code_height = qr_code_width
if context['print_qr']:
qr_code_width = label_width*unit/4
if qr_code_width > label_height*unit:
qr_code_width = label_height*unit
if label_height*unit > qr_code_width:
qr_code_height = qr_code_width
else:
qr_code_height = label_height*unit
else:
qr_code_height = label_height*unit
qr_code_width = 0
# second cell
text_area_width = label_width*unit - qr_code_width
# third cell
Expand All @@ -3634,11 +3652,14 @@ def create_labels(self, request, *args, **kwargs):
# currently, we are not trimming the text to fit on a single row
# this means that lots with a long label_line_1 will spill over onto 2 rows
# we could trim the length to [:20] in the model or here to "fix" this, but it's not a huge problem IMHO
label_qr_code = qr_code.qrcode.maker.make_qr_code_image(label.qr_code, QRCodeOptions(size='T', border=4, error_correction='L', image_format="png",))
image_stream = BytesIO(label_qr_code)
label_qr_code_cell = PImage(image_stream, width=qr_code_width, height=qr_code_height, lazy=0, hAlign="LEFT")
if context['print_qr']:
label_qr_code = qr_code.qrcode.maker.make_qr_code_image(label.qr_code, QRCodeOptions(size='T', border=4, error_correction='L', image_format="png",))
image_stream = BytesIO(label_qr_code)
label_qr_code_cell = PImage(image_stream, width=qr_code_width, height=qr_code_height, lazy=0, hAlign="LEFT")
labels_row.append([label_qr_code_cell])
else:
labels_row.append([Paragraph('', style)]) # margin left cell is empty
label_text_cell = Paragraph(f"{label.label_line_0}<br />{label.label_line_1}<br />{label.label_line_2}<br />{label.label_line_3}", style)
labels_row.append([label_qr_code_cell])
labels_row.append([label_text_cell])
labels_row.append([Paragraph('', style)]) # margin right cell is empty

Expand All @@ -3648,7 +3669,7 @@ def create_labels(self, request, *args, **kwargs):
# Check if the current label is the last label in the list and labels_row is not full
if i == len(labels) - 1 and len(labels_row) < num_cols*3:
# Add empty elements to the labels_row list until it is filled
#print(f"adding {(num_cols*3) - len(labels_row)} extra labels, *3 total columns added")
# print(f"adding {(num_cols*3) - len(labels_row)} extra labels, *3 total columns added")
labels_row += [[Paragraph('', style), Paragraph('', style), Paragraph('', style)]]*((num_cols*3) - len(labels_row))
table_data.append(labels_row)
labels_row = []
Expand All @@ -3658,6 +3679,7 @@ def create_labels(self, request, *args, **kwargs):
col_widths = []
for i in range(num_cols):
col_widths += [qr_code_width,text_area_width,margin_right_width]
print(col_widths)
table = Table(table_data, colWidths=col_widths, rowHeights=row_height)
table.setStyle([
#('GRID', (0, 0), (-1, -1), 1, colors.black),
Expand All @@ -3669,11 +3691,13 @@ def create_labels(self, request, *args, **kwargs):
return response

def get(self, request, *args, **kwargs):
try:
#try:
if True:
return self.create_labels(request, *args, **kwargs)
except:
messages.error(request, "Unable to print labels, this is likely caused by an invalid custom setting here")
return redirect(reverse('printing'))
#except LayoutError: # some day I will track down all the possible error types and add them here
#except:
# messages.error(request, "Unable to print labels, this is likely caused by an invalid custom setting here")
# return redirect(reverse('printing'))

class UnprintedLotLabelsView(LotLabelView):
"""Print lot labels, but only ones that haven't already been printed"""
Expand Down
14 changes: 0 additions & 14 deletions fix_tz_hack.sh

This file was deleted.

32 changes: 32 additions & 0 deletions python_file_hack.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash

set -e

update_file() {
local file="$1"
local search="$2"
local replace="$3"
local additional="$4"

if grep -qF "$search" "$file"; then
sed -ri "/$search/,+$additional c $replace" "$file"
else
echo "Unable to find the text $search in $file, update python_file_hack.sh"
exit 1
fi
}

# this is a hack to overwrite Django's broken TZ stuff that causes errors (500 page) to fail. See https://code.djangoproject.com/ticket/33674
FILE="/usr/local/lib/python3.11/site-packages/django/templatetags/tz.py"
SEARCH=" with timezone.override(self.tz.resolve(context)):"
REPLACE="\ try:\n with timezone.override(self.tz.resolve(context)):\n output = self.nodelist.render(context)\n return output\n except:\n return self.nodelist.render(context)"
update_file "$FILE" "$SEARCH" "$REPLACE" "2"


# Reportlab causes certain label settings to throw an error
# https://github.com/virantha/pypdfocr/issues/80
FILE="/usr/local/lib/python3.11/site-packages/reportlab/platypus/paragraph.py"
SEARCH=" if availWidth<_FUZZ:"
REPLACE="\ # removed\n"
update_file "$FILE" "$SEARCH" "$REPLACE" "2"

0 comments on commit f8fc404

Please sign in to comment.