def get_suggested_categories(user): """Gets the suggested categories of a user for the dashboard""" related = set(get_related_categories(user, detailed=False)) res = [] category_strategy = contains_eager('category') category_strategy.subqueryload('acl_entries') category_strategy.undefer('chain_titles') query = (user.suggested_categories.filter_by(is_ignored=False).join( SuggestedCategory.category).options(category_strategy)) for suggestion in query: category = suggestion.category if (category.is_deleted or category in related or category.suggestions_disabled or any(p.suggestions_disabled for p in category.parent_chain_query)): user.suggested_categories.remove(suggestion) continue if not category.can_access(user): continue res.append({ 'score': suggestion.score, 'categ': category, 'path': truncate_path(category.chain_titles[:-1], chars=50) }) return res
def _process(self): query = (Category.query .filter(Category.id.in_(c.id for c in self.user.favorite_categories)) .options(undefer('chain_titles'))) categories = sorted([(cat, truncate_path(cat.chain_titles[:-1], chars=50)) for cat in query], key=lambda c: (c[0].title, c[1])) return WPUser.render_template('favorites.html', 'favorites', user=self.user, favorite_categories=categories)
def get_suggested_categories(user): """Gets the suggested categories of a user for the dashboard""" from MaKaC.conference import CategoryManager if not redis_write_client: return [] related = user.favorite_categories | user.get_linked_objects('category', 'manager') res = [] for id_, score in suggestions.get_suggestions(user, 'category').iteritems(): try: categ = CategoryManager().getById(id_) except KeyError: suggestions.unsuggest(user, 'category', id_) continue if not categ or categ.isSuggestionsDisabled() or categ in related: continue if any(p.isSuggestionsDisabled() for p in categ.iterParents()): continue if not categ.canAccess(AccessWrapper(user.as_avatar)): continue res.append({ 'score': score, 'categ': categ, 'path': truncate_path(categ.getCategoryPathTitles()[:-1], chars=50) }) return res
def get_related_categories(user, detailed=True): """Gets the related categories of a user for the dashboard""" favorites = set() if user.favorite_categories: favorites = set( Category.query.filter( Category.id.in_(c.id for c in user.favorite_categories)).options( undefer('chain_titles')).all()) managed = set( Category.query.filter( Category.acl_entries.any( db.and_(CategoryPrincipal.type == PrincipalType.user, CategoryPrincipal.user == user, CategoryPrincipal.has_management_role())), ~Category.is_deleted).options(undefer('chain_titles'))) if not detailed: return favorites | managed res = {} for categ in favorites | managed: res[(categ.title, categ.id)] = { 'categ': categ, 'favorite': categ in favorites, 'managed': categ in managed, 'path': truncate_path(categ.chain_titles[:-1], chars=50) } return OrderedDict(sorted(res.items(), key=itemgetter(0)))
def get_suggested_categories(user): """Gets the suggested categories of a user for the dashboard""" related = set(get_related_categories(user, detailed=False)) res = [] category_strategy = contains_eager('category') category_strategy.subqueryload('acl_entries') category_strategy.undefer('chain_titles') query = (user.suggested_categories .filter_by(is_ignored=False) .join(SuggestedCategory.category) .options(category_strategy)) for suggestion in query: category = suggestion.category if (category.is_deleted or category in related or category.suggestions_disabled or any(p.suggestions_disabled for p in category.parent_chain_query)): user.suggested_categories.remove(suggestion) continue if not category.can_access(user): continue res.append({ 'score': suggestion.score, 'categ': category, 'path': truncate_path(category.chain_titles[:-1], chars=50) }) return res
def get_related_categories(user, detailed=True): """Gets the related categories of a user for the dashboard""" favorites = set() if user.favorite_categories: favorites = set(Category.query .filter(Category.id.in_(c.id for c in user.favorite_categories)) .options(undefer('chain_titles')) .all()) managed = set(Category.query .filter(Category.acl_entries.any(db.and_(CategoryPrincipal.type == PrincipalType.user, CategoryPrincipal.user == user, CategoryPrincipal.has_management_role())), ~Category.is_deleted) .options(undefer('chain_titles'))) if not detailed: return favorites | managed res = {} for categ in favorites | managed: res[(categ.title, categ.id)] = { 'categ': categ, 'favorite': categ in favorites, 'managed': categ in managed, 'path': truncate_path(categ.chain_titles[:-1], chars=50) } return OrderedDict(sorted(res.items(), key=itemgetter(0)))
def get_suggested_categories(user): """Gets the suggested categories of a user for the dashboard""" if not redis_write_client: return [] related = {cat.id for cat in get_related_categories(user, detailed=False)} res = [] categ_suggestions = suggestions.get_suggestions(user, 'category') query = (Category.query .filter(Category.id.in_(categ_suggestions), ~Category.id.in_(related), ~Category.is_deleted, ~Category.suggestions_disabled) .options(undefer('chain_titles'))) categories = {c.id: c for c in query} for id_, score in categ_suggestions.iteritems(): try: categ = categories[int(id_)] except KeyError: suggestions.unsuggest(user, 'category', id_) continue if any(p.suggestions_disabled for p in categ.parent_chain_query): suggestions.unsuggest(user, 'category', id_) continue if not categ.can_access(user): continue res.append({ 'score': score, 'categ': categ, 'path': truncate_path(categ.chain_titles[:-1], chars=50) }) return res
def _process(self): categories = sorted( [(cat, truncate_path(cat.getCategoryPathTitles()[:-1], chars=50)) for cat in self.user.favorite_categories], key=lambda c: (c[0].name, c[1])) return WPUser.render_template('favorites.html', user=self.user, favorite_categories=categories)
def get_related_categories(user): """Gets the related categories of a user for the dashboard""" favorites = user.favorite_categories managed = user.get_linked_objects('category', 'manager') res = {} for categ in favorites | managed: res[(categ.getTitle(), categ.getId())] = { 'categ': categ, 'favorite': categ in favorites, 'managed': categ in managed, 'path': truncate_path(categ.getCategoryPathTitles()[:-1], chars=50) } return OrderedDict(sorted(res.items(), key=itemgetter(0)))
def _process(self): categories = sorted([(cat, truncate_path(cat.getCategoryPathTitles()[:-1], chars=50)) for cat in self.user.favorite_categories], key=lambda c: (c[0].name, c[1])) return WPUser.render_template('favorites.html', user=self.user, favorite_categories=categories)
def test_truncate_path(full_path, chars, skip_first, expected): assert truncate_path(full_path, chars=chars, skip_first=skip_first) == expected