• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

inventree / InvenTree / 4565001079

pending completion
4565001079

push

github

Oliver Walters
Use the attribute name as the dict key

25 of 25 new or added lines in 2 files covered. (100.0%)

26493 of 30115 relevant lines covered (87.97%)

0.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.71
/InvenTree/InvenTree/status_codes.py
1
"""Status codes for InvenTree."""
2

3
from django.utils.translation import gettext_lazy as _
1✔
4

5

6
class StatusCode:
1✔
7
    """Base class for representing a set of StatusCodes.
8

9
    This is used to map a set of integer values to text.
10
    """
11

12
    colors = {}
1✔
13

14
    @classmethod
1✔
15
    def render(cls, key, large=False):
1✔
16
        """Render the value as a HTML label."""
17
        # If the key cannot be found, pass it back
18
        if key not in cls.options.keys():
1✔
19
            return key
×
20

21
        value = cls.options.get(key, key)
1✔
22
        color = cls.colors.get(key, 'secondary')
1✔
23

24
        span_class = f'badge rounded-pill bg-{color}'
1✔
25

26
        return "<span class='{cl}'>{value}</span>".format(
1✔
27
            cl=span_class,
28
            value=value
29
        )
30

31
    @classmethod
1✔
32
    def list(cls):
1✔
33
        """Return the StatusCode options as a list of mapped key / value items."""
34
        codes = []
×
35

36
        for key in cls.options.keys():
×
37

38
            opt = {
×
39
                'key': key,
40
                'value': cls.options[key]
41
            }
42

43
            color = cls.colors.get(key, None)
×
44

45
            if color:
×
46
                opt['color'] = color
×
47

48
            codes.append(opt)
×
49

50
        return codes
×
51

52
    @classmethod
1✔
53
    def text(cls, key):
1✔
54
        """Text for supplied status code."""
55
        return cls.options.get(key, None)
×
56

57
    @classmethod
1✔
58
    def items(cls):
1✔
59
        """All status code items."""
60
        return cls.options.items()
1✔
61

62
    @classmethod
1✔
63
    def keys(cls):
1✔
64
        """All status code keys."""
65
        return cls.options.keys()
×
66

67
    @classmethod
1✔
68
    def labels(cls):
1✔
69
        """All status code labels."""
70
        return cls.options.values()
×
71

72
    @classmethod
1✔
73
    def names(cls):
1✔
74
        """Return a map of all 'names' of status codes in this class
75

76
        Will return a dict object, with the attribute name indexed to the integer value.
77

78
        e.g.
79
        {
80
            'PENDING': 10,
81
            'IN_PROGRESS': 20,
82
        }
83
        """
84
        keys = cls.keys()
×
85
        status_names = {}
×
86

87
        for d in dir(cls):
×
88
            if d.startswith('_'):
×
89
                continue
×
90
            if d != d.upper():
×
91
                continue
×
92

93
            value = getattr(cls, d, None)
×
94

95
            if value is None:
×
96
                continue
×
97
            if callable(value):
×
98
                continue
×
99
            if type(value) != int:
×
100
                continue
×
101
            if value not in keys:
×
102
                continue
×
103

104
            status_names[d] = value
×
105

106
        return status_names
×
107

108
    @classmethod
1✔
109
    def label(cls, value):
1✔
110
        """Return the status code label associated with the provided value."""
111
        return cls.options.get(value, value)
1✔
112

113
    @classmethod
1✔
114
    def value(cls, label):
1✔
115
        """Return the value associated with the provided label."""
116
        for k in cls.options.keys():
×
117
            if cls.options[k].lower() == label.lower():
×
118
                return k
×
119

120
        raise ValueError("Label not found")
×
121

122

123
class PurchaseOrderStatus(StatusCode):
1✔
124
    """Defines a set of status codes for a PurchaseOrder."""
125

126
    # Order status codes
127
    PENDING = 10  # Order is pending (not yet placed)
1✔
128
    PLACED = 20  # Order has been placed with supplier
1✔
129
    COMPLETE = 30  # Order has been completed
1✔
130
    CANCELLED = 40  # Order was cancelled
1✔
131
    LOST = 50  # Order was lost
1✔
132
    RETURNED = 60  # Order was returned
1✔
133

134
    options = {
1✔
135
        PENDING: _("Pending"),
136
        PLACED: _("Placed"),
137
        COMPLETE: _("Complete"),
138
        CANCELLED: _("Cancelled"),
139
        LOST: _("Lost"),
140
        RETURNED: _("Returned"),
141
    }
142

143
    colors = {
1✔
144
        PENDING: 'secondary',
145
        PLACED: 'primary',
146
        COMPLETE: 'success',
147
        CANCELLED: 'danger',
148
        LOST: 'warning',
149
        RETURNED: 'warning',
150
    }
151

152
    # Open orders
153
    OPEN = [
1✔
154
        PENDING,
155
        PLACED,
156
    ]
157

