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