def get_context_data(self, **kwargs): context = super(IndexView, self).get_context_data(**kwargs) featured_form = SearchForm({'sort': 'featured'}) popular_form = SearchForm({'sort': 'popular'}) new_form = SearchForm({'sort': 'newest'}) video_pks = Video.objects.filter(site=settings.SITE_ID, status=Video.ACTIVE).values_list( 'pk', flat=True) recent_comments = comments.get_model().objects.filter( site=settings.SITE_ID, content_type=ContentType.objects.get_for_model(Video), object_pk__in=video_pks, is_removed=False, is_public=True).order_by('-submit_date') context.update({ 'featured_videos': NormalizedVideoList(featured_form.search()), 'popular_videos': NormalizedVideoList(popular_form.search()), 'new_videos': NormalizedVideoList(new_form.search()), 'comments': recent_comments }) return context
def get_video_list(self, context): if self.filter_name is None: filter_value = None else: filter_value = self.item.resolve(context) form = self.get_form(filter_value=filter_value) return NormalizedVideoList(form.search())
def get_queryset(self): """Wraps the normal queryset in a :class:`.NormalizedVideoList`.""" qs = super(CompatibleListingView, self).get_queryset() if self.approved_since is not None: if isinstance(qs, SearchQuerySet): qs = qs.exclude(when_approved__exact=DATETIME_NULL_PLACEHOLDER) else: qs = qs.exclude(when_approved__isnull=True) qs = qs.filter(when_approved__gt=(datetime.datetime.now() - self.approved_since)) return NormalizedVideoList(qs)
def get_video_list(self, context): qs = self._search("") qs = self._sort(qs, self.sort) if self.search_filter in self.filters: f = self.filters[self.search_filter] values = [self.item.resolve(context)] if values[0] is not None: cleaned_filters = self._clean_filter_values( {self.search_filter: values}) try: cleaned_filters[self.search_filter][0] except IndexError: # Then this isn't a valid filter - slug for a missing # category, say. Return an empty qs. qs = qs.none() else: qs = self._filter(qs, cleaned_filters) return NormalizedVideoList(qs)
def items(self, obj): """ This feed is unusual enough that we actually need to override :meth:`items`. """ form = self.get_form(obj['request'].GET.dict(), [obj.get('obj')]) # We currently don't support searching combined with the 'order' sort. if 'playlist_order' in obj: # This is a HACK for backwards-compatibility. order_by = '{0}playlistitem___order'.format( '-' if obj['playlist_order'][0] == '-' else '') queryset = obj['obj'].items.order_by(order_by) queryset = form._filter(queryset) else: queryset = form.search() queryset = NormalizedVideoList(queryset) return self._opensearch_items(queryset, obj)
def items(self, obj): """ Handles a list or queryset of items fetched with :meth:`_actual_items` according to the following `OpenSearch` query string parameters: * count * startIndex * startPage More info at http://www.opensearch.org/Specifications/OpenSearch/1.1#OpenSearch_1.1_parameters """ filter_value = obj.get('obj') if self.filter_name is not None: field = self.form_class.base_fields[self.filter_name] if isinstance(field, ModelFilterField): filter_value = [filter_value] form = self.get_form(obj['request'].GET.dict(), filter_value) items = NormalizedVideoList(form.search()) return self._opensearch_items(items, obj)
def items(self, obj): """ This feed is unusual enough that we actually need to override :meth:`items`. """ form = self.get_form(obj['request'].GET.dict(), [obj.get('obj')]) # We currently don't support searching combined with the 'order' sort. if 'playlist_order' in obj: # This is a HACK for backwards-compatibility. order_by = '{0}playlistitem___order'.format( '-' if obj['playlist_order'][0] == '-' else '') items = obj['obj'].items.order_by(order_by) items = form._filter(items) else: items = form.search() items = NormalizedVideoList( items, select_related=[], prefetch_related=['authors', 'taggeditem_set__tag', 'categories']) items = self._opensearch_items(items, obj) return self._bulk_adjusted_items(items)
def get_queryset(self): """ Returns a list based on the results of a haystack search. """ qs = self._search(self._get_query(self.request)) qs = self._sort(qs, self._get_sort(self.request)) self.filter_form = self._get_filter_form(self.request) filters = self._get_filters(self.filter_form, **self.kwargs) self._cleaned_filters = self._clean_filter_values(filters) qs = self._filter(qs, self._cleaned_filters) if self.approved_since is not None: if isinstance(qs, SearchQuerySet): qs = qs.exclude(when_approved__exact=DATETIME_NULL_PLACEHOLDER) else: qs = qs.exclude(when_approved__isnull=True) qs = qs.filter(when_approved__gt=( datetime.datetime.now() - self.approved_since)) return NormalizedVideoList(qs)
def items(self, obj): """ Handles a list or queryset of items fetched with :meth:`_actual_items` according to the following `OpenSearch` query string parameters: * count * startIndex * startPage More info at http://www.opensearch.org/Specifications/OpenSearch/1.1#OpenSearch_1.1_parameters """ qs = self._search(self._get_query(obj['request'])) qs = self._sort(qs, self._get_sort(obj['request'])) qs = self._filter(qs, obj['cleaned_filters']) videos = NormalizedVideoList(qs) opensearch = self._get_opensearch_data(obj) start = opensearch['startindex'] end = start + opensearch['itemsperpage'] opensearch['totalresults'] = len(videos) return videos[start:end]
def get_context_data(self, **kwargs): context = super(IndexView, self).get_context_data(**kwargs) featured_videos = Video.objects.get_featured_videos() popular_videos = self._sort(self._search(''), '-popular') new_videos = Video.objects.get_latest_videos().exclude( feed__avoid_frontpage=True) sitelocation_videos = Video.objects.get_sitelocation_videos() recent_comments = comments.get_model().objects.filter( site=Site.objects.get_current(), content_type=ContentType.objects.get_for_model(Video), object_pk__in=sitelocation_videos.values_list('pk', flat=True), is_removed=False, is_public=True).order_by('-submit_date') context.update({ 'featured_videos': featured_videos, 'popular_videos': NormalizedVideoList(popular_videos), 'new_videos': new_videos, 'comments': recent_comments }) return context
def get_context_data(self, **kwargs): context = super(VideoView, self).get_context_data(**kwargs) context.update({ 'sidebar_modules': self.get_sidebar_modules(), # set edit_video_form to True if the user is an admin for # backwards-compatibility 'edit_video_form': self.request.user_is_admin(), }) site_settings = SiteSettings.objects.get_current() # Data for generating popular videos list. popular_form_data = {'sort': 'popular'} try: category_obj = self.object.categories.all()[0] except IndexError: pass else: # If there are categories, prefer the category that the user # just came from the list view of. referrer = self.request.META.get('HTTP_REFERER') host = self.request.META.get('HTTP_HOST') if referrer and host: if referrer.startswith('http://') or \ referrer.startswith('https://'): referrer = referrer[referrer.index('://') + 3:] if referrer.startswith(host): referrer = referrer[len(host):] try: view, args, kwargs = resolve(referrer) except Resolver404: pass else: from localtv.urls import category_videos if view == category_videos: try: category_obj = self.object.categories.get( slug=kwargs['slug'], site=site_settings.site) except Category.DoesNotExist: pass context['category'] = category_obj popular_form_data['category'] = [category_obj] form = SearchForm(popular_form_data) popular_videos = form.search() context['popular_videos'] = NormalizedVideoList(popular_videos) if site_settings.playlists_enabled: # showing playlists if self.request.user.is_authenticated(): if self.request.user_is_admin() or \ site_settings.playlists_enabled == 1: # user can add videos to playlists context['playlists'] = Playlist.objects.filter( user=self.request.user) playlistitem_qs = self.object.playlistitem_set.all() if self.request.user_is_admin(): # show all playlists pass elif self.request.user.is_authenticated(): # public playlists or my playlists playlistitem_qs = playlistitem_qs.filter( Q(playlist__status=Playlist.PUBLIC) | Q(playlist__user=self.request.user)) else: # just public playlists playlistitem_qs = playlistitem_qs.filter( playlist__status=Playlist.PUBLIC) context['playlistitem_set'] = playlistitem_qs if 'playlist' in self.request.GET: try: playlist = Playlist.objects.get( pk=self.request.GET['playlist']) except (Playlist.DoesNotExist, ValueError): pass else: if (playlist.is_public() or self.request.user_is_admin() or (self.request.user.is_authenticated() and playlist.user_id == self.request.user.pk)): try: context['playlistitem'] = ( self.object.playlistitem_set.get( playlist=playlist)) except PlaylistItem.DoesNotExist: pass return context
def get_video_list(self, context): form = self.get_form(filter_value=self.get_filter_value(context)) return NormalizedVideoList(form.search())