diff --git a/sage_invoice/helpers/funcs.py b/sage_invoice/helpers/funcs.py index 824983b..99748ca 100644 --- a/sage_invoice/helpers/funcs.py +++ b/sage_invoice/helpers/funcs.py @@ -7,14 +7,14 @@ from sage_invoice.service.discovery import JinjaTemplateDiscovery -def get_template_choices(): +def get_template_choices(is_receipt=False): """ Returns a combined list of default and custom templates, formatted for use in a - model's choices field. + model's choices field, filtered by the `is_receipt` flag. """ discovery = JinjaTemplateDiscovery() - default_templates = discovery.get_default_templates() - custom_templates = discovery.get_custom_templates() + default_templates = discovery.get_default_templates(is_receipt=is_receipt) + custom_templates = discovery.get_custom_templates(is_receipt=is_receipt) choices = [(template, template) for template in default_templates] choices += [(template, template) for template in custom_templates] diff --git a/sage_invoice/service/discovery.py b/sage_invoice/service/discovery.py index c6f55af..7feef93 100644 --- a/sage_invoice/service/discovery.py +++ b/sage_invoice/service/discovery.py @@ -1,9 +1,7 @@ import os - from django.apps import apps from django.conf import settings - class JinjaTemplateDiscovery: def __init__(self): self.default_template_dir = "default_invoices" # inside package @@ -12,17 +10,16 @@ def __init__(self): ) # custom templates folder name self.sage_template_prefix = settings.SAGE_MODEL_PREFIX # custom template prefix - def get_default_templates(self): + def get_default_templates(self, is_receipt=False): """Return a list of default templates found in the package's template directory.""" - # Use Django's template loading mechanisms to discover default templates. default_path = os.path.join( apps.get_app_config("sage_invoice").path, "templates", self.default_template_dir, ) - return self._find_templates_in_directory(default_path) + return self._find_templates_in_directory(default_path, is_receipt) - def get_custom_templates(self): + def get_custom_templates(self, is_receipt=False): """Return a list of custom templates found in each app's `templates/` directory.""" template_choices = [] for app_config in apps.get_app_configs(): @@ -32,15 +29,15 @@ def get_custom_templates(self): if os.path.exists(template_dir): template_choices.extend( self._find_templates_in_directory( - template_dir, self.sage_template_prefix + template_dir, is_receipt, self.sage_template_prefix ) ) return template_choices - def _find_templates_in_directory(self, directory, prefix=None): + def _find_templates_in_directory(self, directory, is_receipt=False, prefix=None): """ Helper method to find .jinja2 files in a directory, optionally filtering by - prefix, and return the filenames without the .jinja2 extension. + prefix or `is_receipt`, and return the filenames without the .jinja2 extension. """ if not os.path.exists(directory): return [] @@ -50,7 +47,11 @@ def _find_templates_in_directory(self, directory, prefix=None): if filename.endswith(".jinja2") and ( not prefix or filename.startswith(prefix) ): - templates.append(filename) + # Filter based on is_receipt + if is_receipt and "receipt" in filename: + templates.append(filename) + elif not is_receipt and "receipt" not in filename: + templates.append(filename) # Remove the .jinja2 extension from the filenames filenames = list(map(lambda x: x.replace(".jinja2", ""), templates))