Skip to content

Commit

Permalink
Added some fields to Device models
Browse files Browse the repository at this point in the history
Automatic creation of hash based on device serial number
  • Loading branch information
frasanz committed Oct 3, 2024
1 parent eb4304a commit c663f0c
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 4 deletions.
18 changes: 18 additions & 0 deletions devices/migrations/0002_alter_device_hash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.16 on 2024-10-03 19:32

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('devices', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='device',
name='hash',
field=models.CharField(blank=True, max_length=64, unique=True),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.2.16 on 2024-10-03 19:36

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('devices', '0002_alter_device_hash'),
]

operations = [
migrations.AddField(
model_name='devicemodel',
name='description',
field=models.TextField(blank=True, null=True),
),
migrations.AddField(
model_name='devicemodel',
name='technology',
field=models.CharField(blank=True, max_length=100, null=True),
),
]
18 changes: 18 additions & 0 deletions devices/migrations/0004_devicemodel_picture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.16 on 2024-10-03 19:36

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('devices', '0003_devicemodel_description_devicemodel_technology'),
]

operations = [
migrations.AddField(
model_name='devicemodel',
name='picture',
field=models.ImageField(blank=True, null=True, upload_to='device_pictures/'),
),
]
12 changes: 11 additions & 1 deletion devices/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.db import models
from django.contrib.auth.models import User # Assuming you're using Django's User model for owners
import hashlib

class DeviceModel(models.Model):
"""
Expand All @@ -8,6 +9,9 @@ class DeviceModel(models.Model):
name = models.CharField(max_length=100) # Name of the device model
manufacturer = models.CharField(max_length=100) # Manufacturer of the device
version = models.CharField(max_length=50, blank=True, null=True) # Optional version of the device model
technology = models.CharField(max_length=100, blank=True, null=True) # Optional technology used in the device
description = models.TextField(blank=True, null=True) # Optional description of the device
picture = models.ImageField(upload_to='device_pictures/', blank=True, null=True) # Optional picture of the device
max_radiation_range = models.FloatField(help_text="Maximum radiation range the device can measure (in appropriate units)")

def __str__(self):
Expand All @@ -19,12 +23,18 @@ class Device(models.Model):
"""
device_model = models.ForeignKey(DeviceModel, on_delete=models.CASCADE) # Link to the DeviceModel
serial_number = models.CharField(max_length=100, unique=True) # Unique identifier for the device (serial number)
hash = models.CharField(max_length=64, unique=True) # Unique hash for the device (for identification)
hash = models.CharField(max_length=64, unique=True, blank=True) # Unique hash for the device (for identification)
owner = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True) # Owner of the device
purchase_date = models.DateField(blank=True, null=True) # Optional field for when the device was purchased
calibration_date = models.DateField(blank=True, null=True) # Date of the last calibration
is_active = models.BooleanField(default=True) # Indicates if the device is still in use

# To automatically generate a hash for the device
def save(self, *args, **kwargs):
# Generate a unique hash based on the device serial number
self.hash = hashlib.md5(self.serial_number.encode('utf-8')).hexdigest()
super(Device, self).save(*args, **kwargs)

def __str__(self):
return f"Device {self.serial_number} ({self.device_model.name})"

Expand Down
2 changes: 0 additions & 2 deletions measures/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@

# Register your models here.
admin.site.register(Measurement)


10 changes: 9 additions & 1 deletion measures/apps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@

from django.apps import AppConfig
"""
This module defines the configuration for the 'measures' application.
Classes:
MeasuresConfig(AppConfig): Configuration class for the 'measures' app.
- default_auto_field (str): Specifies the type of auto field to use for primary keys.
- name (str): The name of the application.
"""

class MeasureConfig(AppConfig):
class MeasuresConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'measures'
17 changes: 17 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
asgiref==3.8.1
certifi==2024.8.30
cffi==1.17.1
charset-normalizer==3.3.2
cryptography==43.0.1
Django==4.2.16
django-allauth==65.0.2
djangorestframework==3.15.2
idna==3.10
psycopg2==2.9.9
pycparser==2.22
PyJWT==2.9.0
python-decouple==3.8
requests==2.32.3
sqlparse==0.5.1
typing_extensions==4.12.2
urllib3==2.2.3

0 comments on commit c663f0c

Please sign in to comment.