You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

23 lines
911 B

  1. from celery import shared_task
  2. from django.conf import settings
  3. from django.contrib.auth import get_user_model
  4. from django.shortcuts import get_object_or_404
  5. from api.models import Project
  6. from .pipeline.factories import create_parser, create_bulder, create_cleaner
  7. from .pipeline.readers import Reader
  8. from .pipeline.writers import BulkWriter
  9. @shared_task
  10. def import_dataset(user_id, project_id, filenames, file_format: str, **kwargs):
  11. project = get_object_or_404(Project, pk=project_id)
  12. user = get_object_or_404(get_user_model(), pk=user_id)
  13. parser = create_parser(file_format, **kwargs)
  14. builder = create_bulder(project, **kwargs)
  15. reader = Reader(filenames=filenames, parser=parser, builder=builder)
  16. cleaner = create_cleaner(project)
  17. writer = BulkWriter(batch_size=settings.IMPORT_BATCH_SIZE)
  18. writer.save(reader, project, user, cleaner)
  19. return {'error': writer.errors}