from django.db import models from lang.models import Language from util import is_plural, split_plural, join_plural, msg_checksum class TranslationManager(models.Manager): def update_from_blob(self, subproject, code, path, blob, force = False): ''' Parses translation meta info and creates/updates translation object. ''' lang = Language.objects.get(code = code) trans, created = self.get_or_create( language = lang, subproject = subproject, filename = path) trans.update_from_blob(blob, force) class UnitManager(models.Manager): def update_from_unit(self, translation, unit, pos): ''' Process translation toolkit unit and stores/updates database entry. ''' if hasattr(unit.source, 'strings'): src = join_plural(unit.source.strings) else: src = unit.source ctx = unit.getcontext() checksum = msg_checksum(src, ctx) import trans.models try: dbunit = self.get( translation = translation, checksum = checksum) force = False except: dbunit = trans.models.Unit( translation = translation, checksum = checksum, source = src, context = ctx) force = True dbunit.update_from_unit(unit, pos, force) return dbunit def filter_type(self, rqtype): import trans.models if rqtype == 'all': return self.all() elif rqtype == 'fuzzy': return self.filter(fuzzy = True) elif rqtype == 'untranslated': return self.filter(translated = False) elif rqtype == 'suggestions': sample = self.all()[0] sugs = trans.models.Suggestion.objects.filter( language = sample.translation.language, project = sample.translation.subproject.project) sugs = sugs.values_list('checksum', flat = True) return self.filter(checksum__in = sugs)