# View for semi-static templatized content.
#
# List of valid templates is explicitly managed for (short-term)
# security reasons.

from edxmako.shortcuts import render_to_response, render_to_string
from mako.exceptions import TopLevelLookupException
from django.shortcuts import redirect
from django.conf import settings
from django.http import HttpResponseNotFound, HttpResponseServerError, Http404
from django.views.decorators.csrf import ensure_csrf_cookie
#Bharat Moocs Added on 23 Sep for language option
from dark_lang.models import DarkLangConfig
from util.cache import cache_if_anonymous
#Bharat Moocs Added on 23 Sep for language option
from django.utils import translation


valid_templates = []

if settings.STATIC_GRAB:
    valid_templates = valid_templates + [
        'server-down.html',
        'server-error.html'
        'server-overloaded.html',
    ]


def index(request, template):
    if template in valid_templates:
        return render_to_response('static_templates/' + template, {})
    else:
        return redirect('/')


@ensure_csrf_cookie
@cache_if_anonymous()
def render(request, template):
    """
    This view function renders the template sent without checking that it
    exists. Do not expose template as a regex part of the url. The user should
    not be able to ender any arbitray template name. The correct usage would be:

    url(r'^jobs$', 'static_template_view.views.render', {'template': 'jobs.html'}, name="jobs")
    """
    #return render_to_response('static_templates/' + template, {})


    #Bharat Moocs Added on 23 Sep for language option
    language_options = DarkLangConfig.current().released_languages_list
    cur_language = translation.get_language()
    #BharatMooc 12 Nov 2014: Added links for theme related files such as theme-about and theme-faq to pick up from language folder
    #BharatMooc 22 Dec 2014: Added theme-honor.html in this list to enable Hindi as well as English text to be displayed
    myList=['how_it_works.html','partners.html','theme-about.html','theme-faq.html','theme-research.html','theme-privacy.html','theme-tos.html','theme-contact.html','theme-iitbombayx_faq.html', 'theme-honor.html']

# IITBX 11-APR-2016 :theme chanegd for hi/en language,which is redirect to path(/edx/app/edxapp/themes/THEME_NAME)
# IITBX 11-APR-2016:code changes render_to_response('templates/' )

    if template in myList :
	return render_to_response('templates/' + cur_language + '/' + template, {"language_options":language_options})
    else:
	return render_to_response('static_templates/' + template, {"language_options":language_options})


''''''

@ensure_csrf_cookie
@cache_if_anonymous()
def render_press_release(request, slug):
    """
    Render a press release given a slug.  Similar to the "render" function above,
    but takes a slug and does a basic conversion to convert it to a template file.
    a) all lower case,
    b) convert dashes to underscores, and
    c) appending ".html"
    """
    template = slug.lower().replace('-', '_') + ".html"
    try:
        resp = render_to_response('static_templates/press_releases/' + template, {})
    except TopLevelLookupException:
        raise Http404
    else:
        return resp


def render_404(request):
    return HttpResponseNotFound(render_to_string('static_templates/404.html', {}))


def render_500(request):
    return HttpResponseServerError(render_to_string('static_templates/server-error.html', {}))
