def get(self, request, *args, **kwargs): self.object = self.get_object() abs_url = self.object.get_absolute_url() if self.kwargs['slug'] is None or iri_to_uri(request.path) != abs_url: return HttpResponseRedirect(abs_url) context = self.get_context_data(object=self.object) Watch.add(request, self.object) return self.render_to_response(context)
def get(self, request, *args, **kwargs): self.object = self.get_object() abs_url = self.object.get_absolute_url() if self.kwargs['slug'] is None or request.path != abs_url: return HttpResponseRedirect(abs_url) context = self.get_context_data(object=self.object) Watch.add(request, self.object) return self.render_to_response(context)
def view_video(request, video_id, slug=None): video_qs = Video.objects.annotate(watch_count=Count('watch')) video = get_object_or_404(video_qs, pk=video_id, site=Site.objects.get_current()) if not video.status == Video.ACTIVE and not request.user_is_admin(): raise Http404 if slug is not None and request.path != video.get_absolute_url(): return HttpResponsePermanentRedirect(video.get_absolute_url()) context = { 'current_video': video, # set edit_video_form to True if the user is an admin for # backwards-compatibility 'edit_video_form': request.user_is_admin() } sitelocation = SiteLocation.objects.get_current() popular_videos = Video.objects.get_popular_videos() if video.categories.count(): category_obj = None referrer = request.META.get('HTTP_REFERER') host = 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 = Category.objects.get( slug=kwargs['slug'], site=sitelocation.site) except Category.DoesNotExist: pass else: if not video.categories.filter( pk=category_obj.pk).count(): category_obj = None if category_obj is None: category_obj = video.categories.all()[0] context['category'] = category_obj popular_videos = popular_videos.filter(categories=category_obj) context['popular_videos'] = popular_videos if video.voting_enabled(): import voting user_can_vote = True if request.user.is_authenticated(): max_votes = video.categories.filter( contest_mode__isnull=False).count() * MAX_VOTES_PER_CATEGORY votes = voting.models.Vote.objects.filter( content_type=ContentType.objects.get_for_model(Video), user=request.user).count() if votes >= max_votes: user_can_vote = False context['user_can_vote'] = user_can_vote if user_can_vote: if 'category' in context and context['category'].contest_mode: context['contest_category'] = context['category'] else: context['contest_category'] = video.categories.filter( contest_mode__isnull=False)[0] if sitelocation.playlists_enabled: # showing playlists if request.user.is_authenticated(): if request.user_is_admin() or \ sitelocation.playlists_enabled == 1: # user can add videos to playlists context['playlists'] = Playlist.objects.filter( user=request.user) if request.user_is_admin(): # show all playlists context['playlistitem_set'] = video.playlistitem_set.all() elif request.user.is_authenticated(): # public playlists or my playlists context['playlistitem_set'] = video.playlistitem_set.filter( Q(playlist__status=Playlist.PUBLIC) | Q(playlist__user=request.user)) else: # just public playlists context['playlistitem_set'] = video.playlistitem_set.filter( playlist__status=Playlist.PUBLIC) if 'playlist' in request.GET: try: playlist = Playlist.objects.get(pk=request.GET['playlist']) except (Playlist.DoesNotExist, ValueError): pass else: if (playlist.is_public() or request.user_is_admin() or (request.user.is_authenticated() and playlist.user_id == request.user.pk)): try: context['playlistitem'] = video.playlistitem_set.get( playlist=playlist) except PlaylistItem.DoesNotExist: pass Watch.add(request, video) return render_to_response('localtv/view_video.html', context, context_instance=RequestContext(request))
def view_video(request, video_id, slug=None): video_qs = Video.objects.annotate(watch_count=Count("watch")) video = get_object_or_404(video_qs, pk=video_id, site=Site.objects.get_current()) if not video.status == Video.ACTIVE and not request.user_is_admin(): raise Http404 if slug is not None and request.path != video.get_absolute_url(): return HttpResponsePermanentRedirect(video.get_absolute_url()) context = { "current_video": video, # set edit_video_form to True if the user is an admin for # backwards-compatibility "edit_video_form": request.user_is_admin(), } sitelocation = SiteLocation.objects.get_current() popular_videos = Video.objects.get_popular_videos() if video.categories.count(): category_obj = None referrer = request.META.get("HTTP_REFERER") host = 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 = Category.objects.get(slug=kwargs["slug"], site=sitelocation.site) except Category.DoesNotExist: pass else: if not video.categories.filter(pk=category_obj.pk).count(): category_obj = None if category_obj is None: category_obj = video.categories.all()[0] context["category"] = category_obj popular_videos = popular_videos.filter(categories=category_obj) context["popular_videos"] = popular_videos if video.voting_enabled(): import voting user_can_vote = True if request.user.is_authenticated(): max_votes = video.categories.filter(contest_mode__isnull=False).count() * MAX_VOTES_PER_CATEGORY votes = voting.models.Vote.objects.filter( content_type=ContentType.objects.get_for_model(Video), user=request.user ).count() if votes >= max_votes: user_can_vote = False context["user_can_vote"] = user_can_vote if user_can_vote: if "category" in context and context["category"].contest_mode: context["contest_category"] = context["category"] else: context["contest_category"] = video.categories.filter(contest_mode__isnull=False)[0] if sitelocation.playlists_enabled: # showing playlists if request.user.is_authenticated(): if request.user_is_admin() or sitelocation.playlists_enabled == 1: # user can add videos to playlists context["playlists"] = Playlist.objects.filter(user=request.user) if request.user_is_admin(): # show all playlists context["playlistitem_set"] = video.playlistitem_set.all() elif request.user.is_authenticated(): # public playlists or my playlists context["playlistitem_set"] = video.playlistitem_set.filter( Q(playlist__status=Playlist.PUBLIC) | Q(playlist__user=request.user) ) else: # just public playlists context["playlistitem_set"] = video.playlistitem_set.filter(playlist__status=Playlist.PUBLIC) if "playlist" in request.GET: try: playlist = Playlist.objects.get(pk=request.GET["playlist"]) except (Playlist.DoesNotExist, ValueError): pass else: if ( playlist.is_public() or request.user_is_admin() or (request.user.is_authenticated() and playlist.user_id == request.user.pk) ): try: context["playlistitem"] = video.playlistitem_set.get(playlist=playlist) except PlaylistItem.DoesNotExist: pass Watch.add(request, video) return render_to_response("localtv/view_video.html", context, context_instance=RequestContext(request))