コード例 #1
0
def _callback_handler(sender, updated, **kwargs):

    bulk_tps = bulk_operations(models=(get_user_model(), UserTPScore, TPData,
                                       TPChecksData))
    with keep_data(signals=(update_revisions, )):

        @receiver(update_revisions)
        def update_revisions_handler(**kwargs):
            if updated.revisions is None:
                updated.revisions = set()
            instance = kwargs.get("instance")
            if isinstance(instance, Store):
                updated.revisions.add(kwargs["instance"].parent.pootle_path)
            elif isinstance(instance, Directory):
                updated.revisions.add(kwargs["instance"].pootle_path)

        with bulk_tps:
            _update_stores(sender, updated)

            if updated.tp_data:
                update_data.send(sender.__class__, instance=sender)
            if updated.tp_scores:
                update_scores.send(sender.__class__,
                                   instance=sender,
                                   users=updated.score_users)
    if updated.revisions:
        update_revisions.send(Directory,
                              paths=updated.revisions,
                              keys=["stats", "checks"])
コード例 #2
0
ファイル: utils.py プロジェクト: claudep/pootle
    def purge(self):
        """Purges user from site reverting any changes that they have made.

        The following steps are taken:
        - Delete units created by user and without other submissions.
        - Revert units edited by user.
        - Revert reviews made by user.
        - Revert unit comments by user.
        - Revert unit state changes by user.
        - Delete any remaining submissions and suggestions.
        """

        stores = set()
        with keep_data():
            stores |= self.remove_units_created()
            stores |= self.revert_units_edited()
            stores |= self.revert_units_reviewed()
            stores |= self.revert_units_commented()
            stores |= self.revert_units_state_changed()

            # Delete remaining submissions.
            logger.debug("Deleting remaining submissions for: %s", self.user)
            self.user.submission_set.all().delete()

            # Delete remaining suggestions.
            logger.debug("Deleting remaining suggestions for: %s", self.user)
            self.user.suggestions.all().delete()
        for store in stores:
            update_data.send(store.__class__, instance=store)
コード例 #3
0
    def setup_disabled_project(self):
        from pytest_pootle.factories import (DirectoryFactory,
                                             ProjectDBFactory,
                                             TranslationProjectFactory)
        from pootle.core.contextmanagers import keep_data
        from pootle_format.models import Format
        from pootle_language.models import Language

        with keep_data():
            source_language = Language.objects.get(code="en")
            project = ProjectDBFactory(code="disabled_project0",
                                       fullname="Disabled Project 0",
                                       source_language=source_language)
            project.filetypes.add(Format.objects.get(name="po"))
            project.disabled = True
            project.save()
            language = Language.objects.get(code="language0")
            tp = TranslationProjectFactory(project=project, language=language)
            tp_dir = tp.directory
            tp_dir.obsolete = False
            tp_dir.save()
            self._add_stores(tp, n=(1, 1))
            subdir0 = DirectoryFactory(name="subdir0",
                                       parent=tp.directory,
                                       tp=tp)
            self._add_stores(tp, n=(1, 1), parent=subdir0)
コード例 #4
0
    def purge(self):
        """Purges user from site reverting any changes that they have made.

        The following steps are taken:
        - Delete units created by user and without other submissions.
        - Revert units edited by user.
        - Revert reviews made by user.
        - Revert unit comments by user.
        - Revert unit state changes by user.
        - Delete any remaining submissions and suggestions.
        """

        stores = set()
        with keep_data():
            stores |= self.remove_units_created()
            stores |= self.revert_units_edited()
            stores |= self.revert_units_reviewed()
            stores |= self.revert_units_commented()
            stores |= self.revert_units_state_changed()

            # Delete remaining submissions.
            logger.debug("Deleting remaining submissions for: %s", self.user)
            self.user.submission_set.all().delete()

            # Delete remaining suggestions.
            logger.debug("Deleting remaining suggestions for: %s", self.user)
            self.user.suggestions.all().delete()
        for store in stores:
            update_data.send(store.__class__, instance=store)
