Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions apps/device/constants.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from dataclasses import dataclass
from datetime import datetime
from enum import Enum

from django.db import models

Expand All @@ -22,3 +23,8 @@ class LocationPoint:
class DeviceStatus(models.TextChoices):
ACTIVE = "active"
IN_INVENTORY = "in_inventory"


class DeviceRelation(str, Enum):
LORAWAN = "lorawan_device"
API = "api_device"
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
# Generated by Codex on 2026-08-24

import uuid

import django.db.models.deletion
from django.db import migrations, models


def copy_device_fields_forward(apps, schema_editor):
Device = apps.get_model("device", "Device")
LorawanDevice = apps.get_model("device", "LorawanDevice")

for lorawan_device in LorawanDevice.objects.values(
"id",
"device_id",
"claim_code",
"device__network_server_id",
).iterator():
network_server_id = lorawan_device["device__network_server_id"]
if network_server_id:
LorawanDevice.objects.filter(id=lorawan_device["id"]).update(
network_server_id=network_server_id
)

claim_code = lorawan_device["claim_code"]
if claim_code:
Device.objects.filter(id=lorawan_device["device_id"]).update(
claim_code=claim_code
)


def copy_device_fields_backward(apps, schema_editor):
Device = apps.get_model("device", "Device")
LorawanDevice = apps.get_model("device", "LorawanDevice")

for lorawan_device in LorawanDevice.objects.values(
"id",
"device_id",
"network_server_id",
"device__claim_code",
).iterator():
network_server_id = lorawan_device["network_server_id"]
if network_server_id:
Device.objects.filter(id=lorawan_device["device_id"]).update(
network_server_id=network_server_id
)

claim_code = lorawan_device["device__claim_code"]
if claim_code:
LorawanDevice.objects.filter(id=lorawan_device["id"]).update(
claim_code=claim_code
)


class Migration(migrations.Migration):
dependencies = [
("device", "0020_move_location_to_device"),
("network_server", "0004_alter_networkserver_logo"),
]

operations = [
migrations.AddField(
model_name="device",
name="claim_code",
field=models.CharField(
blank=True,
max_length=100,
null=True,
unique=True,
),
),
migrations.AddField(
model_name="lorawandevice",
name="network_server",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="lorawan_devices",
to="network_server.networkserver",
),
),
migrations.CreateModel(
name="APIDevice",
fields=[
(
"created_at",
models.DateTimeField(auto_now_add=True),
),
(
"updated_at",
models.DateTimeField(auto_now=True),
),
(
"id",
models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
unique=True,
),
),
(
"serial_number",
models.CharField(max_length=100, unique=True),
),
(
"device",
models.OneToOneField(
on_delete=django.db.models.deletion.CASCADE,
related_name="api_device",
to="device.device",
),
),
],
options={
"abstract": False,
},
),
migrations.RunPython(copy_device_fields_forward, copy_device_fields_backward),
migrations.AlterField(
model_name="device",
name="id",
field=models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
unique=True,
),
),
migrations.AlterField(
model_name="lorawandevice",
name="id",
field=models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
unique=True,
),
),
migrations.AlterField(
model_name="spacedevice",
name="id",
field=models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
unique=True,
),
),
migrations.RemoveField(
model_name="device",
name="network_server",
),
migrations.RemoveField(
model_name="lorawandevice",
name="claim_code",
),
]
26 changes: 15 additions & 11 deletions apps/device/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,8 @@


class Device(BaseModel):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
network_server = models.ForeignKey(
NetworkServer,
related_name="devices",
on_delete=models.CASCADE,
blank=True,
null=True,
)
device_model = models.UUIDField(null=True, blank=True)
claim_code = models.CharField(max_length=100, null=True, blank=True, unique=True)
status = models.CharField(
choices=DeviceStatus.choices, default=DeviceStatus.IN_INVENTORY
)
Expand All @@ -35,18 +28,29 @@ class Device(BaseModel):


class LorawanDevice(BaseModel):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
device = models.OneToOneField(
Device, related_name="lorawan_device", on_delete=models.CASCADE
)
dev_eui = models.CharField(max_length=16, unique=True)
join_eui = models.CharField(max_length=16, null=True, blank=True)
app_key = models.CharField(max_length=32, null=True, blank=True)
claim_code = models.CharField(max_length=100, null=True, blank=True, unique=True)
network_server = models.ForeignKey(
NetworkServer,
related_name="lorawan_devices",
on_delete=models.CASCADE,
blank=True,
null=True,
)


class APIDevice(BaseModel):
device = models.OneToOneField(
Device, related_name="api_device", on_delete=models.CASCADE
)
serial_number = models.CharField(max_length=100, unique=True)


class SpaceDevice(BaseModel):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=255)
description = models.TextField(null=True, blank=True)
space = models.ForeignKey(
Expand Down
Loading
Loading