Adds validator for review rating model field

This commit is contained in:
Mouse Reeve 2020-02-10 21:58:25 -08:00
parent d088ae7747
commit 55f5735b33

View file

@ -4,6 +4,7 @@ from model_utils.managers import InheritanceManager
from django.dispatch import receiver from django.dispatch import receiver
from django.contrib.auth.models import AbstractUser from django.contrib.auth.models import AbstractUser
from django.contrib.postgres.fields import JSONField from django.contrib.postgres.fields import JSONField
from django.core.exceptions import ValidationError
from Crypto import Random from Crypto import Random
from Crypto.PublicKey import RSA from Crypto.PublicKey import RSA
import re import re
@ -137,12 +138,16 @@ class FollowActivity(Activity):
super().save(*args, **kwargs) super().save(*args, **kwargs)
def validate_rating(rating):
''' only accept 0-5 as star rating ints '''
if rating < 0 or rating > 5:
raise ValidationError('Rating must be 0-5')
class Review(Activity): class Review(Activity):
''' a book review ''' ''' a book review '''
book = models.ForeignKey('Book', on_delete=models.PROTECT) book = models.ForeignKey('Book', on_delete=models.PROTECT)
name = models.CharField(max_length=255) name = models.CharField(max_length=255)
# TODO: validation rating = models.IntegerField(default=0, validators=[validate_rating])
rating = models.IntegerField(default=0)
review_content = models.TextField() review_content = models.TextField()
def save(self, *args, **kwargs): def save(self, *args, **kwargs):