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.

83 lines
2.1 KiB

  1. import uuid
  2. from django.contrib.auth.models import User
  3. from django.db import models
  4. from api.models import Project
  5. from .managers import ExampleManager, ExampleStateManager
  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(
  12. to=Project,
  13. on_delete=models.CASCADE,
  14. related_name='examples'
  15. )
  16. annotations_approved_by = models.ForeignKey(
  17. to=User,
  18. on_delete=models.SET_NULL,
  19. null=True,
  20. blank=True
  21. )
  22. text = models.TextField(null=True, blank=True)
  23. created_at = models.DateTimeField(auto_now_add=True, db_index=True)
  24. updated_at = models.DateTimeField(auto_now=True)
  25. @property
  26. def comment_count(self):
  27. return Comment.objects.filter(example=self.id).count()
  28. @property
  29. def data(self):
  30. if self.project.is_text_project:
  31. return self.text
  32. else:
  33. return str(self.filename)
  34. class Meta:
  35. ordering = ['created_at']
  36. class ExampleState(models.Model):
  37. objects = ExampleStateManager()
  38. example = models.ForeignKey(
  39. to=Example,
  40. on_delete=models.CASCADE,
  41. related_name='states'
  42. )
  43. confirmed_by = models.ForeignKey(
  44. to=User,
  45. on_delete=models.CASCADE
  46. )
  47. confirmed_at = models.DateTimeField(auto_now=True)
  48. class Meta:
  49. unique_together = (('example', 'confirmed_by'),)
  50. class Comment(models.Model):
  51. text = models.TextField()
  52. example = models.ForeignKey(
  53. to=Example,
  54. on_delete=models.CASCADE,
  55. related_name='comments'
  56. )
  57. user = models.ForeignKey(
  58. to=User,
  59. on_delete=models.CASCADE,
  60. null=True
  61. )
  62. created_at = models.DateTimeField(auto_now_add=True, db_index=True)
  63. updated_at = models.DateTimeField(auto_now=True)
  64. @property
  65. def username(self):
  66. return self.user.username
  67. class Meta:
  68. ordering = ['created_at']