Skip to content

Commit

Permalink
Merge pull request #17 from MohmdFo/main
Browse files Browse the repository at this point in the history
fix(template-discovery): correct template discovery to match files with additional characters after prefix
  • Loading branch information
sepehr-akbarzadeh authored Oct 17, 2024
2 parents 2f93351 + d7dfe43 commit be6fe1c
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 23 deletions.
32 changes: 32 additions & 0 deletions sage_invoice/migrations/0002_alter_invoice_template_choice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 5.1.2 on 2024-10-17 09:47

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("sage_invoice", "0001_initial"),
]

operations = [
migrations.AlterField(
model_name="invoice",
name="template_choice",
field=models.CharField(
choices=[
("quotation_1", "quotation_1"),
("quotation_2", "quotation_2"),
("quotation_3", "quotation_3"),
("quotation_4", "quotation_4"),
("receipt1", "receipt1"),
("receipt2", "receipt2"),
("receipt3", "receipt3"),
],
db_comment="Template choice for the invoice",
help_text="The template you want for your invoice",
max_length=20,
verbose_name="Template choice",
),
),
]
20 changes: 11 additions & 9 deletions sage_invoice/service/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
class JinjaTemplateDiscovery:
def __init__(self):
self.default_template_dir = "default_invoices" # inside package
self.custom_template_dir = (
self.sage_template_dir = (
settings.SAGE_MODEL_TEMPLATE
) # custom templates folder name
self.custom_template_prefix = (
self.sage_template_prefix = (
settings.SAGE_MODEL_PREFIX
) # custom template prefix

Expand All @@ -29,28 +29,30 @@ def get_custom_templates(self):
template_choices = []
for app_config in apps.get_app_configs():
template_dir = os.path.join(
app_config.path, "templates", self.custom_template_dir
app_config.path, "templates", self.sage_template_dir
)
if os.path.exists(template_dir):
template_choices.extend(
self._find_templates_in_directory(
template_dir, self.custom_template_prefix
template_dir, self.sage_template_prefix
)
)
return template_choices

def _find_templates_in_directory(self, directory, prefix=None):
"""
Helper method to find .jinja2 files in a directory, optionally filtering by
prefix.
prefix, and return the filenames without the .jinja2 extension.
"""
if not os.path.exists(directory):
return []

templates = []
for filename in os.listdir(directory):
if filename.endswith(".jinja2") and (
not prefix or filename.startswith(prefix)
):
if filename.endswith(".jinja2") and (not prefix or filename.startswith(prefix)):
templates.append(filename)
return templates

# Remove the .jinja2 extension from the filenames
filenames = list(map(lambda x: x.replace('.jinja2', ''), templates))

return filenames
12 changes: 5 additions & 7 deletions sage_invoice/service/invoice_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,16 @@ def __init__(self) -> None:
Jinja2.
"""
logger.info("Initializing QuotationService")
self.template_discovery = JinjaTemplateDiscovery(
models_dir=getattr(settings, "SAGE_MODEL_TEMPLATE", "default_invoices")
)
self.template_discovery = JinjaTemplateDiscovery()
self.env = Environment(
loader=FileSystemLoader(self.template_discovery.models_dir),
loader=FileSystemLoader(self.template_discovery.sage_template_dir),
autoescape=select_autoescape(
["html", "xml"]
), # Enable autoescape for HTML and XML templates
)
logger.info(
"Template discovery set to directory: %s",
self.template_discovery.models_dir,
self.template_discovery.sage_template_dir,
)

def render_quotation(self, queryset: QuerySet) -> str:
Expand All @@ -50,7 +48,7 @@ def render_quotation(self, queryset: QuerySet) -> str:
"""
logger.info("Rendering quotation")
invoice = queryset.first()
context = self.render_contax(queryset)
context = self.render_context(queryset)
is_receipt = invoice.receipt
template_number = "".join(filter(str.isdigit, invoice.template_choice))
logger.info("Selected template number: %s", template_number)
Expand All @@ -65,7 +63,7 @@ def render_quotation(self, queryset: QuerySet) -> str:

return template.render(context)

def render_contax(self, queryset: QuerySet) -> Dict[str, Any]:
def render_context(self, queryset: QuerySet) -> Dict[str, Any]:
"""Prepare the context data for rendering a quotation.
Args:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions sage_invoice/tests/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_init(self, mock_template_discovery):
assert service.template_discovery == mock_template_discovery.return_value
assert service.env is not None
mock_template_discovery.assert_called_once_with(
models_dir="sage_invoice" # Default directory
sage_template_dir="sage_invoice" # Default directory
)

@pytest.fixture
Expand Down Expand Up @@ -90,13 +90,13 @@ def test_render_quotation_template_not_found(self, mock_get_template_path, invoi

mock_get_template_path.assert_called_once()

def test_render_contax(self, invoice):
def test_render_context(self, invoice):
# Test context generation for the invoice
service = QuotationService()

# Ensure contacts and other fields are handled correctly
invoice.contacts = ["you@example.com", "1234567890"]
context = service.render_contax(invoice)
context = service.render_context(invoice)

assert context["title"] == invoice.title
assert context["tracking_code"] == invoice.tracking_code
Expand Down
8 changes: 4 additions & 4 deletions sage_invoice/views/invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ def get_context_data(self, **kwargs):
invoice_slug = self.kwargs.get("slug")
invoice = Invoice.objects.filter(slug=invoice_slug).first()
service = QuotationService()
rendered_content = service.render_contax(invoice)
rendered_content = service.render_context(invoice)
context.update(rendered_content)

# Dynamically choose the template based on invoice type and choice
if invoice.receipt:
self.template_name = f"receipt{invoice.template_choice}.html"
self.template_name = f"{invoice.template_choice}.html"
else:
self.template_name = f"quotation{invoice.template_choice}.html"
self.template_name = f"{invoice.template_choice}.html"

return context

Expand Down Expand Up @@ -79,7 +79,7 @@ def get(self, request, *args, **kwargs):
continue

service = QuotationService()
rendered_content = service.render_contax(invoice)
rendered_content = service.render_context(invoice)

# Determine the template based on invoice type
if invoice.receipt:
Expand Down

0 comments on commit be6fe1c

Please sign in to comment.