# Universal Subtitles, universalsubtitles.org
# 
# Copyright (C) 2010 Participatory Culture Foundation
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see 
# http://www.gnu.org/licenses/agpl-3.0.html.

from django import template
from videos.forms import SubtitlesUploadForm, PasteTranscriptionForm, CreateVideoUrlForm
from django.utils.translation import ugettext_lazy as _, ungettext

register = template.Library()

@register.inclusion_tag('videos/_upload_subtitles.html', takes_context=True)
def upload_subtitles(context, video):
    context['video'] = video
    initial = {}
    if 'language' in context:
        initial['language'] = context['language'].language    
    context['form'] = SubtitlesUploadForm(context['user'], initial=initial)
    return context

@register.inclusion_tag('videos/_paste_transcription.html', takes_context=True)    
def paste_transcription(context):
    #It is just template of pop-up, you should add link with 'upload-transcript-button' class
    initial = {}
    if 'language' in context:
        initial['language'] = context['language'].language

    context['form'] = PasteTranscriptionForm(context['user'], initial=initial)
    return context

@register.simple_tag
def complete_indicator(language):
    if language.is_original or language.is_forked:
        v = language.version()
        count = v and v.subtitle_set.count() or 0
        return ungettext('%(count)s Line', '%(count)s Lines', count) % {'count': count}
    return '%i %%' % language.percent_done

@register.simple_tag
def complete_color(language):
    if language.is_original or language.is_forked:
        return 'full'
    
    val = language.percent_done
    if val >= 95:
        return 'eighty'
    elif val >= 80:
        return 'sixty'
    elif val >= 30:
        return 'fourty'
    else:
        return 'twenty'

@register.inclusion_tag('videos/_video_url_panel.html', takes_context=True)
def video_url_panel(context):
    video = context['video']
    context['form'] = CreateVideoUrlForm(context['user'], initial={'video': video.pk})
    context['video_urls'] = video.videourl_set.all()
    return context

@register.simple_tag
def video_url_count(video):
    return video.videourl_set.count()
@@ -954,16 +954,53 @@
 ngettext
+%0Afrom django.utils import translation
 %0A%0Aregist
@@ -1184,32 +1184,44 @@
 ial = %7B%7D%0A    if 
+context.get(
 'language' in co
@@ -1210,35 +1210,37 @@
 t('language'
- in
+) and
  context
 :%0A        in
@@ -1219,32 +1219,53 @@
 ge') and context
+%5B'language'%5D.language
 :%0A        initia
@@ -1308,20 +1308,83 @@
 language
-    
+%0A    else:%0A        initial%5B'language'%5D = translation.get_language()
 %0A    con
@@ -1708,16 +1708,28 @@
 %0A    if 
+context.get(
 'languag
@@ -1734,19 +1734,21 @@
 age'
- in
+) and
  context
 :%0A  
@@ -1743,16 +1743,37 @@
  context
+%5B'language'%5D.language
 :%0A      
@@ -1825,16 +1825,82 @@
 anguage%0A
+    else:%0A        initial%5B'language'%5D = translation.get_language()
 %0A    con

add user's default language for subtitle paste and upload, http://bugzilla.pculture.org/show_bug.cgi?id=15314