Package: inventree-bulk-plugin
inventree-bulk-pluginThis plugin helps you bulk create storage locations and part categories inInvenTreeby using customized naming strategies. That means you not only have the option to generate multidimensional* names for stock locations or part categories, but also have the option to save the templates for later usage if your storage room uses e.g. drawer towers, saved templates help to ensure naming consistency for all later added towers.[!NOTE]
multidimensional means that you are not limited to namings likeD1,D2, .. but also something likeD1.A,D1.B,D2.A,D2.B, ...🌟ScreenshotsThis will generate the previous mentioned example:If you want to try out the templates on you're own, you can just copy the below json to your clipboard and use the "New untitled schema from clipboard" button to import them (seeimport/export).{"name":"Example","template_type":"STOCK_LOCATION","template":{"version":"1.0.0","input":{},"templates":[],"output":{"parent_name_match":"true","dimensions":["*NUMERIC","*ALPHA"],"count":["3","2"],"generate":{"name":"D{{dim.1}}.{{dim.2}}"},"childs":[]}}}📄 Some more examplesBut even nested generations don't stop you.{"name":"","template_type":"STOCK_LOCATION","template":{"version":"1.0.0","input":{},"templates":[],"output":{"parent_name_match":"true","dimensions":["*NUMERIC"],"count":["3"],"generate":{"name":"D{{dim.1}}"},"childs":[{"parent_name_match":"true","dimensions":["*ALPHA(casing=upper)"],"count":["2"],"generate":{"name":"{{par.gen.name}}-{{dim.1}}"}}]}}}And with parent name match, you can even generate different sub-locations depending of the name of the parent.{"name":"","template_type":"STOCK_LOCATION","template":{"version":"1.0.0","input":{},"templates":[],"output":{"parent_name_match":"true","dimensions":["*NUMERIC"],"count":["3"],"generate":{"name":"D{{dim.1}}"},"childs":[{"parent_name_match":"{{par.gen.name == \"D1\"}}","dimensions":["*ALPHA"],"count":["2"],"generate":{"name":"D.{{dim.1}}"}},{"parent_name_match":"true","dimensions":["*NUMERIC"],"count":["1"],"generate":{"name":"{{dim.1}}"}}]}}}Pad your numeric dimensions with zeros.{"name":"","template_type":"STOCK_LOCATION","template":{"version":"1.0.0","input":{},"templates":[],"output":{"parent_name_match":"true","dimensions":["*NUMERIC"],"count":["100"],"generate":{"name":"D{{dim.1.zfill(dim.1.len|string|length)}}"},"childs":[]}}}Extend from a template.{"name":"","template_type":"STOCK_LOCATION","template":{"version":"1.0.0","input":{},"templates":[{"name":"Drawer sections","parent_name_match":"true","dimensions":["*ALPHA"],"count":["2"],"generate":{"name":"{{par.gen.name}} Section {{dim.1}}"}}],"output":{"parent_name_match":"true","dimensions":["*NUMERIC"],"count":["100"],"generate":{"name":"D{{dim.1.zfill(dim.1.len|string|length)}}"},"childs":[{"parent_name_match":"true","dimensions":[""],"count":[""],"generate":{"name":""},"extends":"Drawer sections"}]}}}Use more generate keys.{"name":"","template_type":"STOCK_LOCATION","template":{"version":"1.0.0","input":{},"templates":[],"output":{"parent_name_match":"true","dimensions":["*NUMERIC"],"count":["100"],"generate":{"name":"D{{dim.1}}","description":"Drawer {{dim.1}}","structural":"{{dim.1|int<=3}}","external":true}}}}Using input to reuse a saved template.{"name":"","template_type":"STOCK_LOCATION","template":{"version":"1.0.0","input":{"drawers":"10"},"templates":[],"output":{"parent_name_match":"true","dimensions":["*NUMERIC"],"count":["{{inp.drawers}}"],"generate":{"name":"D{{dim.1}}"},"childs":[]}}}And then use the preview/bulk create formUse the global contextYou can use the global context for some more complex things like generating resistor values in e.g. the E12 row.{"name":"Resistors","template_type":"PART","template":{"version":"1.0.0","input":{"packages":"0805,1206","tolerances":"5,10"},"templates":[],"output":{"parent_name_match":"true","dimensions":["{{inp.packages}}","{{inp.tolerances}}","0-8","1-12"],"count":[null,null,null,null],"generate":{"name":"R_{{global.formatted_value}}_{{dim.2}}%_{{dim.1}}","parameters":[{"template":"2","value":"{{global.formatted_value}}"},{"template":"3","value":"{{dim.2}}%"},{"template":"1","value":"{{dim.1}}"}]},"global_context":"{% set values = [1,1.2,1.5,1.8,2.2,2.7,3.3,3.9,4.7,5.6,6.8,8.2] %}\n{% set units = [\"\", \"k\", \"M\"] %}\n\n{% set value = values[dim.4|int-1] * 10 ** (dim.3|int) %}\n{% set formatted_value = \"{:.1f}\".format(value * 10 ** -(dim.3|int//3*3)).removesuffix(\".0\").replace(\".\", \",\") + units[dim.3|int//3] + \"Ω\" %}\n"}}}⚙️ InstallationInstall this plugin as follows:Make sure you allow the use of the url integration and app integration (seeWhy does this plugin needs the app mixin?)Goto Settings > Plugins > Install Plugin, enterinventree-bulk-pluginas package name. Enable the confirm switch and click submit.Restart your server and activate the plugin.Stop your server and runinvoke update(for docker installs it isdocker-compose inventree-server invoke update). This ensures that all migrations run and the static files get collected. You can now start your server again and start using the plugin.[!IMPORTANT]
At least InvenTree v0.12.7 is required to use this plugin.🏃 UsageYou can bulk create sub-stocklocations, sub-partcategories and parts (Seegenerate types). Go to one and use the bulk creation panel on the side for the type you want to generate. Edit asaved templatewith thebulk creation editoror create a new untitled to setup a generation quickly. Use"Preview/Bulk create"to bulkcreate using a saved template in combination with inputs. Templates can als beimported and exportedfrom clipboard or a file which is useful if you want to follow along with the documentation. On most places this documentation shows the template in a codeblock with a copy button.Saved templatesYou can save bulk creation templates to ensure consistency along your storage trees. Let's say you have a bunch of drawer towers. With saved templates you can now easily store your templates to re-use it when you want to add a new tower to the system.[!NOTE]
You can useinputsto make your bulk creation schema dynamic in amount of drawers or their names and then use thepreview/bulk createdialog to quickly generate objects at different locations.Import exportTemplates can be imported and exported into the clipboard or can be downloaded as a file. Use the dropdown menu at the right of the "New untitled template" button for importing. Exporting can be done from theeditor viewwith the clipboard/download icon button.Preview/Bulk createThe preview bulk create dialog can be used to quickly generate objects at different locations by using inputs to make them dynamic. One use case would be the Resistor part generation template shown at thescreenshot section.Bulk creation editorThe bulk creation editor helps you to define the generation schema.Jinja2You can useJinja2 templatingin every field (except in theinputsection). You can also use filters to manipulate the dimension output.Global jinja2 contextinp.<key>- Accessinput variables, e.g. ({{inp.drawer_count|int / 2}})Extra useful filtersto_json(value: Any, **kwargs)- Convert any value into a json string. Uses thejson.dumpsmethod from python under the hood, thereforethesekwargsare available.from_json(value: str, **kwargs)- Convert a string value into a python variable. Uses thejson.loadsmethod from python under the hood, thereforethesekwargsare available.to_csv(value: list[dict[str, str]], **kwargs)- Convert a csv like python list of dicts to a csv string. Uses thecsv.DictWritermethod from python under the hood, thereforethesekwargsare available.from_csv(value: str, **kwargs)- Convert a string value in csv structure into a python list of dicts. Uses thecsv.DictReadermethod from python under the hood, thereforethesekwargsare available.Debug toolsdebug- the debug command from jinja2 is available too and can be used as{% debug %}, seejinja2 docsInputYou can define key/value pairs of inputs which you can later reference in your schema via{{inp.<key>}}. This is useful forsaved templatesto dynamically generate the amount of locations as you want, but still keep the structure.TemplatesYou can define templates from which you can later extend in your output. Template values can also be overwritten.Template name- Template name, is later used to select for extendingFor the rest of the fields seeoutput.OutputParent name matchFirst child that matches the parent name matcher will be chosen for generating the child's for a specific parent. This must evaluate to something that can be casted to a boolean. You can use Jinja2 for dynamically decide based on the parent. E.g.{{par.gen.name == "D1"}}. The global jinja2 andparcontext is available here.ExtendsSelect a template to extend fromGlobal contextGlobal context can be used to set up some more complex variables and reuse them between fields. Under the hood this template gets imported asglobalby prepending{% import global_context_template as global with context %}to every generate field. Therefore you can also use the dimensions and every available context variable there too. But note that the defined variables are only valid in that parent they are defined in, not in their childs. This is a limitation of the import function of jinja2 templates.Dimensions/CountDimensions are a way to add various counting strategies to your naming. You can add a dimension by clicking on "Add dimension" and remove it via the red "X" on the right of the dimension field.Adimensioncan contain comma separated generators which generate the values for you. There are three types of generators. You can use thecountfield to limit a dimension to a specific amount of generating items. These generators can have arguments parsed via the following syntax:GENERATOR(key1=value,key2=value), whereGENERATORis the name/range.Generator types:Word:any arbitrary word, not starting with*. E.g.hello worldRanges:ranges are defined with a-in the middleE.g.a-bxInfinity:infinity generators start with a*E.g.*NUMERICAvailable Generators:Numeric generator:*NUMERIC(start=0,end=10,step=2,count=5)or0-10(step=2)Alpha generator:*ALPHA(casing=upper|lower,start=A,end=F,step=2,count=3)ora-z(step=2)Example:1-3,hello,*NUMERIC(start=1,step=2,end=10),*ALPHA(casing=upper,end=B),A-D(step=2), this will generate the following dimension:1,2,3,hello,1,3,5,7,9,A,B,A,C.[!IMPORTANT]
Infinity generators need acountargument or a global count limitation, otherwise generation will fail.GenerateThese fields my differ between the different available generate objects. They correspond to the generated items property. For example "Name" will be the name of the created location. For more info and also about the available jinja2 context, seegeneration types.Generate fields can be added with just one click from a list of available fields. If you're missing an field, pleaseopen an issue. There are several different types of inputs for native values like numbers, booleans, model references, ... . You can either use the native fields or switch to template mode by clicking on the blue template icon on the right.Boolean fields then must evaluate to something that can be interpreted as a boolean liketrueorfalse.Number fields then must evaluate to a numberModel reference fields then must evaluate to a valid idChild'sChild's are a way to add some nesting to your bulk creation tree. You can use them for e.g. generating sections in every of your drawer. You can use theParent name matchoption to add for your drawers named fromDrawer 1-Drawer 10two sections while your other drawers have different sections.Generation typesYou can bulk create sub-stocklocations, sub-partcategories andpartswith there different options. All of those are tree objects, that means objects that can have childs and extend from templates that can be defined.For all of those, the following context is available.KeyDescriptionlencount of elements this child will generateidxcurrent global index of the generated itemdim.<x>x-th dimension, one-based (e.g.{{dim.1}}to access the first dimension)dim.<x>.idxcurrent index in the x-th dimensiondim.<x>.lencount of items the x-th dimension haspar.<...>parent's contextpar.dim.<x>parents's dimensionspar.gen.<name>parent's generated fields (e.g. to reuse the parents name{{par.gen.name}})par.par.<...>parent's parent context, can be nested deeplyglobal.<...>Access any variable defined inglobal contextPartsParts use the tree generation feature for part variant. You can either generate variants for an already created template part by using the "variants of" attribute (note that for the root generate element, templating is not supported in this field), or create a template part with variants by using the childs feature. The "variant of" field then gets automatically assigned the parent part if its not set for the child. The only thing you need to make sure is that the parent part has set theis_template: trueoption, otherwise creation will fail. The plugin then tries to copy unset fields from the template part if they are not set for the child. Parameters get copied too, even there are parameters defined for the child.Example:{"name":"","template_type":"PART","template":{"version":"1.0.0","input":{},"templates":[],"output":{"parent_name_match":"true","dimensions":[],"count":[],"generate":{"name":"Wall paint","description":"This is awesome paint","is_template":true},"childs":[{"parent_name_match":"true","dimensions":["red,blue,green"],"count":[""],"generate":{"name":"{{par.gen.name}} - {{dim.1}}"}}]}}}Parts have an additional context:KeyDescriptionpar.category.<...>category fields this part is in🧑‍💻 DevelopmentInstall as editable install to your inventree installation viapip install -e /path/to/inventree-bulk-pluginEnable the plugin and runinvoke migrateto run the migrationInstall js dependencies viacd inventree_bulk_plugin/frontend && npm ciConfigure the base url of your vite dev server (that you need to start vianpm run dev) in the InvenTreeconfig.ymlas e.g.customize.inventree_bulk_plugin_dev_url: http://localhost:5210Restart InvenTree and start vite dev server vianpm run dev❓ FAQWhy does this plugin needs the App Mixin?This plugin uses the App Mixin to add a custom model to the database to manage stored templates which ensure consistency along your creation of storage trees. (SeeSaved templates). Additionally the App Mixin is used to provide the static files that are required for the reactive interface powered by preact.Why does this plugin needs the Url Mixin?This plugin uses the Url Mixin to expose custom API endpoints for previewing and bulk create locations.