def _callback_handler(model, updated): # delete to_delete = None if updated.delete_ids is not None: to_delete = model.objects.filter( pk__in=updated.delete_ids) if updated.delete_qs is not None: to_delete = ( updated.delete_qs if to_delete is None else to_delete | updated.delete_qs) if to_delete is not None: delete.send( model, objects=to_delete) # create if updated.create is not None: create.send( model, objects=updated.create) # update should_update = ( updated.update_objects is not None or updated.updates is not None) if should_update: update.send( model, objects=updated.update_objects, updates=updated.updates, update_fields=updated.update_fields)
def update(self, keys=None): parents = list(self.parents.values_list("id", flat=True)) revisions = self.get_revisions(parents, keys=keys) missing_revisions = [] existing_ids = [] revision_map = { '%s-%s' % (x['object_id'], x['key']): x['id'] for x in revisions.values("id", "object_id", "key")} for parent in parents: for key in keys or [""]: id = '%s-%s' % (parent, key) if id in revision_map: existing_ids.append(revision_map[id]) else: missing_revisions.append(dict( object_id=parent, key=key)) new_revision = self.new_revision updates = { id: dict(value=new_revision) for id in existing_ids} if updates: update.send( Revision, updates=updates) if missing_revisions: create.send( Revision, objects=list( self.create_missing_revisions( missing_revisions, new_revision)))
def set_scores(self, calculated_scores, existing=None): calculated_scores = list(self.iterate_scores(calculated_scores)) score_dict = {(score[0], score[1]): score[2] for score in calculated_scores} updates = {} if existing: scores = existing else: scores = self.find_existing_scores(calculated_scores) or [] for score in scores: id, date, user, score, reviewed, suggested, translated = score newscore = score_dict.get((date, user), None) if newscore is None: # delete ? continue oldscore = dict(score=score, reviewed=reviewed, translated=translated, suggested=suggested) for k in ["score", "suggested", "reviewed", "translated"]: _newscore = round(newscore.get(k, 0), 2) if round(oldscore[k], 2) != _newscore: updates[id] = updates.get(id, {}) updates[id][k] = _newscore del score_dict[(date, user)] if updates: update.send(self.score_model, updates=updates) if score_dict: self.create_scores(score_dict)
def set_scores(self, calculated_scores, existing=None): calculated_scores = list(self.iterate_scores(calculated_scores)) score_dict = { (score[0], score[1]): score[2] for score in calculated_scores} updates = {} if existing: scores = existing else: scores = self.find_existing_scores(calculated_scores) or [] for score in scores: id, date, user, score, reviewed, suggested, translated = score newscore = score_dict.get((date, user), None) if newscore is None: # delete ? continue oldscore = dict( score=score, reviewed=reviewed, translated=translated, suggested=suggested) for k in ["score", "suggested", "reviewed", "translated"]: _newscore = round(newscore.get(k, 0), 2) if round(oldscore[k], 2) != _newscore: updates[id] = updates.get(id, {}) updates[id][k] = _newscore del score_dict[(date, user)] if updates: update.send( self.score_model, updates=updates) if score_dict: self.create_scores(score_dict)
def set_scores(self, calculated_scores, existing=None): update.send( self.score_model, updates={ user: dict(score=score) for user, score in calculated_scores.iterator()})
def update(self, keys=None): parents = list(self.parents.values_list("id", flat=True)) revisions = self.get_revisions(parents, keys=keys) missing_revisions = [] existing_ids = [] revision_map = { '%s-%s' % (x['object_id'], x['key']): x['id'] for x in revisions.values("id", "object_id", "key")} for parent in parents: for key in keys or [""]: id = '%s-%s' % (parent, key) if id in revision_map: existing_ids.append(revision_map[id]) else: missing_revisions.append(dict( object_id=parent, key=key)) new_revision = self.new_revision update.send( Revision, updates={ id: dict(value=new_revision) for id in existing_ids}) if missing_revisions: create.send( Revision, objects=list( self.create_missing_revisions( missing_revisions, new_revision)))
def save_data(self, fields=None): update.send(self.data.__class__, instance=self.data, update_fields=fields) # this ensures that any calling code gets the # correct revision. It doesnt refresh the last # created/updated fks tho self.model.data = self.data
def save_data(self, fields=None): update.send( self.data.__class__, instance=self.data, update_fields=fields) # this ensures that any calling code gets the # correct revision. It doesnt refresh the last # created/updated fks tho self.model.data = self.data
def set_check_data(self, store_data=None): checks = {} existing_checks = self.model.check_data.values_list( "pk", "category", "name", "count") for pk, category, name, count in existing_checks: checks[(category, name)] = (pk, count) to_update = [] to_add = [] for check in store_data["checks"]: category = check["category"] name = check["name"] count = check["count"] check_exists = ( checks.get((category, name))) if not check_exists: to_add.append(check) continue elif checks[(category, name)][1] != count: to_update.append((checks[(category, name)][0], dict(count=count))) del checks[(category, name)] check_data = None for category, name in checks.keys(): if check_data is None: check_data = self.model.check_data.filter( category=category, name=name) else: check_data = check_data | self.model.check_data.filter( category=category, name=name) if checks: delete.send(check_data.model, objects=check_data) if to_update: to_update = dict(to_update) update.send( self.model.check_data.model, updates=to_update) if not to_add: return create.send( self.model.check_data.model, objects=[ self.check_data_field.related_model( **{self.related_name: self.model, "category": check["category"], "name": check["name"], "count": check["count"]}) for check in to_add])
def set_check_data(self, store_data=None): checks = {} existing_checks = self.model.check_data.values_list( "pk", "category", "name", "count") for pk, category, name, count in existing_checks: checks[(category, name)] = (pk, count) to_update = [] to_add = [] for check in store_data["checks"]: category = check["category"] name = check["name"] count = check["count"] check_exists = ( checks.get((category, name))) if not check_exists: to_add.append(check) continue elif checks[(category, name)][1] != count: to_update.append((checks[(category, name)][0], dict(count=count))) del checks[(category, name)] check_data = None for category, name in checks.keys(): if check_data is None: check_data = self.model.check_data.filter( category=category, name=name) else: check_data = check_data | self.model.check_data.filter( category=category, name=name) if checks: delete.send(check_data.model, objects=check_data) if to_update: to_update = dict(to_update) update.send( self.model.check_data.model, updates=to_update) if not to_add: return create.send( self.model.check_data.model, objects=[ self.check_data_field.related_model( **{self.related_name: self.model, "category": check["category"], "name": check["name"], "count": check["count"]}) for check in to_add])
def test_contextmanager_bulk_ops_update(tp0, store0): unit = store0.units.first() store1 = tp0.stores.filter(name="store1.po").first() store2 = tp0.stores.filter(name="store2.po").first() class Update(object): store_updated = None unit_updated = None store_called = 0 unit_called = 0 unit_updates = None with keep_data(signals=[update]): updated = Update() @receiver(update, sender=Unit) def handle_unit_update(**kwargs): assert "instance" not in kwargs updated.unit_updated = kwargs["objects"] updated.unit_called += 1 updated.unit_update_fields = kwargs.get("update_fields") updated.unit_updates = kwargs.get("updates") @receiver(update, sender=Store) def handle_store_update(**kwargs): updated.store_called += 1 if "objects" in kwargs: updated.store_updated = kwargs["objects"] else: assert "instance" in kwargs with bulk_operations(Unit): update.send(Unit, instance=unit) update.send(Unit, objects=list(store1.unit_set.all())) update.send(Unit, objects=list(store2.unit_set.all())) update.send(Unit, update_fields=set(["foo", "bar"])) update.send(Unit, update_fields=set(["bar", "baz"])) update.send(Store, instance=store0) update.send( Store, objects=list(store1.translation_project.stores.filter( id=store1.id))) update.send( Store, objects=list(store2.translation_project.stores.filter( id=store2.id))) assert updated.unit_called == 1 assert isinstance(updated.unit_updated, list) assert qs_match( Unit.objects.filter( id__in=( un.id for un in updated.unit_updated)), (store0.unit_set.filter(id=unit.id) | store1.unit_set.all() | store2.unit_set.all())) assert updated.store_called == 3 assert updated.unit_update_fields == set(["bar", "baz", "foo"]) updated = Update() d1 = {23: dict(foo=True), 45: dict(bar=False)} d2 = {67: dict(baz=89)} with bulk_operations(Unit): update.send(Unit, updates=d1) update.send(Unit, updates=d2) d1.update(d2) assert updated.unit_updates == d1