158
    # Failed orders
159
    FAILED = [
1✔
160
        CANCELLED,
161
        LOST,
162
        RETURNED
163
    ]
164

165

166
class SalesOrderStatus(StatusCode):
1✔
167
    """Defines a set of status codes for a SalesOrder."""
168

169
    PENDING = 10  # Order is pending
1✔
170
    SHIPPED = 20  # Order has been shipped to customer
1✔
171
    CANCELLED = 40  # Order has been cancelled
1✔
172
    LOST = 50  # Order was lost
1✔
173
    RETURNED = 60  # Order was returned
1✔
174

175
    options = {
1✔
176
        PENDING: _("Pending"),
177
        SHIPPED: _("Shipped"),
178
        CANCELLED: _("Cancelled"),
179
        LOST: _("Lost"),
180
        RETURNED: _("Returned"),
181
    }
182

183
    colors = {
1✔
184
        PENDING: 'secondary',
185
        SHIPPED: 'success',
186
        CANCELLED: 'danger',
187
        LOST: 'warning',
188
        RETURNED: 'warning',
189
    }
190

191
    # Open orders
192
    OPEN = [
1✔
193
        PENDING,
194
    ]
195

196
    # Completed orders
197
    COMPLETE = [
1✔
198
        SHIPPED,
199
    ]
200

201

202
class StockStatus(StatusCode):
1✔
203
    """Status codes for Stock."""
204

205
    OK = 10  # Item is OK
1✔
206
    ATTENTION = 50  # Item requires attention
1✔
207
    DAMAGED = 55  # Item is damaged
1✔
208
    DESTROYED = 60  # Item is destroyed
1✔
209
    REJECTED = 65  # Item is rejected
1✔
210
    LOST = 70  # Item has been lost
1✔
211
    QUARANTINED = 75  # Item has been quarantined and is unavailable
1✔
212
    RETURNED = 85  # Item has been returned from a customer
1✔
213

214
    options = {
1✔
215
        OK: _("OK"),
216
        ATTENTION: _("Attention needed"),
217
        DAMAGED: _("Damaged"),
218
        DESTROYED: _("Destroyed"),
219
        LOST: _("Lost"),
220
        REJECTED: _("Rejected"),
221
        QUARANTINED: _("Quarantined"),
222
        RETURNED: _("Returned"),
223
    }
224

225
    colors = {
1✔
226
        OK: 'success',
227
        ATTENTION: 'warning',
228
        DAMAGED: 'danger',
229
        DESTROYED: 'danger',
230
        LOST: 'dark',
231
        REJECTED: 'danger',
232
        QUARANTINED: 'info'
233
    }
234

235
    # The following codes correspond to parts that are 'available' or 'in stock'
236
    AVAILABLE_CODES = [
1✔
237
        OK,
238
        ATTENTION,
239
        DAMAGED,
240
        RETURNED,
241
    ]
242

243

244
class StockHistoryCode(StatusCode):
1✔
245
    """Status codes for StockHistory."""
246

247
    LEGACY = 0
1✔
248

249
    CREATED = 1
1✔
250

251
    # Manual editing operations
252
    EDITED = 5
1✔
253
    ASSIGNED_SERIAL = 6
1✔
254

255
    # Manual stock operations
256
    STOCK_COUNT = 10
1✔
257
    STOCK_ADD = 11
1✔
258
    STOCK_REMOVE = 12
1✔
259

260
    # Location operations
261
    STOCK_MOVE = 20
1✔
262

263
    # Installation operations
264
    INSTALLED_INTO_ASSEMBLY = 30
1✔
265
    REMOVED_FROM_ASSEMBLY = 31
1✔
266

267
    INSTALLED_CHILD_ITEM = 35
1✔
268
    REMOVED_CHILD_ITEM = 36
1✔
269

270
    # Stock splitting operations
271
    SPLIT_FROM_PARENT = 40
1✔
272
    SPLIT_CHILD_ITEM = 42
1✔
273

274
    # Stock merging operations
275
    MERGED_STOCK_ITEMS = 45
1✔
276

277
    # Convert stock item to variant
278
    CONVERTED_TO_VARIANT = 48
1✔
279

280
    # Build order codes
281
    BUILD_OUTPUT_CREATED = 50
1✔
282
    BUILD_OUTPUT_COMPLETED = 55
1✔
283
    BUILD_CONSUMED = 57
1✔
284

285
    # Sales order codes
286
    SHIPPED_AGAINST_SALES_ORDER = 60
1✔
287

288
    # Purchase order codes
289
    RECEIVED_AGAINST_PURCHASE_ORDER = 70
1✔
290

291
    # Return order codes
292
    RETURNED_AGAINST_RETURN_ORDER = 80
1✔
293

294
    # Customer actions
295
    SENT_TO_CUSTOMER = 100
1✔
296
    RETURNED_FROM_CUSTOMER = 105
1✔
297