コード例 #5
0
ファイル: env.py プロジェクト: cloph/pootle
    def setup_templates(self):
        from pootle.core.contextmanagers import keep_data
        from pootle.core.signals import update_data
        from pootle_project.models import Project
        from pootle_translationproject.contextmanagers import update_tp_after
        from pytest_pootle.factories import (
            LanguageDBFactory, TranslationProjectFactory)

        tps = []
        with keep_data():
            templates = LanguageDBFactory(code="templates")

            for project in Project.objects.all():
                # add a TP to the project for each language
                tp = TranslationProjectFactory(project=project, language=templates)
                # As there are no files on the FS we have to currently unobsolete
                # the directory
                tp_dir = tp.directory
                tp_dir.obsolete = False
                tp_dir.save()
                self._add_template_stores(tp)
                tps.append(tp)
        for tp in tps:
            with update_tp_after(tp):
                for store in tp.stores.all():
                    update_data.send(
                        store.__class__,
                        instance=store)
コード例 #6
0
def _update_stores(sender, updated):
    # call signals for stores
    bulk_stores = bulk_operations(models=(UserStoreScore, StoreData,
                                          StoreChecksData))
    with keep_data(suppress=(sender.__class__, )):
        with bulk_stores:
            _handle_update_stores(sender, updated)
コード例 #7
0
    def setup_templates(self):
        from pootle.core.contextmanagers import keep_data
        from pootle.core.signals import update_data
        from pootle_project.models import Project
        from pootle_translationproject.contextmanagers import update_tp_after
        from pytest_pootle.factories import (LanguageDBFactory,
                                             TranslationProjectFactory)

        tps = []
        with keep_data():
            templates = LanguageDBFactory(code="templates")

            for project in Project.objects.all():
                # add a TP to the project for each language
                tp = TranslationProjectFactory(project=project,
                                               language=templates)
                # As there are no files on the FS we have to currently unobsolete
                # the directory
                tp_dir = tp.directory
                tp_dir.obsolete = False
                tp_dir.save()
                self._add_template_stores(tp)
                tps.append(tp)
        for tp in tps:
            with update_tp_after(tp):
                for store in tp.stores.all():
                    update_data.send(store.__class__, instance=store)
コード例 #8
0
ファイル: contextmanagers.py プロジェクト: ta2-1/pootle
def _update_stores(sender, updated):
    # call signals for stores
    bulk_stores = bulk_operations(
        models=(
            UserStoreScore,
            StoreData,
            StoreChecksData))
    with keep_data(suppress=(sender.__class__, )):
        with bulk_stores:
            _handle_update_stores(sender, updated)
コード例 #9
0
ファイル: updater.py プロジェクト: yiyibooks/pootle
 def refresh_scores(self, users=None, existing=None, existing_tps=None):
     suppress_tp_scores = keep_data(signals=(update_scores, ),
                                    suppress=(TranslationProject, ))
     existing = existing or self.get_store_scores(self.tp)
     with bulk_operations(UserTPScore):
         with suppress_tp_scores:
             with bulk_operations(UserStoreScore):
                 for store in self.tp.stores.iterator():
                     score_updater.get(store.__class__)(store).update(
                         users=users, existing=existing.get(store.id))
         self.update(users=users, existing=existing_tps)
コード例 #10
0
ファイル: updater.py プロジェクト: yiyibooks/pootle
    def refresh_scores(self, users=None, **kwargs):
        suppress_user_scores = keep_data(signals=(update_scores, ),
                                         suppress=(get_user_model(), ))
        tp_scores = self.get_tp_scores()

        with bulk_operations(get_user_model()):
            with suppress_user_scores:
                for tp in TranslationProject.objects.all():
                    score_updater.get(tp.__class__)(tp).refresh_scores(
                        users=users, existing_tps=tp_scores.get(tp.id))
                self.update(users=users)
