# Path: easymode/utils/languagecode.py
# def fix_language_code(url, current_language):
#     """
#     Fixes the language code as follows:
#     
#     If there is only one language used in the site, it strips the language code.
#     If there are more languages used, it will make sure that the url has the current
#     language as a prefix.
#     
#     >>> # multiple languages defined
#     >>> settings.LANGUAGES = (('en-us','English'),('de','German'),('nl-be','Belgium dutch'),('fr-be','Belgium french'),)
#     >>> settings.USE_SHORT_LANGUAGE_CODES = False
#     >>> activate('en-us')
#     >>> fix_language_code('/de/example.html', 'en-us')
#     '/en-us/example.html'
#     >>> settings.USE_SHORT_LANGUAGE_CODES = True
#     >>> fix_language_code('/de/example.html', 'en-us')
#     '/en/example.html'
#     >>> fix_language_code('/en/example.html', 'en-us')
#     '/example.html'
#     >>> # only one language defined
#     >>> settings.LANGUAGES = (('en-us', 'English'), )
#     >>> fix_language_code('/en-us/example.html', 'en-us')
#     '/example.html'
#     
#     :param url: The (absolute) url to be fixed eg. '/hi/how/is/it'.
#     :param current_language: A language code defined in ``settings.LANGUAGES``.
#     :rtype: The fixed url.
#     """
#     stripped_url = strip_language_code(url)
#     if not getattr(settings, 'MASTER_SITE', False) and len(settings.LANGUAGES) == 1:
#         # no MASTER_SITE and only one language, do not add language code to url
#         return stripped_url
#     
#     # add the language code to the url        
#     return u"/%s%s" % (get_shorthand_from_language_code(current_language), stripped_url)


from django import http
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ObjectDoesNotExist
from easymode.utils.languagecode import fix_language_code


@staff_member_required
def preview(request, content_type_id, object_id):
    """
    This is an override for django.views.default.shortcut.
    It assumes that get_absolute_url returns an absolute url, so
    it does not do any of the very elaborate link checking that shortcut does.
    
    This version adds the language code to the url. (/en/blaat/).
    """
    
    try:
        content_type = ContentType.objects.get(pk=content_type_id)
        obj = content_type.get_object_for_this_type(pk=object_id)
    except ObjectDoesNotExist:
        raise http.Http404("Content type %s object %s doesn't exist" % (content_type_id, object_id))
    try:
        absolute_url = obj.get_absolute_url()
    except AttributeError:
        raise http.Http404("%s objects don't have get_absolute_url() methods" % content_type.name)
        
    if absolute_url.startswith('http://') or absolute_url.startswith('https://'):        
        http.HttpResponseRedirect(absolute_url)        
    else:

# Path: easymode/utils/languagecode.py
# def fix_language_code(url, current_language):
#     """
#     Fixes the language code as follows:
#     
#     If there is only one language used in the site, it strips the language code.
#     If there are more languages used, it will make sure that the url has the current
#     language as a prefix.
#     
#     >>> # multiple languages defined
#     >>> settings.LANGUAGES = (('en-us','English'),('de','German'),('nl-be','Belgium dutch'),('fr-be','Belgium french'),)
#     >>> settings.USE_SHORT_LANGUAGE_CODES = False
#     >>> activate('en-us')
#     >>> fix_language_code('/de/example.html', 'en-us')
#     '/en-us/example.html'
#     >>> settings.USE_SHORT_LANGUAGE_CODES = True
#     >>> fix_language_code('/de/example.html', 'en-us')
#     '/en/example.html'
#     >>> fix_language_code('/en/example.html', 'en-us')
#     '/example.html'
#     >>> # only one language defined
#     >>> settings.LANGUAGES = (('en-us', 'English'), )
#     >>> fix_language_code('/en-us/example.html', 'en-us')
#     '/example.html'
#     
#     :param url: The (absolute) url to be fixed eg. '/hi/how/is/it'.
#     :param current_language: A language code defined in ``settings.LANGUAGES``.
#     :rtype: The fixed url.
#     """
#     stripped_url = strip_language_code(url)
#     if not getattr(settings, 'MASTER_SITE', False) and len(settings.LANGUAGES) == 1:
#         # no MASTER_SITE and only one language, do not add language code to url
#         return stripped_url
#     
#     # add the language code to the url        
#     return u"/%s%s" % (get_shorthand_from_language_code(current_language), stripped_url)


# Path: easymode/views.py
from django import http
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ObjectDoesNotExist
from easymode.utils.languagecode import fix_language_code



@staff_member_required
def preview(request, content_type_id, object_id):
    """
    This is an override for django.views.default.shortcut.
    It assumes that get_absolute_url returns an absolute url, so
    it does not do any of the very elaborate link checking that shortcut does.
    
    This version adds the language code to the url. (/en/blaat/).
    """
    
    try:
        content_type = ContentType.objects.get(pk=content_type_id)
        obj = content_type.get_object_for_this_type(pk=object_id)
    except ObjectDoesNotExist:
        raise http.Http404("Content type %s object %s doesn't exist" % (content_type_id, object_id))
    try:
        absolute_url = obj.get_absolute_url()
    except AttributeError:
        raise http.Http404("%s objects don't have get_absolute_url() methods" % content_type.name)
        
    if absolute_url.startswith('http://') or absolute_url.startswith('https://'):        
        http.HttpResponseRedirect(absolute_url)        
    else:

        absolute_url = fix_language_code(absolute_url, request.LANGUAGE_CODE)                