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.

86 lines
3.2 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. import uuid
  2. from django.contrib.auth.models import User
  3. from django.core.exceptions import ValidationError
  4. from django.db import models
  5. from django_drf_filepond.models import DrfFilePondStoredStorage
  6. from .managers import ExampleManager, ExampleStateManager
  7. from projects.models import Project
  8. class Example(models.Model):
  9. objects = ExampleManager()
  10. uuid = models.UUIDField(default=uuid.uuid4, editable=False, db_index=True, unique=True)
  11. meta = models.JSONField(default=dict)
  12. filename = models.FileField(default=".", max_length=1024, storage=DrfFilePondStoredStorage())
  13. upload_name = models.CharField(max_length=512)
  14. project = models.ForeignKey(to=Project, on_delete=models.CASCADE, related_name="examples")
  15. annotations_approved_by = models.ForeignKey(to=User, on_delete=models.SET_NULL, null=True, blank=True)
  16. text = models.TextField(null=True, blank=True)
  17. score = models.FloatField(default=100)
  18. created_at = models.DateTimeField(auto_now_add=True, db_index=True)
  19. updated_at = models.DateTimeField(auto_now=True)
  20. @property
  21. def comment_count(self):
  22. return Comment.objects.filter(example=self.id).count()
  23. @property
  24. def data(self):
  25. if self.project.is_text_project:
  26. return self.text
  27. else:
  28. return str(self.filename)
  29. class Meta:
  30. ordering = ["created_at"]
  31. class Assignment(models.Model):
  32. id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
  33. project = models.ForeignKey(to=Project, on_delete=models.CASCADE, related_name="assignments")
  34. example = models.ForeignKey(to=Example, on_delete=models.CASCADE, related_name="assignments")
  35. assignee = models.ForeignKey(to=User, on_delete=models.CASCADE)
  36. created_at = models.DateTimeField(auto_now_add=True, db_index=True)
  37. updated_at = models.DateTimeField(auto_now=True)
  38. class Meta:
  39. unique_together = (("example", "assignee"),)
  40. def clean(self):
  41. # assignee must be a member of the project
  42. if not self.project.members.filter(id=self.assignee.id).exists():
  43. raise ValidationError("Assignee must be a member of the project")
  44. # example must be in the project
  45. if not self.project.examples.filter(id=self.example.id).exists():
  46. raise ValidationError("Example must be in the project")
  47. return super().clean()
  48. class ExampleState(models.Model):
  49. objects = ExampleStateManager()
  50. example = models.ForeignKey(to=Example, on_delete=models.CASCADE, related_name="states")
  51. confirmed_by = models.ForeignKey(to=User, on_delete=models.CASCADE)
  52. confirmed_at = models.DateTimeField(auto_now=True)
  53. class Meta:
  54. unique_together = (("example", "confirmed_by"),)
  55. class Comment(models.Model):
  56. text = models.TextField()
  57. example = models.ForeignKey(to=Example, on_delete=models.CASCADE, related_name="comments")
  58. user = models.ForeignKey(to=User, on_delete=models.CASCADE, null=True)
  59. created_at = models.DateTimeField(auto_now_add=True, db_index=True)
  60. updated_at = models.DateTimeField(auto_now=True)
  61. @property
  62. def username(self):
  63. return self.user.username
  64. class Meta:
  65. ordering = ["created_at"]