<s> """<STR_LIT>""" <EOL> from django . utils . text import capfirst , get_text_list <EOL> from django . contrib . admin . util import flatten_fieldsets <EOL> from django . http import HttpResponseRedirect <EOL> from django . utils . encoding import force_unicode <EOL> import re <EOL> from copy import deepcopy <EOL> from django . conf import settings <EOL> from django import forms <EOL> from django . contrib import admin <EOL> from django . db . models import Model <EOL> from django . forms . util import ErrorList , ValidationError <EOL> from django . forms . models import BaseInlineFormSet , ModelFormMetaclass <EOL> from django . utils . translation import ugettext as _ <EOL> from django . template . loader import find_template <EOL> from django . template import TemplateDoesNotExist <EOL> from multilingual . languages import get_default_language <EOL> from multilingual . utils import GLL <EOL> MULTILINGUAL_PREFIX = '<STR_LIT>' <EOL> MULTILINGUAL_INLINE_PREFIX = '<STR_LIT>' <EOL> def gll ( func ) : <EOL> def wrapped ( cls , request , * args , ** kwargs ) : <EOL> cls . use_language = request . GET . get ( '<STR_LIT>' , request . GET . get ( '<STR_LIT>' , get_default_language ( ) ) ) <EOL> GLL . lock ( cls . use_language ) <EOL> resp = func ( cls , request , * args , ** kwargs ) <EOL> GLL . release ( ) <EOL> return resp <EOL> wrapped . __name__ = func . __name__ <EOL> wrapped . __doc__ = func . __doc__ <EOL> return wrapped <EOL> def standard_get_fill_check_field ( stdopts ) : <EOL> if hasattr ( stdopts , '<STR_LIT>' ) : <EOL> opts = stdopts . translation_model . _meta <EOL> for field in opts . fields : <EOL> if field . name in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> continue <EOL> if not ( field . blank or field . null ) : <EOL> return field . name <EOL> return None <EOL> def relation_hack ( form , fields , prefix = '<STR_LIT>' ) : <EOL> opts = form . instance . _meta <EOL> localm2m = [ m2m . attname for m2m in opts . local_many_to_many ] <EOL> externalfk = [ obj . field . related_query_name ( ) for obj in opts . get_all_related_objects ( ) ] <EOL> externalm2m = [ m2m . get_accessor_name ( ) for m2m in opts . get_all_related_many_to_many_objects ( ) ] <EOL> for name , db_field in fields : <EOL> full_name = '<STR_LIT>' % ( prefix , name ) <EOL> if full_name in form . fields : <EOL> value = getattr ( form . instance , name , '<STR_LIT>' ) <EOL> if isinstance ( value , Model ) : <EOL> value = value . pk <EOL> elif name in localm2m : <EOL> value = value . all ( ) <EOL> elif name in externalfk : <EOL> value = value . all ( ) <EOL> elif name in externalm2m : <EOL> value = value . all ( ) <EOL> form . fields [ full_name ] . initial = value <EOL> class MultilingualInlineModelForm ( forms . ModelForm ) : <EOL> def __init__ ( self , data = None , files = None , auto_id = '<STR_LIT>' , prefix = None , <EOL> initial = None , error_class = ErrorList , label_suffix = '<STR_LIT::>' , <EOL> empty_permitted = False , instance = None ) : <EOL> """<STR_LIT>""" <EOL> super ( MultilingualInlineModelForm , self ) . __init__ ( data , files , auto_id , <EOL> prefix , initial , error_class , label_suffix , empty_permitted , instance ) <EOL> if self . instance . pk : <EOL> relation_hack ( self , get_translated_fields ( self . instance ) , MULTILINGUAL_INLINE_PREFIX ) <EOL> class MultilingualInlineFormSet ( BaseInlineFormSet ) : <EOL> def get_queryset ( self ) : <EOL> if self . queryset is not None : <EOL> qs = self . queryset <EOL> else : <EOL> qs = self . model . _default_manager . get_query_set ( ) <EOL> if not qs . ordered : <EOL> qs = qs . order_by ( self . model . _meta . pk . name ) <EOL> if self . max_num > <NUM_LIT:0> : <EOL> _queryset = qs [ : self . max_num ] <EOL> else : <EOL> _queryset = qs <EOL> return _queryset <EOL> def save_new ( self , form , commit = True ) : <EOL> """<STR_LIT>""" <EOL> kwargs = { self . fk . get_attname ( ) : self . instance . pk } <EOL> new_obj = self . model ( ** kwargs ) <EOL> self . _prepare_multilingual_object ( new_obj , form ) <EOL> return forms . save_instance ( form , new_obj , exclude = [ self . _pk_field . name ] , commit = commit ) <EOL> def save_existing ( self , form , instance , commit = True ) : <EOL> """<STR_LIT>""" <EOL> self . _prepare_multilingual_object ( instance , form ) <EOL> return forms . save_instance ( form , instance , exclude = [ self . _pk_field . name ] , commit = commit ) <EOL> def _prepare_multilingual_object ( self , obj , form ) : <EOL> opts = obj . _meta <EOL> for realname , fieldname in self . ml_fields . items ( ) : <EOL> field = opts . get_field_by_name ( realname ) [ <NUM_LIT:0> ] <EOL> m = re . match ( r'<STR_LIT>' % MULTILINGUAL_INLINE_PREFIX , fieldname ) <EOL> if m : <EOL> field . save_form_data ( self . instance , form . cleaned_data [ fieldname ] ) <EOL> setattr ( obj , realname , getattr ( self . instance , realname . rsplit ( '<STR_LIT:_>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] ) ) <EOL> class MultilingualInlineAdmin ( admin . TabularInline ) : <EOL> formset = MultilingualInlineFormSet <EOL> form = MultilingualInlineModelForm <EOL> template = '<STR_LIT>' <EOL> inline_css_class = None <EOL> use_language = None <EOL> fill_check_field = None <EOL> def __init__ ( self , parent_model , admin_site ) : <EOL> super ( MultilingualInlineAdmin , self ) . __init__ ( parent_model , admin_site ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> self . fields = self . use_fields <EOL> def get_formset ( self , request , obj = None , ** kwargs ) : <EOL> FormSet = super ( MultilingualInlineAdmin , self ) . get_formset ( request , obj , ** kwargs ) <EOL> FormSet . use_language = GLL . language_code <EOL> FormSet . ml_fields = { } <EOL> for name , field in get_translated_fields ( self . model , GLL . language_code ) : <EOL> fieldname = '<STR_LIT>' % ( MULTILINGUAL_INLINE_PREFIX , name ) <EOL> FormSet . form . base_fields [ fieldname ] = self . formfield_for_dbfield ( field , request = request ) <EOL> FormSet . ml_fields [ name ] = fieldname <EOL> return FormSet <EOL> def queryset ( self , request ) : <EOL> """<STR_LIT>""" <EOL> qs = super ( MultilingualInlineAdmin , self ) . queryset ( request ) <EOL> return qs . filter ( translations__language_code = GLL . language_code ) . distinct ( ) <EOL> def get_fill_check_field ( self ) : <EOL> if self . fill_check_field is None : <EOL> self . fill_check_field = standard_get_fill_check_field ( self . model . _meta ) <EOL> return self . fill_check_field <EOL> class MultilingualModelAdminForm ( forms . ModelForm ) : <EOL> def __init__ ( self , data = None , files = None , auto_id = '<STR_LIT>' , prefix = None , <EOL> initial = None , error_class = ErrorList , label_suffix = '<STR_LIT::>' , <EOL> empty_permitted = False , instance = None ) : <EOL> """<STR_LIT>""" <EOL> super ( MultilingualModelAdminForm , self ) . __init__ ( data , files , auto_id , prefix , <EOL> initial , error_class , label_suffix , <EOL> empty_permitted , instance ) <EOL> if self . instance . pk : <EOL> fields = [ ( f , getattr ( self . instance , "<STR_LIT>" % ( f , GLL . language_code ) , '<STR_LIT>' ) ) for f in self . ml_fields ] <EOL> relation_hack ( self , fields ) <EOL> def clean ( self ) : <EOL> cleaned_data = super ( MultilingualModelAdminForm , self ) . clean ( ) <EOL> self . validate_ml_unique ( ) <EOL> return cleaned_data <EOL> def validate_ml_unique ( self ) : <EOL> form_errors = [ ] <EOL> if not hasattr ( self . instance . _meta , '<STR_LIT>' ) : <EOL> return <EOL> for check in self . instance . _meta . translation_model . _meta . unique_together [ : ] : <EOL> lookup_kwargs = { '<STR_LIT>' : GLL . language_code } <EOL> for field_name in check : <EOL> if self . cleaned_data . get ( field_name ) is not None : <EOL> lookup_kwargs [ field_name ] = self . cleaned_data . get ( field_name ) <EOL> if len ( check ) == <NUM_LIT:2> and '<STR_LIT>' in check and '<STR_LIT>' in check : <EOL> continue <EOL> qs = self . instance . _meta . translation_model . objects . filter ( ** lookup_kwargs ) <EOL> if self . instance . pk is not None : <EOL> qs = qs . exclude ( master = self . instance . pk ) <EOL> if qs . count ( ) : <EOL> model_name = capfirst ( self . instance . _meta . verbose_name ) <EOL> field_labels = [ ] <EOL> for field_name in check : <EOL> if field_name == "<STR_LIT>" : <EOL> field_labels . append ( _ ( "<STR_LIT>" ) ) <EOL> elif field_name == "<STR_LIT>" : <EOL> continue <EOL> else : <EOL> field_labels . append ( self . instance . _meta . translation_model . _meta . get_field_by_name ( field_name ) [ <NUM_LIT:0> ] . verbose_name ) <EOL> field_labels = get_text_list ( field_labels , _ ( '<STR_LIT>' ) ) <EOL> form_errors . append ( <EOL> _ ( u"<STR_LIT>" ) % { '<STR_LIT>' : unicode ( model_name ) , <EOL> '<STR_LIT>' : unicode ( field_labels ) } <EOL> ) <EOL> if form_errors : <EOL> raise ValidationError ( form_errors ) <EOL> def save ( self , commit = True ) : <EOL> self . _prepare_multilingual_object ( self . instance , self ) <EOL> return super ( MultilingualModelAdminForm , self ) . save ( commit ) <EOL> def _prepare_multilingual_object ( self , obj , form ) : <EOL> opts = self . instance . _meta <EOL> for name in self . ml_fields : <EOL> field = opts . get_field_by_name ( name ) [ <NUM_LIT:0> ] <EOL> field . save_form_data ( self . instance , form . cleaned_data [ name ] ) <EOL> setattr ( obj , "<STR_LIT>" % ( name , GLL . language_code ) , getattr ( self . instance , name ) ) <EOL> class MultilingualModelAdmin ( admin . ModelAdmin ) : <EOL> change_form_template = "<STR_LIT>" <EOL> form = MultilingualModelAdminForm <EOL> _multilingual_model_admin = True <EOL> use_language = None <EOL> fill_check_field = None <EOL> _use_hacks = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> class Media : <EOL> css = { <EOL> '<STR_LIT:all>' : ( '<STR_LIT>' % settings . MEDIA_URL , ) <EOL> } <EOL> def __init__ ( self , model , admin_site ) : <EOL> for attr in self . _use_hacks : <EOL> if hasattr ( self , '<STR_LIT>' % attr ) : <EOL> setattr ( self , attr , getattr ( self , '<STR_LIT>' % attr ) ) <EOL> super ( MultilingualModelAdmin , self ) . __init__ ( model , admin_site ) <EOL> def get_fill_check_field ( self ) : <EOL> if self . fill_check_field is None : <EOL> self . fill_check_field = standard_get_fill_check_field ( self . model . _meta ) <EOL> return self . fill_check_field <EOL> def get_form ( self , request , obj = None , ** kwargs ) : <EOL> for inline in self . inline_instances : <EOL> if isinstance ( inline , MultilingualInlineAdmin ) : <EOL> inline . use_language = GLL . language_code <EOL> Form = super ( MultilingualModelAdmin , self ) . get_form ( request , obj , ** kwargs ) <EOL> Form . ml_fields = { } <EOL> for name , field in get_default_translated_fields ( self . model ) : <EOL> if not field . editable : <EOL> continue <EOL> form_field = self . formfield_for_dbfield ( field , request = request ) <EOL> local_name = "<STR_LIT>" % ( name , GLL . language_code ) <EOL> Form . ml_fields [ name ] = form_field <EOL> Form . base_fields [ name ] = form_field <EOL> Form . use_language = GLL . language_code <EOL> return Form <EOL> def placeholder_plugin_filter ( self , request , queryset ) : <EOL> """<STR_LIT>""" <EOL> if not request : <EOL> return queryset <EOL> if GLL . is_active : <EOL> return queryset . filter ( language = GLL . language_code ) <EOL> return queryset <EOL> @ gll <EOL> def change_view ( self , * args , ** kwargs ) : <EOL> return super ( MultilingualModelAdmin , self ) . change_view ( * args , ** kwargs ) <EOL> @ gll <EOL> def add_view ( self , * args , ** kwargs ) : <EOL> return super ( MultilingualModelAdmin , self ) . add_view ( * args , ** kwargs ) <EOL> @ gll <EOL> def delete_view ( self , * args , ** kwargs ) : <EOL> return super ( MultilingualModelAdmin , self ) . delete_view ( * args , ** kwargs ) <EOL> def render_change_form ( self , request , context , add = False , change = False , form_url = '<STR_LIT>' , obj = None ) : <EOL> filled_languages = [ ] <EOL> fill_check_field = self . get_fill_check_field ( ) <EOL> if obj and fill_check_field is not None : <EOL> from django . db . models . fields import CharField <EOL> kwargs = { '<STR_LIT>' % fill_check_field : False } <EOL> if isinstance ( self . model . _meta . translation_model . _meta . get_field_by_name ( fill_check_field ) [ <NUM_LIT:0> ] , CharField ) : <EOL> kwargs [ '<STR_LIT>' % fill_check_field ] = '<STR_LIT>' <EOL> filled_languages = [ t [ <NUM_LIT:0> ] for t in obj . translations . filter ( ** kwargs ) . values_list ( '<STR_LIT>' ) ] <EOL> context . update ( { <EOL> '<STR_LIT>' : GLL . language_code , <EOL> '<STR_LIT>' : GLL . language_code , <EOL> '<STR_LIT>' : filled_languages , <EOL> '<STR_LIT>' : self . get_old_template ( ) , <EOL> } ) <EOL> return super ( MultilingualModelAdmin , self ) . render_change_form ( request , context , add , change , form_url , obj ) <EOL> def get_old_template ( self ) : <EOL> opts = self . model . _meta <EOL> app_label = opts . app_label <EOL> search_templates = [ <EOL> "<STR_LIT>" % ( app_label , opts . object_name . lower ( ) ) , <EOL> "<STR_LIT>" % app_label , <EOL> "<STR_LIT>" <EOL> ] <EOL> for template in search_templates : <EOL> try : <EOL> find_template ( template ) <EOL> return template <EOL> except TemplateDoesNotExist : <EOL> pass <EOL> def response_change ( self , request , obj ) : <EOL> if request . POST . has_key ( "<STR_LIT>" ) : <EOL> opts = obj . _meta <EOL> msg = _ ( '<STR_LIT>' ) % { '<STR_LIT:name>' : force_unicode ( opts . verbose_name ) , '<STR_LIT>' : force_unicode ( obj ) } <EOL> self . message_user ( request , msg + '<STR_LIT:U+0020>' + _ ( "<STR_LIT>" ) ) <EOL> lang , path = request . GET . get ( '<STR_LIT>' , get_default_language ( ) ) , request . path <EOL> if lang : <EOL> lang = "<STR_LIT>" % lang <EOL> if request . REQUEST . has_key ( '<STR_LIT>' ) : <EOL> path += "<STR_LIT>" + "<STR_LIT>" % lang <EOL> else : <EOL> path += "<STR_LIT>" % lang <EOL> return HttpResponseRedirect ( path ) <EOL> return super ( MultilingualModelAdmin , self ) . response_change ( request , obj ) <EOL> def get_translated_fields ( model , language = None ) : <EOL> meta = model . _meta <EOL> if not hasattr ( meta , '<STR_LIT>' ) : <EOL> if hasattr ( meta , '<STR_LIT>' ) : <EOL> meta = meta . translation_model . _meta <EOL> else : <EOL> return <EOL> if not language : <EOL> for name , ( field , non_default ) in meta . translated_fields . items ( ) : <EOL> if non_default : <EOL> yield name , field <EOL> else : <EOL> for field in meta . fields : <EOL> if field . primary_key : <EOL> continue <EOL> name = field . name + "<STR_LIT>" % language <EOL> field = meta . translated_fields . get ( name , None ) <EOL> if field : <EOL> yield name , field [ <NUM_LIT:0> ] <EOL> def get_default_translated_fields ( model ) : <EOL> if hasattr ( model . _meta , '<STR_LIT>' ) : <EOL> for name , ( field , non_default ) in model . _meta . translation_model . _meta . translated_fields . items ( ) : <EOL> if not non_default : <EOL> yield name , field </s>
