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.

108 lines
2.9 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. from typing import List
  2. from django.conf import settings
  3. from model_mommy import mommy
  4. from projects.models import (
  5. BOUNDING_BOX,
  6. DOCUMENT_CLASSIFICATION,
  7. IMAGE_CAPTIONING,
  8. IMAGE_CLASSIFICATION,
  9. INTENT_DETECTION_AND_SLOT_FILLING,
  10. SEGMENTATION,
  11. SEQ2SEQ,
  12. SEQUENCE_LABELING,
  13. SPEECH2TEXT,
  14. Member,
  15. Role,
  16. )
  17. from roles.tests.utils import create_default_roles
  18. from users.tests.utils import make_user
  19. class ProjectData:
  20. def __init__(self, item, members):
  21. self.item = item
  22. self.members = members
  23. @property
  24. def id(self):
  25. return self.item.id
  26. @property
  27. def admin(self):
  28. return self.members[0]
  29. @property
  30. def approver(self):
  31. return self.members[1]
  32. @property
  33. def annotator(self):
  34. return self.members[2]
  35. @property
  36. def staffs(self):
  37. return [self.approver, self.annotator]
  38. def assign_user_to_role(project_member, project, role_name):
  39. role, _ = Role.objects.get_or_create(name=role_name)
  40. if Member.objects.filter(user=project_member, project=project).exists():
  41. mapping = Member.objects.get(user=project_member, project=project)
  42. mapping.role = role
  43. mapping.save()
  44. else:
  45. mapping = Member.objects.get_or_create(role_id=role.id, user_id=project_member.id, project_id=project.id)
  46. return mapping
  47. def make_project(task: str, users: List[str], roles: List[str], collaborative_annotation=False, **kwargs):
  48. create_default_roles()
  49. # create users.
  50. users = [make_user(name) for name in users]
  51. # create a project.
  52. project_model = {
  53. DOCUMENT_CLASSIFICATION: "TextClassificationProject",
  54. SEQUENCE_LABELING: "SequenceLabelingProject",
  55. SEQ2SEQ: "Seq2seqProject",
  56. SPEECH2TEXT: "Speech2TextProject",
  57. IMAGE_CLASSIFICATION: "ImageClassificationProject",
  58. INTENT_DETECTION_AND_SLOT_FILLING: "IntentDetectionAndSlotFillingProject",
  59. BOUNDING_BOX: "BoundingBoxProject",
  60. SEGMENTATION: "SegmentationProject",
  61. IMAGE_CAPTIONING: "ImageCaptioningProject",
  62. }.get(task, "Project")
  63. project = mommy.make(
  64. _model=project_model,
  65. project_type=task,
  66. collaborative_annotation=collaborative_annotation,
  67. created_by=users[0],
  68. **kwargs,
  69. )
  70. # assign roles to the users.
  71. for user, role in zip(users, roles):
  72. assign_user_to_role(user, project, role)
  73. return ProjectData(item=project, members=users)
  74. def make_tag(project):
  75. return mommy.make("Tag", project=project)
  76. def prepare_project(task: str = "Any", collaborative_annotation=False, **kwargs):
  77. return make_project(
  78. task=task,
  79. users=["admin", "approver", "annotator"],
  80. roles=[
  81. settings.ROLE_PROJECT_ADMIN,
  82. settings.ROLE_ANNOTATION_APPROVER,
  83. settings.ROLE_ANNOTATOR,
  84. ],
  85. collaborative_annotation=collaborative_annotation,
  86. **kwargs,
  87. )