Exemplo n.º 1
0
 def _sync_to_xtle(self, merge=False, xtle_wins=None):
     """
     Update Xtle ``Store`` with the parsed FS file.
     """
     tmp_store = self.deserialize()
     if not tmp_store:
         logger.warn("File staged for sync has disappeared: %s", self.path)
         return
     if xtle_wins is None:
         resolve_conflict = (self.store_fs.resolve_conflict or SOURCE_WINS)
     elif xtle_wins:
         resolve_conflict = XTLE_WINS
     else:
         resolve_conflict = SOURCE_WINS
     if merge:
         revision = self.store_fs.last_sync_revision or 0
     else:
         # We set the revision to *anything* higher than the Store's
         # This is analogous to the `overwrite` option in
         # Store.update_from_disk
         revision = Revision.get() + 1
     update_revision, __ = self.store.update(
         tmp_store,
         submission_type=SubmissionTypes.SYSTEM,
         user=self.latest_user,
         store_revision=revision,
         resolve_conflict=resolve_conflict)
     logger.debug("Pulled file: %s", self.path)
     return update_revision
Exemplo n.º 2
0
    def revert_units_edited(self):
        """Revert unit edits made by a user to previous edit.
        """
        stores = set()
        # Revert unit target where user is the last submitter.
        changes = self.user.submitted.select_related("unit").iterator()
        for unit_change in changes:
            unit = unit_change.unit
            stores.add(unit.store)

            # Find the last submission by different user that updated the
            # unit.target.
            edits = unit.get_edits().exclude(submitter=self.user)
            updates = {}
            unit_updates = {}
            if edits.exists():
                last_edit = edits.latest("pk")
                unit_updates["target_f"] = last_edit.new_value
                updates["submitted_by_id"] = last_edit.submitter_id
                updates["submitted_on"] = last_edit.creation_time
                logger.debug("Unit edit reverted: %s", repr(unit))
            else:
                # if there is no previous submissions set the target to "" and
                # set the unit.change.submitted_by to None
                unit_updates["target_f"] = ""
                updates["submitted_by"] = None
                updates["submitted_on"] = unit.creation_time
                logger.debug("Unit edit removed: %s", repr(unit))

            # Increment revision
            unit_change.__class__.objects.filter(id=unit_change.id).update(
                **updates)
            unit.__class__.objects.filter(id=unit.id).update(
                revision=Revision.incr(), **unit_updates)
        return stores
Exemplo n.º 3
0
    def _update(self, store, user=None, store_revision=None,
                submission_type=None, resolve_conflict=XTLE_WINS,
                allow_add_and_obsolete=True):
        old_state = self.target_store.state

        if user is None:
            User = get_user_model()
            user = User.objects.get_system_user()

        update_revision = None
        changes = {}
        try:
            diff = StoreDiff(self.target_store, store, store_revision).diff()
            if diff is not None:
                update_revision = Revision.incr()
                changes = self.update_from_diff(
                    store,
                    store_revision,
                    diff, update_revision,
                    user, submission_type,
                    resolve_conflict,
                    allow_add_and_obsolete)
        finally:
            if old_state < PARSED:
                self.target_store.state = PARSED
                self.target_store.save()
            has_changed = any(x > 0 for x in changes.values())
            if has_changed:
                logger.info(
                    u"[update] %s units in %s [revision: %d]",
                    get_change_str(changes),
                    self.target_store.xtle_path,
                    (self.target_store.data.max_unit_revision or 0))
        return update_revision, changes
Exemplo n.º 4
0
    def revert_units_commented(self):
        """Revert comments made by user on units to previous comment or else
        just remove the comment.
        """
        stores = set()
        # Revert unit comments where self.user is latest commenter.
        changes = self.user.commented.select_related("unit").iterator()
        for unit_change in changes:
            unit = unit_change.unit
            stores.add(unit.store)

            # Find comments by other self.users
            comments = unit.get_comments().exclude(submitter=self.user)
            change = {}
            if comments.exists():
                # If there are previous comments by others update the
                # translator_comment, commented_by, and commented_on
                last_comment = comments.latest('pk')
                translator_comment = last_comment.new_value
                change["commented_by_id"] = last_comment.submitter_id
                change["commented_on"] = last_comment.creation_time
                logger.debug("Unit comment reverted: %s", repr(unit))
            else:
                translator_comment = ""
                change["commented_by"] = None
                change["commented_on"] = None
                logger.debug("Unit comment removed: %s", repr(unit))
            unit_change.__class__.objects.filter(id=unit_change.id).update(
                **change)
            unit.__class__.objects.filter(id=unit.id).update(
                translator_comment=translator_comment,
                revision=Revision.incr())
        return stores
