Skip to content

Commit

Permalink
Refactor: Code for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
antidoid committed Feb 3, 2024
1 parent 80f022e commit 578bc41
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions qrCodeGenerator/function_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,6 @@ def GenerateQRCode(req: func.HttpRequest) -> func.HttpResponse:
)

try:
# Generate the qrcode
qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_L)
qr.add_data(url)

qrCodeData = qr.make_image(
image_factory=StyledPilImage,
module_drawer=getModuleDrawer(style),
color_mask=SolidFillColorMask(
front_color=getRGBValue(color),
back_color=(255, 255, 255)
)
)

# create a blob client
blobServiceClient = BlobServiceClient.from_connection_string(
conn_str=connectionString)
Expand All @@ -55,14 +42,29 @@ def GenerateQRCode(req: func.HttpRequest) -> func.HttpResponse:
blobName = f"{modifiedUrl}-{style}-{color}.png"
blobClient = containerClient.get_blob_client(blobName)

# Upload the qrcode to blob container if it already doesn't exists
# Check if the blob with same name already exists
if not blobClient.exists():
# Generate the qrcode
qr = qrcode.QRCode(
error_correction=qrcode.constants.ERROR_CORRECT_L)
qr.add_data(url)

qrCodeData = qr.make_image(
image_factory=StyledPilImage,
module_drawer=getModuleDrawer(style),
color_mask=SolidFillColorMask(
front_color=getRGBValue(color),
back_color=(255, 255, 255)
)
)

# Converting qrcode data to bytes
buffer = io.BytesIO()
qrCodeData.save(buffer)
buffer.seek(0)
qrcodeInBytes = buffer.read()

# Upload the blob
blobClient.upload_blob(qrcodeInBytes)

return func.HttpResponse(
Expand Down

0 comments on commit 578bc41

Please sign in to comment.