mirror of
https://github.com/daniviga/bite.git
synced 2024-11-22 21:16:12 +01:00
Manage serial numbers on POST (#7)
* Update subscription if exists * POST with serial #
This commit is contained in:
parent
e4a3684951
commit
4cebe6a8f5
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,3 +1,5 @@
|
|||
*.swp
|
||||
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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")
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -125,3 +125,5 @@ USE_TZ = True
|
|||
# https://docs.djangoproject.com/en/3.0/howto/static-files/
|
||||
|
||||
STATIC_URL = '/static/'
|
||||
|
||||
SKIP_WHITELIST = True
|
||||
|
|
|
@ -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',)
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
{"device": 1,
|
||||
{"device": "abcde1234",
|
||||
"clock": 1591194712,
|
||||
"payload": {
|
||||
"id": "sensor.server.domain",
|
||||
"light": 434,
|
||||
|
|
Loading…
Reference in New Issue
Block a user