1
0
mirror of https://github.com/daniviga/bite.git synced 2024-11-23 05:16:13 +01:00

Manage serial numbers on POST (#7)

* Update subscription if exists

* POST with serial #
This commit is contained in:
Daniele Viganò 2020-06-03 18:57:53 +02:00 committed by GitHub
parent e4a3684951
commit 4cebe6a8f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 34 additions and 22 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
*.swp
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]

View File

@ -80,7 +80,7 @@ void setup(void) {
Serial.println("DEBUG: clock updated via NTP.");
#endif
json["device"] = 1; // FIXME
json["device"] = serial;
// payload["id"] = serverName;
}

View File

@ -1,10 +1,16 @@
from django.db import models
from django.conf import settings
from django.core.exceptions import ValidationError
def device_validation(value):
published_devices = WhiteList.objects.filter(serial=value,
is_published=True)
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
)
if not published_devices:
raise ValidationError("Device is not published")

View File

@ -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

View File

@ -125,3 +125,5 @@ USE_TZ = True
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = '/static/'
SKIP_WHITELIST = True

View File

@ -1,20 +1,14 @@
from rest_framework import serializers
from api.serializers import DeviceSerializer
from api.models import Device
from telemetry.models import Telemetry
class TelemetrySerializer(serializers.ModelSerializer):
# device = DeviceSerializer(read_only=True)
def validate(self, data):
return data
device = serializers.SlugRelatedField(
slug_field='serial',
queryset=Device.objects.all()
)
class Meta:
model = Telemetry
fields = ('time', 'device', 'clock', 'payload',)
# class WhiteListSerializer(serializers.ModelSerializer):
# class Meta:
# model = Device
# fields = ('serial', 'creation_time', 'updated_time',)

View File

@ -1,4 +1,5 @@
{"device": 1,
{"device": "abcde1234",
"clock": 1591194712,
"payload": {
"id": "sensor.server.domain",
"light": 434,