<s> from django . shortcuts import render_to_response , get_object_or_404 , redirect <EOL> from django import forms <EOL> from django . template import RequestContext <EOL> from django . http import HttpResponseRedirect , HttpResponse <EOL> from django . core . urlresolvers import reverse <EOL> from django . views . decorators . csrf import csrf_exempt <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . db . models import Q <EOL> from django . views . generic . list_detail import object_list <EOL> from django . core . paginator import Paginator , EmptyPage , PageNotAnInteger <EOL> from django . core import serializers <EOL> from videoportal . models import Video , Comment , Channel , Collection <EOL> from videoportal . forms import VideoForm , CommentForm <EOL> from transloadit . client import Client <EOL> from taggit . models import Tag <EOL> import appsettings as settings <EOL> import djangotasks <EOL> import simplejson as json <EOL> import urllib2 <EOL> import datetime <EOL> import os <EOL> import shutil <EOL> import re <EOL> from threading import Event <EOL> from traceback import print_exc <EOL> from sys import argv <EOL> from operator import attrgetter <EOL> import itertools <EOL> def list ( request ) : <EOL> '''<STR_LIT>''' <EOL> queryset = itertools . chain ( Video . objects . filter ( encodingDone = True , published = True ) . order_by ( '<STR_LIT>' , '<STR_LIT>' ) , Collection . objects . all ( ) . order_by ( '<STR_LIT>' ) ) <EOL> queryset_sorted = sorted ( queryset , key = attrgetter ( '<STR_LIT:date>' , '<STR_LIT>' ) , reverse = True ) <EOL> paginator = Paginator ( queryset_sorted , <NUM_LIT:15> ) <EOL> channel_list = Channel . objects . all ( ) <EOL> page = request . GET . get ( '<STR_LIT>' ) <EOL> try : <EOL> videos = paginator . page ( page ) <EOL> except PageNotAnInteger : <EOL> videos = paginator . page ( <NUM_LIT:1> ) <EOL> except EmptyPage : <EOL> videos = paginator . page ( paginator . num_pages ) <EOL> return render_to_response ( '<STR_LIT>' , { '<STR_LIT>' : videos , '<STR_LIT>' : channel_list } , <EOL> context_instance = RequestContext ( request ) ) <EOL> def channel_list ( request , slug ) : <EOL> '''<STR_LIT>''' <EOL> channel = get_object_or_404 ( Channel , slug = slug ) <EOL> queryset = itertools . chain ( Video . objects . filter ( encodingDone = True , published = True , channel__slug = slug ) . order_by ( '<STR_LIT>' , '<STR_LIT>' ) , Collection . objects . filter ( channel__slug = slug ) . order_by ( '<STR_LIT>' ) ) <EOL> queryset_sorted = sorted ( queryset , key = attrgetter ( '<STR_LIT:date>' , '<STR_LIT>' ) , reverse = True ) <EOL> paginator = Paginator ( queryset_sorted , <NUM_LIT:15> ) <EOL> channel_list = Channel . objects . all ( ) <EOL> page = request . GET . get ( '<STR_LIT>' ) <EOL> try : <EOL> videos = paginator . page ( page ) <EOL> except PageNotAnInteger : <EOL> videos = paginator . page ( <NUM_LIT:1> ) <EOL> except EmptyPage : <EOL> videos = paginator . page ( paginator . num_pages ) <EOL> return render_to_response ( '<STR_LIT>' , { '<STR_LIT>' : videos , '<STR_LIT>' : channel , '<STR_LIT>' : channel_list } , <EOL> context_instance = RequestContext ( request ) ) <EOL> def detail ( request , slug ) : <EOL> '''<STR_LIT>''' <EOL> if request . method == '<STR_LIT:POST>' : <EOL> comment = Comment ( video = Video . objects . get ( slug = slug ) , ip = request . META [ "<STR_LIT>" ] ) <EOL> video = get_object_or_404 ( Video , slug = slug ) <EOL> emptyform = CommentForm ( ) <EOL> form = CommentForm ( request . POST , instance = comment ) <EOL> if form . is_valid ( ) : <EOL> comment = form . save ( commit = False ) <EOL> comment . save ( ) <EOL> comments = Comment . objects . filter ( moderated = True , video = video ) . order_by ( '<STR_LIT>' ) <EOL> message = "<STR_LIT>" <EOL> return render_to_response ( '<STR_LIT>' , { '<STR_LIT>' : video , '<STR_LIT>' : emptyform , '<STR_LIT>' : comments , '<STR_LIT:message>' : message } , context_instance = RequestContext ( request ) ) <EOL> else : <EOL> video = get_object_or_404 ( Video , slug = slug ) <EOL> form = CommentForm ( ) <EOL> comments = Comment . objects . filter ( moderated = True , video = video ) . order_by ( '<STR_LIT>' ) <EOL> return render_to_response ( '<STR_LIT>' , { '<STR_LIT>' : video , '<STR_LIT>' : form , '<STR_LIT>' : comments } , <EOL> context_instance = RequestContext ( request ) ) <EOL> def iframe ( request , slug ) : <EOL> '''<STR_LIT>''' <EOL> video = get_object_or_404 ( Video , slug = slug ) <EOL> return render_to_response ( '<STR_LIT>' , { '<STR_LIT>' : video } , context_instance = RequestContext ( request ) ) <EOL> def tag ( request , tag ) : <EOL> '''<STR_LIT>''' <EOL> videolist = Video . objects . filter ( encodingDone = True , published = True , tags__slug__in = [ tag ] ) . order_by ( '<STR_LIT>' ) <EOL> tag_name = get_object_or_404 ( Tag , slug = tag ) <EOL> return render_to_response ( '<STR_LIT>' , { '<STR_LIT>' : videolist , '<STR_LIT>' : tag_name } , <EOL> context_instance = RequestContext ( request ) ) <EOL> def collection ( request , slug ) : <EOL> '''<STR_LIT>''' <EOL> collection = get_object_or_404 ( Collection , slug = slug ) <EOL> videolist = collection . videos . filter ( encodingDone = True , published = True ) <EOL> return render_to_response ( '<STR_LIT>' , { '<STR_LIT>' : videolist , '<STR_LIT>' : collection } , <EOL> context_instance = RequestContext ( request ) ) <EOL> def search ( request ) : <EOL> '''<STR_LIT>''' <EOL> query_string = '<STR_LIT>' <EOL> found_entries = None <EOL> if ( '<STR_LIT:q>' in request . GET ) and request . GET [ '<STR_LIT:q>' ] . strip ( ) : <EOL> query_string = request . GET [ '<STR_LIT:q>' ] <EOL> entry_query = get_query ( query_string , [ '<STR_LIT:title>' , '<STR_LIT:description>' , '<STR_LIT>' ] ) <EOL> found_entries = Video . objects . filter ( entry_query ) . order_by ( '<STR_LIT>' ) <EOL> return render_to_response ( '<STR_LIT>' , <EOL> { '<STR_LIT>' : query_string , '<STR_LIT>' : found_entries } , <EOL> context_instance = RequestContext ( request ) ) <EOL> def search_json ( request ) : <EOL> '''<STR_LIT>''' <EOL> query_string = '<STR_LIT>' <EOL> found_entries = None <EOL> if ( '<STR_LIT:q>' in request . GET ) and request . GET [ '<STR_LIT:q>' ] . strip ( ) : <EOL> query_string = request . GET [ '<STR_LIT:q>' ] <EOL> entry_query = get_query ( query_string , [ '<STR_LIT:title>' , '<STR_LIT:description>' , '<STR_LIT>' ] ) <EOL> found_entries = Video . objects . filter ( entry_query ) . order_by ( '<STR_LIT>' ) <EOL> data = serializers . serialize ( '<STR_LIT>' , found_entries ) <EOL> return HttpResponse ( data , content_type = '<STR_LIT>' ) <EOL> def tag_json ( request , tag ) : <EOL> videolist = Video . objects . filter ( encodingDone = True , published = True , tags__name__in = [ tag ] ) . order_by ( '<STR_LIT>' ) <EOL> data = serializers . serialize ( '<STR_LIT>' , videolist ) <EOL> return HttpResponse ( data , content_type = '<STR_LIT>' ) <EOL> @ login_required ( login_url = '<STR_LIT>' ) <EOL> def submit ( request ) : <EOL> '''<STR_LIT>''' <EOL> if request . user . is_authenticated ( ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = VideoForm ( request . POST , request . FILES or None ) <EOL> if form . is_valid ( ) : <EOL> cmodel = form . save ( ) <EOL> if cmodel . originalFile : <EOL> if settings . USE_TRANLOADIT : <EOL> client = Client ( settings . TRANSLOAD_AUTH_KEY , settings . TRANSLOAD_AUTH_SECRET ) <EOL> params = None <EOL> if ( cmodel . kind == <NUM_LIT:0> ) : <EOL> params = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : cmodel . originalFile . url , <EOL> } <EOL> } , <EOL> '<STR_LIT>' : settings . TRANSLOAD_TEMPLATE_VIDEO_ID , <EOL> '<STR_LIT>' : settings . TRANSLOAD_NOTIFY_URL <EOL> } <EOL> if ( cmodel . kind == <NUM_LIT:1> ) : <EOL> params = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : cmodel . originalFile . url , <EOL> } <EOL> } , <EOL> '<STR_LIT>' : settings . TRANSLOAD_TEMPLATE_AUDIO_ID , <EOL> '<STR_LIT>' : settings . TRANSLOAD_NOTIFY_URL <EOL> } <EOL> if ( cmodel . kind == <NUM_LIT:2> ) : <EOL> params = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : cmodel . originalFile . url , <EOL> } <EOL> } , <EOL> '<STR_LIT>' : settings . TRANSLOAD_TEMPLATE_VIDEO_AUDIO_ID , <EOL> '<STR_LIT>' : settings . TRANSLOAD_NOTIFY_URL <EOL> } <EOL> result = client . request ( ** params ) <EOL> cmodel . assemblyid = result [ '<STR_LIT>' ] <EOL> cmodel . published = cmodel . autoPublish <EOL> cmodel . encodingDone = False <EOL> cmodel . save ( ) <EOL> else : <EOL> cmodel . save ( ) <EOL> djangotasks . register_task ( cmodel . encode_media , "<STR_LIT>" ) <EOL> encoding_task = djangotasks . task_for_object ( cmodel . encode_media ) <EOL> djangotasks . run_task ( encoding_task ) <EOL> if settings . USE_BITTORRENT : <EOL> djangotasks . register_task ( cmodel . create_bittorrent , "<STR_LIT>" ) <EOL> torrent_task = djangotasks . task_for_object ( cmodel . create_bittorrent ) <EOL> djangotasks . run_task ( torrent_task ) <EOL> cmodel . user = request . user <EOL> cmodel . save ( ) <EOL> return redirect ( list ) <EOL> return render_to_response ( '<STR_LIT>' , <EOL> { '<STR_LIT>' : form } , <EOL> context_instance = RequestContext ( request ) ) <EOL> else : <EOL> form = VideoForm ( ) <EOL> return render_to_response ( '<STR_LIT>' , <EOL> { '<STR_LIT>' : form } , <EOL> context_instance = RequestContext ( request ) ) <EOL> else : <EOL> return render_to_response ( '<STR_LIT>' , <EOL> context_instance = RequestContext ( request ) ) <EOL> @ login_required ( login_url = '<STR_LIT>' ) <EOL> def status ( request ) : <EOL> if settings . USE_BITTORRENT : <EOL> processing_videos = Video . objects . filter ( Q ( encodingDone = False ) | Q ( torrentDone = False ) ) <EOL> else : <EOL> processing_videos = Video . objects . filter ( encodingDone = False ) <EOL> running_tasks = [ ] <EOL> for video in processing_videos : <EOL> tasks = djangotasks . models . Task . objects . filter ( model = "<STR_LIT>" , object_id = video . pk ) <EOL> running_tasks . append ( tasks ) <EOL> return render_to_response ( '<STR_LIT>' , <EOL> { '<STR_LIT>' : processing_videos , '<STR_LIT>' : running_tasks } , <EOL> context_instance = RequestContext ( request ) ) <EOL> @ csrf_exempt <EOL> def encodingdone ( request ) : <EOL> '''<STR_LIT>''' <EOL> if request . method == '<STR_LIT:POST>' : <EOL> data = json . loads ( request . POST [ '<STR_LIT>' ] ) <EOL> try : <EOL> video = Video . objects . get ( assemblyid = data [ '<STR_LIT>' ] ) <EOL> if ( video . kind == <NUM_LIT:0> ) : <EOL> results = data [ '<STR_LIT>' ] <EOL> resultItem = results [ settings . TRANSLOAD_MP4_ENCODE ] <EOL> resultFirst = resultItem [ <NUM_LIT:0> ] <EOL> video . mp4URL = resultFirst [ '<STR_LIT:url>' ] <EOL> video . mp4Size = resultFirst [ '<STR_LIT:size>' ] <EOL> resultMeta = resultFirst [ '<STR_LIT>' ] <EOL> video . duration = str ( resultMeta [ '<STR_LIT>' ] ) <EOL> resultItem = results [ settings . TRANSLOAD_WEBM_ENCODE ] <EOL> resultFirst = resultItem [ <NUM_LIT:0> ] <EOL> video . webmURL = resultFirst [ '<STR_LIT:url>' ] <EOL> video . webmSize = resultFirst [ '<STR_LIT:size>' ] <EOL> resultItem = results [ settings . TRANSLOAD_THUMB_ENCODE ] <EOL> resultFirst = resultItem [ <NUM_LIT:0> ] <EOL> video . videoThumbURL = resultFirst [ '<STR_LIT:url>' ] <EOL> elif ( video . kind == <NUM_LIT:1> ) : <EOL> results = data [ '<STR_LIT>' ] <EOL> resultItem = results [ settings . TRANSLOAD_MP3_ENCODE ] <EOL> resultFirst = resultItem [ <NUM_LIT:0> ] <EOL> video . mp3URL = resultFirst [ '<STR_LIT:url>' ] <EOL> video . mp3Size = resultFirst [ '<STR_LIT:size>' ] <EOL> resultMeta = resultFirst [ '<STR_LIT>' ] <EOL> video . duration = str ( resultMeta [ '<STR_LIT>' ] ) <EOL> resultItem = results [ settings . TRANSLOAD_OGG_ENCODE ] <EOL> resultFirst = resultItem [ <NUM_LIT:0> ] <EOL> video . oggURL = resultFirst [ '<STR_LIT:url>' ] <EOL> video . oggSize = resultFirst [ '<STR_LIT:size>' ] <EOL> elif ( video . kind == <NUM_LIT:2> ) : <EOL> results = data [ '<STR_LIT>' ] <EOL> resultItem = results [ settings . TRANSLOAD_MP4_ENCODE ] <EOL> resultFirst = resultItem [ <NUM_LIT:0> ] <EOL> video . mp4URL = resultFirst [ '<STR_LIT:url>' ] <EOL> video . mp4Size = resultFirst [ '<STR_LIT:size>' ] <EOL> resultMeta = resultFirst [ '<STR_LIT>' ] <EOL> video . duration = str ( resultMeta [ '<STR_LIT>' ] ) <EOL> resultItem = results [ settings . TRANSLOAD_WEBM_ENCODE ] <EOL> resultFirst = resultItem [ <NUM_LIT:0> ] <EOL> video . webmURL = resultFirst [ '<STR_LIT:url>' ] <EOL> video . webmSize = resultFirst [ '<STR_LIT:size>' ] <EOL> resultItem = results [ settings . TRANSLOAD_MP3_ENCODE ] <EOL> resultFirst = resultItem [ <NUM_LIT:0> ] <EOL> video . mp3URL = resultFirst [ '<STR_LIT:url>' ] <EOL> video . mp3Size = resultFirst [ '<STR_LIT:size>' ] <EOL> resultItem = results [ settings . TRANSLOAD_OGG_ENCODE ] <EOL> resultFirst = resultItem [ <NUM_LIT:0> ] <EOL> video . oggURL = resultFirst [ '<STR_LIT:url>' ] <EOL> video . oggSize = resultFirst [ '<STR_LIT:size>' ] <EOL> resultItem = results [ settings . TRANSLOAD_THUMB_ENCODE ] <EOL> resultFirst = resultItem [ <NUM_LIT:0> ] <EOL> video . videoThumbURL = resultFirst [ '<STR_LIT:url>' ] <EOL> video . encodingDone = True <EOL> video . save ( ) <EOL> except Video . DoesNotExist : <EOL> raise Http404 <EOL> return HttpResponse ( "<STR_LIT>" ) <EOL> else : <EOL> return render_to_response ( '<STR_LIT>' , <EOL> context_instance = RequestContext ( request ) ) <EOL> def normalize_query ( query_string , <EOL> findterms = re . compile ( r'<STR_LIT>' ) . findall , <EOL> normspace = re . compile ( r'<STR_LIT>' ) . sub ) : <EOL> '''<STR_LIT>''' <EOL> return [ normspace ( '<STR_LIT:U+0020>' , ( t [ <NUM_LIT:0> ] or t [ <NUM_LIT:1> ] ) . strip ( ) ) for t in findterms ( query_string ) ] <EOL> def get_query ( query_string , search_fields ) : <EOL> '''<STR_LIT>''' <EOL> query = None <EOL> terms = normalize_query ( query_string ) <EOL> for term in terms : <EOL> or_query = None <EOL> for field_name in search_fields : <EOL> q = Q ( ** { "<STR_LIT>" % field_name : term } ) <EOL> if or_query is None : <EOL> or_query = q <EOL> else : <EOL> or_query = or_query | q <EOL> if query is None : <EOL> query = or_query <EOL> else : <EOL> query = query & or_query <EOL> return query </s>
