79567403

Date: 2025-04-10 19:14:46
Score: 0.5
Natty:
Report link

Thank you so much everyone for such quick answers!

Even though the Factory method pattern feels like a very concrete way of going forward, I chose the __new__ method override suggested by Juanpa, mainy because I'm lazy :)

My PDFRenderer class now looks something like this

def __new__(cls, *args, **kwargs):
        if settings.USE_PDF_RENDERER.lower() == "reportlab":
            # Keep it here to avoid circular import
            from .reportlab import ReportlabRenderer
            cls = ReportlabRenderer
        else:
            raise ValueError(f"Unsupported PDF renderer: {settings.USE_PDF_RENDERER}! Ensure the 'USE_PDF_RENDERER' setting is set to 'reportlab' in the project settings.")

        return object.__new__(cls)

where the project settings (django) parameter decides which renderer to opt for. Overall this fits in nicely with the Django way of doing it so I chose to go this route.

Reasons:
  • Blacklisted phrase (0.5): Thank you
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Mitrajeet Golsangi