コード例 #1
0
ファイル: views.py プロジェクト: zitsen/pontoon
def _save_new_strings(project, source):
    """Save a batch of strings into an existing project.

    This function takes a batch of new strings as a blob of text, separate individual
    strings by new lines, and then stores each one as a new source string for the project.

    :arg Project project: the Project object to which new strings will be associated
    :arg string source: a text of new-line-separated source strings

    :returns: True if new strings have been saved, False otherwise

    """
    new_strings = source.strip().split("\n")

    # Remove empty strings from the list.
    new_strings = [x.strip() for x in new_strings if x.strip()]

    if new_strings:
        # Create a new fake resource for that project.
        resource = _get_resource_for_database_project(project)
        resource.total_strings = len(new_strings)
        resource.save()

        # Insert all new strings into Entity objects, associated to the fake resource.
        new_entities = []
        for index, new_string in enumerate(new_strings):
            string = new_string.strip()
            new_entities.append(
                Entity(string=string, resource=resource, order=index))

        Entity.objects.bulk_create(new_entities)

        return True

    return False
コード例 #2
0
    def execute_create_db(self):
        for vcs_entity in self.changes['create_db']:
            entity = Entity(**self.get_entity_updates(vcs_entity))
            entity.save()  # We can't use bulk_create since we need a PK

            for locale_code, vcs_translation in vcs_entity.translations.items():
                for plural_form, string in vcs_translation.strings.items():
                    self.translations_to_create.append(Translation(
                        entity=entity,
                        locale=self.locales[locale_code],
                        string=string,
                        plural_form=plural_form,
                        approved=not vcs_translation.fuzzy,
                        approved_date=self.now if not vcs_translation.fuzzy else None,
                        fuzzy=vcs_translation.fuzzy
                    ))
コード例 #3
0
def _save_new_strings(project, source):
    """Save a batch of strings into an existing project.

    This function takes a batch of new strings as a blob of text, separate individual
    strings by new lines, and then stores each one as a new source string for the project.

    :arg Project project: the Project object to which new strings will be associated
    :arg string source: a text of new-line-separated source strings

    :returns: True if new strings have been saved, False otherwise

    """
    new_strings = source.strip().split('\n')

    # Remove empty strings from the list.
    new_strings = [x.strip() for x in new_strings if x.strip()]

    if new_strings:
        # Create a new fake resource for that project.
        resource = Resource(path='database', project=project, total_strings=len(new_strings))
        resource.save()

        # Insert all new strings into Entity objects, associated to the fake resource.
        new_entities = []
        for new_string in new_strings:
            string = new_string.strip()
            new_entities.append(Entity(string=string, resource=resource))

        Entity.objects.bulk_create(new_entities)

        # Enable the new Resource for all active locales for that project.
        locales = (
            ProjectLocale.objects
            .filter(project=project)
            .values_list('locale_id', flat=True)
        )
        for locale_id in locales:
            tr = TranslatedResource(
                locale_id=locale_id,
                resource=resource,
            )
            tr.save()
            tr.calculate_stats()

        return True

    return False