2020-06-02 00:57:20 +02:00
|
|
|
from django.db import models
|
2020-06-03 18:57:53 +02:00
|
|
|
from django.conf import settings
|
2020-06-02 00:57:20 +02:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
|
|
|
|
|
|
|
|
def device_validation(value):
|
2020-06-03 18:57:53 +02:00
|
|
|
if settings.SKIP_WHITELIST and settings.DEBUG:
|
|
|
|
return # skip validation in debug mode when SKIP_WHITELIST is True
|
|
|
|
|
|
|
|
published_devices = WhiteList.objects.filter(
|
|
|
|
serial=value,
|
|
|
|
is_published=True
|
|
|
|
)
|
2020-06-02 00:57:20 +02:00
|
|
|
if not published_devices:
|
|
|
|
raise ValidationError("Device is not published")
|
|
|
|
|
|
|
|
|
|
|
|
class WhiteList(models.Model):
|
|
|
|
serial = models.CharField(max_length=128, unique=True)
|
|
|
|
creation_time = models.DateTimeField(auto_now_add=True)
|
|
|
|
updated_time = models.DateTimeField(auto_now=True)
|
|
|
|
is_published = models.BooleanField(default=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ['serial', 'updated_time']
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.serial
|
|
|
|
|
|
|
|
|
|
|
|
class Device(models.Model):
|
|
|
|
serial = models.CharField(max_length=128, unique=True,
|
|
|
|
validators=[device_validation])
|
|
|
|
creation_time = models.DateTimeField(auto_now_add=True)
|
|
|
|
updated_time = models.DateTimeField(auto_now=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ['updated_time', 'serial']
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.serial
|