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.

61 lines
2.1 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.db import models
  4. from django_drf_filepond.models import DrfFilePondStoredStorage
  5. from .managers import ExampleManager, ExampleStateManager
  6. from projects.models import Project
  7. class Example(models.Model):
  8. objects = ExampleManager()
  9. uuid = models.UUIDField(default=uuid.uuid4, editable=False, db_index=True, unique=True)
  10. meta = models.JSONField(default=dict)
  11. filename = models.FileField(default=".", max_length=1024, storage=DrfFilePondStoredStorage())
  12. upload_name = models.CharField(max_length=512)
  13. project = models.ForeignKey(to=Project, on_delete=models.CASCADE, related_name="examples")
  14. annotations_approved_by = models.ForeignKey(to=User, on_delete=models.SET_NULL, null=True, blank=True)
  15. text = models.TextField(null=True, blank=True)
  16. created_at = models.DateTimeField(auto_now_add=True, db_index=True)
  17. updated_at = models.DateTimeField(auto_now=True)
  18. @property
  19. def comment_count(self):
  20. return Comment.objects.filter(example=self.id).count()
  21. @property
  22. def data(self):
  23. if self.project.is_text_project:
  24. return self.text
  25. else:
  26. return str(self.filename)
  27. class Meta:
  28. ordering = ["created_at"]
  29. class ExampleState(models.Model):
  30. objects = ExampleStateManager()
  31. example = models.ForeignKey(to=Example, on_delete=models.CASCADE, related_name="states")
  32. confirmed_by = models.ForeignKey(to=User, on_delete=models.CASCADE)
  33. confirmed_at = models.DateTimeField(auto_now=True)
  34. class Meta:
  35. unique_together = (("example", "confirmed_by"),)
  36. class Comment(models.Model):
  37. text = models.TextField()
  38. example = models.ForeignKey(to=Example, on_delete=models.CASCADE, related_name="comments")
  39. user = models.ForeignKey(to=User, on_delete=models.CASCADE, null=True)
  40. created_at = models.DateTimeField(auto_now_add=True, db_index=True)
  41. updated_at = models.DateTimeField(auto_now=True)
  42. @property
  43. def username(self):
  44. return self.user.username
  45. class Meta:
  46. ordering = ["created_at"]