コード例 #11
0
ファイル: store.py プロジェクト: yiyibooks/pootle
def param_update_store_test(request, tp0, member, member2):
    from pootle.core.contextmanagers import keep_data
    from pootle.core.signals import update_data

    store = StoreDBFactory(translation_project=tp0, parent=tp0.directory)

    with keep_data():
        test = _setup_store_test(store, member, member2,
                                 UPDATE_STORE_TESTS[request.param])
    update_data.send(store.__class__, instance=store)

    with keep_data():
        update_store(test[0],
                     units=test[1],
                     store_revision=test[2],
                     user=member2,
                     resolve_conflict=test[3])
    update_data.send(store.__class__, instance=store)

    return test
コード例 #12
0
ファイル: utils.py プロジェクト: arky/pootle
 def clone_store(self, store, target_dir):
     """Clone given Store to target Directory"""
     cloned = target_dir.child_stores.create(
         name=store.name,
         translation_project=target_dir.translation_project)
     with keep_data(signals=(update_checks, )):
         cloned.update(cloned.deserialize(store.serialize()))
         cloned.state = store.state
         cloned.filetype = store.filetype
         cloned.save()
     self.clone_checks(store, cloned)
     return cloned
コード例 #13
0
 def clone_store(self, store, target_dir):
     """Clone given Store to target Directory"""
     cloned = target_dir.child_stores.create(
         name=store.name,
         translation_project=target_dir.translation_project)
     with keep_data(signals=(update_checks, )):
         cloned.update(cloned.deserialize(store.serialize()))
         cloned.state = store.state
         cloned.filetype = store.filetype
         cloned.save()
     self.clone_checks(store, cloned)
     return cloned
コード例 #14
0
ファイル: updater.py プロジェクト: cloph/pootle
    def refresh_scores(self, users=None, **kwargs):
        suppress_user_scores = keep_data(
            signals=(update_scores, ),
            suppress=(get_user_model(), ))
        tp_scores = self.get_tp_scores()

        with bulk_operations(get_user_model()):
            with suppress_user_scores:
                for tp in TranslationProject.objects.all():
                    score_updater.get(tp.__class__)(tp).refresh_scores(
                        users=users,
                        existing_tps=tp_scores.get(tp.id))
                self.update(users=users)
コード例 #15
0
ファイル: updater.py プロジェクト: cloph/pootle
 def refresh_scores(self, users=None, existing=None, existing_tps=None):
     suppress_tp_scores = keep_data(
         signals=(update_scores, ),
         suppress=(TranslationProject, ))
     existing = existing or self.get_store_scores(self.tp)
     with bulk_operations(UserTPScore):
         with suppress_tp_scores:
             with bulk_operations(UserStoreScore):
                 for store in self.tp.stores.iterator():
                     score_updater.get(store.__class__)(store).update(
                         users=users,
                         existing=existing.get(store.id))
         self.update(users=users, existing=existing_tps)
コード例 #16
0
ファイル: refresh_scores.py プロジェクト: cloph/pootle
def test_refresh_scores_recalculate(capfd, store0, member):
    """Recalculate scores."""
    # delete the 2 most prolific contribs to speed up
    unit = store0.units.filter(suggestion__state__name="pending").first()
    suggestion = unit.suggestion_set.filter(state__name="pending").first()
    member_score = member.score
    with keep_data():
        review.get(suggestion.__class__)(
            [suggestion], reviewer=member).accept()
    member.refresh_from_db()
    assert member.score == member_score
    call_command('refresh_scores')
    member.refresh_from_db()
    assert member.score > member_score
コード例 #17
0
ファイル: models.py プロジェクト: YESLTD/pootle
def scan_languages(**kwargs):
    instance = kwargs["instance"]
    created = kwargs.get("created", False)
    raw = kwargs.get("raw", False)

    if not created or raw or instance.disabled:
        return

    if not instance.filetypes.all().exists():
        instance.filetypes.add(Format.objects.get(name="po"))

    if instance.treestyle == 'pootle_fs':
        return

    for language in Language.objects.iterator():
        with keep_data():
            tp = create_translation_project(language, instance)
        if tp is not None:
            with keep_data(tp, suppress=(tp.__class__, )):
                result = tp.update_from_disk()
                if result:
                    update_data.send(tp.__class__, instance=tp)
                    update_scores.send(tp.__class__, instance=tp)
