From 74afdf1f3aac233e500cedb8869700ddcc8002dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Mon, 27 Feb 2012 18:05:42 +0100 Subject: [PATCH] Faster processing by avoiding two writes on creating new entry --- trans/managers.py | 20 +++++++++++++++----- trans/models.py | 6 +++--- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/trans/managers.py b/trans/managers.py index 5b4639dd4..f8bdbf13f 100644 --- a/trans/managers.py +++ b/trans/managers.py @@ -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 diff --git a/trans/models.py b/trans/models.py index ea886e78f..7eba75c98 100644 --- a/trans/models.py +++ b/trans/models.py @@ -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)