Use Enum and constraint for valid notification types.
This commit is contained in:
@ -88,6 +88,9 @@ class Tag(FedireadsModel):
|
||||
unique_together = ('user', 'book', 'name')
|
||||
|
||||
|
||||
NotificationType = models.TextChoices(
|
||||
'NotificationType', 'FAVORITE REPLY TAG FOLLOW FOLLOW_REQUEST')
|
||||
|
||||
class Notification(FedireadsModel):
|
||||
''' you've been tagged, liked, followed, etc '''
|
||||
user = models.ForeignKey('User', on_delete=models.PROTECT)
|
||||
@ -99,18 +102,12 @@ class Notification(FedireadsModel):
|
||||
related_status = models.ForeignKey(
|
||||
'Status', on_delete=models.PROTECT, null=True)
|
||||
read = models.BooleanField(default=False)
|
||||
notification_type = models.CharField(max_length=255)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
# TODO: there's probably a real way to do enums
|
||||
types = [
|
||||
'FAVORITE',
|
||||
'REPLY',
|
||||
'TAG',
|
||||
'FOLLOW',
|
||||
'FOLLOW_REQUEST'
|
||||
notification_type = models.CharField(
|
||||
max_length=255, choices=NotificationType.choices)
|
||||
class Meta:
|
||||
constraints = [
|
||||
models.CheckConstraint(
|
||||
check=models.Q(notification_type__in=NotificationType.values),
|
||||
name="notification_type_valid",
|
||||
)
|
||||
]
|
||||
if not self.notification_type in types:
|
||||
raise ValueError('Invalid notitication type')
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
|
Reference in New Issue
Block a user