server/tags: replace ILIKE expressions

While it makes sense to use ILIKE in search configs to expose side
effects regarding _ and % for power users to use, it certainly didn't
make sense in places changed by this commit. For example, visiting
/tag/___ led to internal server error.
This commit is contained in:
rr- 2016-06-18 11:27:08 +02:00
parent 3c3d0dbb8d
commit 4ec826d0a5
2 changed files with 6 additions and 5 deletions

View File

@ -1,4 +1,5 @@
import re import re
import sqlalchemy
from szurubooru import config, db, errors from szurubooru import config, db, errors
from szurubooru.func import util, snapshots, cache from szurubooru.func import util, snapshots, cache
@ -37,7 +38,7 @@ def create_category(name, color):
def update_category_name(category, name): def update_category_name(category, name):
if not name: if not name:
raise InvalidTagCategoryNameError('Name cannot be empty.') raise InvalidTagCategoryNameError('Name cannot be empty.')
expr = db.TagCategory.name.ilike(name) expr = sqlalchemy.func.lower(db.TagCategory.name) == name.lower()
if category.tag_category_id: if category.tag_category_id:
expr = expr & (db.TagCategory.tag_category_id != category.tag_category_id) expr = expr & (db.TagCategory.tag_category_id != category.tag_category_id)
already_exists = db.session.query(db.TagCategory).filter(expr).count() > 0 already_exists = db.session.query(db.TagCategory).filter(expr).count() > 0
@ -61,7 +62,7 @@ def update_category_color(category, color):
def try_get_category_by_name(name): def try_get_category_by_name(name):
return db.session \ return db.session \
.query(db.TagCategory) \ .query(db.TagCategory) \
.filter(db.TagCategory.name.ilike(name)) \ .filter(sqlalchemy.func.lower(db.TagCategory.name) == name.lower()) \
.one_or_none() .one_or_none()
def get_category_by_name(name): def get_category_by_name(name):

View File

@ -90,7 +90,7 @@ def try_get_tag_by_name(name):
return db.session \ return db.session \
.query(db.Tag) \ .query(db.Tag) \
.join(db.TagName) \ .join(db.TagName) \
.filter(db.TagName.name.ilike(name)) \ .filter(sqlalchemy.func.lower(db.TagName.name) == name.lower()) \
.one_or_none() .one_or_none()
def get_tag_by_name(name): def get_tag_by_name(name):
@ -105,7 +105,7 @@ def get_tags_by_names(names):
return [] return []
expr = sqlalchemy.sql.false() expr = sqlalchemy.sql.false()
for name in names: for name in names:
expr = expr | db.TagName.name.ilike(name) expr = expr | (sqlalchemy.func.lower(db.TagName.name) == name.lower())
return db.session.query(db.Tag).join(db.TagName).filter(expr).all() return db.session.query(db.Tag).join(db.TagName).filter(expr).all()
def get_or_create_tags_by_names(names): def get_or_create_tags_by_names(names):
@ -192,7 +192,7 @@ def update_tag_names(tag, names):
for name in names: for name in names:
if util.value_exceeds_column_size(name, db.TagName.name): if util.value_exceeds_column_size(name, db.TagName.name):
raise InvalidTagNameError('Name is too long.') raise InvalidTagNameError('Name is too long.')
expr = expr | db.TagName.name.ilike(name) expr = expr | (sqlalchemy.func.lower(db.TagName.name) == name.lower())
if tag.tag_id: if tag.tag_id:
expr = expr & (db.TagName.tag_id != tag.tag_id) expr = expr & (db.TagName.tag_id != tag.tag_id)
existing_tags = db.session.query(db.TagName).filter(expr).all() existing_tags = db.session.query(db.TagName).filter(expr).all()