コード例 #18
0
ファイル: refresh_scores.py プロジェクト: YESLTD/pootle
def test_refresh_scores_recalculate(capfd, store0, member):
    """Recalculate scores."""
    # delete the 2 most prolific contribs to speed up
    unit = store0.units.filter(suggestion__state__name="pending").first()
    suggestion = unit.suggestion_set.filter(state__name="pending").first()
    member_score = member.score
    with keep_data():
        review.get(suggestion.__class__)([suggestion],
                                         reviewer=member).accept()
    member.refresh_from_db()
    assert member.score == member_score
    call_command('refresh_scores')
    member.refresh_from_db()
    assert member.score > member_score
コード例 #19
0
ファイル: env.py プロジェクト: cloph/pootle
    def setup_subdirs(self):
        from pytest_pootle.factories import DirectoryFactory

        from pootle.core.contextmanagers import keep_data
        from pootle_translationproject.models import TranslationProject

        with keep_data():
            for tp in TranslationProject.objects.all():
                subdir0 = DirectoryFactory(
                    name="subdir0", parent=tp.directory, tp=tp)
                subdir1 = DirectoryFactory(
                    name="subdir1", parent=subdir0, tp=tp)
                self._add_stores(tp, n=(2, 1), parent=subdir0)
                self._add_stores(tp, n=(1, 1), parent=subdir1)
コード例 #20
0
ファイル: store.py プロジェクト: arky/pootle
def param_update_store_test(request, tp0, member, member2):
    from pootle.core.contextmanagers import keep_data
    from pootle.core.signals import update_data

    store = StoreDBFactory(
        translation_project=tp0,
        parent=tp0.directory)

    with keep_data():
        test = _setup_store_test(
            store, member, member2,
            UPDATE_STORE_TESTS[request.param])
    update_data.send(store.__class__, instance=store)

    with keep_data():
        update_store(
            test[0],
            units=test[1],
            store_revision=test[2],
            user=member2,
            resolve_conflict=test[3])
    update_data.send(store.__class__, instance=store)

    return test
コード例 #21
0
ファイル: contextmanagers.py プロジェクト: yiyibooks/pootle
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
コード例 #22
0
ファイル: contextmanagers.py プロジェクト: arky/pootle
def _handle_update_stores(sender, updated):

    @receiver(update_data, sender=sender.__class__)
    def update_tp_data_handler(**kwargs):
        updated.tp_data = True
        update_data.disconnect(
            update_tp_data_handler,
            sender=sender.__class__)

    @receiver(update_scores, sender=sender.__class__)
    def update_tp_scores_handler(**kwargs):
        updated.tp_scores = True
        update_scores.disconnect(
            update_tp_scores_handler,
            sender=sender.__class__)

    if updated.checks:
        with keep_data(suppress=(Store, ), signals=(update_data, )):

            @receiver(update_data, sender=Store)
            def extra_update_data_handler_(**kwargs):
                updated.data = updated.data or {}
                updated.data[kwargs["instance"].id] = kwargs["instance"]

            with bulk_operations(QualityCheck):
                for to_check in updated.checks.values():
                    store = to_check["store"]
                    units = (
                        [unit for unit in to_check["units"]]
                        if to_check["units"]
                        else None)
                    update_checks.send(
                        store.__class__,
                        instance=store,
                        units=units)

    if updated.data:
        stores = updated.data.values()
        for store in stores:
            update_data.send(
                Store,
                instance=store)
    if updated.score_stores:
        for store in updated.score_stores.values():
            update_scores.send(
                store.__class__,
                instance=store,
                users=updated.score_users)
コード例 #23
0
def scan_projects(**kwargs):
    instance = kwargs["instance"]
    created = kwargs.get("created", False)
    raw = kwargs.get("raw", False)

    if not created or raw:
        return

    old_style_projects = Project.objects.enabled().exclude(
        treestyle="pootle_fs")

    for project in old_style_projects.iterator():
        with keep_data():
            tp = create_translation_project(instance, project)
        if tp is not None:
            tp.update_from_disk()
