Skip to content

Commit

Permalink
django: clean up model permissions and timestamps
Browse files Browse the repository at this point in the history
Signed-off-by: Jonas Remmert <jremmert@gmx.net>
  • Loading branch information
jonas-rem committed Jul 4, 2024
1 parent 47ec2af commit eab0ba8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
7 changes: 3 additions & 4 deletions server/django/sensordata/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,10 @@ class EventResourceInline(admin.TabularInline):

@admin.register(Resource)
class ResourceAdmin(admin.ModelAdmin):
list_display = ('endpoint', 'resource_type', 'timestamp')
list_display = ('endpoint', 'resource_type', 'timestamp_created')
search_fields = ('endpoint__endpoint', 'resource_type__name')
list_filter = ('endpoint__endpoint', 'resource_type', 'timestamp')
readonly_fields = ('endpoint', 'resource_type', 'timestamp', 'int_value',
'float_value', 'str_value')
list_filter = ('endpoint__endpoint', 'resource_type', 'timestamp_created')
readonly_fields = ('timestamp_created',)

@admin.register(Event)
class EventAdmin(admin.ModelAdmin):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by Django 5.0.6 on 2024-06-14 13:46

import django.utils.timezone
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("sensordata", "0007_rename_end_time_event_time_remove_event_start_time"),
]

operations = [
migrations.AlterUniqueTogether(
name="resource",
unique_together=set(),
),
migrations.AddField(
model_name="resource",
name="timestamp_created",
field=models.DateTimeField(
auto_now_add=True, default=django.utils.timezone.now
),
preserve_default=False,
),
migrations.AlterField(
model_name="event",
name="time",
field=models.DateTimeField(auto_now_add=True),
),
migrations.RemoveField(
model_name="resource",
name="timestamp",
),
]
11 changes: 4 additions & 7 deletions server/django/sensordata/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,10 @@ class Resource(models.Model):
int_value = models.IntegerField(null=True, blank=True)
float_value = models.FloatField(null=True, blank=True)
str_value = models.CharField(max_length=512, null=True, blank=True)
timestamp = models.DateTimeField()

class Meta:
unique_together = ('endpoint', 'resource_type', 'timestamp')
timestamp_created = models.DateTimeField(auto_now_add=True, blank=True)

def __str__(self):
return f"{self.endpoint} - {self.resource_type} - {self.timestamp}"
return f"{self.endpoint} - {self.resource_type} - {self.timestamp_created}"

# Gets the correct value field, based on the linked ResourceType
def get_value(self):
Expand All @@ -67,7 +64,7 @@ class Event(models.Model):
"""
endpoint = models.ForeignKey(Endpoint, on_delete=models.PROTECT)
event_type = models.CharField(max_length=100)
time = models.DateTimeField()
time = models.DateTimeField(auto_now_add=True, blank=True)

def __str__(self):
return f"{self.endpoint} - {self.event_type} - {self.time}"
Expand Down Expand Up @@ -97,7 +94,7 @@ class Status(models.TextChoices):
default=Status.QUEUED,
)
transmit_counter = models.IntegerField(default=0)
timestamp_created = models.DateTimeField(auto_now_add=True)
timestamp_created = models.DateTimeField(auto_now_add=True, blank=True)
last_attempt = models.DateTimeField(auto_now_add=False, null=True)

class Firmware(models.Model):
Expand Down
3 changes: 0 additions & 3 deletions server/django/sensordata/serializers/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from rest_framework import serializers
from ..models import ResourceType, Resource, Event, EventResource
from django.utils import timezone
from ..tasks import process_pending_operations
import logging

Expand Down Expand Up @@ -44,7 +43,6 @@ def create_event(self, endpoint, event_type):
event_data = {
'endpoint': endpoint,
'event_type': event_type,
'time': timezone.now()
}
self.event = Event.objects.create(**event_data)

Expand Down Expand Up @@ -81,7 +79,6 @@ def handle_resource(self, endpoint, obj_id, res):
resource_data = {
'endpoint': endpoint,
'resource_type': resource_type,
'timestamp': timezone.now(),
data_type: res['value']
}

Expand Down

0 comments on commit eab0ba8

Please sign in to comment.