summaryrefslogtreecommitdiff
path: root/indoteknik_api/controllers/controller.py
diff options
context:
space:
mode:
Diffstat (limited to 'indoteknik_api/controllers/controller.py')
-rw-r--r--indoteknik_api/controllers/controller.py40
1 files changed, 15 insertions, 25 deletions
diff --git a/indoteknik_api/controllers/controller.py b/indoteknik_api/controllers/controller.py
index 86628400..5b20f7d4 100644
--- a/indoteknik_api/controllers/controller.py
+++ b/indoteknik_api/controllers/controller.py
@@ -70,40 +70,30 @@ class Controller(http.Controller):
'value': {},
'query': {}
}
- for key in queries:
- rules = queries[key]
- is_number = len([r for r in rules if r == 'number']) > 0
-
- has_alias = [r for r in rules if r.startswith('alias:')]
- alias = key
- if len(has_alias) > 0:
- alias = has_alias[0].replace('alias:', '')
-
- has_default = [r for r in rules if r.startswith('default:')]
- default = None
- if len(has_default) > 0:
- default = has_default[0].replace('default:', '')
-
- value = kw.get(key, '')
- if value in ['null', 'undefined']:
+
+ for key, rules in queries.items():
+ is_number = 'number' in rules
+ alias = next((r.replace('alias:', '') for r in rules if r.startswith('alias:')), key)
+ default = next((r.replace('default:', '') for r in rules if r.startswith('default:')), None)
+
+ if (value := kw.get(key, '')) in ['null', 'undefined']:
value = ''
- for rule in rules:
- if rule == 'required' and not value:
- result['reason'].append(key + ' is ' + rule)
- elif rule == 'number' and value and not value.isdigit():
- result['reason'].append(key + ' must be ' + rule)
+ if 'required' in rules and not value:
+ result['reason'].append(f"{key} is required")
+ if 'number' in rules and value and not value.isdigit():
+ result['reason'].append(f"{key} must be a number")
result['query'][key] = value
+
if not value and default:
value = default
if is_number and value.isdigit():
value = int(value)
- if not value and not default:
- value = None
+
result['value'][alias] = value
- if len(result['reason']) > 0:
- result['valid'] = False
+ result['valid'] = not result['reason']
+
return result
def time_to_str(self, object, format):