コード例 #24
0
    def setup_subdirs(self):
        from pytest_pootle.factories import DirectoryFactory

        from pootle.core.contextmanagers import keep_data
        from pootle_translationproject.models import TranslationProject

        with keep_data():
            for tp in TranslationProject.objects.all():
                subdir0 = DirectoryFactory(name="subdir0",
                                           parent=tp.directory,
                                           tp=tp)
                subdir1 = DirectoryFactory(name="subdir1",
                                           parent=subdir0,
                                           tp=tp)
                self._add_stores(tp, n=(2, 1), parent=subdir0)
                self._add_stores(tp, n=(1, 1), parent=subdir1)
コード例 #25
0
ファイル: models.py プロジェクト: ta2-1/pootle
def scan_projects(**kwargs):
    instance = kwargs["instance"]
    created = kwargs.get("created", False)
    raw = kwargs.get("raw", False)

    if not created or raw:
        return

    old_style_projects = Project.objects.enabled().exclude(
        treestyle="pootle_fs")

    for project in old_style_projects.iterator():
        with keep_data():
            tp = create_translation_project(instance, project)
        if tp is not None:
            tp.update_from_disk()
コード例 #26
0
ファイル: env.py プロジェクト: cloph/pootle
    def setup_tps(self):
        from pootle.core.contextmanagers import keep_data
        from pootle_project.models import Project
        from pootle_language.models import Language
        from pytest_pootle.factories import TranslationProjectFactory

        with keep_data():
            for project in Project.objects.select_related("source_language").all():
                for language in Language.objects.exclude(code="en"):
                    # add a TP to the project for each language
                    tp = TranslationProjectFactory(
                        project=project, language=language)
                    # As there are no files on the FS we have to currently
                    # unobsolete the directory
                    tp_dir = tp.directory
                    tp_dir.obsolete = False
                    tp_dir.save()
                    self._add_stores(tp)
コード例 #27
0
    def setup_tps(self):
        from pootle.core.contextmanagers import keep_data
        from pootle_project.models import Project
        from pootle_language.models import Language
        from pytest_pootle.factories import TranslationProjectFactory

        with keep_data():
            for project in Project.objects.select_related(
                    "source_language").all():
                for language in Language.objects.exclude(code="en"):
                    # add a TP to the project for each language
                    tp = TranslationProjectFactory(project=project,
                                                   language=language)
                    # As there are no files on the FS we have to currently
                    # unobsolete the directory
                    tp_dir = tp.directory
                    tp_dir.obsolete = False
                    tp_dir.save()
                    self._add_stores(tp)
コード例 #28
0
ファイル: models.py プロジェクト: ta2-1/pootle
def scan_languages(**kwargs):
    instance = kwargs["instance"]
    created = kwargs.get("created", False)
    raw = kwargs.get("raw", False)

    if not created or raw or instance.disabled:
        return

    if not instance.filetypes.all().exists():
        instance.filetypes.add(Format.objects.get(name="po"))

    if instance.treestyle == 'pootle_fs':
        return

    for language in Language.objects.iterator():
        with keep_data():
            tp = create_translation_project(language, instance)
        if tp is not None:
            tp.update_from_disk()
コード例 #29
0
ファイル: contextmanagers.py プロジェクト: arky/pootle
def update_tp_after(sender, **kwargs):
    updated = Updated()

    with keep_data():

        @receiver(update_data, sender=Store)
        def update_data_handler(**kwargs):
            if updated.data is None:
                updated.data = {}
            updated.data[kwargs["instance"].id] = kwargs["instance"]

        @receiver(update_checks)
        def update_check_handler(**kwargs):
            if updated.checks is None:
                updated.checks = {}
            units = None
            if isinstance(kwargs.get("instance"), Store):
                store = kwargs["instance"]
                units = set(kwargs.get("units") or [])
            else:
                store = kwargs["instance"].store
                units = set([kwargs["instance"].id])
            updated.checks[store.id] = updated.checks.get(
                store.id,
                dict(store=store, units=set()))
            if units is not None:
                updated.checks[store.id]["units"] |= units

        @receiver(update_scores, sender=Store)
        def update_scores_handler(**kwargs):
            if not updated.score_stores:
                updated.score_stores = {}
            if "instance" in kwargs:
                updated.score_stores[kwargs["instance"].id] = kwargs["instance"]
            if "users" in kwargs:
                updated.score_users = (
                    (updated.score_users or set())
                    | set(kwargs["users"]))
        yield
    kwargs.get("callback", _callback_handler)(
        sender, updated, **kwargs)
