Skip to content

plugin-validation-purchase-orders#12094

Merged
SchrodingersGat merged 2 commits into
inventree:masterfrom
NerosKi:order-validation
Jun 7, 2026
Merged

plugin-validation-purchase-orders#12094
SchrodingersGat merged 2 commits into
inventree:masterfrom
NerosKi:order-validation

Conversation

@NerosKi
Copy link
Copy Markdown
Contributor

@NerosKi NerosKi commented Jun 5, 2026

Fixes #12076

Problem

Custom validation plugins implementing the validate_batch_code hook are enforced
when stock is created directly, but not when receiving stock against a Purchase
Order. Invalid/empty batch codes (and any other plugin validation) silently pass on
the PO receipt path.

Cause

Plugin validation hooks only fire through the model lifecycle:

  • validate_batch_code via StockItem.clean()
  • validate_model_instance via StockItem.save() / full_clean()
    (PluginValidationMixin.run_plugin_validation())

PurchaseOrder.receive_line_items() creates stock via StockItem.objects.bulk_create()
and StockItem._create_serial_numbers(), both of which bypass save()/clean(), so
the hooks are never reached. (Serial-number validation already worked because it is
called explicitly in the receive serializer.)

Fix

Invoke the existing plugin validation hooks explicitly in receive_line_items() — no
validation logic is added to core, the hooks the bulk path skips are simply called:

  • item.validate_batch_code()plugin.validate_batch_code
  • item.run_plugin_validation()plugin.validate_model_instance

Both creation branches are covered:

  • Non-serialized items are validated before bulk_create().
  • Serialized items are validated after _create_serial_numbers().

receive_line_items() is wrapped in @transaction.atomic, so a ValidationError
raised by any plugin rolls back all inserts — no partial/invalid stock is committed.

Testing

Manual testing has been performed by using a plugin that checks for the existence of various elements such as batch code, unit price, qr code etc. Tested adding stock the default way and via receiving stock in purchase orders. Tested nothing strange happens if no plugin is added. All seems ok.

@NerosKi NerosKi requested a review from SchrodingersGat as a code owner June 5, 2026 13:37
@netlify
Copy link
Copy Markdown

netlify Bot commented Jun 5, 2026

Deploy Preview for inventree-web-pui-preview ready!

Name Link
🔨 Latest commit ecaec9c
🔍 Latest deploy log https://app.netlify.com/projects/inventree-web-pui-preview/deploys/6a23207e5b0ee50008bc8933
😎 Deploy Preview https://deploy-preview-12094--inventree-web-pui-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 95 (🟢 up 2 from production)
Accessibility: 82 (no change from production)
Best Practices: 100 (no change from production)
SEO: 78 (no change from production)
PWA: -
View the detailed breakdown and full score reports
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@matmair matmair added the bug Identifies a bug which needs to be addressed label Jun 5, 2026
@matmair matmair modified the milestones: 1.4.0, 1.3.4 Jun 5, 2026
@matmair matmair added backport Apply this label to a PR to enable auto-backport action backport-to-1.3.x labels Jun 5, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 5, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.47%. Comparing base (2e4bf37) to head (ecaec9c).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #12094   +/-   ##
=======================================
  Coverage   91.47%   91.47%           
=======================================
  Files         979      979           
  Lines       52331    52336    +5     
=======================================
+ Hits        47870    47875    +5     
  Misses       4461     4461           
Flag Coverage Δ
backend 90.48% <100.00%> (+<0.01%) ⬆️

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

Components Coverage Δ
Backend Apps 91.77% <100.00%> (+<0.01%) ⬆️
Backend General 93.53% <ø> (ø)
Frontend ∅ <ø> (∅)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Member

@matmair matmair left a comment

Choose a reason for hiding this comment

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

LGTM

@matmair matmair modified the milestones: 1.3.4, 1.3.5 Jun 7, 2026
@matmair
Copy link
Copy Markdown
Member

matmair commented Jun 7, 2026

@SchrodingersGat looks good to me

@SchrodingersGat SchrodingersGat merged commit a63ca93 into inventree:master Jun 7, 2026
42 checks passed
@SchrodingersGat
Copy link
Copy Markdown
Member

@NerosKi thanks for the contribution

@SchrodingersGat SchrodingersGat modified the milestones: 1.3.5, 1.4.0 Jun 7, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 7, 2026

💔 All backports failed

Status Branch Result
1.3.x Backport failed because of merge conflicts

Manual backport

To create the backport manually run:

backport --pr 12094

Questions ?

Please refer to the Backport tool documentation and see the Github Action logs for details

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

Labels

backport Apply this label to a PR to enable auto-backport action backport-to-1.3.x bug Identifies a bug which needs to be addressed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

validate_batch_code plugin hook is not enforced when receiving against a purchase order

3 participants