def video_list(request): """Show a list of recent videos, optionally filtered by region or search.""" page = request.GET.get('page', 1) region = request.GET.get('region', None) ctx = {} if waffle.flag_is_active(request, 'r3'): form = VideoSearchForm(request.GET) try: videos = form.perform_search() except ValidationError: videos = search_videos() ctx['form'] = form else: videos = Video.objects.filter(approved=True).order_by('-created') if region: countries = regions.get_countries(region) if countries: videos = videos.filter( user__userprofile__country__in=countries) paginator = Paginator(videos, 12) try: videos = paginator.page(page) except PageNotAnInteger: videos = paginator.page(1) # Default to first page except EmptyPage: videos = paginator.page(paginator.num_pages) # Empty page goes to last ctx['videos'] = videos return render(request, 'videos/2013/list.html', ctx)
def search_videos(query="", fields=None, region=None, sort=None): """ Retrieve a sorted list of videos, optionally filtered by a text search query and region. :param query: Search query. The video title and description are searched, as well as the full name of the user who created the video. Limited to a simple "icontains" filter. :param fields: Fields to apply search query to. Defaults to video title, description, and user's full name and nickname. :param region: If present, filters videos to only those found in the specified region. Values for this parameter can be found in `flicks.base.regions`. :param sort: How to sort the resulting videos. Defaults to a random sort that changes every hour. Other valid options are 'popular', which sorts by vote count, and 'title', which sorts alphabetically by title. """ qs = Video.objects.filter(approved=True) query = query.strip() # Text search if query: filters = [] fields = fields or DEFAULT_QUERY_FIELDS for term in query.split(None, 6): # Limit to 6 to guard against abuse. for field in fields: kwarg = "{0}__icontains".format(field) filters.append(Q(**{kwarg: term})) qs = qs.filter(reduce(operator.or_, filters)) # Mash 'em all together! # Filter by region if region is not None: countries = regions.get_countries(region) if countries: qs = qs.filter(user__userprofile__country__in=countries) # Sorting if sort == "popular": # NOTE: Naming this count `vote_count` triggers a bug in Django since # there's a property named that on the model. qs = qs.annotate(votecount=Count("voters")).order_by("-votecount") elif sort == "title": qs = qs.order_by("title") # Default sort is by title. else: qs = qs.order_by("random_ordering") return qs
def search_videos(query='', fields=None, region=None, sort=None): """ Retrieve a sorted list of videos, optionally filtered by a text search query and region. :param query: Search query. The video title and description are searched, as well as the full name of the user who created the video. Limited to a simple "icontains" filter. :param fields: Fields to apply search query to. Defaults to video title, description, and user's full name and nickname. :param region: If present, filters videos to only those found in the specified region. Values for this parameter can be found in `flicks.base.regions`. :param sort: How to sort the resulting videos. Defaults to a random sort that changes every hour. Other valid options are 'popular', which sorts by vote count, and 'title', which sorts alphabetically by title. """ qs = Video.objects.filter(approved=True) query = query.strip() # Text search if query: filters = [] fields = fields or DEFAULT_QUERY_FIELDS for term in query.split(None, 6): # Limit to 6 to guard against abuse. for field in fields: kwarg = '{0}__icontains'.format(field) filters.append(Q(**{kwarg: term})) qs = qs.filter(reduce(operator.or_, filters)) # Mash 'em all together! # Filter by region if region is not None: countries = regions.get_countries(region) if countries: qs = qs.filter(user__userprofile__country__in=countries) # Sorting if sort == 'popular': # NOTE: Naming this count `vote_count` triggers a bug in Django since # there's a property named that on the model. qs = qs.annotate(votecount=Count('voters')).order_by('-votecount') elif sort == 'title': qs = qs.order_by('title') # Default sort is by title. else: qs = qs.order_by('random_ordering') return qs
def video_list(request): """Show a list of recent videos, optionally filtered by region or search.""" page = request.GET.get('page', 1) region = request.GET.get('region', None) videos = Video.objects.filter(approved=True).order_by('-created') if region: countries = regions.get_countries(region) if countries: videos = videos.filter(user__userprofile__country__in=countries) paginator = Paginator(videos, 12) try: videos = paginator.page(page) except PageNotAnInteger: videos = paginator.page(1) # Default to first page except EmptyPage: videos = paginator.page(paginator.num_pages) # Empty page goes to last return render(request, 'videos/2013/list.html', {'videos': videos})
def test_error(self): eq_(regions.get_countries(7), None) eq_(regions.get_countries('7'), None) eq_(regions.get_countries('not a number'), None) eq_(regions.get_countries(False), None)
def test_basic(self): eq_(regions.get_countries(1), ['us', 'fr']) eq_(regions.get_countries('1'), ['us', 'fr']) eq_(regions.get_countries(2), ['ws']) eq_(regions.get_countries('2'), ['ws'])