コード例 #30
0
def _callback_handler(sender, updated, **kwargs):

    bulk_pootle = bulk_operations(
        models=(
            get_user_model(),
            UserTPScore,
            UserStoreScore,
            TPData,
            TPChecksData,
            StoreData,
            StoreChecksData))

    with keep_data(signals=(update_revisions, )):
        with bulk_pootle:

            @receiver(update_revisions)
            def handle_update_revisions(**kwargs):
                updated.revisions = True

            if updated.checks:
                update_checks.send(
                    sender.__class__,
                    instance=sender,
                    units=updated.checks,
                    **kwargs)
            if updated.data:
                update_data.send(
                    sender.__class__,
                    instance=sender,
                    **kwargs)
            if updated.scores:
                update_scores.send(
                    sender.__class__,
                    instance=sender,
                    users=updated.scores,
                    **kwargs)
    if updated.revisions:
        update_revisions.send(
            sender.__class__,
            instance=sender,
            keys=["stats", "checks"])
コード例 #31
0
def update_tp_after(sender, **kwargs):
    updated = Updated()

    with keep_data():

        @receiver(update_data, sender=Store)
        def update_data_handler(**kwargs):
            if updated.data is None:
                updated.data = {}
            updated.data[kwargs["instance"].id] = kwargs["instance"]

        @receiver(update_checks)
        def update_check_handler(**kwargs):
            if updated.checks is None:
                updated.checks = {}
            units = None
            if isinstance(kwargs.get("instance"), Store):
                store = kwargs["instance"]
                units = set(kwargs.get("units") or [])
            else:
                store = kwargs["instance"].store
                units = set([kwargs["instance"].id])
            updated.checks[store.id] = updated.checks.get(
                store.id, dict(store=store, units=set()))
            if units is not None:
                updated.checks[store.id]["units"] |= units

        @receiver(update_scores, sender=Store)
        def update_scores_handler(**kwargs):
            if not updated.score_stores:
                updated.score_stores = {}
            if "instance" in kwargs:
                updated.score_stores[
                    kwargs["instance"].id] = kwargs["instance"]
            if "users" in kwargs:
                updated.score_users = ((updated.score_users or set())
                                       | set(kwargs["users"]))

        yield
    kwargs.get("callback", _callback_handler)(sender, updated, **kwargs)
コード例 #32
0
def test_contextmanager_keep_data(store0, no_update_data_):

    result = []

    @receiver(update_data, sender=Store)
    def update_data_handler(**kwargs):
        store = kwargs["instance"]
        result.append(store)

    update_data.send(Store, instance=store0)
    assert result == [store0]

    result.remove(store0)

    # with keep_data decorator signal is suppressed
    with keep_data():
        update_data.send(Store, instance=store0)
    assert result == []

    # works again now
    update_data.send(Store, instance=store0)
    assert result == [store0]
コード例 #33
0
def test_contextmanager_keep_data(store0, no_update_data_):

    result = []

    @receiver(update_data, sender=Store)
    def update_data_handler(**kwargs):
        store = kwargs["instance"]
        result.append(store)

    update_data.send(Store, instance=store0)
    assert result == [store0]

    result.remove(store0)

    # with keep_data decorator signal is suppressed
    with keep_data():
        update_data.send(Store, instance=store0)
    assert result == []

    # works again now
    update_data.send(Store, instance=store0)
    assert result == [store0]
