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.

320 lines
10 KiB

5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. import string
  2. from django.db import models
  3. from django.dispatch import receiver
  4. from django.db.models.signals import post_save, pre_delete
  5. from django.urls import reverse
  6. from django.conf import settings
  7. from django.contrib.auth.models import User
  8. from django.contrib.staticfiles.storage import staticfiles_storage
  9. from django.core.exceptions import ValidationError
  10. from polymorphic.models import PolymorphicModel
  11. from .managers import AnnotationManager, Seq2seqAnnotationManager
  12. DOCUMENT_CLASSIFICATION = 'DocumentClassification'
  13. SEQUENCE_LABELING = 'SequenceLabeling'
  14. SEQ2SEQ = 'Seq2seq'
  15. PROJECT_CHOICES = (
  16. (DOCUMENT_CLASSIFICATION, 'document classification'),
  17. (SEQUENCE_LABELING, 'sequence labeling'),
  18. (SEQ2SEQ, 'sequence to sequence'),
  19. )
  20. class Project(PolymorphicModel):
  21. name = models.CharField(max_length=100)
  22. description = models.TextField(default='')
  23. guideline = models.TextField(default='')
  24. created_at = models.DateTimeField(auto_now_add=True)
  25. updated_at = models.DateTimeField(auto_now=True)
  26. users = models.ManyToManyField(User, related_name='projects')
  27. project_type = models.CharField(max_length=30, choices=PROJECT_CHOICES)
  28. randomize_document_order = models.BooleanField(default=False)
  29. collaborative_annotation = models.BooleanField(default=False)
  30. def get_absolute_url(self):
  31. return reverse('upload', args=[self.id])
  32. @property
  33. def image(self):
  34. raise NotImplementedError()
  35. def get_bundle_name(self):
  36. raise NotImplementedError()
  37. def get_bundle_name_upload(self):
  38. raise NotImplementedError()
  39. def get_bundle_name_download(self):
  40. raise NotImplementedError()
  41. def get_annotation_serializer(self):
  42. raise NotImplementedError()
  43. def get_annotation_class(self):
  44. raise NotImplementedError()
  45. def get_storage(self, data):
  46. raise NotImplementedError()
  47. def __str__(self):
  48. return self.name
  49. class TextClassificationProject(Project):
  50. @property
  51. def image(self):
  52. return staticfiles_storage.url('assets/images/cats/text_classification.jpg')
  53. def get_bundle_name(self):
  54. return 'document_classification'
  55. def get_bundle_name_upload(self):
  56. return 'upload_text_classification'
  57. def get_bundle_name_download(self):
  58. return 'download_text_classification'
  59. def get_annotation_serializer(self):
  60. from .serializers import DocumentAnnotationSerializer
  61. return DocumentAnnotationSerializer
  62. def get_annotation_class(self):
  63. return DocumentAnnotation
  64. def get_storage(self, data):
  65. from .utils import ClassificationStorage
  66. return ClassificationStorage(data, self)
  67. class SequenceLabelingProject(Project):
  68. @property
  69. def image(self):
  70. return staticfiles_storage.url('assets/images/cats/sequence_labeling.jpg')
  71. def get_bundle_name(self):
  72. return 'sequence_labeling'
  73. def get_bundle_name_upload(self):
  74. return 'upload_sequence_labeling'
  75. def get_bundle_name_download(self):
  76. return 'download_sequence_labeling'
  77. def get_annotation_serializer(self):
  78. from .serializers import SequenceAnnotationSerializer
  79. return SequenceAnnotationSerializer
  80. def get_annotation_class(self):
  81. return SequenceAnnotation
  82. def get_storage(self, data):
  83. from .utils import SequenceLabelingStorage
  84. return SequenceLabelingStorage(data, self)
  85. class Seq2seqProject(Project):
  86. @property
  87. def image(self):
  88. return staticfiles_storage.url('assets/images/cats/seq2seq.jpg')
  89. def get_bundle_name(self):
  90. return 'seq2seq'
  91. def get_bundle_name_upload(self):
  92. return 'upload_seq2seq'
  93. def get_bundle_name_download(self):
  94. return 'download_seq2seq'
  95. def get_annotation_serializer(self):
  96. from .serializers import Seq2seqAnnotationSerializer
  97. return Seq2seqAnnotationSerializer
  98. def get_annotation_class(self):
  99. return Seq2seqAnnotation
  100. def get_storage(self, data):
  101. from .utils import Seq2seqStorage
  102. return Seq2seqStorage(data, self)
  103. class Label(models.Model):
  104. PREFIX_KEYS = (
  105. ('ctrl', 'ctrl'),
  106. ('shift', 'shift'),
  107. ('ctrl shift', 'ctrl shift')
  108. )
  109. SUFFIX_KEYS = tuple(
  110. (c, c) for c in string.ascii_lowercase
  111. )
  112. text = models.CharField(max_length=100)
  113. prefix_key = models.CharField(max_length=10, blank=True, null=True, choices=PREFIX_KEYS)
  114. suffix_key = models.CharField(max_length=1, blank=True, null=True, choices=SUFFIX_KEYS)
  115. project = models.ForeignKey(Project, related_name='labels', on_delete=models.CASCADE)
  116. background_color = models.CharField(max_length=7, default='#209cee')
  117. text_color = models.CharField(max_length=7, default='#ffffff')
  118. created_at = models.DateTimeField(auto_now_add=True)
  119. updated_at = models.DateTimeField(auto_now=True)
  120. def __str__(self):
  121. return self.text
  122. def clean(self):
  123. # Don't allow shortcut key not to have a suffix key.
  124. if self.prefix_key and not self.suffix_key:
  125. raise ValidationError('Shortcut key may not have a suffix key.')
  126. # each shortcut (prefix key + suffix key) can only be assigned to one label
  127. if self.suffix_key or self.prefix_key:
  128. other_labels = self.project.labels.exclude(id=self.id)
  129. if other_labels.filter(suffix_key=self.suffix_key, prefix_key=self.prefix_key).exists():
  130. raise ValidationError('A label with this shortcut already exists in the project')
  131. super().clean()
  132. class Meta:
  133. unique_together = (
  134. ('project', 'text'),
  135. )
  136. class Document(models.Model):
  137. text = models.TextField()
  138. project = models.ForeignKey(Project, related_name='documents', on_delete=models.CASCADE)
  139. meta = models.TextField(default='{}')
  140. created_at = models.DateTimeField(auto_now_add=True)
  141. updated_at = models.DateTimeField(auto_now=True)
  142. annotations_approved_by = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
  143. def __str__(self):
  144. return self.text[:50]
  145. class Annotation(models.Model):
  146. objects = AnnotationManager()
  147. prob = models.FloatField(default=0.0)
  148. manual = models.BooleanField(default=False)
  149. user = models.ForeignKey(User, on_delete=models.CASCADE)
  150. created_at = models.DateTimeField(auto_now_add=True)
  151. updated_at = models.DateTimeField(auto_now=True)
  152. class Meta:
  153. abstract = True
  154. class DocumentAnnotation(Annotation):
  155. document = models.ForeignKey(Document, related_name='doc_annotations', on_delete=models.CASCADE)
  156. label = models.ForeignKey(Label, on_delete=models.CASCADE)
  157. class Meta:
  158. unique_together = ('document', 'user', 'label')
  159. class SequenceAnnotation(Annotation):
  160. document = models.ForeignKey(Document, related_name='seq_annotations', on_delete=models.CASCADE)
  161. label = models.ForeignKey(Label, on_delete=models.CASCADE)
  162. start_offset = models.IntegerField()
  163. end_offset = models.IntegerField()
  164. def clean(self):
  165. if self.start_offset >= self.end_offset:
  166. raise ValidationError('start_offset is after end_offset')
  167. class Meta:
  168. unique_together = ('document', 'user', 'label', 'start_offset', 'end_offset')
  169. class Seq2seqAnnotation(Annotation):
  170. # Override AnnotationManager for custom functionality
  171. objects = Seq2seqAnnotationManager()
  172. document = models.ForeignKey(Document, related_name='seq2seq_annotations', on_delete=models.CASCADE)
  173. text = models.CharField(max_length=500)
  174. class Meta:
  175. unique_together = ('document', 'user', 'text')
  176. class Role(models.Model):
  177. name = models.CharField(max_length=100, unique=True)
  178. description = models.TextField(default='')
  179. created_at = models.DateTimeField(auto_now_add=True)
  180. updated_at = models.DateTimeField(auto_now=True)
  181. def __str__(self):
  182. return self.name
  183. class RoleMapping(models.Model):
  184. user = models.ForeignKey(User, related_name='role_mappings', on_delete=models.CASCADE)
  185. project = models.ForeignKey(Project, related_name='role_mappings', on_delete=models.CASCADE)
  186. role = models.ForeignKey(Role, on_delete=models.CASCADE)
  187. created_at = models.DateTimeField(auto_now_add=True)
  188. updated_at = models.DateTimeField(auto_now=True)
  189. def clean(self):
  190. other_rolemappings = self.project.role_mappings.exclude(id=self.id)
  191. if other_rolemappings.filter(user=self.user, project=self.project).exists():
  192. raise ValidationError('This user is already assigned to a role in this project.')
  193. class Meta:
  194. unique_together = ("user", "project", "role")
  195. @receiver(post_save, sender=RoleMapping)
  196. def add_linked_project(sender, instance, created, **kwargs):
  197. if not created:
  198. return
  199. userInstance = instance.user
  200. projectInstance = instance.project
  201. if userInstance and projectInstance:
  202. user = User.objects.get(pk=userInstance.pk)
  203. project = Project.objects.get(pk=projectInstance.pk)
  204. user.projects.add(project)
  205. user.save()
  206. @receiver(post_save)
  207. def add_superusers_to_project(sender, instance, created, **kwargs):
  208. if not created:
  209. return
  210. if sender not in Project.__subclasses__():
  211. return
  212. superusers = User.objects.filter(is_superuser=True)
  213. admin_role = Role.objects.filter(name=settings.ROLE_PROJECT_ADMIN).first()
  214. if superusers and admin_role:
  215. RoleMapping.objects.bulk_create(
  216. [RoleMapping(role_id=admin_role.id, user_id=superuser.id, project_id=instance.id)
  217. for superuser in superusers]
  218. )
  219. @receiver(post_save, sender=User)
  220. def add_new_superuser_to_projects(sender, instance, created, **kwargs):
  221. if created and instance.is_superuser:
  222. admin_role = Role.objects.filter(name=settings.ROLE_PROJECT_ADMIN).first()
  223. projects = Project.objects.all()
  224. if admin_role and projects:
  225. RoleMapping.objects.bulk_create(
  226. [RoleMapping(role_id=admin_role.id, user_id=instance.id, project_id=project.id)
  227. for project in projects]
  228. )
  229. @receiver(pre_delete, sender=RoleMapping)
  230. def delete_linked_project(sender, instance, using, **kwargs):
  231. userInstance = instance.user
  232. projectInstance = instance.project
  233. if userInstance and projectInstance:
  234. user = User.objects.get(pk=userInstance.pk)
  235. project = Project.objects.get(pk=projectInstance.pk)
  236. user.projects.remove(project)
  237. user.save()