Ejemplo n.º 1
0
    def generate_change(self, request, author, oldunit, change_action):
        """
        Creates Change entry for saving unit.
        """
        # Notify about new contributor
        user_changes = Change.objects.filter(translation=self.translation,
                                             user=request.user)
        if not user_changes.exists():
            notify_new_contributor(self, request.user)

        # Action type to store
        if change_action is not None:
            action = change_action
        elif oldunit.translated:
            action = Change.ACTION_CHANGE
        else:
            action = Change.ACTION_NEW

        # Should we store history of edits?
        if self.translation.subproject.save_history:
            history_target = self.target
        else:
            history_target = ''

        # Create change object
        Change.objects.create(unit=self,
                              translation=self.translation,
                              action=action,
                              user=request.user,
                              author=author,
                              target=history_target)
Ejemplo n.º 2
0
    def generate_change(self, request, author, oldunit, change_action):
        """
        Creates Change entry for saving unit.
        """
        # Notify about new contributor
        user_changes = Change.objects.filter(
            translation=self.translation,
            user=request.user
        )
        if not user_changes.exists():
            notify_new_contributor(self, request.user)

        # Action type to store
        if change_action is not None:
            action = change_action
        elif oldunit.translated:
            action = Change.ACTION_CHANGE
        else:
            action = Change.ACTION_NEW

        # Should we store history of edits?
        if self.translation.subproject.save_history:
            history_target = self.target
        else:
            history_target = ''

        # Create change object
        Change.objects.create(
            unit=self,
            translation=self.translation,
            action=action,
            user=request.user,
            author=author,
            target=history_target
        )
Ejemplo n.º 3
0
    def test_notify_new_contributor(self):
        unit = self.get_unit()
        notify_new_contributor(unit, self.second_user())

        # Check mail
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject, "[Weblate] New contributor in Test/Test - Czech")
Ejemplo n.º 4
0
    def test_notify_new_contributor(self):
        unit = self.get_unit()
        notify_new_contributor(unit, self.second_user())

        # Check mail
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject,
                         '[Weblate] New contributor in Test/Test - Czech')
Ejemplo n.º 5
0
    def save_backend(self, request, propagate=True, gen_change=True,
                     change_action=None, user=None):
        '''
        Stores unit to backend.

        Optional user parameters defines authorship of a change.
        '''
        from weblate.accounts.models import (
            notify_new_translation, notify_new_contributor
        )
        from weblate.trans.models.changes import Change

        # Update lock timestamp
        self.translation.update_lock(request)

        # For case when authorship specified, use user from request
        if user is None:
            user = request.user

        # Store to backend
        try:
            (saved, pounit) = self.translation.update_unit(self, request, user)
        except FileLockException:
            weblate.logger.error('failed to lock backend for %s!', self)
            messages.error(
                request,
                _(
                    'Failed to store message in the backend, '
                    'lock timeout occurred!'
                )
            )
            return False

        # Handle situation when backend did not find the message
        if pounit is None:
            weblate.logger.error('message %s disappeared!', self)
            messages.error(
                request,
                _(
                    'Message not found in backend storage, '
                    'it is probably corrupted.'
                )
            )
            # Try reloading from backend
            self.translation.check_sync(True)
            return False

        # Get old unit from database (for notifications)
        oldunit = Unit.objects.get(id=self.id)

        # Return if there was no change
        # We have to explicitly check for fuzzy flag change on monolingual
        # files, where we handle it ourselves without storing to backend
        if not saved and oldunit.fuzzy == self.fuzzy:
            # Propagate if we should
            if propagate:
                self.propagate(request, change_action)
            return False

        # Update translated flag
        self.translated = pounit.is_translated()

        # Update comments as they might have been changed (eg, fuzzy flag
        # removed)
        self.flags = pounit.get_flags()

        # Save updated unit to database
        self.save(backend=True)

        # Update translation stats
        old_translated = self.translation.translated
        self.translation.update_stats()

        # Notify subscribed users about new translation
        notify_new_translation(self, oldunit, request.user)

        # Update user stats
        user.profile.translated += 1
        user.profile.save()

        # Notify about new contributor
        user_changes = Change.objects.filter(
            translation=self.translation,
            user=request.user
        )
        if not user_changes.exists():
            notify_new_contributor(self, request.user)

        # Generate Change object for this change
        if gen_change:
            self.generate_change(request, user, oldunit, change_action)

        # Force commiting on completing translation
        if (old_translated < self.translation.translated
                and self.translation.translated == self.translation.total):
            self.translation.commit_pending(request)
            Change.objects.create(
                translation=self.translation,
                action=Change.ACTION_COMPLETE,
                user=request.user,
                author=user
            )

        # Propagate to other projects
        if propagate:
            self.propagate(request, change_action)

        return True
Ejemplo n.º 6
0
    def save_backend(self,
                     request,
                     propagate=True,
                     gen_change=True,
                     change_action=None,
                     user=None):
        '''
        Stores unit to backend.

        Optional user parameters defines authorship of a change.
        '''
        from weblate.accounts.models import (notify_new_translation,
                                             notify_new_contributor)
        from weblate.trans.models.changes import Change

        # Update lock timestamp
        self.translation.update_lock(request)

        # For case when authorship specified, use user from request
        if user is None:
            user = request.user

        # Store to backend
        try:
            (saved, pounit) = self.translation.update_unit(self, request, user)
        except FileLockException:
            weblate.logger.error('failed to lock backend for %s!', self)
            messages.error(
                request,
                _('Failed to store message in the backend, '
                  'lock timeout occurred!'))
            return False

        # Handle situation when backend did not find the message
        if pounit is None:
            weblate.logger.error('message %s disappeared!', self)
            messages.error(
                request,
                _('Message not found in backend storage, '
                  'it is probably corrupted.'))
            # Try reloading from backend
            self.translation.check_sync(True)
            return False

        # Get old unit from database (for notifications)
        oldunit = Unit.objects.get(id=self.id)

        # Return if there was no change
        # We have to explicitly check for fuzzy flag change on monolingual
        # files, where we handle it ourselves without storing to backend
        if not saved and oldunit.fuzzy == self.fuzzy:
            # Propagate if we should
            if propagate:
                self.propagate(request, change_action)
            return False

        # Update translated flag
        self.translated = pounit.is_translated()

        # Update comments as they might have been changed (eg, fuzzy flag
        # removed)
        self.flags = pounit.get_flags()

        # Save updated unit to database
        self.save(backend=True)

        # Update translation stats
        old_translated = self.translation.translated
        self.translation.update_stats()

        # Notify subscribed users about new translation
        notify_new_translation(self, oldunit, request.user)

        # Update user stats
        user.profile.translated += 1
        user.profile.save()

        # Notify about new contributor
        user_changes = Change.objects.filter(translation=self.translation,
                                             user=request.user)
        if not user_changes.exists():
            notify_new_contributor(self, request.user)

        # Generate Change object for this change
        if gen_change:
            self.generate_change(request, user, oldunit, change_action)

        # Force commiting on completing translation
        if (old_translated < self.translation.translated
                and self.translation.translated == self.translation.total):
            self.translation.commit_pending(request)
            Change.objects.create(translation=self.translation,
                                  action=Change.ACTION_COMPLETE,
                                  user=request.user,
                                  author=user)

        # Propagate to other projects
        if propagate:
            self.propagate(request, change_action)

        return True