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
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