def _get_resource_for_database_project(project):
    """Return the Resource object of an in database project.

    If the project has no resource yet, create a new one and return it.
    Otherwise, return the existing resource.

    Note that a database project should always have only one resource.

    :arg Project project: the in-database Project object

    :returns: the unique Resource object associated with the project

    """
    try:
        return Resource.objects.get(
            project=project,
        )
    except Resource.DoesNotExist:
        # There's no resource for that project yet, create one.
        resource = Resource(
            path='database',
            project=project,
        )
        resource.save()
        return resource
    except Resource.MultipleObjectsReturned:
        # There are several resources for this project, that should not
        # be allowed. Log an error and raise.
        log.error(
            'There is more than 1 Resource for in_database project %s' %
            project.name
        )
        raise
Example #2
0
def update_resources(db_project, vcs_project):
    """Update the database on what resource files exist in VCS."""
    log.debug(f"Scanning {vcs_project.source_directory_path}")
    vcs_changed_files, vcs_removed_files = vcs_project.changed_source_files

    removed_resources = db_project.resources.filter(path__in=vcs_removed_files)
    removed_paths = removed_resources.values_list("path", flat=True)

    changed_resources = db_project.resources.filter(path__in=vcs_changed_files)
    changed_paths = changed_resources.values_list("path", flat=True)

    added_paths = []

    log.debug("Removed files: {}".format(", ".join(removed_paths) or "None"))
    removed_resources.delete()

    for relative_path, vcs_resource in vcs_project.resources.items():
        resource, created = db_project.resources.get_or_create(
            path=relative_path)
        resource.format = Resource.get_path_format(relative_path)
        resource.total_strings = len(vcs_resource.entities)
        resource.save()

        if created:
            added_paths.append(relative_path)

    log.debug("Added files: {}".format(", ".join(added_paths) or "None"))
    return added_paths, removed_paths, changed_paths
Example #3
0
def update_resources(db_project, vcs_project):
    """Update the database on what resource files exist in VCS."""
    log.debug('Scanning {}'.format(vcs_project.source_directory_path))
    _, vcs_removed_files = vcs_project.changed_source_files

    removed_resources = db_project.resources.filter(path__in=vcs_removed_files)
    removed_paths = removed_resources.values_list('path', flat=True)

    added_paths = []

    log.debug('Removed files: {}'.format(', '.join(removed_paths) or 'None'))
    removed_resources.delete()

    for relative_path, vcs_resource in vcs_project.resources.items():
        resource, created = db_project.resources.get_or_create(
            path=relative_path)
        resource.format = Resource.get_path_format(relative_path)
        resource.total_strings = len(vcs_resource.entities)
        resource.save()

        if created:
            added_paths.append(relative_path)

    log.debug('Added files: {}'.format(', '.join(added_paths) or 'None'))
    return removed_paths, added_paths
Example #4
0
def update_resources(db_project, vcs_project):
    """Update the database on what resource files exist in VCS."""
    log.debug('Scanning {}'.format(vcs_project.source_directory_path))
    vcs_changed_files, vcs_removed_files = vcs_project.changed_source_files

    removed_resources = db_project.resources.filter(path__in=vcs_removed_files)
    removed_paths = removed_resources.values_list('path', flat=True)

    changed_resources = db_project.resources.filter(path__in=vcs_changed_files)
    changed_paths = changed_resources.values_list('path', flat=True)

    added_paths = []

    log.debug('Removed files: {}'.format(', '.join(removed_paths) or 'None'))
    removed_resources.delete()

    for relative_path, vcs_resource in vcs_project.resources.items():
        resource, created = db_project.resources.get_or_create(path=relative_path)
        resource.format = Resource.get_path_format(relative_path)
        resource.total_strings = len(vcs_resource.entities)
        resource.save()

        if created:
            added_paths.append(relative_path)

    log.debug('Added files: {}'.format(', '.join(added_paths) or 'None'))
    return added_paths, removed_paths, changed_paths
Example #5
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
Example #6
0
    def update_resources(self, db_project, vcs_project):
        """Update the database on what resource files exist in VCS."""
        relative_paths = vcs_project.resources.keys()
        db_project.resource_set.exclude(path__in=relative_paths).delete()

        for relative_path, vcs_resource in vcs_project.resources.items():
            resource, created = db_project.resource_set.get_or_create(path=relative_path)
            resource.format = Resource.get_path_format(relative_path)
            resource.entity_count = len(vcs_resource.entities)
            resource.save()
Example #7
0
def update_resources(db_project, vcs_project):
    """Update the database on what resource files exist in VCS."""
    relative_paths = vcs_project.resources.keys()
    db_project.resources.exclude(path__in=relative_paths).delete()

    for relative_path, vcs_resource in vcs_project.resources.items():
        resource, created = db_project.resources.get_or_create(path=relative_path)
        resource.format = Resource.get_path_format(relative_path)
        resource.entity_count = len(vcs_resource.entities)
        resource.save()
Example #8
0
def update_resources(db_project, vcs_project):
    """Update the database on what resource files exist in VCS."""
    log.debug('Scanning {}'.format(vcs_project.source_directory_path()))
    relative_paths = vcs_project.resources.keys()
    removed_resources = db_project.resources.exclude(path__in=relative_paths)
    removed_paths = removed_resources.values_list('path', flat=True)
    log.debug('Removed paths: {}'.format(', '.join(removed_paths)))
    removed_resources.delete()

    for relative_path, vcs_resource in vcs_project.resources.items():
        resource, created = db_project.resources.get_or_create(path=relative_path)
        resource.format = Resource.get_path_format(relative_path)
        resource.total_strings = len(vcs_resource.entities)
        resource.save()
    return removed_paths