Skip to content

Commit

Permalink
django: adapt to change database model
Browse files Browse the repository at this point in the history
Signed-off-by: Jonas Remmert <jremmert@gmx.net>
  • Loading branch information
jonas-rem authored and Kappuccino111 committed Jun 4, 2024
1 parent 5cff09d commit 67a1bb0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
16 changes: 8 additions & 8 deletions server/django/sensordata/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

@admin.register(Device)
class DeviceAdmin(admin.ModelAdmin):
list_display = ('device_id', 'name')
search_fields = ('device_id', 'name')
readonly_fields = ('device_id', 'name')
list_display = ('endpoint',)
search_fields = ('endpoint',)
readonly_fields = ('endpoint',)

def get_model_perms(self, request):
return {
Expand Down Expand Up @@ -51,14 +51,14 @@ def get_model_perms(self, request):
@admin.register(Resource)
class ResourceAdmin(admin.ModelAdmin):
list_display = ('device', 'resource_type', 'timestamp')
search_fields = ('device__device_id', 'resource_type__name')
search_fields = ('device__endpoint', 'resource_type__name')
list_filter = ('device', 'resource_type', 'timestamp')


@admin.register(Event)
class EventAdmin(admin.ModelAdmin):
list_display = ('device', 'event_type', 'start_time', 'end_time')
search_fields = ('device__device_id', 'event_type')
search_fields = ('device__endpoint', 'event_type')
list_filter = ('device', 'event_type')


Expand All @@ -73,7 +73,7 @@ class EventResourceAdmin(admin.ModelAdmin):
class DeviceOperationAdmin(admin.ModelAdmin):
list_display = ('resource', 'operation_type', 'status', 'timestamp_sent',
'retransmit_counter', 'last_attempt')
search_fields = ('resource__device__device_id', 'operation_type', 'status')
search_fields = ('resource__device__endpoint', 'operation_type', 'status')
list_filter = ('resource__resource_type', 'operation_type', 'status', 'timestamp_sent')
readonly_fields = ('status', 'timestamp_sent', 'retransmit_counter',
'last_attempt', 'operation_type')
Expand Down Expand Up @@ -111,7 +111,7 @@ def save_model(self, request, obj, form, change):

# Construct the URL based on the device, object_id, and resource_id
url = (
f'{LESHAN_URI}/clients/{device.device_id}/'
f'{LESHAN_URI}/clients/{device.endpoint}/'
f'{resource_type.object_id}/0/{resource_type.resource_id}'
)
params = {'timeout': 5, 'format': 'CBOR'}
Expand All @@ -127,7 +127,7 @@ def save_model(self, request, obj, form, change):
response = requests.put(url, params=params, headers=headers, json=data)

if response.status_code == 200:
log.debug(f'Data sent to device {device.device_id} successfully')
log.debug(f'Data sent to device {device.endpoint} successfully')
log.debug(f'Response: {response.status_code} - {response.json()}')
obj.status = 'completed'
else:
Expand Down
4 changes: 2 additions & 2 deletions server/django/sensordata/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ def create(self, validated_data):
res = validated_data['res']
val = validated_data['val']

# ep maps to Device.device_id
device, _ = Device.objects.get_or_create(device_id=ep, defaults={'name': ep})
# ep maps to Device.endpoint
device, _ = Device.objects.get_or_create(endpoint=ep)

# Check if value is an object with instances
if val['kind'] == 'obj' and 'instances' in val:
Expand Down
2 changes: 1 addition & 1 deletion server/django/sensordata/tests/test_restapi_single.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_create_sensor_data_from_json_payloads(self):
val = pl['val']

# Verify the Device was created
device = Device.objects.get(device_id=ep)
device = Device.objects.get(endpoint=ep)
self.assertIsNotNone(device)
self.assertEqual(device.name, ep)

Expand Down

0 comments on commit 67a1bb0

Please sign in to comment.