Skip to content

Commit

Permalink
remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Oct 17, 2024
1 parent 930200e commit b886953
Showing 1 changed file with 0 additions and 350 deletions.
350 changes: 0 additions & 350 deletions auctions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,9 @@
from el_pagination.views import AjaxListView
from PIL import Image
from qr_code.qrcode.utils import QRCodeOptions
from reportlab.lib import colors
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.units import cm, inch
from reportlab.platypus import (
Image as PImage,
)
from reportlab.platypus import (
ImageAndFlowables,
KeepInFrame,
Paragraph,
SimpleDocTemplate,
Table,
)
from user_agents import parse
from webpush import send_user_notification
from webpush.models import PushInformation
Expand Down Expand Up @@ -4620,331 +4610,6 @@ def dispatch(self, request, *args, **kwargs):
messages.error(request, "Your account doesn't have permission to view this page.")
return redirect("/")

def get_context_data_old(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
context["page_height"] = 11
context["label_width"] = 2.5
context["label_height"] = 0.96
context["label_margin_right"] = 0.2
context["label_margin_bottom"] = 0.03
context["page_margin_top"] = 0.6
context["page_margin_bottom"] = 0.1
context["page_margin_left"] = 0.16
context["page_margin_right"] = 0.1
context["font_size"] = 10
context["unit"] = "in"
elif user_label_prefs.preset == "lg":
# Avery 18262 labels
context["page_width"] = 8.5
context["page_height"] = 11
context["label_width"] = 3.9
context["label_height"] = 1.2
context["label_margin_right"] = 0.2
context["label_margin_bottom"] = 0.125
context["page_margin_top"] = 0.88
context["page_margin_bottom"] = 0.6
context["page_margin_left"] = 0.19
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"] = 0.1
context["label_margin_bottom"] = 0.1
context["page_margin_top"] = 0.1
context["page_margin_bottom"] = 0.1
context["page_margin_left"] = 0.1
context["page_margin_right"] = 0.1
context["font_size"] = 14
context["unit"] = "in"
context["print_qr"] = True
else:
context.update(
{f"{field.name}": getattr(user_label_prefs, field.name) for field in UserLabelPrefs._meta.get_fields()}
)
return context

def create_labels_unused(self, request, *args, **kwargs):
"""Scratchpad attempting to use reportlab for more advanced stuff, probably safe to remove"""
# Get context data and set up PDF response
context = self.get_context_data(kwargs=kwargs)
response = HttpResponse(content_type="application/pdf")

# Generate label file name
label_name = re.sub(r"[^a-zA-Z0-9]", "_", (self.filename or "labels").lower())
response["Content-Disposition"] = f'attachment; filename="{label_name}.pdf"'

# Fetch label and page dimensions from context
label_width = context.get("label_width")
label_height = context.get("label_height")
label_margin_right = context.get("label_margin_right")
margin_bottom = context.get("label_margin_bottom")
page_margin_top = context.get("page_margin_top")
page_margin_bottom = context.get("page_margin_bottom")
page_margin_left = context.get("page_margin_left")
page_margin_right = context.get("page_margin_right")

# Use inch or cm as unit
unit = inch if context.get("unit") == "in" else cm
font_size = context.get("font_size")
page_width = context.get("page_width")
page_height = context.get("page_height")

# Get queryset and append empty labels
labels = self.get_queryset()
labels = (["empty"] * context["empty_labels"]) + list(labels)

# Set up document
doc = SimpleDocTemplate(
response,
pagesize=[page_width * unit, page_height * unit],
leftMargin=page_margin_left * unit,
rightMargin=page_margin_right * unit,
topMargin=page_margin_top * unit,
bottomMargin=page_margin_bottom * unit,
)

elements = []
page_width = page_width * unit - page_margin_left * unit - page_margin_right * unit # Adjust page width

first_column_width = label_width * unit / 5
first_column_width = min(first_column_width, label_height * unit)
# if first_column_width < stringWidth("123456789", "Helvetica", font_size):
# lot number won't fit, make it larger but keep qr code size the same

text_area_width = label_width * unit - first_column_width
margin_right_width = label_margin_right * unit
column_width = first_column_width + text_area_width + margin_right_width
row_height = (label_height + margin_bottom) * unit

num_cols = int(page_width / column_width)

# Paragraph style
style = ParagraphStyle(
name="Normal",
fontName="Helvetica",
fontSize=font_size,
# leading=font_size * 1.3,
borderColor=colors.blue,
borderWidth=1,
)

# Optional style with borders (visual debugging)
style_with_border = ParagraphStyle(
name="NormalWithBorder",
fontName="Helvetica",
fontSize=font_size,
leading=font_size * 1.3,
borderColor=colors.red,
borderWidth=1,
borderPadding=2,
)

# Create rows and table data
labels_row = []
table_data = []

for i, label in enumerate(labels):
if label == "empty":
labels_row.append([[Paragraph("", style)] * 3])
else:
# 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
# lot number is special, it always must fit on one row

if context["print_qr"]:
frame = Paragraph("123456789", style)
frame = KeepInFrame(
first_column_width,
font_size * 2,
[
frame,
],
mode="shrink",
)
# labels_row.append(frame, self.generate_qr_code(
# label, first_column_width, first_column_width),)
# labels_row.append(Spacer(1, 12))
labels_row.append([frame, self.generate_qr_code(label, first_column_width, first_column_width)])
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_with_border,
)
labels_row.append([label_text_cell])
labels_row.append([Paragraph("", style)]) # Empty right margin

# Append completed rows to table data
if (i + 1) % num_cols == 0 or i == len(labels) - 1:
if i == len(labels) - 1 and len(labels_row) < num_cols * 3:
labels_row += [[Paragraph("", style)] * 3] * ((num_cols * 3) - len(labels_row))
table_data.append(labels_row)
labels_row = []