コード例 #34
0
def _handle_update_stores(sender, updated):
    @receiver(update_data, sender=sender.__class__)
    def update_tp_data_handler(**kwargs):
        updated.tp_data = True
        update_data.disconnect(update_tp_data_handler, sender=sender.__class__)

    @receiver(update_scores, sender=sender.__class__)
    def update_tp_scores_handler(**kwargs):
        updated.tp_scores = True
        update_scores.disconnect(update_tp_scores_handler,
                                 sender=sender.__class__)

    if updated.checks:
        with keep_data(suppress=(Store, ), signals=(update_data, )):

            @receiver(update_data, sender=Store)
            def extra_update_data_handler_(**kwargs):
                updated.data = updated.data or {}
                updated.data[kwargs["instance"].id] = kwargs["instance"]

            with bulk_operations(QualityCheck):
                for to_check in updated.checks.values():
                    store = to_check["store"]
                    units = ([unit for unit in to_check["units"]]
                             if to_check["units"] else None)
                    update_checks.send(store.__class__,
                                       instance=store,
                                       units=units)

    if updated.data:
        stores = updated.data.values()
        for store in stores:
            update_data.send(Store, instance=store)
    if updated.score_stores:
        for store in updated.score_stores.values():
            update_scores.send(store.__class__,
                               instance=store,
                               users=updated.score_users)
コード例 #35
0
ファイル: contextmanagers.py プロジェクト: ta2-1/pootle
def _callback_handler(sender, updated, **kwargs):

    bulk_tps = bulk_operations(
        models=(
            get_user_model(),
            UserTPScore,
            TPData,
            TPChecksData))
    with keep_data(signals=(update_revisions, )):

        @receiver(update_revisions)
        def update_revisions_handler(**kwargs):
            if updated.revisions is None:
                updated.revisions = set()
            instance = kwargs.get("instance")
            if isinstance(instance, Store):
                updated.revisions.add(kwargs["instance"].parent.pootle_path)
            elif isinstance(instance, Directory):
                updated.revisions.add(kwargs["instance"].pootle_path)

        with bulk_tps:
            _update_stores(sender, updated)

            if updated.tp_data:
                update_data.send(
                    sender.__class__,
                    instance=sender)
            if updated.tp_scores:
                update_scores.send(
                    sender.__class__,
                    instance=sender,
                    users=updated.score_users)
    if updated.revisions:
        update_revisions.send(
            Directory,
            paths=updated.revisions,
            keys=["stats", "checks"])
コード例 #36
0
def update_store_after(sender, **kwargs):
    signals = [
        update_checks,
        update_data,
        update_revisions,
        update_scores]

    with keep_data(signals=signals):
        updated = Updated()

        @receiver(update_checks, sender=Unit)
        def handle_update_checks(**kwargs):
            if updated.checks is None:
                updated.checks = set()
            updated.checks.add(kwargs["instance"].id)

        @receiver(update_data, sender=sender.__class__)
        def handle_update_data(**kwargs):
            updated.data = True
            update_data.disconnect(
                handle_update_data,
                sender=sender.__class__)

        @receiver(update_scores, sender=sender.__class__)
        def handle_update_scores(**kwargs):
            if updated.scores is None:
                updated.scores = set()
            updated.scores = (
                updated.scores
                | set(kwargs.get("users") or []))
        yield

    if "kwargs" in kwargs:
        kwargs.update(kwargs.pop("kwargs"))
    kwargs.get("callback", _callback_handler)(
        sender, updated, **kwargs)
コード例 #37
0
ファイル: env.py プロジェクト: cloph/pootle
    def setup_disabled_project(self):
        from pytest_pootle.factories import (DirectoryFactory,
                                             ProjectDBFactory,
                                             TranslationProjectFactory)
        from pootle.core.contextmanagers import keep_data
        from pootle_format.models import Format
        from pootle_language.models import Language

        with keep_data():
            source_language = Language.objects.get(code="en")
            project = ProjectDBFactory(code="disabled_project0",
                                       fullname="Disabled Project 0",
                                       source_language=source_language)
            project.filetypes.add(Format.objects.get(name="po"))
            project.disabled = True
            project.save()
            language = Language.objects.get(code="language0")
            tp = TranslationProjectFactory(project=project, language=language)
            tp_dir = tp.directory
            tp_dir.obsolete = False
            tp_dir.save()
            self._add_stores(tp, n=(1, 1))
            subdir0 = DirectoryFactory(name="subdir0", parent=tp.directory, tp=tp)
            self._add_stores(tp, n=(1, 1), parent=subdir0)
