Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
 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])
Ejemplo n.º 3
0
 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])
Ejemplo n.º 4
0
 def delete_checks(self, checks):
     """Delete checks that are no longer used.
     """
     return delete.send(self.checks_qs.model,
                        objects=self.checks_qs.filter(name__in=checks))
Ejemplo n.º 5
0
def test_contextmanager_bulk_ops_delete(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_deleted = None
        unit_deleted = None
        store_called = 0
        unit_called = 0

    with keep_data(signals=[delete]):
        updated = Update()

        @receiver(delete, sender=Unit)
        def handle_unit_delete(**kwargs):
            assert "instance" not in kwargs
            updated.unit_deleted = kwargs["objects"]
            updated.unit_called += 1

        @receiver(delete, sender=Store)
        def handle_store_delete(**kwargs):
            updated.store_called += 1
            if "objects" in kwargs:
                updated.store_deleted = kwargs["objects"]
            else:
                assert "instance" in kwargs

        with bulk_operations(Unit):
            delete.send(Unit, instance=unit)
            delete.send(Unit, objects=store1.unit_set.all())
            delete.send(Unit, objects=store2.unit_set.all())
            delete.send(Store, instance=store0)
            delete.send(
                Store,
                objects=store1.translation_project.stores.filter(
                    id=store1.id))
            delete.send(
                Store,
                objects=store2.translation_project.stores.filter(
                    id=store2.id))
        assert updated.unit_called == 1
        assert qs_match(
            updated.unit_deleted,
            (store0.unit_set.filter(id=unit.id)
             | store1.unit_set.all()
             | store2.unit_set.all()))
        assert updated.store_called == 3
Ejemplo n.º 6
0
Archivo: utils.py Proyecto: arky/pootle
 def delete_checks(self, checks):
     """Delete checks that are no longer used.
     """
     return delete.send(
         self.checks_qs.model,
         objects=self.checks_qs.filter(name__in=checks))