Skip to content

Commit

Permalink
fix(views): Add support is_receipt param for filter base on that
Browse files Browse the repository at this point in the history
- Update get_template_choices to accept an is_receipt parameter.

Closes(#22)
  • Loading branch information
ARYAN-NIKNEZHAD committed Oct 30, 2024
1 parent e756582 commit c0559a5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
8 changes: 4 additions & 4 deletions sage_invoice/helpers/funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
21 changes: 11 additions & 10 deletions sage_invoice/service/discovery.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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():
Expand All @@ -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 []
Expand All @@ -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))
Expand Down

0 comments on commit c0559a5

Please sign in to comment.