if label != "empty":
label.label_printed = True
label.save()

# Set column widths for table
col_widths = [first_column_width, text_area_width, margin_right_width] * num_cols

# Build the table
table = Table(table_data, colWidths=col_widths, rowHeights=row_height)
table.setStyle(
[
("ALIGN", (0, 0), (-1, -1), "LEFT"),
("VALIGN", (0, 0), (-1, -1), "TOP"),
]
)

elements.append(table)
doc.build(elements)

return response

def create_labels_reportlab(self, request, *args, **kwargs):
"""Works fine but can't be customized"""
context = self.get_context_data(kwargs=kwargs)
response = HttpResponse(content_type="application/pdf")
label_name = re.sub(r"[^a-zA-Z0-9]", "_", (self.filename or "labels").lower())
response["Content-Disposition"] = f'attachment; filename="{label_name}.pdf"'
label_width = context.get("label_width")
label_height = context.get("label_height")
label_margin_right = context.get("label_margin_right")
margin_bottom = context.get("label_margin_bottom")
page_margin_top = context.get("page_margin_top")
page_margin_bottom = context.get("page_margin_bottom")
page_margin_left = context.get("page_margin_left")
page_margin_right = context.get("page_margin_right")
unit = inch if context.get("unit") == "in" else cm
font_size = context.get("font_size")
page_width = context.get("page_width")
page_height = context.get("page_height")

labels = self.get_queryset()
labels = (["empty"] * context["empty_labels"]) + list(labels)

doc = SimpleDocTemplate(
response,
pagesize=[page_width * unit, page_height * unit],
leftMargin=page_margin_left * unit,
rightMargin=page_margin_right * unit,
topMargin=page_margin_top * unit,
bottomMargin=page_margin_bottom * unit,
)
elements = []
# remove margins from page width
page_width = page_width * unit - page_margin_left * unit - page_margin_right * unit
# each label is broken into 3 parts, with a separate cell for each:
# first cell
if context["print_qr"]:
qr_code_width = label_width * unit / 5
if qr_code_width > label_height * unit / 2:
qr_code_width = label_height * unit / 2
if label_height * unit > qr_code_width:
qr_code_height = qr_code_width
else:
qr_code_height = label_height * unit
else:
qr_code_width = 0
# second cell
text_area_width = label_width * unit - qr_code_width
# third cell
margin_right_width = label_margin_right * unit
# total width of each label is the sum of all 3 cells
column_width = qr_code_width + text_area_width + margin_right_width
# row height is the same for all 3 parts
row_height = (label_height + margin_bottom) * unit
num_cols = int(page_width / column_width)
labels_row = []
table_data = []
style = ParagraphStyle(
name="Normal",
fontName="Helvetica",
fontSize=font_size,
leading=font_size * 1.3,
)

style_with_border = ParagraphStyle(
name="Normal",
fontName="Helvetica",
fontSize=context.get("font_size"),
leading=context.get("font_size") * 1.3,
borderColor=colors.red, # Add border color
borderWidth=1, # Add border width for visualization
borderPadding=2, # Add padding to avoid text touching the border
)

for i, label in enumerate(labels):
if label == "empty":
labels_row += [[Paragraph("", style), Paragraph("", style), Paragraph("", style)]] * 3
else:
# 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
# if context["print_qr"]:
# labels_row.append([self.generate_qr_code(label, qr_code_width, qr_code_height)])
# 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,
# )
label_text_cell = ImageAndFlowables(
self.generate_qr_code(label, qr_code_width, qr_code_height),
Paragraph("some text here it's a lot of text very long, just great", style_with_border),
imageSide="left",
)
labels_row.append([label_text_cell])
# margin right cell is empty
labels_row.append([Paragraph("", style)])

# Check if the current label is the last label in the current row or the last label in the list
if (i + 1) % num_cols == 0 or i == len(labels) - 1:
# logger.debug("we have reached the end, %s in total", len(labels))
# 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
# logger.debug("adding %s extra labels, *3 total columns added", (num_cols * 3) - len(labels_row))
labels_row += [
[
Paragraph("", style),
Paragraph("", style),
Paragraph("", style),
]
] * ((num_cols * 3) - len(labels_row))
table_data.append(labels_row)
labels_row = []
if label != "empty":
label.label_printed = True
label.save()
col_widths = []
for i in range(num_cols):
col_widths += [qr_code_width, text_area_width, margin_right_width]
table = Table(table_data, colWidths=col_widths, rowHeights=row_height)
table.setStyle(
[
# ('GRID', (0, 0), (-1, -1), 1, colors.black),
("ALIGN", (0, 0), (-1, -1), "LEFT"),
("VALIGN", (0, 0), (-1, -1), "TOP"),
]
)
elements.append(table)
doc.build(elements)
return response

def get_pdf_filename(self):
label_name = re.sub(r"[^a-zA-Z0-9]", "_", (self.filename or "labels").lower())
return f"{label_name}.pdf"
Expand Down Expand Up @@ -5076,21 +4741,6 @@ def get_context_data(self, **kwargs):
context["all_borders"] = False
return context

def get(self, request, *args, **kwargs):
if request.user.is_superuser:
return super().get(self, request, *args, **kwargs)
# return self.create_labels(request, *args, **kwargs)
else:
try:
return self.create_labels(request, *args, **kwargs)
# 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"))

def generate_qr_code(self, label, qr_code_width, qr_code_height):
label_qr_code = qr_code.qrcode.maker.make_qr_code_image(
label.qr_code,
Expand Down

0 comments on commit b886953

Please sign in to comment.