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.

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