models.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. from django.db import models
  2. from django.urls import reverse
  3. from django.contrib.auth.models import User
  4. from django.db.models.signals import pre_save
  5. from django.dispatch import receiver
  6. from django.utils import timezone
  7. import datetime
  8. # TODO: Add Idea categories
  9. # these are columns in the ethics canvas
  10. # TODO: Add Idea ordering (preserve the order of ideas shown)
  11. # TODO: CanvasTag instead of IdeaTag
  12. # canvases have tags and text within ideas is highlighted with these tags
  13. class Canvas(models.Model):
  14. """
  15. The model for canvas metadata
  16. """
  17. title = models.CharField(
  18. max_length = 255,
  19. db_index = True,
  20. help_text="The title of the canvas"
  21. )
  22. date_created = models.DateTimeField(auto_now_add = True, db_index = True)
  23. date_modified = models.DateTimeField(auto_now = True, db_index = True)
  24. public = models.BooleanField(default = False, db_index = True)
  25. admins = models.ManyToManyField(User, related_name = 'admins')
  26. users = models.ManyToManyField(User, related_name = 'users')
  27. def __str__(self):
  28. """
  29. String of the Canvas
  30. """
  31. return self.title
  32. def get_absolute_url(self):
  33. return reverse('canvas-detail', args = [str(self.pk)])
  34. class Meta:
  35. ordering = ('date_modified',)
  36. @staticmethod
  37. @receiver(pre_save, sender=Canvas)
  38. def ensure_canvas_has_atleast_one_admin(sender, instance, **kwargs):
  39. if instance.admins.count == 0:
  40. raise Exception('Canvas should have at least one admin.')
  41. class IdeaCategory(models.Model):
  42. """
  43. Model associating ideas with a category
  44. """
  45. description = models.CharField(max_length = 50, help_text = "Category Description")
  46. def __str__(self):
  47. return self.description
  48. class CanvasTag(models.Model):
  49. """
  50. Model representing tags that relate ideas - many to many relationship, there may exist many different tags to an idea and vice versa
  51. Composite key made of the ideaID foreign key and the tagID
  52. """
  53. text = models.CharField(max_length = 255)
  54. def __str__(self):
  55. return self.text
  56. class Idea(models.Model):
  57. """
  58. Model representing an Idea on the canvas
  59. """
  60. # limit on charfields is 255
  61. text = models.CharField(max_length = 255, help_text = "The description of the idea")
  62. date_created = models.DateTimeField(auto_now_add = True, db_index = True)
  63. date_modified = models.DateTimeField(auto_now = True, db_index = True)
  64. category = models.ForeignKey('IdeaCategory', on_delete = models.CASCADE, null = True)
  65. canvas = models.ForeignKey('Canvas', on_delete = models.CASCADE, null = True)
  66. canvas_tags = models.ManyToManyField('CanvasTag', related_name = 'canvas_tags', blank = True)
  67. def __str__(self):
  68. return self.text
  69. # for now, order by created in ascending order (oldest at top)
  70. class Meta:
  71. ordering = ('date_created',)
  72. class Comment(models.Model):
  73. """
  74. Model representing comments, 1-1 relationship as a single comment does not apply do different ideas
  75. """
  76. text = models.CharField(max_length = 255, help_text = "Type a comment")
  77. resolved = models.BooleanField(default = False, db_index = True)
  78. user = models.ForeignKey(User, on_delete = models.CASCADE)
  79. idea = models.ForeignKey('Idea', on_delete = models.CASCADE, null = True)
  80. def __str__(self):
  81. if isResolved == true:
  82. return 'Comment: ' + self.text + '\n STATUS: Resolved'
  83. else:
  84. return 'Comment: ' + self.text + '\n STATUS: Unresolved'
  85. class Meta:
  86. ordering = ('resolved',)