##// 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 40 from rhodecode.model.auth_token import AuthTokenModel
41 41 from rhodecode.model.comment import CommentsModel
42 42 from rhodecode.model.db import (
43 IntegrityError, or_, in_filter_generator,
43 IntegrityError, or_, in_filter_generator, select,
44 44 Repository, UserEmailMap, UserApiKeys, UserFollowing,
45 45 PullRequest, UserBookmark, RepoGroup, ChangesetStatus)
46 46 from rhodecode.model.meta import Session
@@ -511,8 +511,15 b' class MyAccountView(BaseAppView, DataGri'
511 511 def my_account_bookmarks(self):
512 512 c = self.load_default_context()
513 513 c.active = 'bookmarks'
514 c.bookmark_items = UserBookmark.get_bookmarks_for_user(
515 self._rhodecode_db_user.user_id, cache=False)
514
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 523 return self._get_template_context(c)
517 524
518 525 def _process_bookmark_entry(self, entry, user_id):
@@ -885,17 +885,6 b' class User(Base, BaseModel):'
885 885 return new_recovery_codes
886 886
887 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 888 def extra_valid_auth_tokens(cls, user, role=None):
900 889 tokens = UserApiKeys.query().filter(UserApiKeys.user == user)\
901 890 .filter(or_(UserApiKeys.expires == -1,
@@ -1021,13 +1010,24 b' class User(Base, BaseModel):'
1021 1010 @user_data.setter
1022 1011 def user_data(self, val):
1023 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 1014 try:
1026 1015 self._user_data = safe_bytes(json.dumps(val))
1027 1016 except Exception:
1028 1017 log.error(traceback.format_exc())
1029 1018
1030 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 1031 def get_by_username(cls, username, case_insensitive=False,
1032 1032 cache=False):
1033 1033
@@ -1611,7 +1611,7 b' class UserGroup(Base, BaseModel):'
1611 1611 user_group = cls.query()
1612 1612 if cache:
1613 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 1615 return user_group.get(user_group_id)
1616 1616
1617 1617 def permissions(self, with_admins=True, with_owner=True,
@@ -2005,7 +2005,7 b' class Repository(Base, BaseModel):'
2005 2005 if val:
2006 2006 return val
2007 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 2009 q = q.options(
2010 2010 FromCache("sql_cache_short", cache_key))
2011 2011
@@ -5643,18 +5643,23 b' class UserBookmark(Base, BaseModel):'
5643 5643
5644 5644 @classmethod
5645 5645 def get_bookmarks_for_user(cls, user_id, cache=True):
5646 bookmarks = cls.query() \
5647 .filter(UserBookmark.user_id == user_id) \
5648 .options(joinedload(UserBookmark.repository)) \
5649 .options(joinedload(UserBookmark.repository_group)) \
5646 bookmarks = select(
5647 UserBookmark.title,
5648 UserBookmark.position,
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 5655 .order_by(UserBookmark.position.asc())
5651 5656
5652 5657 if cache:
5653 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 5664 def __repr__(self):
5660 5665 return f'<UserBookmark({self.position} @ {self.redirect_url!r})>'
@@ -114,6 +114,7 b' class UserModel(BaseModel):'
114 114 else:
115 115 user = self.sa.query(User)\
116 116 .filter(User.username == username)
117
117 118 if cache:
118 119 name_key = _hash_key(username)
119 120 user = user.options(
@@ -765,25 +766,29 b' class UserModel(BaseModel):'
765 766 'AuthUser: fill data execution based on: '
766 767 'user_id:%s api_key:%s username:%s', user_id, api_key, username)
767 768 try:
769 found_with = ''
768 770 dbuser = None
769 771 if user_id:
770 772 dbuser = self.get(user_id)
773 found_with = 'user_id'
771 774 elif api_key:
772 775 dbuser = self.get_by_auth_token(api_key)
776 found_with = 'auth_token'
773 777 elif username:
774 778 dbuser = self.get_by_username(username)
779 found_with = 'username'
775 780
776 781 if not dbuser:
777 782 log.warning(
778 'Unable to lookup user by id:%s api_key:%s username:%s',
779 user_id, token_obfuscate(api_key), username)
783 'Unable to lookup user by id:%s api_key:%s username:%s, found with: %s',
784 user_id, token_obfuscate(api_key), username, found_with)
780 785 return False
781 786 if not dbuser.active:
782 787 log.debug('User `%s:%s` is inactive, skipping fill data',
783 788 username, user_id)
784 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 793 attrs = {
789 794 'user_id': dbuser.user_id,
@@ -83,14 +83,14 b''
83 83 <table class="rctable">
84 84 ## generate always 10 entries
85 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 87 <input type="hidden" name="__start__" value="bookmark:mapping"/>
88 88 % if item is None:
89 89 ## empty placehodlder
90 90 ${form_item()}
91 91 % else:
92 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 94 % endif
95 95 <input type="hidden" name="__end__" value="bookmark:mapping"/>
96 96 % endfor
@@ -651,26 +651,26 b''
651 651 % endif
652 652 % for item in c.bookmark_items:
653 653 <li>
654 % if item.repository:
654 % if item.repo_id:
655 655 <div>
656 656 <a class="bookmark-item" href="${h.route_path('my_account_goto_bookmark', bookmark_id=item.position)}">
657 657 <code>${item.position}</code>
658 % if item.repository.repo_type == 'hg':
658 % if item.repo_type == 'hg':
659 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 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 663 <i class="icon-svn" title="${_('Repository')}" style="font-size: 16px"></i>
664 664 % endif
665 ${(item.title or h.shorter(item.repository.repo_name, 30))}
665 ${(item.title or h.shorter(item.repo_name, 30))}
666 666 </a>
667 667 </div>
668 % elif item.repository_group:
668 % elif item.group_id:
669 669 <div>
670 670 <a class="bookmark-item" href="${h.route_path('my_account_goto_bookmark', bookmark_id=item.position)}">
671 671 <code>${item.position}</code>
672 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 674 </a>
675 675 </div>
676 676 % else:
General Comments 0
You need to be logged in to leave comments. Login now