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.

71 lines
2.6 KiB

2 years ago
2 years ago
  1. import os
  2. import shutil
  3. import uuid
  4. from celery import shared_task
  5. from celery.utils.log import get_task_logger
  6. from django.conf import settings
  7. from django.shortcuts import get_object_or_404
  8. from .pipeline.dataset import Dataset
  9. from .pipeline.factories import (
  10. create_comment,
  11. create_formatter,
  12. create_labels,
  13. create_writer,
  14. )
  15. from .pipeline.services import ExportApplicationService
  16. from data_export.models import ExportedExample
  17. from projects.models import Member, Project
  18. logger = get_task_logger(__name__)
  19. def create_collaborative_dataset(project: Project, dirpath: str, confirmed_only: bool, formatters, writer):
  20. is_text_project = project.is_text_project
  21. if confirmed_only:
  22. examples = ExportedExample.objects.confirmed(project)
  23. else:
  24. examples = ExportedExample.objects.filter(project=project)
  25. labels = create_labels(project, examples)
  26. comments = create_comment(examples)
  27. dataset = Dataset(examples, labels, comments, is_text_project)
  28. service = ExportApplicationService(dataset, formatters, writer)
  29. filepath = os.path.join(dirpath, f"all.{writer.extension}")
  30. service.export(filepath)
  31. def create_individual_dataset(project: Project, dirpath: str, confirmed_only: bool, formatters, writer):
  32. is_text_project = project.is_text_project
  33. members = Member.objects.filter(project=project)
  34. for member in members:
  35. if confirmed_only:
  36. examples = ExportedExample.objects.confirmed(project, user=member.user)
  37. else:
  38. examples = ExportedExample.objects.filter(project=project)
  39. labels = create_labels(project, examples, member.user)
  40. comments = create_comment(examples, member.user)
  41. dataset = Dataset(examples, labels, comments, is_text_project)
  42. service = ExportApplicationService(dataset, formatters, writer)
  43. filepath = os.path.join(dirpath, f"{member.username}.{writer.extension}")
  44. service.export(filepath)
  45. @shared_task
  46. def export_dataset(project_id, file_format: str, confirmed_only=False):
  47. project = get_object_or_404(Project, pk=project_id)
  48. dirpath = os.path.join(settings.MEDIA_ROOT, str(uuid.uuid4()))
  49. os.makedirs(dirpath, exist_ok=True)
  50. formatters = create_formatter(project, file_format)
  51. writer = create_writer(file_format)
  52. if project.collaborative_annotation:
  53. create_collaborative_dataset(project, dirpath, confirmed_only, formatters, writer)
  54. else:
  55. create_individual_dataset(project, dirpath, confirmed_only, formatters, writer)
  56. zip_file = shutil.make_archive(dirpath, "zip", dirpath)
  57. shutil.rmtree(dirpath)
  58. return zip_file