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.

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