<s> import inspect <EOL> from django . core . urlresolvers import reverse <EOL> from django . template import Node , Library , TemplateSyntaxError <EOL> from django . utils . translation import get_language <EOL> from django . utils import six <EOL> from parler . models import TranslatableModel , TranslationDoesNotExist <EOL> from parler . utils . context import switch_language , smart_override <EOL> register = Library ( ) <EOL> class ObjectLanguageNode ( Node ) : <EOL> def __init__ ( self , nodelist , object_var , language_var = None ) : <EOL> self . nodelist = nodelist <EOL> self . object_var = object_var <EOL> self . language_var = language_var <EOL> def render ( self , context ) : <EOL> object = self . object_var . resolve ( context ) <EOL> new_language = self . language_var . resolve ( context ) if self . language_var else get_language ( ) <EOL> if not isinstance ( object , TranslatableModel ) : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" . format ( object ) ) <EOL> with switch_language ( object , new_language ) : <EOL> output = self . nodelist . render ( context ) <EOL> return output <EOL> @ register . tag <EOL> def objectlanguage ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . split_contents ( ) <EOL> if len ( bits ) == <NUM_LIT:2> : <EOL> object_var = parser . compile_filter ( bits [ <NUM_LIT:1> ] ) <EOL> language_var = None <EOL> elif len ( bits ) == <NUM_LIT:3> : <EOL> object_var = parser . compile_filter ( bits [ <NUM_LIT:1> ] ) <EOL> language_var = parser . compile_filter ( bits [ <NUM_LIT:2> ] ) <EOL> else : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" % bits [ <NUM_LIT:0> ] ) <EOL> nodelist = parser . parse ( ( '<STR_LIT>' , ) ) <EOL> parser . delete_first_token ( ) <EOL> return ObjectLanguageNode ( nodelist , object_var , language_var ) <EOL> @ register . assignment_tag ( takes_context = True ) <EOL> def get_translated_url ( context , lang_code , object = None ) : <EOL> """<STR_LIT>""" <EOL> view = context . get ( '<STR_LIT>' , None ) <EOL> request = context [ '<STR_LIT>' ] <EOL> if object is not None : <EOL> qs = '<STR_LIT>' <EOL> else : <EOL> object = context . get ( '<STR_LIT:object>' , None ) or context . get ( '<STR_LIT>' , None ) or context . get ( '<STR_LIT>' , None ) <EOL> qs = request . META . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> try : <EOL> if view is not None : <EOL> get_view_url = getattr ( view , '<STR_LIT>' , None ) <EOL> if get_view_url : <EOL> with smart_override ( lang_code ) : <EOL> return _url_qs ( view . get_view_url ( ) , qs ) <EOL> if object is None : <EOL> object = getattr ( view , '<STR_LIT:object>' , None ) <EOL> if object is not None and hasattr ( object , '<STR_LIT>' ) : <EOL> if isinstance ( object , TranslatableModel ) : <EOL> with switch_language ( object , lang_code ) : <EOL> return _url_qs ( object . get_absolute_url ( ) , qs ) <EOL> else : <EOL> with smart_override ( lang_code ) : <EOL> return _url_qs ( object . get_absolute_url ( ) , qs ) <EOL> except TranslationDoesNotExist : <EOL> return '<STR_LIT>' <EOL> resolver_match = _get_resolver_match ( request ) <EOL> if resolver_match is None : <EOL> return '<STR_LIT>' <EOL> with smart_override ( lang_code ) : <EOL> clean_kwargs = _cleanup_urlpattern_kwargs ( resolver_match . kwargs ) <EOL> return _url_qs ( reverse ( resolver_match . view_name , args = resolver_match . args , kwargs = clean_kwargs , current_app = resolver_match . app_name ) , qs ) <EOL> def _url_qs ( url , qs ) : <EOL> if qs and '<STR_LIT:?>' not in url : <EOL> return url + "<STR_LIT:?>" + qs <EOL> else : <EOL> return url <EOL> @ register . filter <EOL> def get_translated_field ( object , field ) : <EOL> """<STR_LIT>""" <EOL> return object . safe_translation_getter ( field , language_code = get_language ( ) ) <EOL> def _cleanup_urlpattern_kwargs ( kwargs ) : <EOL> return dict ( ( k , v ) for k , v in six . iteritems ( kwargs ) if not inspect . isclass ( v ) ) <EOL> def _get_resolver_match ( request ) : <EOL> try : <EOL> return request . resolver_match <EOL> except AttributeError : <EOL> from django . core . urlresolvers import resolve <EOL> return resolve ( request . path_info ) </s>
