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)
ctx = unit.getcontext()
dbunit, created = self.get_or_create(
translation = translation,
source = src,
context = ctx)
dbunit.update_from_unit(unit)
import trans.models
try:
dbunit = self.get(
translation = translation,
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

View File

@ -203,18 +203,18 @@ class Unit(models.Model):
objects = UnitManager()
def update_from_unit(self, unit):
def update_from_unit(self, unit, force):
location = ', '.join(unit.getlocations())
flags = '' # FIXME
target = join_plural(unit.target.strings)
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
self.location = location
self.flags = flags
self.target = target
self.fuzzy = fuzzy
self.save()
self.save(force_insert = force)
def is_plural(self):
return is_plural(self.source)