コード例 #38
0
ファイル: initdb.py プロジェクト: dwaynebailey/pootle
 def init_db(self, create_projects=True):
     with transaction.atomic():
         with keep_data(signals=(update_revisions, )):
             self._init_db(create_projects)
コード例 #39
0
def _no_context_data():
    with keep_data(signals=(context_data, )):
        yield
コード例 #40
0
ファイル: contextmanagers.py プロジェクト: yiyibooks/pootle
def test_contextmanager_bulk_operations(store0):
    unit = store0.units.first()

    class Update(object):
        data_created = None
        qc_created = None
        data_called = 0
        qc_called = 0

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

        @receiver(create, sender=QualityCheck)
        def handle_qc_create(**kwargs):
            assert "instance" not in kwargs
            updated.qc_created = kwargs["objects"]
            updated.qc_called += 1

        @receiver(create, sender=StoreChecksData)
        def handle_data_create(**kwargs):
            updated.data_called += 1
            if "objects" in kwargs:
                updated.data_created = kwargs["objects"]
            else:
                assert "instance" in kwargs

        with bulk_operations(QualityCheck):
            qc_creates = _create_qc_events(unit)
            data_creates = _create_data_events(unit)
        assert updated.qc_created == qc_creates
        assert updated.qc_called == 1
        assert updated.data_created != data_creates
        assert updated.data_called == 5

        # nested update
        updated = Update()
        with bulk_operations(QualityCheck):
            with bulk_operations(QualityCheck):
                qc_creates = _create_qc_events(unit)
                data_creates = _create_data_events(unit)
        assert updated.qc_created == qc_creates
        assert updated.qc_called == 1
        assert updated.data_created != data_creates
        assert updated.data_called == 5

        # nested update - different models
        updated = Update()
        with bulk_operations(StoreChecksData):
            with bulk_operations(QualityCheck):
                qc_creates = _create_qc_events(unit)
                data_creates = _create_data_events(unit)
        assert updated.qc_created == qc_creates
        assert updated.qc_called == 1
        assert updated.data_created == data_creates
        assert updated.data_called == 1

        updated = Update()
        with bulk_operations(models=(StoreChecksData, QualityCheck)):
            qc_creates = _create_qc_events(unit)
            data_creates = _create_data_events(unit)
        assert updated.qc_created == qc_creates
        assert updated.qc_called == 1
        assert updated.data_created == data_creates
        assert updated.data_called == 1
コード例 #41
0
ファイル: contextmanagers.py プロジェクト: yiyibooks/pootle
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
コード例 #42
0
ファイル: initdb.py プロジェクト: mansil/pootle
 def init_db(self, create_projects=True):
     with keep_data(signals=(update_revisions, )):
         with bulk_operations(models=(TPData, TPChecksData)):
             self._init_db(create_projects)
コード例 #43
0
def _no_wordcount():
    with keep_data(signals=(wordcount, )):
        yield
コード例 #44
0
def _no_tp_tool():
    with keep_data(signals=(tp_tool, )):
        yield
コード例 #45
0
def test_direct_language_match_filename(language_code, path_name, matched):

    with keep_data(signals=(update_revisions, ), suppress=(Directory, Store)):
        LanguageDBFactory(code="pt_BR")

    assert direct_language_match_filename(language_code, path_name) is matched
コード例 #46
0
ファイル: templatetags.py プロジェクト: yiyibooks/pootle
def test_fs_tags_upstream(project0):
    with keep_data(upstream):
        _test_fs_tags_upstram(project0)
コード例 #47
0
ファイル: signals.py プロジェクト: yiyibooks/pootle
def _no_update_data():
    with keep_data(signals=(update_data, )):
        yield
コード例 #48
0
def _no_fs_finder():
    with keep_data(signals=(fs_finder, )):
        yield