mirror of
https://github.com/daniviga/bite.git
synced 2025-04-18 22:00:11 +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):
|
||||
published_devices = WhiteList.objects.filter(serial=value,
|
||||
is_published=True)
|
||||
if value.startswith('ZZ'): # simulated devices
|
||||
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:
|
||||
raise ValidationError("Device is not published")
|
||||
|
||||
|
@ -1,14 +1,21 @@
|
||||
from rest_framework import serializers
|
||||
from api.models import Device, WhiteList
|
||||
from api.models import Device, device_validation
|
||||
|
||||
|
||||
class DeviceSerializer(serializers.ModelSerializer):
|
||||
serial = serializers.CharField(
|
||||
max_length=128,
|
||||
validators=[device_validation],
|
||||
) # disable unique validation
|
||||
|
||||
class Meta:
|
||||
model = Device
|
||||
fields = ('serial', 'creation_time', 'updated_time',)
|
||||
fields = '__all__'
|
||||
read_only_fields = ('creation_time', 'updated_time')
|
||||
|
||||
|
||||
# class WhiteListSerializer(serializers.ModelSerializer):
|
||||
# class Meta:
|
||||
# model = Device
|
||||
# fields = ('serial', 'creation_time', 'updated_time',)
|
||||
def create(self, validated_data):
|
||||
device, created = Device.objects.update_or_create(
|
||||
serial=validated_data['serial'],
|
||||
defaults={'serial': validated_data['serial']},
|
||||
)
|
||||
return device
|
||||
|
Loading…
Reference in New Issue
Block a user