def setUpBeforeMigration(self, apps_before): Resource = apps_before.get_model('resources', 'Resource') User = apps_before.get_model('accounts', 'User') Organization = apps_before.get_model('organization', 'Organization') Project = apps_before.get_model('organization', 'Project') user = User.objects.create(username='******') org = Organization.objects.create(name='Test Org') project = Project.objects.create(name='Test Proj', organization=org) base_path = ( 'https://s3-us-west-2.amazonaws.com/cadasta-resources/' ) # cannot call custom save methods on models in migrations # as models are serialized from migration scripts # so custom methods are not available # docs.djangoproject.com/en/1.9/topics/migrations/#historical-models for f in range(10): file_name = base_path + 'test_' + str(f) + '.csv' resource_name = 'test-resource-' + str(f) Resource.objects.create( id=random_id(), name=resource_name, file=file_name, mime_type='text/csv', contributor=user, project=project ) Resource.objects.create( id=random_id(), file=base_path + 'test.jpg', mime_type='image/jpg', contributor=user, project=project )
def setUpBeforeMigration(self, apps_before): Resource = apps_before.get_model('resources', 'Resource') User = apps_before.get_model('accounts', 'User') Organization = apps_before.get_model('organization', 'Organization') Project = apps_before.get_model('organization', 'Project') user = User.objects.create(username='******') org = Organization.objects.create(name='Test Org') project = Project.objects.create(name='Test Proj', organization=org) base_path = ( 'https://s3-us-west-2.amazonaws.com/cadasta-resources/' 'resources/' ) for f in range(10): file_name = base_path + 'test_' + str(f) + '.csv' resource_name = 'test-resource-' + str(f) Resource.objects.create( id=random_id(), name=resource_name, file=file_name, mime_type='text/csv', contributor=user, project=project ) Resource.objects.create( id=random_id(), file=base_path + 'test.jpg', mime_type='image/jpg', contributor=user, project=project )
def done(self, form_list, **kwargs): form_data = [form.cleaned_data for form in form_list] name = form_data[0]['name'] description = form_data[0]['description'] mime_type = form_data[0]['mime_type'] is_resource = form_data[0]['is_resource'] original_file = form_data[0]['original_file'] file = form_data[0]['file'] type = self.storage.get_step_data( 'select_file').get('select_file-type') entity_types = self.storage.get_step_data('select_file').getlist( 'select_file-entity_types' ) path = self.file_storage.path(file.name) map_attrs_data = self.storage.get_step_data('map_attributes') project = self.get_project() org = project.organization config_dict = { 'project': project, 'file': path, 'type': type, 'entity_types': entity_types.copy(), 'party_name_field': form_data[2]['party_name_field'], 'party_type_field': form_data[2]['party_type_field'], 'location_type_field': form_data[2]['location_type_field'], 'geometry_field': form_data[2]['geometry_field'], 'attributes': map_attrs_data.getlist('attributes', None), } importer = self._get_importer(type, path) importer.import_data(config_dict) if is_resource: default_storage = DefaultStorage() file.seek(0) ext = file.name[file.name.rfind('.'):] resource = Resource( name=name, description=description, original_file=original_file, mime_type=mime_type, contributor=self.request.user, project=self.get_project()) upload_to = getattr(resource.file.field, 'upload_to') url = '' while not url: temp_url = upload_to + '/' + random_id() + ext if not Resource.objects.filter( file__contains=temp_url).exists(): url = default_storage.save(temp_url, file.read()) resource.file.url = url resource.save() ContentObject.objects.create(resource=resource, content_object=resource.project) return redirect('organization:project-dashboard', organization=org.slug, project=project.slug)
def randomize_imported_resource_filenames(apps, schema_editor): Resource = apps.get_model("resources", "Resource") resources = Resource.objects.filter(mime_type='text/csv') for resource in resources: url = resource.file.url if url.endswith('.csv'): random_filename = '/' + random_id() + '.csv' parts = list(os.path.split(url)) base_url = parts[:-1] url = '/'.join(base_url) + random_filename resource.file.url = url resource.save()
def done(self, form_list, **kwargs): form_data = [form.cleaned_data for form in form_list] name = form_data[0]['name'] description = form_data[0]['description'] mime_type = form_data[0]['mime_type'] is_resource = form_data[0]['is_resource'] original_file = form_data[0]['original_file'] file = form_data[0]['file'] type = self.storage.get_step_data('select_file').get( 'select_file-type') entity_types = self.storage.get_step_data('select_file').getlist( 'select_file-entity_types') path = self.file_storage.path(file.name) map_attrs_data = self.storage.get_step_data('map_attributes') project = self.get_project() org = project.organization allowed_tenure_types = get_types( 'tenure_type', TENURE_RELATIONSHIP_TYPES, questionnaire_id=project.current_questionnaire) allowed_location_types = get_types( 'location_type', TYPE_CHOICES, questionnaire_id=project.current_questionnaire) config_dict = { 'project': project, 'file': path, 'type': type, 'entity_types': entity_types.copy(), 'party_name_field': form_data[2]['party_name_field'], 'party_type_field': form_data[2]['party_type_field'], 'location_type_field': form_data[2]['location_type_field'], 'geometry_field': form_data[2]['geometry_field'], 'attributes': map_attrs_data.getlist('attributes', None), 'allowed_tenure_types': allowed_tenure_types, 'allowed_location_types': allowed_location_types } importer = self._get_importer(type, path) importer.import_data(config_dict) if is_resource: default_storage = DefaultStorage() file.seek(0) ext = file.name[file.name.rfind('.'):] resource = Resource(name=name, description=description, original_file=original_file, mime_type=mime_type, contributor=self.request.user, project=self.get_project()) upload_to = getattr(resource.file.field, 'upload_to') url = '' while not url: temp_url = upload_to + '/' + random_id() + ext if not Resource.objects.filter( file__contains=temp_url).exists(): url = default_storage.save(temp_url, file.read()) resource.file.url = url resource.save() ContentObject.objects.create(resource=resource, content_object=resource.project) return redirect('organization:project-dashboard', organization=org.slug, project=project.slug)