##// END OF EJS Templates
fix(caching): fixed problems with Cache query for users....
super-admin -
r5365:ae8a165b default
parent child Browse files
Show More
@@ -40,7 +40,7 b' from rhodecode.lib.utils2 import safe_in'
40 from rhodecode.model.auth_token import AuthTokenModel
40 from rhodecode.model.auth_token import AuthTokenModel
41 from rhodecode.model.comment import CommentsModel
41 from rhodecode.model.comment import CommentsModel
42 from rhodecode.model.db import (
42 from rhodecode.model.db import (
43 IntegrityError, or_, in_filter_generator,
43 IntegrityError, or_, in_filter_generator, select,
44 Repository, UserEmailMap, UserApiKeys, UserFollowing,
44 Repository, UserEmailMap, UserApiKeys, UserFollowing,
45 PullRequest, UserBookmark, RepoGroup, ChangesetStatus)
45 PullRequest, UserBookmark, RepoGroup, ChangesetStatus)
46 from rhodecode.model.meta import Session
46 from rhodecode.model.meta import Session
@@ -511,8 +511,15 b' class MyAccountView(BaseAppView, DataGri'
511 def my_account_bookmarks(self):
511 def my_account_bookmarks(self):
512 c = self.load_default_context()
512 c = self.load_default_context()
513 c.active = 'bookmarks'
513 c.active = 'bookmarks'
514 c.bookmark_items = UserBookmark.get_bookmarks_for_user(
514
515 self._rhodecode_db_user.user_id, cache=False)
515 user_bookmarks = \
516 select(UserBookmark, Repository, RepoGroup) \
517 .where(UserBookmark.user_id == self._rhodecode_user.user_id) \
518 .outerjoin(Repository, Repository.repo_id == UserBookmark.bookmark_repo_id) \
519 .outerjoin(RepoGroup, RepoGroup.group_id == UserBookmark.bookmark_repo_group_id) \
520 .order_by(UserBookmark.position.asc())
521
522 c.user_bookmark_items = Session().execute(user_bookmarks).all()
516 return self._get_template_context(c)
523 return self._get_template_context(c)
517
524
518 def _process_bookmark_entry(self, entry, user_id):
525 def _process_bookmark_entry(self, entry, user_id):
@@ -885,17 +885,6 b' class User(Base, BaseModel):'
885 return new_recovery_codes
885 return new_recovery_codes
886
886
887 @classmethod
887 @classmethod
888 def get(cls, user_id, cache=False):
889 if not user_id:
890 return
891
892 user = cls.query()
893 if cache:
894 user = user.options(
895 FromCache("sql_cache_short", f"get_users_{user_id}"))
896 return user.get(user_id)
897
898 @classmethod
899 def extra_valid_auth_tokens(cls, user, role=None):
888 def extra_valid_auth_tokens(cls, user, role=None):
900 tokens = UserApiKeys.query().filter(UserApiKeys.user == user)\
889 tokens = UserApiKeys.query().filter(UserApiKeys.user == user)\
901 .filter(or_(UserApiKeys.expires == -1,
890 .filter(or_(UserApiKeys.expires == -1,
@@ -1021,13 +1010,24 b' class User(Base, BaseModel):'
1021 @user_data.setter
1010 @user_data.setter
1022 def user_data(self, val):
1011 def user_data(self, val):
1023 if not isinstance(val, dict):
1012 if not isinstance(val, dict):
1024 raise Exception('user_data must be dict, got %s' % type(val))
1013 raise Exception(f'user_data must be dict, got {type(val)}')
1025 try:
1014 try:
1026 self._user_data = safe_bytes(json.dumps(val))
1015 self._user_data = safe_bytes(json.dumps(val))
1027 except Exception:
1016 except Exception:
1028 log.error(traceback.format_exc())
1017 log.error(traceback.format_exc())
1029
1018
1030 @classmethod
1019 @classmethod
1020 def get(cls, user_id, cache=False):
1021 if not user_id:
1022 return
1023
1024 user = cls.query()
1025 if cache:
1026 user = user.options(
1027 FromCache("sql_cache_short", f"get_users_{user_id}"))
1028 return user.get(user_id)
1029
1030 @classmethod
1031 def get_by_username(cls, username, case_insensitive=False,
1031 def get_by_username(cls, username, case_insensitive=False,
1032 cache=False):
1032 cache=False):
1033
1033
@@ -1611,7 +1611,7 b' class UserGroup(Base, BaseModel):'
1611 user_group = cls.query()
1611 user_group = cls.query()
1612 if cache:
1612 if cache:
1613 user_group = user_group.options(
1613 user_group = user_group.options(
1614 FromCache("sql_cache_short", "get_users_group_%s" % user_group_id))
1614 FromCache("sql_cache_short", f"get_users_group_{user_group_id}"))
1615 return user_group.get(user_group_id)
1615 return user_group.get(user_group_id)
1616
1616
1617 def permissions(self, with_admins=True, with_owner=True,
1617 def permissions(self, with_admins=True, with_owner=True,
@@ -2005,7 +2005,7 b' class Repository(Base, BaseModel):'
2005 if val:
2005 if val:
2006 return val
2006 return val
2007 else:
2007 else:
2008 cache_key = "get_repo_by_name_%s" % _hash_key(repo_name)
2008 cache_key = f"get_repo_by_name_{_hash_key(repo_name)}"
2009 q = q.options(
2009 q = q.options(
2010 FromCache("sql_cache_short", cache_key))
2010 FromCache("sql_cache_short", cache_key))
2011
2011
@@ -5643,18 +5643,23 b' class UserBookmark(Base, BaseModel):'
5643
5643
5644 @classmethod
5644 @classmethod
5645 def get_bookmarks_for_user(cls, user_id, cache=True):
5645 def get_bookmarks_for_user(cls, user_id, cache=True):
5646 bookmarks = cls.query() \
5646 bookmarks = select(
5647 .filter(UserBookmark.user_id == user_id) \
5647 UserBookmark.title,
5648 .options(joinedload(UserBookmark.repository)) \
5648 UserBookmark.position,
5649 .options(joinedload(UserBookmark.repository_group)) \
5649 ) \
5650 .add_columns(Repository.repo_id, Repository.repo_type, Repository.repo_name) \
5651 .add_columns(RepoGroup.group_id, RepoGroup.group_name) \
5652 .where(UserBookmark.user_id == user_id) \
5653 .outerjoin(Repository, Repository.repo_id == UserBookmark.bookmark_repo_id) \
5654 .outerjoin(RepoGroup, RepoGroup.group_id == UserBookmark.bookmark_repo_group_id) \
5650 .order_by(UserBookmark.position.asc())
5655 .order_by(UserBookmark.position.asc())
5651
5656
5652 if cache:
5657 if cache:
5653 bookmarks = bookmarks.options(
5658 bookmarks = bookmarks.options(
5654 FromCache("sql_cache_short", "get_user_{}_bookmarks".format(user_id))
5659 FromCache("sql_cache_short", f"get_user_{user_id}_bookmarks")
5655 )
5660 )
5656
5661
5657 return bookmarks.all()
5662 return Session().execute(bookmarks).all()
5658
5663
5659 def __repr__(self):
5664 def __repr__(self):
5660 return f'<UserBookmark({self.position} @ {self.redirect_url!r})>'
5665 return f'<UserBookmark({self.position} @ {self.redirect_url!r})>'
@@ -114,6 +114,7 b' class UserModel(BaseModel):'
114 else:
114 else:
115 user = self.sa.query(User)\
115 user = self.sa.query(User)\
116 .filter(User.username == username)
116 .filter(User.username == username)
117
117 if cache:
118 if cache:
118 name_key = _hash_key(username)
119 name_key = _hash_key(username)
119 user = user.options(
120 user = user.options(
@@ -765,25 +766,29 b' class UserModel(BaseModel):'
765 'AuthUser: fill data execution based on: '
766 'AuthUser: fill data execution based on: '
766 'user_id:%s api_key:%s username:%s', user_id, api_key, username)
767 'user_id:%s api_key:%s username:%s', user_id, api_key, username)
767 try:
768 try:
769 found_with = ''
768 dbuser = None
770 dbuser = None
769 if user_id:
771 if user_id:
770 dbuser = self.get(user_id)
772 dbuser = self.get(user_id)
773 found_with = 'user_id'
771 elif api_key:
774 elif api_key:
772 dbuser = self.get_by_auth_token(api_key)
775 dbuser = self.get_by_auth_token(api_key)
776 found_with = 'auth_token'
773 elif username:
777 elif username:
774 dbuser = self.get_by_username(username)
778 dbuser = self.get_by_username(username)
779 found_with = 'username'
775
780
776 if not dbuser:
781 if not dbuser:
777 log.warning(
782 log.warning(
778 'Unable to lookup user by id:%s api_key:%s username:%s',
783 'Unable to lookup user by id:%s api_key:%s username:%s, found with: %s',
779 user_id, token_obfuscate(api_key), username)
784 user_id, token_obfuscate(api_key), username, found_with)
780 return False
785 return False
781 if not dbuser.active:
786 if not dbuser.active:
782 log.debug('User `%s:%s` is inactive, skipping fill data',
787 log.debug('User `%s:%s` is inactive, skipping fill data',
783 username, user_id)
788 username, user_id)
784 return False
789 return False
785
790
786 log.debug('AuthUser: filling found user:%s data', dbuser)
791 log.debug('AuthUser: filling found user:%s data, found with: %s', dbuser, found_with)
787
792
788 attrs = {
793 attrs = {
789 'user_id': dbuser.user_id,
794 'user_id': dbuser.user_id,
@@ -83,14 +83,14 b''
83 <table class="rctable">
83 <table class="rctable">
84 ## generate always 10 entries
84 ## generate always 10 entries
85 <input type="hidden" name="__start__" value="bookmarks:sequence"/>
85 <input type="hidden" name="__start__" value="bookmarks:sequence"/>
86 % for item in (c.bookmark_items + [None for i in range(10)])[:10]:
86 % for item in (c.user_bookmark_items + [None for i in range(10)])[:10]:
87 <input type="hidden" name="__start__" value="bookmark:mapping"/>
87 <input type="hidden" name="__start__" value="bookmark:mapping"/>
88 % if item is None:
88 % if item is None:
89 ## empty placehodlder
89 ## empty placehodlder
90 ${form_item()}
90 ${form_item()}
91 % else:
91 % else:
92 ## actual entry
92 ## actual entry
93 ${form_item(position=item.position, title=item.title, redirect_url=item.redirect_url, repo=item.repository, repo_group=item.repository_group)}
93 ${form_item(position=item[0].position, title=item[0].title, redirect_url=item[0].redirect_url, repo=item[1], repo_group=item[2])}
94 % endif
94 % endif
95 <input type="hidden" name="__end__" value="bookmark:mapping"/>
95 <input type="hidden" name="__end__" value="bookmark:mapping"/>
96 % endfor
96 % endfor
@@ -651,26 +651,26 b''
651 % endif
651 % endif
652 % for item in c.bookmark_items:
652 % for item in c.bookmark_items:
653 <li>
653 <li>
654 % if item.repository:
654 % if item.repo_id:
655 <div>
655 <div>
656 <a class="bookmark-item" href="${h.route_path('my_account_goto_bookmark', bookmark_id=item.position)}">
656 <a class="bookmark-item" href="${h.route_path('my_account_goto_bookmark', bookmark_id=item.position)}">
657 <code>${item.position}</code>
657 <code>${item.position}</code>
658 % if item.repository.repo_type == 'hg':
658 % if item.repo_type == 'hg':
659 <i class="icon-hg" title="${_('Repository')}" style="font-size: 16px"></i>
659 <i class="icon-hg" title="${_('Repository')}" style="font-size: 16px"></i>
660 % elif item.repository.repo_type == 'git':
660 % elif item.repo_type == 'git':
661 <i class="icon-git" title="${_('Repository')}" style="font-size: 16px"></i>
661 <i class="icon-git" title="${_('Repository')}" style="font-size: 16px"></i>
662 % elif item.repository.repo_type == 'svn':
662 % elif item.repo_type == 'svn':
663 <i class="icon-svn" title="${_('Repository')}" style="font-size: 16px"></i>
663 <i class="icon-svn" title="${_('Repository')}" style="font-size: 16px"></i>
664 % endif
664 % endif
665 ${(item.title or h.shorter(item.repository.repo_name, 30))}
665 ${(item.title or h.shorter(item.repo_name, 30))}
666 </a>
666 </a>
667 </div>
667 </div>
668 % elif item.repository_group:
668 % elif item.group_id:
669 <div>
669 <div>
670 <a class="bookmark-item" href="${h.route_path('my_account_goto_bookmark', bookmark_id=item.position)}">
670 <a class="bookmark-item" href="${h.route_path('my_account_goto_bookmark', bookmark_id=item.position)}">
671 <code>${item.position}</code>
671 <code>${item.position}</code>
672 <i class="icon-repo-group" title="${_('Repository group')}" style="font-size: 14px"></i>
672 <i class="icon-repo-group" title="${_('Repository group')}" style="font-size: 14px"></i>
673 ${(item.title or h.shorter(item.repository_group.group_name, 30))}
673 ${(item.title or h.shorter(item.group_name, 30))}
674 </a>
674 </a>
675 </div>
675 </div>
676 % else:
676 % else:
General Comments 0
You need to be logged in to leave comments. Login now