298
    options = {
1✔
299
        LEGACY: _('Legacy stock tracking entry'),
300

301
        CREATED: _('Stock item created'),
302

303
        EDITED: _('Edited stock item'),
304
        ASSIGNED_SERIAL: _('Assigned serial number'),
305

306
        STOCK_COUNT: _('Stock counted'),
307
        STOCK_ADD: _('Stock manually added'),
308
        STOCK_REMOVE: _('Stock manually removed'),
309

310
        STOCK_MOVE: _('Location changed'),
311

312
        INSTALLED_INTO_ASSEMBLY: _('Installed into assembly'),
313
        REMOVED_FROM_ASSEMBLY: _('Removed from assembly'),
314

315
        INSTALLED_CHILD_ITEM: _('Installed component item'),
316
        REMOVED_CHILD_ITEM: _('Removed component item'),
317

318
        SPLIT_FROM_PARENT: _('Split from parent item'),
319
        SPLIT_CHILD_ITEM: _('Split child item'),
320

321
        MERGED_STOCK_ITEMS: _('Merged stock items'),
322

323
        CONVERTED_TO_VARIANT: _('Converted to variant'),
324

325
        SENT_TO_CUSTOMER: _('Sent to customer'),
326
        RETURNED_FROM_CUSTOMER: _('Returned from customer'),
327

328
        BUILD_OUTPUT_CREATED: _('Build order output created'),
329
        BUILD_OUTPUT_COMPLETED: _('Build order output completed'),
330
        BUILD_CONSUMED: _('Consumed by build order'),
331

332
        SHIPPED_AGAINST_SALES_ORDER: _("Shipped against Sales Order"),
333

334
        RECEIVED_AGAINST_PURCHASE_ORDER: _('Received against Purchase Order'),
335

336
        RETURNED_AGAINST_RETURN_ORDER: _('Returned against Return Order'),
337
    }
338

339

340
class BuildStatus(StatusCode):
1✔
341
    """Build status codes."""
342

343
    PENDING = 10  # Build is pending / active
1✔
344
    PRODUCTION = 20  # BuildOrder is in production
1✔
345
    CANCELLED = 30  # Build was cancelled
1✔
346
    COMPLETE = 40  # Build is complete
1✔
347

348
    options = {
1✔
349
        PENDING: _("Pending"),
350
        PRODUCTION: _("Production"),
351
        CANCELLED: _("Cancelled"),
352
        COMPLETE: _("Complete"),
353
    }
354

355
    colors = {
1✔
356
        PENDING: 'secondary',
357
        PRODUCTION: 'primary',
358
        COMPLETE: 'success',
359
        CANCELLED: 'danger',
360
    }
361

362
    ACTIVE_CODES = [
1✔
363
        PENDING,
364
        PRODUCTION,
365
    ]
366

367

368
class ReturnOrderStatus(StatusCode):
1✔
369
    """Defines a set of status codes for a ReturnOrder"""
370

371
    # Order is pending, waiting for receipt of items
372
    PENDING = 10
1✔
373

374
    # Items have been received, and are being inspected
375
    IN_PROGRESS = 20
1✔
376

377
    COMPLETE = 30
1✔
378
    CANCELLED = 40
1✔
379

380
    OPEN = [
1✔
381
        PENDING,
382
        IN_PROGRESS,
383
    ]
384

385
    options = {
1✔
386
        PENDING: _("Pending"),
387
        IN_PROGRESS: _("In Progress"),
388
        COMPLETE: _("Complete"),
389
        CANCELLED: _("Cancelled"),
390
    }
391

392
    colors = {
1✔
393
        PENDING: 'secondary',
394
        IN_PROGRESS: 'primary',
395
        COMPLETE: 'success',
396
        CANCELLED: 'danger',
397
    }
398

399

400
class ReturnOrderLineStatus(StatusCode):
1✔
401
    """Defines a set of status codes for a ReturnOrderLineItem"""
402

403
    PENDING = 10
1✔
404

405
    # Item is to be returned to customer, no other action
406
    RETURN = 20
1✔
407

408
    # Item is to be repaired, and returned to customer
409
    REPAIR = 30
1✔
410

411
    # Item is to be replaced (new item shipped)
412
    REPLACE = 40
1✔
413

414
    # Item is to be refunded (cannot be repaired)
415
    REFUND = 50
1✔
416

417
    # Item is rejected
418
    REJECT = 60
1✔
419

420
    options = {
1✔
421
        PENDING: _('Pending'),
422
        RETURN: _('Return'),
423
        REPAIR: _('Repair'),
424
        REFUND: _('Refund'),
425
        REPLACE: _('Replace'),
426
        REJECT: _('Reject')
427
    }
428

429
    colors = {
1✔
430
        PENDING: 'secondary',
431
        RETURN: 'success',
432
        REPAIR: 'primary',
433
        REFUND: 'info',
434
        REPLACE: 'warning',
435
        REJECT: 'danger',
436
    }
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc