Add function to generate "keys" for test results.

- As the keys are to be used for dict-based lookup (in a template) then they cannot contains spaces.
- May as well enforce lower-case encoding!
This commit is contained in:
Oliver Walters 2020-05-17 12:59:03 +10:00
parent c54cb2b280
commit 17f241774f
3 changed files with 18 additions and 2 deletions

View File

@ -19,6 +19,20 @@ from .version import inventreeVersion, inventreeInstanceName
from .settings import MEDIA_URL, STATIC_URL from .settings import MEDIA_URL, STATIC_URL
def generateTestKey(test_name):
"""
Generate a test 'key' for a given test name.
This must not have spaces as it will be used for dict lookup in a template.
Tests must be named such that they will have unique keys.
"""
key = test_name.strip().lower()
key = key.replace(" ", "")
return key
def getMediaUrl(filename): def getMediaUrl(filename):
""" """
Return the qualified access path for the given file, Return the qualified access path for the given file,

View File

@ -962,7 +962,8 @@ class StockItem(MPTTModel):
result_map = {} result_map = {}
for result in results: for result in results:
result_map[result.test] = result key = helpers.generateTestKey(result.test)
result_map[key] = result
return result_map return result_map

View File

@ -429,5 +429,6 @@ class TestResultTest(StockTest):
self.assertEqual(len(result_map), 3) self.assertEqual(len(result_map), 3)
for test in ['Firmware Version', 'Settings Checksum', 'Temperature Test']: # Keys are all lower-case and do not contain spaces
for test in ['firmwareversion', 'settingschecksum', 'temperaturetest']:
self.assertIn(test, result_map.keys()) self.assertIn(test, result_map.keys())