Faster processing by avoiding two writes on creating new entry

This commit is contained in:
Michal Čihař 2012-02-27 18:05:42 +01:00
parent 3cce2540f3
commit 74afdf1f3a
2 changed files with 18 additions and 8 deletions

View File

@ -23,9 +23,19 @@ class UnitManager(models.Manager):
''' '''
src = join_plural(unit.source.strings) src = join_plural(unit.source.strings)
ctx = unit.getcontext() ctx = unit.getcontext()
dbunit, created = self.get_or_create( import trans.models
translation = translation, try:
source = src, dbunit = self.get(
context = ctx) translation = translation,
dbunit.update_from_unit(unit) source = src,
context = ctx)
force = False
except:
dbunit = trans.models.Unit(
translation = translation,
source = src,
context = ctx)
force = True
dbunit.update_from_unit(unit, force)
return dbunit return dbunit

View File

@ -203,18 +203,18 @@ class Unit(models.Model):
objects = UnitManager() objects = UnitManager()
def update_from_unit(self, unit): def update_from_unit(self, unit, force):
location = ', '.join(unit.getlocations()) location = ', '.join(unit.getlocations())
flags = '' # FIXME flags = '' # FIXME
target = join_plural(unit.target.strings) target = join_plural(unit.target.strings)
fuzzy = unit.isfuzzy() fuzzy = unit.isfuzzy()
if location == self.location and flags == self.flags and target == self.target and fuzzy == self.fuzzy: if not force and location == self.location and flags == self.flags and target == self.target and fuzzy == self.fuzzy:
return return
self.location = location self.location = location
self.flags = flags self.flags = flags
self.target = target self.target = target
self.fuzzy = fuzzy self.fuzzy = fuzzy
self.save() self.save(force_insert = force)
def is_plural(self): def is_plural(self):
return is_plural(self.source) return is_plural(self.source)