forked from mirrors/bookwyrm
Use Enum and constraint for valid notification types.
This commit is contained in:
parent
6b5644ed00
commit
1693473fd2
2 changed files with 33 additions and 14 deletions
22
fedireads/migrations/0016_auto_20200313_1337.py
Normal file
22
fedireads/migrations/0016_auto_20200313_1337.py
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
# Generated by Django 3.0.3 on 2020-03-13 13:37
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('fedireads', '0015_auto_20200311_1212'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='notification',
|
||||||
|
name='notification_type',
|
||||||
|
field=models.CharField(choices=[('FAVORITE', 'Favorite'), ('REPLY', 'Reply'), ('TAG', 'Tag'), ('FOLLOW', 'Follow'), ('FOLLOW_REQUEST', 'Follow Request')], max_length=255),
|
||||||
|
),
|
||||||
|
migrations.AddConstraint(
|
||||||
|
model_name='notification',
|
||||||
|
constraint=models.CheckConstraint(check=models.Q(notification_type__in=['FAVORITE', 'REPLY', 'TAG', 'FOLLOW', 'FOLLOW_REQUEST']), name='notification_type_valid'),
|
||||||
|
),
|
||||||
|
]
|
|
@ -88,6 +88,9 @@ class Tag(FedireadsModel):
|
||||||
unique_together = ('user', 'book', 'name')
|
unique_together = ('user', 'book', 'name')
|
||||||
|
|
||||||
|
|
||||||
|
NotificationType = models.TextChoices(
|
||||||
|
'NotificationType', 'FAVORITE REPLY TAG FOLLOW FOLLOW_REQUEST')
|
||||||
|
|
||||||
class Notification(FedireadsModel):
|
class Notification(FedireadsModel):
|
||||||
''' you've been tagged, liked, followed, etc '''
|
''' you've been tagged, liked, followed, etc '''
|
||||||
user = models.ForeignKey('User', on_delete=models.PROTECT)
|
user = models.ForeignKey('User', on_delete=models.PROTECT)
|
||||||
|
@ -99,18 +102,12 @@ class Notification(FedireadsModel):
|
||||||
related_status = models.ForeignKey(
|
related_status = models.ForeignKey(
|
||||||
'Status', on_delete=models.PROTECT, null=True)
|
'Status', on_delete=models.PROTECT, null=True)
|
||||||
read = models.BooleanField(default=False)
|
read = models.BooleanField(default=False)
|
||||||
notification_type = models.CharField(max_length=255)
|
notification_type = models.CharField(
|
||||||
|
max_length=255, choices=NotificationType.choices)
|
||||||
def save(self, *args, **kwargs):
|
class Meta:
|
||||||
# TODO: there's probably a real way to do enums
|
constraints = [
|
||||||
types = [
|
models.CheckConstraint(
|
||||||
'FAVORITE',
|
check=models.Q(notification_type__in=NotificationType.values),
|
||||||
'REPLY',
|
name="notification_type_valid",
|
||||||
'TAG',
|
)
|
||||||
'FOLLOW',
|
|
||||||
'FOLLOW_REQUEST'
|
|
||||||
]
|
]
|
||||||
if not self.notification_type in types:
|
|
||||||
raise ValueError('Invalid notitication type')
|
|
||||||
super().save(*args, **kwargs)
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue