Skip to content

fix(report): support S3 cloud media storage for report and label template loading (#12426) - #12943

Merged
SchrodingersGat merged 9 commits into
inventree:masterfrom
xyz-is-best:fix/s3-template-storage-loading
Oct 13, 2026
Merged

SchrodingersGat merged 9 commits into
inventree:masterfrom
xyz-is-best:fix/s3-template-storage-loading

Conversation

@xyz-is-best

Copy link
Copy Markdown
Contributor

Summary of Changes

Fixes #12426 by supporting remote cloud storage backends (e.g. AWS S3 or MinIO via django-storages) when resolving and rendering report and label templates.

Root Cause

Previously:

  1. TemplateUploadMixin.template_name unconditionally prepended settings.MEDIA_ROOT, which assumes templates are saved to a local disk path. When cloud media storage is active, the file does not exist under MEDIA_ROOT, causing TemplateDoesNotExist during template lookup.
  2. InvenTree storage backends often configure bucket or location prefixes (e.g. storage.location = 'inventree-server'), causing relative path mismatches when looking up template contents or snippets from default_storage.

Key Fixes

  1. TemplateUploadMixin.template_name: Checks whether the file exists locally under settings.MEDIA_ROOT. If not (remote storage), returns the relative storage name directly instead of a nonexistent local path.
  2. ReportTemplateBase.get_template_content(): Implemented direct retrieval from template_file.storage, resolving bucket/location prefix mismatches dynamically.
  3. ReportTemplateBase.render_as_string(): Added fallback to render from storage content if template loader resolution fails.
  4. InvenTreeStorageTemplateLoader: Added and registered a custom template loader that resolves template snippets and includes directly from default_storage.
  5. Environment Graceful Import: Updated report/fetcher.py to gracefully handle weasyprint import without hard sys.exit(1) when C-libraries are missing during non-PDF test runs.

Verification & Testing

Added hermetic test suite S3ReportTemplateTest with MockS3Storage in src/backend/InvenTree/report/tests.py:

  • test_template_name_remote_storage: Asserts storage-relative path resolution when not on local disk.
  • test_get_template_content_with_prefix: Validates bucket/location prefix resolution.
  • test_get_template_content_without_prefix: Validates direct storage template content loading.
  • test_get_template_content_missing: Asserts TemplateDoesNotExist is raised on missing files.
  • test_render_as_string_fallback: Validates fallback rendering from storage content.
  • test_storage_template_loader_resolution: Validates InvenTreeStorageTemplateLoader resolution.
  • test_render_to_string_with_storage_loader: Validates Django render_to_string() resolution from storage.

Test Results:

  • pytest src/backend/InvenTree/report/tests.py -k "S3ReportTemplateTest or DefaultTemplateFileTest" -> 9 passed, 0 failed
  • uv run ruff check -> 0 errors
  • uv run ruff format --check -> Clean

…late loading (inventree#12426)

- Prevent TemplateDoesNotExist when using cloud storage backends (e.g. S3 via django-storages)
- Update TemplateUploadMixin.template_name to return relative storage path when local file does not exist on disk
- Implement ReportTemplateBase.get_template_content() to load content from storage backend and resolve bucket/location prefix mismatches
- Add fallback in ReportTemplateBase.render_as_string() to render from storage content when template loader lookup fails
- Register InvenTreeStorageTemplateLoader to allow template snippets to resolve directly from default_storage
- Add unit tests in S3ReportTemplateTest covering prefix resolution, remote rendering, and storage template loading
@netlify

netlify Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for inventree-web-pui-preview canceled.

Name Link
🔨 Latest commit 8eb432a
🔍 Latest deploy log https://app.netlify.com/projects/inventree-web-pui-preview/deploys/6aceab5c7cadb80429574304

Comment thread src/backend/InvenTree/report/fetcher.py
Comment thread src/backend/InvenTree/report/models.py
Comment thread src/backend/InvenTree/report/models.py Outdated

@matmair matmair left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting approach overall, looks good. Thank you submitting for it.

I have some questions on a part of the patchset, looking forward to getting some insights.

@matmair matmair added this to the 1.6.0 milestone Sep 28, 2026
@matmair matmair added bug Identifies a bug which needs to be addressed report Report/Label generation labels Sep 28, 2026
@codecov

codecov Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.35632% with 22 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.25%. Comparing base (99893f3) to head (8eb432a).

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #12943      +/-   ##
==========================================
- Coverage   87.28%   87.25%   -0.04%     
==========================================
  Files        1515     1515              
  Lines      104716   104512     -204     
  Branches    11902    11743     -159     
==========================================
- Hits        91406    91190     -216     
- Misses      13243    13259      +16     
+ Partials       67       63       -4     
Flag Coverage Δ
backend 92.06% <87.35%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
Backend Apps 92.69% <88.23%> (-0.02%) ⬇️
Backend General 94.49% <ø> (ø)
Frontend 79.31% <ø> (-0.17%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@matmair

matmair commented Oct 2, 2026

Copy link
Copy Markdown
Member

@xyz-is-best are you ok with a maintainer fixing the nitpicks that we mentioned? This looks good overall and I would love to get this into 1.6.0

@matmair

matmair commented Oct 10, 2026

Copy link
Copy Markdown
Member

@SchrodingersGat this is ready if we include xyz-is-best#1 in it

matmair and others added 3 commits October 12, 2026 20:24
this is unrealstic in a full enviroment; the only reason for this is some very constraint sandbox - which is not our run enviroment
@SchrodingersGat
SchrodingersGat merged commit a5ea65a into inventree:master Oct 13, 2026
48 checks passed
@SchrodingersGat

Copy link
Copy Markdown
Member

@xyz-is-best thanks for the contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Identifies a bug which needs to be addressed report Report/Label generation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

S3 Media Storage Template Loading

3 participants