From d934864e5355655aadfec1a682bcce26a852560d Mon Sep 17 00:00:00 2001 From: Evan Blaudy Date: Thu, 24 Aug 2023 16:38:52 +0200 Subject: [PATCH] [qa][crud] fix casting of boolean values --- zou/app/blueprints/crud/base.py | 7 +++---- zou/app/models/person.py | 4 ++-- zou/app/utils/query.py | 10 +++++++++- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/zou/app/blueprints/crud/base.py b/zou/app/blueprints/crud/base.py index aeccf81f81..5d351d0ffd 100644 --- a/zou/app/blueprints/crud/base.py +++ b/zou/app/blueprints/crud/base.py @@ -7,10 +7,9 @@ from flask_jwt_extended import jwt_required from sqlalchemy.exc import IntegrityError, StatementError -from sqlalchemy import func from zou.app.mixin import ArgsMixin -from zou.app.utils import events, fields, permissions +from zou.app.utils import events, fields, permissions, query from zou.app.services.exception import ( ArgumentsException, WrongParameterException, @@ -86,13 +85,13 @@ def build_filters(self, options): in_filter.append( field_key.in_( [ - func.cast(value, field_key.type) + query.cast_value(value, field_key) for value in value_array ] ) ) else: - filters[key] = func.cast(value, field_key.type) + filters[key] = query.cast_value(value, field_key) return (many_join_filter, in_filter, name_filter, filters) diff --git a/zou/app/models/person.py b/zou/app/models/person.py index a0c2e22955..1245354224 100644 --- a/zou/app/models/person.py +++ b/zou/app/models/person.py @@ -94,10 +94,10 @@ class Person(db.Model, BaseMixin, SerializerMixin): ldap_uid = db.Column(db.String(60), unique=True, default=None) def __repr__(self): - return "" % self.full_name() + return f"" def full_name(self): - return "%s %s" % (self.first_name, self.last_name) + return f"{self.first_name} {self.last_name}" def fido_devices(self): if self.fido_credentials is None: diff --git a/zou/app/utils/query.py b/zou/app/utils/query.py index 196e36740c..34421050ee 100644 --- a/zou/app/utils/query.py +++ b/zou/app/utils/query.py @@ -1,7 +1,8 @@ import math from zou.app import app -from zou.app.utils import fields +from zou.app.utils import fields, string +from sqlalchemy import func def get_query_criterions_from_request(request): @@ -77,3 +78,10 @@ def apply_sort_by(model, query, sort_by): else: sort_field = model.updated_at.desc() return query.order_by(sort_field) + + +def cast_value(value, field_key): + if field_key.type.python_type is bool: + return string.strtobool(value) + else: + return func.cast(value, field_key.type)