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): ...@@ -53,7 +53,7 @@ class FileUnit(object):
''' '''
self.unit = unit self.unit = unit
self.template = template self.template = template
if unit is None: if template is not None:
self.mainunit = template self.mainunit = template
else: else:
self.mainunit = unit self.mainunit = unit
...@@ -165,6 +165,14 @@ class FileUnit(object): ...@@ -165,6 +165,14 @@ class FileUnit(object):
else: else:
return self.unit.istranslated() 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): def is_translatable(self):
''' '''
Checks whether unit is translatable. Checks whether unit is translatable.
...@@ -315,6 +323,36 @@ class FileFormat(object): ...@@ -315,6 +323,36 @@ class FileFormat(object):
''' '''
self.store.save() 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 @property
def mimetype(self): def mimetype(self):
''' '''
......
...@@ -443,35 +443,16 @@ class Translation(models.Model): ...@@ -443,35 +443,16 @@ class Translation(models.Model):
# Position of current unit # Position of current unit
pos = 1 pos = 1
if not self.store.has_template: for unit in self.store.translatable_units():
for unit in self.store.store.units: newunit, is_new = Unit.objects.update_from_unit(
# We care only about translatable strings self, unit, pos
if not is_translatable(unit): )
continue was_new = was_new or (is_new and not newunit.translated)
newunit, is_new = Unit.objects.update_from_unit( pos += 1
self, unit, pos try:
) oldunits.remove(newunit.id)
was_new = was_new or (is_new and not newunit.translated) except:
pos += 1 pass
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
# Delete not used units # Delete not used units
units_to_delete = Unit.objects.filter( units_to_delete = Unit.objects.filter(
......
...@@ -37,24 +37,21 @@ from trans.data import IGNORE_SIMILAR ...@@ -37,24 +37,21 @@ from trans.data import IGNORE_SIMILAR
from trans.filelock import FileLockException from trans.filelock import FileLockException
from trans.util import ( from trans.util import (
is_plural, split_plural, join_plural, is_plural, split_plural, join_plural,
msg_checksum, get_source, get_target, get_context, msg_checksum, is_translated,
is_translated,
) )
logger = logging.getLogger('weblate') logger = logging.getLogger('weblate')
class UnitManager(models.Manager): 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. Process translation toolkit unit and stores/updates database entry.
''' '''
if template is None: src = unit.get_source()
src = get_source(unit) ctx = unit.get_context()
ctx = get_context(unit)
else: # TODO: move to unit
src = get_target(template)
ctx = get_context(template)
checksum = msg_checksum(src, ctx) checksum = msg_checksum(src, ctx)
# Try getting existing unit # Try getting existing unit
...@@ -85,7 +82,7 @@ class UnitManager(models.Manager): ...@@ -85,7 +82,7 @@ class UnitManager(models.Manager):
created = True created = True
# Update all details # Update all details
dbunit.update_from_unit(unit, pos, created, template) dbunit.update_from_unit(unit, pos, created )
# Return result # Return result
return dbunit, created return dbunit, created
...@@ -418,47 +415,23 @@ class Unit(models.Model): ...@@ -418,47 +415,23 @@ class Unit(models.Model):
self.translation.get_translate_url(), self.checksum 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. Updates Unit from ttkit unit.
''' '''
# Template is optional # Get unit attributes
if template is None: location = unit.get_locations()
template = unit flags = unit.get_flags()
# Merge locations target = unit.get_target()
location = ', '.join(template.getlocations()) comment = unit.get_comments()
# Merge flags fuzzy = unit.is_fuzzy()
if unit is not None and hasattr(unit, 'typecomments'): translated = unit.is_translated()
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
# Update checks on fuzzy update or on content change # Update checks on fuzzy update or on content change
same_content = (target == self.target) same_content = (target == self.target)
same_fuzzy = (fuzzy == self.fuzzy) same_fuzzy = (fuzzy == self.fuzzy)
# TODO: move to unit class
if fuzzy and hasattr(unit, 'prev_source'): if fuzzy and hasattr(unit, 'prev_source'):
if hasattr(unit.prev_source, 'strings'): if hasattr(unit.prev_source, 'strings'):
previous_source = join_plural(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