<s> from django . shortcuts import render_to_response <EOL> from django . http import HttpResponseRedirect , Http404 <EOL> from django . core . urlresolvers import reverse <EOL> from django . template . context import RequestContext <EOL> from django . conf import settings <EOL> from django . contrib . auth . decorators import user_passes_test <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from datatrans import utils <EOL> from datatrans . models import KeyValue <EOL> from datatrans . utils import count_model_words <EOL> def can_translate ( user ) : <EOL> if not user . is_authenticated ( ) : <EOL> return False <EOL> elif user . is_superuser : <EOL> return True <EOL> else : <EOL> group_name = getattr ( settings , '<STR_LIT>' , None ) <EOL> if group_name : <EOL> from django . contrib . auth . models import Group <EOL> translators = Group . objects . get ( name = group_name ) <EOL> return translators in user . groups . all ( ) <EOL> else : <EOL> return user . is_staff <EOL> def _get_model_slug ( model ) : <EOL> ct = ContentType . objects . get_for_model ( model ) <EOL> return u'<STR_LIT>' % ( ct . app_label , ct . model ) <EOL> def _get_model_entry ( slug ) : <EOL> app_label , model_slug = slug . split ( '<STR_LIT:.>' ) <EOL> try : <EOL> ct = ContentType . objects . get ( app_label = app_label , model = model_slug ) <EOL> except ContentType . DoesNotExist : <EOL> raise Http404 ( u'<STR_LIT>' ) <EOL> model_class = ct . model_class ( ) <EOL> registry = utils . get_registry ( ) <EOL> if not model_class in registry : <EOL> raise Http404 ( u'<STR_LIT>' ) <EOL> return model_class <EOL> def _get_model_stats ( model , filter = lambda x : x ) : <EOL> default_lang = utils . get_default_language ( ) <EOL> registry = utils . get_registry ( ) <EOL> keyvalues = filter ( KeyValue . objects . for_model ( model , registry [ model ] . values ( ) ) . exclude ( language = default_lang ) ) <EOL> total = keyvalues . count ( ) <EOL> done = keyvalues . filter ( edited = True , fuzzy = False ) . count ( ) <EOL> return ( done * <NUM_LIT:100> / total if total > <NUM_LIT:0> else <NUM_LIT:0> , done , total ) <EOL> @ user_passes_test ( can_translate , settings . LOGIN_URL ) <EOL> def model_list ( request ) : <EOL> """<STR_LIT>""" <EOL> registry = utils . get_registry ( ) <EOL> default_lang = utils . get_default_language ( ) <EOL> languages = [ l for l in settings . LANGUAGES if l [ <NUM_LIT:0> ] != default_lang ] <EOL> models = [ { '<STR_LIT>' : _get_model_slug ( model ) , <EOL> '<STR_LIT>' : u'<STR_LIT:%s>' % model . _meta . verbose_name , <EOL> '<STR_LIT>' : [ u'<STR_LIT:%s>' % f . verbose_name for f in registry [ model ] . values ( ) ] , <EOL> '<STR_LIT>' : _get_model_stats ( model ) , <EOL> '<STR_LIT>' : count_model_words ( model ) , <EOL> '<STR_LIT>' : [ <EOL> ( <EOL> l [ <NUM_LIT:0> ] , <EOL> l [ <NUM_LIT:1> ] , <EOL> _get_model_stats ( <EOL> model , <EOL> filter = lambda x : x . filter ( language = l [ <NUM_LIT:0> ] ) <EOL> ) , <EOL> ) <EOL> for l in languages <EOL> ] , <EOL> } for model in registry ] <EOL> total_words = sum ( m [ '<STR_LIT>' ] for m in models ) <EOL> context = { '<STR_LIT>' : models , '<STR_LIT>' : total_words } <EOL> return render_to_response ( '<STR_LIT>' , <EOL> context , <EOL> context_instance = RequestContext ( request ) ) <EOL> def commit_translations ( request ) : <EOL> translations = [ <EOL> ( KeyValue . objects . get ( pk = int ( k . split ( '<STR_LIT:_>' ) [ <NUM_LIT:1> ] ) ) , v ) <EOL> for k , v in request . POST . items ( ) if '<STR_LIT>' in k ] <EOL> for keyvalue , translation in translations : <EOL> empty = '<STR_LIT>' % keyvalue . pk in request . POST <EOL> ignore = '<STR_LIT>' % keyvalue . pk in request . POST <EOL> if translation != '<STR_LIT>' or empty or ignore : <EOL> if keyvalue . value != translation : <EOL> if not ignore : <EOL> keyvalue . value = translation <EOL> keyvalue . fuzzy = False <EOL> if ignore : <EOL> keyvalue . fuzzy = False <EOL> keyvalue . edited = True <EOL> keyvalue . save ( ) <EOL> def get_context_object ( model , fields , language , default_lang , object ) : <EOL> object_item = { } <EOL> object_item [ '<STR_LIT:name>' ] = unicode ( object ) <EOL> object_item [ '<STR_LIT:id>' ] = object . id <EOL> object_item [ '<STR_LIT>' ] = object_fields = [ ] <EOL> for field in fields . values ( ) : <EOL> key = model . objects . filter ( pk = object . pk ) . values ( field . name ) [ <NUM_LIT:0> ] [ field . name ] <EOL> original = KeyValue . objects . get_keyvalue ( key , default_lang , object , field . name ) <EOL> translation = KeyValue . objects . get_keyvalue ( key , language , object , field . name ) <EOL> object_fields . append ( { <EOL> '<STR_LIT:name>' : field . name , <EOL> '<STR_LIT>' : unicode ( field . verbose_name ) , <EOL> '<STR_LIT>' : original , <EOL> '<STR_LIT>' : translation <EOL> } ) <EOL> return object_item <EOL> def needs_translation ( model , fields , language , object ) : <EOL> for field in fields . values ( ) : <EOL> key = model . objects . filter ( pk = object . pk ) . values ( field . name ) [ <NUM_LIT:0> ] [ field . name ] <EOL> translation = KeyValue . objects . get_keyvalue ( key , language ) <EOL> if not translation . edited : <EOL> return True <EOL> return False <EOL> def editor ( request , model , language , objects ) : <EOL> registry = utils . get_registry ( ) <EOL> fields = registry [ model ] <EOL> default_lang = utils . get_default_language ( ) <EOL> model_name = u'<STR_LIT:%s>' % model . _meta . verbose_name <EOL> first_unedited_translation = None <EOL> object_list = [ ] <EOL> for object in objects : <EOL> context_object = get_context_object ( <EOL> model , fields , language , default_lang , object ) <EOL> object_list . append ( context_object ) <EOL> if first_unedited_translation is None : <EOL> for field in context_object [ '<STR_LIT>' ] : <EOL> tr = field [ '<STR_LIT>' ] <EOL> if not tr . edited : <EOL> first_unedited_translation = tr <EOL> break <EOL> context = { '<STR_LIT>' : model_name , <EOL> '<STR_LIT>' : object_list , <EOL> '<STR_LIT>' : default_lang , <EOL> '<STR_LIT>' : language , <EOL> '<STR_LIT>' : _get_model_stats ( <EOL> model , lambda x : x . filter ( language = language ) ) , <EOL> '<STR_LIT>' : first_unedited_translation } <EOL> return render_to_response ( <EOL> '<STR_LIT>' , context , <EOL> context_instance = RequestContext ( request ) ) <EOL> def selector ( request , model , language , objects ) : <EOL> fields = utils . get_registry ( ) [ model ] <EOL> for object in objects : <EOL> if needs_translation ( model , fields , language , object ) : <EOL> object . todo = True <EOL> context = { <EOL> '<STR_LIT>' : model . __name__ , <EOL> '<STR_LIT>' : objects <EOL> } <EOL> return render_to_response ( <EOL> '<STR_LIT>' , context , <EOL> context_instance = RequestContext ( request ) ) <EOL> @ user_passes_test ( can_translate , settings . LOGIN_URL ) <EOL> def object_detail ( request , slug , language , object_id ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> commit_translations ( request ) <EOL> return HttpResponseRedirect ( '<STR_LIT:.>' ) <EOL> model = _get_model_entry ( slug ) <EOL> objects = model . objects . filter ( id = int ( object_id ) ) <EOL> return editor ( request , model , language , objects ) <EOL> @ user_passes_test ( can_translate , settings . LOGIN_URL ) <EOL> def model_detail ( request , slug , language ) : <EOL> '''<STR_LIT>''' <EOL> if request . method == '<STR_LIT:POST>' : <EOL> commit_translations ( request ) <EOL> return HttpResponseRedirect ( '<STR_LIT:.>' ) <EOL> model = _get_model_entry ( slug ) <EOL> meta = utils . get_meta ( ) [ model ] <EOL> objects = model . objects . all ( ) <EOL> if getattr ( meta , '<STR_LIT>' , False ) : <EOL> return selector ( request , model , language , objects ) <EOL> else : <EOL> return editor ( request , model , language , objects ) <EOL> @ user_passes_test ( can_translate , settings . LOGIN_URL ) <EOL> def make_messages ( request ) : <EOL> utils . make_messages ( ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> @ user_passes_test ( can_translate , settings . LOGIN_URL ) <EOL> def obsolete_list ( request ) : <EOL> from django . db . models import Q <EOL> default_lang = utils . get_default_language ( ) <EOL> all_obsoletes = utils . find_obsoletes ( ) . order_by ( '<STR_LIT>' ) <EOL> obsoletes = all_obsoletes . filter ( Q ( edited = True ) | Q ( language = default_lang ) ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> all_obsoletes . delete ( ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> context = { '<STR_LIT>' : obsoletes } <EOL> return render_to_response ( '<STR_LIT>' , context , context_instance = RequestContext ( request ) ) </s>
