mirror of
https://github.com/daniviga/bite.git
synced 2025-04-20 22:41:18 +02:00
Update subscription if exists
This commit is contained in:
parent
7bc8ec0d96
commit
956e5a8ceb
@ -3,8 +3,16 @@ from django.core.exceptions import ValidationError
|
|||||||
|
|
||||||
|
|
||||||
def device_validation(value):
|
def device_validation(value):
|
||||||
published_devices = WhiteList.objects.filter(serial=value,
|
if value.startswith('ZZ'): # simulated devices
|
||||||
is_published=True)
|
published_devices = WhiteList.objects.filter(
|
||||||
|
serial='ZZ%',
|
||||||
|
is_published=True
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
published_devices = WhiteList.objects.filter(
|
||||||
|
serial=value,
|
||||||
|
is_published=True
|
||||||
|
)
|
||||||
if not published_devices:
|
if not published_devices:
|
||||||
raise ValidationError("Device is not published")
|
raise ValidationError("Device is not published")
|
||||||
|
|
||||||
|
@ -1,14 +1,21 @@
|
|||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
from api.models import Device, WhiteList
|
from api.models import Device, device_validation
|
||||||
|
|
||||||
|
|
||||||
class DeviceSerializer(serializers.ModelSerializer):
|
class DeviceSerializer(serializers.ModelSerializer):
|
||||||
|
serial = serializers.CharField(
|
||||||
|
max_length=128,
|
||||||
|
validators=[device_validation],
|
||||||
|
) # disable unique validation
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Device
|
model = Device
|
||||||
fields = ('serial', 'creation_time', 'updated_time',)
|
fields = '__all__'
|
||||||
|
read_only_fields = ('creation_time', 'updated_time')
|
||||||
|
|
||||||
|
def create(self, validated_data):
|
||||||
# class WhiteListSerializer(serializers.ModelSerializer):
|
device, created = Device.objects.update_or_create(
|
||||||
# class Meta:
|
serial=validated_data['serial'],
|
||||||
# model = Device
|
defaults={'serial': validated_data['serial']},
|
||||||
# fields = ('serial', 'creation_time', 'updated_time',)
|
)
|
||||||
|
return device
|
||||||
|
Loading…
x
Reference in New Issue
Block a user