diff options
-rw-r--r-- | routes/heroes.py | 7 | ||||
-rw-r--r-- | utils/strings.py | 25 |
2 files changed, 29 insertions, 3 deletions
diff --git a/routes/heroes.py b/routes/heroes.py index 9b631a5..ab1b0ba 100644 --- a/routes/heroes.py +++ b/routes/heroes.py @@ -36,6 +36,7 @@ from utils.errors import ( not_data_found ) from utils.decorators import superuser +from utils.strings import capitalize_each from bson.objectid import ObjectId heroes = Blueprint("heroes", __name__) @@ -86,7 +87,7 @@ def create_heroe(): url=request.json['image']['url'] ) heroe = HeroeModel( - name=request.json['name'].capitalize(), + name=capitalize_each(request.json['name']), powerstats=powerstats, biography=biography, appearance=appearance, @@ -318,7 +319,7 @@ def get_image_heroe(id): def get_some_heroe_name(name): """Search heroe by name""" try: - name = name.capitalize() + name = capitalize_each(name) heroe = HeroeModel.objects.filter(name=name) if len(heroe) > 0: data = heroe.exclude('added_by').to_json() @@ -407,7 +408,7 @@ def put_heroe(id): url=request.json['image']['url'] ) heroe.update( - name=request.json['name'].capitalize(), + name=capitalize_each(request.json['name']), powerstats=powerstats, biography=biography, appearance=appearance, diff --git a/utils/strings.py b/utils/strings.py new file mode 100644 index 0000000..e28912b --- /dev/null +++ b/utils/strings.py @@ -0,0 +1,25 @@ +""" +Capitalize each Word in a String +""" + + +def capitalize_each(a): + textLen = len(a) + for i in range(textLen): + ch = a[i] + if i == 0: + ascVal = ord(ch) + if ascVal >= 97 and ascVal <= 122: + ascVal = ascVal-32 + ascVal = chr(ascVal) + index = 0 + a = a[:index] + ascVal + a[index+1:] + if ch == " ": + ch = a[i+1] + ascVal = ord(ch) + if ascVal >= 97 and ascVal <= 122: + ascVal = ascVal-32 + ascVal = chr(ascVal) + index = i+1 + a = a[:index] + ascVal + a[index+1:] + return a |