Commit 5d8dc390 authored by Michal Čihař's avatar Michal Čihař

Use wrapper unit class instead of directly using ttkit

parent 3e6e69f7
......@@ -53,7 +53,7 @@ class FileUnit(object):
'''
self.unit = unit
self.template = template
if unit is None:
if template is not None:
self.mainunit = template
else:
self.mainunit = unit
......@@ -165,6 +165,14 @@ class FileUnit(object):
else:
return self.unit.istranslated()
def is_fuzzy(self):
'''
Checks whether unit is translated.
'''
if self.unit is None:
return False
return self.unit.isfuzzy()
def is_translatable(self):
'''
Checks whether unit is translatable.
......@@ -315,6 +323,36 @@ class FileFormat(object):
'''
self.store.save()
def translatable_units(self):
'''
Generator of translatable units.
'''
if not self.has_template:
for tt_unit in self.store.units:
# Create wrapper object
unit = FileUnit(tt_unit)
# We care only about translatable strings
if not unit.is_translatable():
continue
yield unit
else:
for template_unit in self.template_store.units:
# Create wrapper object (not translated)
unit = FileUnit(None, template_unit)
# We care only about translatable strings
if not unit.is_translatable():
continue
# Get translated unit (if it exists)
unit.unit = self.store.findid(template_unit.getid())
yield unit
@property
def mimetype(self):
'''
......
......@@ -443,35 +443,16 @@ class Translation(models.Model):
# Position of current unit
pos = 1
if not self.store.has_template:
for unit in self.store.store.units:
# We care only about translatable strings
if not is_translatable(unit):
continue
newunit, is_new = Unit.objects.update_from_unit(
self, unit, pos
)
was_new = was_new or (is_new and not newunit.translated)
pos += 1
try:
oldunits.remove(newunit.id)
except:
pass
else:
for template_unit in self.store.template_store.units:
# We care only about translatable strings
if not is_translatable(template_unit):
continue
unit = self.store.store.findid(template_unit.getid())
newunit, is_new = Unit.objects.update_from_unit(
self, unit, pos, template=template_unit
)
was_new = was_new or (is_new and not newunit.translated)
pos += 1
try:
oldunits.remove(newunit.id)
except:
pass
for unit in self.store.translatable_units():
newunit, is_new = Unit.objects.update_from_unit(
self, unit, pos
)
was_new = was_new or (is_new and not newunit.translated)
pos += 1
try:
oldunits.remove(newunit.id)
except:
pass
# Delete not used units
units_to_delete = Unit.objects.filter(
......
......@@ -37,24 +37,21 @@ from trans.data import IGNORE_SIMILAR
from trans.filelock import FileLockException
from trans.util import (
is_plural, split_plural, join_plural,
msg_checksum, get_source, get_target, get_context,
is_translated,
msg_checksum, is_translated,
)
logger = logging.getLogger('weblate')
class UnitManager(models.Manager):
def update_from_unit(self, translation, unit, pos, template=None):
def update_from_unit(self, translation, unit, pos):
'''
Process translation toolkit unit and stores/updates database entry.
'''
if template is None:
src = get_source(unit)
ctx = get_context(unit)
else:
src = get_target(template)
ctx = get_context(template)
src = unit.get_source()
ctx = unit.get_context()
# TODO: move to unit
checksum = msg_checksum(src, ctx)
# Try getting existing unit
......@@ -85,7 +82,7 @@ class UnitManager(models.Manager):
created = True
# Update all details
dbunit.update_from_unit(unit, pos, created, template)
dbunit.update_from_unit(unit, pos, created )
# Return result
return dbunit, created
......@@ -418,47 +415,23 @@ class Unit(models.Model):
self.translation.get_translate_url(), self.checksum
)
def update_from_unit(self, unit, pos, created, template=None):
def update_from_unit(self, unit, pos, created):
'''
Updates Unit from ttkit unit.
'''
# Template is optional
if template is None:
template = unit
# Merge locations
location = ', '.join(template.getlocations())
# Merge flags
if unit is not None and hasattr(unit, 'typecomments'):
flags = ', '.join(unit.typecomments)
elif template is not None and hasattr(template, 'typecomments'):
flags = ', '.join(template.typecomments)
else:
flags = ''
# Get target
target = get_target(unit)
# Get data from unit
if unit is None:
fuzzy = False
translated = False
if template is None:
comment = ''
else:
comment = template.getnotes()
else:
fuzzy = unit.isfuzzy()
translated = is_translated(unit)
comment = unit.getnotes()
if template is not None:
# Avoid duplication in case template has same comments
template_comment = template.getnotes()
if template_comment != comment:
comment = template_comment + ' ' + comment
# Get unit attributes
location = unit.get_locations()
flags = unit.get_flags()
target = unit.get_target()
comment = unit.get_comments()
fuzzy = unit.is_fuzzy()
translated = unit.is_translated()
# Update checks on fuzzy update or on content change
same_content = (target == self.target)
same_fuzzy = (fuzzy == self.fuzzy)
# TODO: move to unit class
if fuzzy and hasattr(unit, 'prev_source'):
if hasattr(unit.prev_source, 'strings'):
previous_source = join_plural(unit.prev_source.strings)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment