def create_group(**kwargs): name = kwargs.pop("name", str(random.random()).replace(".", "_")) description = kwargs.pop("description", str(random.random())) x = Category(name=name, description=description, **kwargs) x.save() return x
def create_group(**kwargs): name = kwargs.pop('name', str(random.random()).replace('.', '_')) description = kwargs.pop('description', str(random.random())) x = Category(name=name, description=description, **kwargs) x.save() return x
def test_disabled_group(self): group = create_group() self.assertFalse(isinstance(Category.get(group.name), DisabledCategory)) group.visibility = Visibility.DISABLED group.save() self.assertTrue(isinstance(Category.get(group.name), DisabledCategory)) group.visibility = Visibility.CURATED group.save() self.assertFalse(isinstance(Category.get(group.name), DisabledCategory))
def test_top_curation(self): group = create_group() for x in range(3): user = create_user() FollowCategory(user=user, category=group).save() group.details.force() self.assertTrue(group.name in [top['name'] for top in Category.get_top_details()], repr(Category.get_top_details())) group.visibility = Visibility.CURATED group.save() self.assertFalse(group.name in [top['name'] for top in Category.get_top_details()])
def test_top_curation(self): group = create_group() for x in range(3): user = create_user() FollowCategory(user=user, category=group).save() group.details.force() self.assertTrue( group.name in [top['name'] for top in Category.get_top_details()], repr(Category.get_top_details())) group.visibility = Visibility.CURATED group.save() self.assertFalse( group.name in [top['name'] for top in Category.get_top_details()])
def nav_aware(url): default_category = Category.get_default(request.user).details() # If we don't have a nav_category (user / about page) it should be the default for this user. _nav_category = nav_category or default_category if _nav_category['name'] != default_category['name']: url += "?nav=%s" % _nav_category['name'] return url
def api_browse_monsters(request, payload=None): """ accepts posted json in the following format {'offset': 0, 'count': 9} returns client sanitized comment details """ if not payload: payload = {'offset':0, 'count':9} offset = payload['offset'] count = 9 category = Category.get(name=MONSTER_GROUP) sort = 'new' nav = browse.Navigation.load_json_or_404( payload, sort=sort, category=category, mobile=request.is_mobile, replies_only=True, public_only=True, offset=payload['offset'], count=payload['count'], ) data = { 'monsters': mobile_details_from_queryset(browse.get_front_comments(request.user, nav)), } return data
def order_by_time_plus_log_stickers_and_replies(): comments = list( Comment.public .filter(timestamp__gte=time.time() - 24*60*60) .filter(category__in=Category.get_whitelisted()) .exclude(reply_content=None) ) return sorted(comments, key=lambda comment: _log_key(comment, s=1, r=1))
def __init__(self, params): if isinstance(params, basestring): self.group = params self.skip = 0 else: self.group = params['group'] self.skip = params['skip'] group = Category.get(self.group) if group is None: raise Http404 self.posts = self.public_posts()
def _get_from_redis(viewer, nav): manager = Comment.browse_objects if nav.sort == 'pinned': if not viewer.is_authenticated(): raise PermissionDenied() rlbb = viewer.redis.pinned_bump_buffer else: # The 'curated' visibility is used to curate the sections of everything. if nav.category == Category.ALL: manager = Comment.curated_browse_objects if nav.category == Category.MY: if not viewer.is_authenticated(): raise PermissionDenied() following = Category.objects.filter(followers__user=viewer) rlbb = _get_aggregate_rlbb(following, nav) elif not viewer.is_authenticated() and nav.category == Category.ALL: # Implicitly whitelisted groups rlbb = _get_aggregate_rlbb(Category.objects.in_bulk_list(Category.get_whitelisted()), nav) else: rlbb = _get_buffer(nav.category, nav) if nav.sort != 'hot': ids = rlbb[nav.slice] elif nav.hot_sort_type: ids = frontpage_algorithms.get_slice(nav.hot_sort_type, nav.slice) else: ids = _get_hot_slice_by_threads(rlbb, nav.slice) qs = manager if nav.sort not in ['active']: qs = qs.exclude(reply_content=None) # Remove user-hidden comments. ids = remove_hidden_comment_ids(viewer, ids) return qs.in_bulk_list(ids)
def landing(request, **kwargs): category = Category.get(name=MONSTER_GROUP) sort = 'new' kwargs['offset'] = request.GET.get('offset', 0) show_pins = False nav = browse.Navigation.load_json_or_404( kwargs, sort=sort, category=category, mobile=request.is_mobile, replies_only=True, public_only=True, ) front_data = { 'tiles': browse.get_browse_tiles(request.user, nav), 'render_options': tile_render_options(sort, show_pins), } # Overrides the default nav category that gets set in a context processor. request.nav_category = category sort_types = [] if sort in ['active', 'new']: sort_types.extend([ ('active threads', '/x/%s/active' % category.name), ('new posts', '/x/%s/new' % category.name) ]) active_sort_url = '/x/%s/%s' % (category.name, sort) nav_data = nav.dump_json() front_data.update(locals()) front_data['nav_category'] = category.details() front_data['DOMAIN'] = settings.DOMAIN return r2r_jinja('monster/landing.html', front_data)
def load_json(cls, data, **kwargs): int_or_none = lambda key: int(str(data[key]), 10) if data.get(key) else None str_or_none = lambda key: str(data[key]) if data.get(key) else None args = { 'year': int_or_none('year'), 'month': int_or_none('month'), 'day': int_or_none('day'), 'sort': str_or_none('sort'), 'hot_sort_type': str_or_none('hot_sort_type'), 'userpage_type': str_or_none('userpage_type'), 'category': Category.get(data.get('category')), 'tag': str_or_none('tag'), 'offset': int_or_none('offset') or 0, 'user': User.objects.get_or_none(username=data['user']) if data.get('user') else None, 'replies_only': bool(data.get('replies_only')), 'public_only': bool(data.get('public_only')), 'mobile': bool(data.get('mobile')), 'public_api': bool(data.get('public_api')), 'count': int_or_none('count'), } args.update(kwargs) return cls(**args)
def test_whitelisted_categories(self): group = create_group(name=Config['featured_groups'][0]) group2 = create_group(name='foobar') self.assertTrue(group.id in Category.get_whitelisted()) self.assertFalse(group2.id in Category.get_whitelisted())