##// END OF EJS Templates
fix(db-calls): fixed issues with caches calculations when passed search fields were empty
super-admin -
r5645:0d5acb53 default
parent child Browse files
Show More
@@ -35,7 +35,7 b' import collections'
35 35
36 36 import pyotp
37 37 from sqlalchemy import (
38 or_, and_, not_, func, cast, TypeDecorator, event, select,
38 or_, and_, not_, func, cast, TypeDecorator, event, select, delete,
39 39 true, false, null, union_all,
40 40 Index, Sequence, UniqueConstraint, ForeignKey, CheckConstraint, Column,
41 41 Boolean, String, Unicode, UnicodeText, DateTime, Integer, LargeBinary,
@@ -274,6 +274,22 b' class BaseModel(object):'
274 274 return stmt
275 275
276 276 @classmethod
277 def delete(cls, custom_cls=None):
278 """
279 stmt = cls.delete().where(cls.user_id==1)
280 # optionally
281 stmt = cls.delete(User).where(cls.user_id==1)
282 result = cls.execute(stmt)
283 """
284
285 if custom_cls:
286 stmt = delete(custom_cls)
287 else:
288 stmt = delete(cls)
289 return stmt
290
291
292 @classmethod
277 293 def execute(cls, stmt):
278 294 return Session().execute(stmt)
279 295
@@ -1075,28 +1091,26 b' class User(Base, BaseModel):'
1075 1091 @classmethod
1076 1092 def get(cls, user_id, cache=False):
1077 1093 if not user_id:
1078 return
1094 return None
1079 1095
1080 1096 q = cls.select().where(cls.user_id == user_id)
1081 1097 if cache:
1082 q = q.options(
1083 FromCache("sql_cache_short", f"get_users_{user_id}"))
1098 q = q.options(FromCache("sql_cache_short", f"get_users_{user_id}"))
1084 1099 return cls.execute(q).scalar_one_or_none()
1085 1100
1086 1101 @classmethod
1087 def get_by_username(cls, username, case_insensitive=False,
1088 cache=False):
1102 def get_by_username(cls, username, case_insensitive=False, cache=False):
1103 if not username:
1104 return None
1089 1105
1090 1106 if case_insensitive:
1091 q = cls.select().where(
1092 func.lower(cls.username) == func.lower(username))
1107 q = cls.select().where(func.lower(cls.username) == func.lower(username))
1093 1108 else:
1094 1109 q = cls.select().where(cls.username == username)
1095 1110
1096 1111 if cache:
1097 1112 hash_key = _hash_key(username)
1098 q = q.options(
1099 FromCache("sql_cache_short", f"get_user_by_name_{hash_key}"))
1113 q = q.options(FromCache("sql_cache_short", f"get_user_by_name_{hash_key}"))
1100 1114
1101 1115 return cls.execute(q).scalar_one_or_none()
1102 1116
@@ -1125,6 +1139,8 b' class User(Base, BaseModel):'
1125 1139
1126 1140 @classmethod
1127 1141 def get_by_email(cls, email, case_insensitive=False, cache=False):
1142 if not email:
1143 return None
1128 1144
1129 1145 if case_insensitive:
1130 1146 q = cls.select().where(func.lower(cls.email) == func.lower(email))
@@ -1133,8 +1149,7 b' class User(Base, BaseModel):'
1133 1149
1134 1150 if cache:
1135 1151 email_key = _hash_key(email)
1136 q = q.options(
1137 FromCache("sql_cache_short", f"get_email_key_{email_key}"))
1152 q = q.options(FromCache("sql_cache_short", f"get_email_key_{email_key}"))
1138 1153
1139 1154 ret = cls.execute(q).scalar_one_or_none()
1140 1155
@@ -1147,8 +1162,8 b' class User(Base, BaseModel):'
1147 1162 q = q.where(UserEmailMap.email == email)
1148 1163 q = q.options(joinedload(UserEmailMap.user))
1149 1164 if cache:
1150 q = q.options(
1151 FromCache("sql_cache_short", f"get_email_map_key_{email_key}"))
1165 email_key = _hash_key(email)
1166 q = q.options(FromCache("sql_cache_short", f"get_email_map_key_{email_key}"))
1152 1167
1153 1168 result = cls.execute(q).scalar_one_or_none()
1154 1169 ret = getattr(result, 'user', None)
@@ -1642,11 +1657,12 b' class UserGroup(Base, BaseModel):'
1642 1657 return f"<{self.cls_name}('id:{self.users_group_id}:{self.users_group_name}')>"
1643 1658
1644 1659 @classmethod
1645 def get_by_group_name(cls, group_name, cache=False,
1646 case_insensitive=False):
1660 def get_by_group_name(cls, group_name, cache=False, case_insensitive=False):
1661 if not group_name:
1662 return None
1663
1647 1664 if case_insensitive:
1648 q = cls.query().filter(func.lower(cls.users_group_name) ==
1649 func.lower(group_name))
1665 q = cls.query().filter(func.lower(cls.users_group_name) == func.lower(group_name))
1650 1666
1651 1667 else:
1652 1668 q = cls.query().filter(cls.users_group_name == group_name)
@@ -2968,9 +2984,11 b' class RepoGroup(Base, BaseModel):'
2968 2984
2969 2985 @classmethod
2970 2986 def get_by_group_name(cls, group_name, cache=False, case_insensitive=False):
2987 if not group_name:
2988 return None
2989
2971 2990 if case_insensitive:
2972 gr = cls.query().filter(func.lower(cls.group_name)
2973 == func.lower(group_name))
2991 gr = cls.query().filter(func.lower(cls.group_name) == func.lower(group_name))
2974 2992 else:
2975 2993 gr = cls.query().filter(cls.group_name == group_name)
2976 2994 if cache:
General Comments 0
You need to be logged in to leave comments. Login now