2012-02-27 16:09:31 +01:00
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
from lang.models import Language
|
|
|
|
|
2012-02-28 11:24:53 +01:00
|
|
|
from util import is_plural, split_plural, join_plural, msg_checksum
|
2012-02-27 17:54:42 +01:00
|
|
|
|
2012-02-27 16:09:31 +01:00
|
|
|
class TranslationManager(models.Manager):
|
2012-02-28 15:21:52 +01:00
|
|
|
def update_from_blob(self, subproject, code, path, blob, force = False):
|
2012-02-27 16:09:31 +01:00
|
|
|
'''
|
|
|
|
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)
|
2012-02-28 15:21:52 +01:00
|
|
|
trans.update_from_blob(blob, force)
|
2012-02-27 16:09:31 +01:00
|
|
|
|
2012-02-27 17:50:47 +01:00
|
|
|
class UnitManager(models.Manager):
|
2012-02-28 15:12:41 +01:00
|
|
|
def update_from_unit(self, translation, unit, pos):
|
2012-02-27 17:50:47 +01:00
|
|
|
'''
|
|
|
|
Process translation toolkit unit and stores/updates database entry.
|
|
|
|
'''
|
2012-03-01 14:45:47 +01:00
|
|
|
if hasattr(unit.source, 'strings'):
|
|
|
|
src = join_plural(unit.source.strings)
|
|
|
|
else:
|
|
|
|
src = unit.source
|
2012-02-27 17:50:47 +01:00
|
|
|
ctx = unit.getcontext()
|
2012-02-28 11:24:53 +01:00
|
|
|
checksum = msg_checksum(src, ctx)
|
2012-02-27 18:05:42 +01:00
|
|
|
import trans.models
|
|
|
|
try:
|
|
|
|
dbunit = self.get(
|
|
|
|
translation = translation,
|
2012-02-28 11:24:53 +01:00
|
|
|
checksum = checksum)
|
2012-02-27 18:05:42 +01:00
|
|
|
force = False
|
|
|
|
except:
|
|
|
|
dbunit = trans.models.Unit(
|
|
|
|
translation = translation,
|
2012-02-28 11:24:53 +01:00
|
|
|
checksum = checksum,
|
2012-02-27 18:05:42 +01:00
|
|
|
source = src,
|
|
|
|
context = ctx)
|
|
|
|
force = True
|
|
|
|
|
2012-02-28 15:12:41 +01:00
|
|
|
dbunit.update_from_unit(unit, pos, force)
|
2012-02-27 17:50:47 +01:00
|
|
|
return dbunit
|
2012-02-28 15:00:23 +01:00
|
|
|
|
|
|
|
def filter_type(self, rqtype):
|
2012-03-01 14:03:23 +01:00
|
|
|
import trans.models
|
2012-02-28 15:00:23 +01:00
|
|
|
if rqtype == 'all':
|
|
|
|
return self.all()
|
|
|
|
elif rqtype == 'fuzzy':
|
|
|
|
return self.filter(fuzzy = True)
|
|
|
|
elif rqtype == 'untranslated':
|
|
|
|
return self.filter(translated = False)
|
2012-03-01 14:03:23 +01:00
|
|
|
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)
|
|
|
|
|