Exemplo n.º 5
0
    def revert_units_reviewed(self):
        """Revert reviews made by user on suggestions to previous state.
        """

        stores = set()
        pending = SuggestionState.objects.get(name="pending")

        # Revert reviews by this user.
        for review in self.user.get_suggestion_reviews().iterator():
            suggestion = review.suggestion
            stores.add(suggestion.unit.store)
            if suggestion.user_id == self.user.id:
                # If the suggestion was also created by this user then remove
                # both review and suggestion.
                suggestion.delete()
                logger.debug("Suggestion removed: %s", (suggestion))
            elif suggestion.reviewer_id == self.user.id:
                # If the suggestion is showing as reviewed by the user, then
                # set the suggestion back to pending and update
                # reviewer/review_time.
                suggestion.state = pending
                suggestion.reviewer = None
                suggestion.review_time = None
                suggestion.save()
                logger.debug("Suggestion reverted: %s", (suggestion))

            # Remove the review.
            review.delete()

        changes = self.user.reviewed.select_related("unit").iterator()
        for unit_change in changes:
            unit = unit_change.unit
            stores.add(unit.store)
            unit.suggestion_set.filter(reviewer=self.user).update(
                state=SuggestionState.objects.get(name="pending"),
                reviewer=None)
            unit_updates = {}
            updates = {}
            if not unit.target:
                unit_updates["state"] = UNTRANSLATED
                updates["reviewed_by"] = None
                updates["reviewed_on"] = None
            else:
                old_state_sub = unit.submission_set.exclude(
                    submitter=self.user).filter(
                        field=SubmissionFields.STATE).order_by(
                            "-creation_time", "-pk").first()
                if old_state_sub:
                    unit_updates["state"] = old_state_sub.new_value
                    updates["reviewed_by"] = old_state_sub.submitter
                    updates["reviewed_on"] = old_state_sub.creation_time
            logger.debug("Unit reviewed_by removed: %s", repr(unit))
            unit_change.__class__.objects.filter(id=unit_change.id).update(
                **updates)
            # Increment revision
            unit.__class__.objects.filter(id=unit.id).update(
                revision=Revision.incr(), **unit_updates)
        return stores
Exemplo n.º 6
0
 def increment_unsynced_unit_revision(self, store_revision,
                                      update_revision):
     filter_by = {
         'revision__gt': store_revision or 0,
         'revision__lt': update_revision,
         'state__gt': OBSOLETE}
     return self.target_store.unit_set.filter(
         **filter_by).update(
             revision=Revision.incr())
Exemplo n.º 7
0
    def toggle_qualitycheck(self, check_id, false_positive, user):
        check = self.qualitycheck_set.get(id=check_id)

        if check.false_positive == false_positive:
            return

        self.revision = Revision.incr()
        self.save(reviewed_by=user)
        toggle.send(check.__class__,
                    instance=check,
                    false_positive=false_positive)
Exemplo n.º 8
0
def handle_unit_pre_save(**kwargs):
    unit = kwargs["instance"]
    auto_translated = False

    if unit.source_updated:
        # update source related fields
        wc = unit.counter.count_words(unit.source_f.strings)
        if not wc and not bool(filter(None, unit.target_f.strings)):
            # auto-translate untranslated strings
            unit.target = unit.source
            unit.state = FUZZY
            auto_translated = True
    if unit.target_updated:
        # update target related fields
        unit.target_wordcount = unit.counter.count_words(unit.target_f.strings)
        unit.target_length = len(unit.target_f)
        if filter(None, unit.target_f.strings):
            if unit.state == UNTRANSLATED:
                unit.state = TRANSLATED
        else:
            # if it was TRANSLATED then set to UNTRANSLATED
            if unit.state > FUZZY:
                unit.state = UNTRANSLATED

    # Updating unit from the .po file set its revision property to
    # a new value (the same for all units during its store updated)
    # since that change doesn't require further sync but note that
    # auto_translated units require further sync
    update_revision = (unit.revision is None
                       or (not unit.revision_updated and
                           (unit.updated and not auto_translated)))
    if update_revision:
        unit.revision = Revision.incr()

    if unit.index is None:
        unit.index = unit.store.max_index() + 1
    unitid = uniqueid.get(unit.__class__)(unit)
    if unitid.changed:
        unit.setid(unitid.getid())
Exemplo n.º 9
0
    def revert_units_state_changed(self):
        """Revert unit edits made by a user to previous edit.
        """
        stores = set()
        # Delete orphaned submissions.
        self.user.submission_set.filter(unit__isnull=True).delete()

        for submission in self.user.get_unit_states_changed().iterator():
            unit = submission.unit
            stores.add(unit.store)

            # We have to get latest by pk as on mysql precision is not to
            # microseconds - so creation_time can be ambiguous
            if submission != unit.get_state_changes().latest('pk'):
                # If the unit has been changed more recently we don't need to
                # revert the unit state.
                submission.delete()
                return
            submission.delete()
            other_submissions = (unit.get_state_changes().exclude(
                submitter=self.user))
            if other_submissions.exists():
                new_state = other_submissions.latest('pk').new_value
            else:
                new_state = UNTRANSLATED
            if new_state != unit.state:
                if unit.state == FUZZY:
                    unit.markfuzzy(False)
                elif new_state == FUZZY:
                    unit.markfuzzy(True)
                unit.state = new_state

                # Increment revision
                unit.__class__.objects.filter(id=unit.id).update(
                    revision=Revision.incr())
                logger.debug("Unit state reverted: %s", repr(unit))
        return stores
Exemplo n.º 10
0
 def create_revision(self):
     Revision.initialize()