Coverage for src/backend/InvenTree/plugin/samples/integration/sample.py: 0%
26 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 17:47 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 17:47 +0000
1"""Sample implementations for IntegrationPlugin."""
3import json
5from django.core.exceptions import ValidationError
6from django.http import HttpResponse
7from django.urls import include, path
8from django.utils.translation import gettext_lazy as _
10from plugin import InvenTreePlugin
11from plugin.mixins import AppMixin, NavigationMixin, SettingsMixin, UrlsMixin
14def validate_json(value):
15 """Example validator for json input."""
16 try:
17 json.loads(value)
18 except Exception as e:
19 raise ValidationError(str(e))
22class SampleIntegrationPlugin(
23 AppMixin, SettingsMixin, UrlsMixin, NavigationMixin, InvenTreePlugin
24):
25 """A full plugin example."""
27 NAME = 'SampleIntegrationPlugin'
28 SLUG = 'sample'
29 TITLE = 'Sample Plugin'
31 NAVIGATION_TAB_NAME = 'Sample Nav'
32 NAVIGATION_TAB_ICON = 'fas fa-plus'
34 def view_test(self, request):
35 """Very basic view."""
36 return HttpResponse(f'Hi there {request.user.username} this works')
38 def setup_urls(self):
39 """Urls that are exposed by this plugin."""
40 he_urls = [
41 path('he/', self.view_test, name='he'),
42 path('ha/', self.view_test, name='ha'),
43 ]
45 return [
46 path('hi/', self.view_test, name='hi'),
47 path('ho/', include(he_urls), name='ho'),
48 ]
50 USER_SETTINGS = {
51 'USER_SETTING_1': {
52 'name': _('User Setting 1'),
53 'description': _('A user setting that can be changed by the user'),
54 'default': 'Default Value',
55 },
56 'USER_SETTING_2': {
57 'name': _('User Setting 2'),
58 'description': _('Another user setting'),
59 'default': True,
60 'validator': bool,
61 },
62 'USER_SETTING_3': {
63 'name': _('User Setting 3'),
64 'description': _('A user setting with choices'),
65 'choices': [('X', 'Choice X'), ('Y', 'Choice Y'), ('Z', 'Choice Z')],
66 'default': 'X',
67 },
68 }
70 SETTINGS = {
71 'PO_FUNCTION_ENABLE': {
72 'name': _('Enable PO'),
73 'description': _('Enable PO functionality in InvenTree interface'),
74 'default': True,
75 'validator': bool,
76 },
77 'API_KEY': {
78 'name': _('API Key'),
79 'description': _('Key required for accessing external API'),
80 'required': True,
81 },
82 'NUMERICAL_SETTING': {
83 'name': _('Numerical'),
84 'description': _('A numerical setting'),
85 'validator': int,
86 'default': 123,
87 'units': 'metres',
88 },
89 'CHOICE_SETTING': {
90 'name': _('Choice Setting'),
91 'description': _('A setting with multiple choices'),
92 'choices': [('A', 'Anaconda'), ('B', 'Bat'), ('C', 'Cat'), ('D', 'Dog')],
93 'default': 'A',
94 },
95 'SELECT_COMPANY': {
96 'name': 'Supplier',
97 'description': 'Select a supplier object from the database',
98 'model': 'company.company',
99 'model_filters': {'is_supplier': True},
100 },
101 'SELECT_PART': {
102 'name': 'Part',
103 'description': 'Select a part object from the database',
104 'model': 'part.part',
105 'model_filters': {'active': True},
106 },
107 'PROTECTED_SETTING': {
108 'name': 'Protected Setting',
109 'description': 'A protected setting, hidden from the UI',
110 'default': 'ABC-123',
111 'protected': True,
112 },
113 'VALIDATOR_SETTING': {
114 'name': 'JSON validator Setting',
115 'description': 'A setting using a JSON validator',
116 'validator': validate_json,
117 },
118 }
120 NAVIGATION = [{'name': 'SampleIntegration', 'link': 'plugin:sample:hi'}]