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.

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