diff --git a/DOSPORTAL/admin.py b/DOSPORTAL/admin.py index 1e53495..25b7707 100644 --- a/DOSPORTAL/admin.py +++ b/DOSPORTAL/admin.py @@ -6,6 +6,9 @@ from import_export.admin import ImportExportModelAdmin +from django_q import models as q_models +from django_q import admin as q_admin + class AirportsResource(resources.ModelResource): class Meta: model = Airports @@ -43,8 +46,24 @@ def user_count(self, obj): admin.site.register(measurement) admin.site.register(Organization, OrganizationAdmin) #admin.site.register(OrganizationUser) -admin.site.register(Record) -admin.site.register(Detector) + + +class RecordAdmin(admin.ModelAdmin): + def get_form(self, request, obj=None, **kwargs): + if obj: + form = obj.calibration_select_form() + else: + form = super().get_form(request, obj, **kwargs) + return form + +admin.site.register(Record, RecordAdmin) + + +class DetectorAdmin(admin.ModelAdmin): + filter_horizontal = ('calib',) + pass + +admin.site.register(Detector, DetectorAdmin) admin.site.register(DetectorLogbook) admin.site.register(DetectorType) admin.site.register(DetectorCalib) @@ -59,8 +78,6 @@ def user_count(self, obj): admin.site.register(SpectrumData) -from django_q import models as q_models -from django_q import admin as q_admin admin.site.unregister([q_models.Failure]) @admin.register(q_models.Failure) diff --git a/DOSPORTAL/forms.py b/DOSPORTAL/forms.py index cd80b85..e2b9ae7 100644 --- a/DOSPORTAL/forms.py +++ b/DOSPORTAL/forms.py @@ -107,5 +107,5 @@ class Meta: class DetectorEditForm(forms.ModelForm): class Meta: model = Detector - fields = ["name", "type", 'sn', "calib", "manufactured_date", "data", "owner", "access"] + fields = ["name", "type", 'sn', "manufactured_date", "data", "owner", "access"] diff --git a/DOSPORTAL/migrations/0018_record_calib.py b/DOSPORTAL/migrations/0018_record_calib.py new file mode 100644 index 0000000..2098be9 --- /dev/null +++ b/DOSPORTAL/migrations/0018_record_calib.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2.11 on 2024-04-06 20:21 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('DOSPORTAL', '0017_record_metadata_file'), + ] + + operations = [ + migrations.AddField( + model_name='record', + name='calib', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='records', to='DOSPORTAL.detectorcalib'), + ), + ] diff --git a/DOSPORTAL/migrations/0019_remove_detector_calib.py b/DOSPORTAL/migrations/0019_remove_detector_calib.py new file mode 100644 index 0000000..caf4724 --- /dev/null +++ b/DOSPORTAL/migrations/0019_remove_detector_calib.py @@ -0,0 +1,17 @@ +# Generated by Django 4.2.11 on 2024-04-06 20:24 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('DOSPORTAL', '0018_record_calib'), + ] + + operations = [ + migrations.RemoveField( + model_name='detector', + name='calib', + ), + ] diff --git a/DOSPORTAL/migrations/0020_detector_detector_calibration.py b/DOSPORTAL/migrations/0020_detector_detector_calibration.py new file mode 100644 index 0000000..6a3976d --- /dev/null +++ b/DOSPORTAL/migrations/0020_detector_detector_calibration.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.11 on 2024-04-06 20:24 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('DOSPORTAL', '0019_remove_detector_calib'), + ] + + operations = [ + migrations.AddField( + model_name='detector', + name='Detector calibration', + field=models.ManyToManyField(blank=True, help_text='Detector calibration', related_name='detectors', to='DOSPORTAL.detectorcalib'), + ), + ] diff --git a/DOSPORTAL/migrations/0021_remove_detectorcalib_cabib.py b/DOSPORTAL/migrations/0021_remove_detectorcalib_cabib.py new file mode 100644 index 0000000..5cca376 --- /dev/null +++ b/DOSPORTAL/migrations/0021_remove_detectorcalib_cabib.py @@ -0,0 +1,17 @@ +# Generated by Django 4.2.11 on 2024-04-06 20:28 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('DOSPORTAL', '0020_detector_detector_calibration'), + ] + + operations = [ + migrations.RemoveField( + model_name='detectorcalib', + name='cabib', + ), + ] diff --git a/DOSPORTAL/migrations/0022_remove_detector_detector_calibration_and_more.py b/DOSPORTAL/migrations/0022_remove_detector_detector_calibration_and_more.py new file mode 100644 index 0000000..b50d115 --- /dev/null +++ b/DOSPORTAL/migrations/0022_remove_detector_detector_calibration_and_more.py @@ -0,0 +1,22 @@ +# Generated by Django 4.2.11 on 2024-04-06 20:49 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('DOSPORTAL', '0021_remove_detectorcalib_cabib'), + ] + + operations = [ + migrations.RemoveField( + model_name='detector', + name='Detector calibration', + ), + migrations.AddField( + model_name='detector', + name='calibration', + field=models.ManyToManyField(blank=True, help_text='Detector calibration', to='DOSPORTAL.detectorcalib'), + ), + ] diff --git a/DOSPORTAL/migrations/0023_rename_calibration_detector_calib.py b/DOSPORTAL/migrations/0023_rename_calibration_detector_calib.py new file mode 100644 index 0000000..99377e4 --- /dev/null +++ b/DOSPORTAL/migrations/0023_rename_calibration_detector_calib.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.11 on 2024-04-06 20:49 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('DOSPORTAL', '0022_remove_detector_detector_calibration_and_more'), + ] + + operations = [ + migrations.RenameField( + model_name='detector', + old_name='calibration', + new_name='calib', + ), + ] diff --git a/DOSPORTAL/migrations/0024_remove_detector_calib_detectorcalib_created_and_more.py b/DOSPORTAL/migrations/0024_remove_detector_calib_detectorcalib_created_and_more.py new file mode 100644 index 0000000..95507b7 --- /dev/null +++ b/DOSPORTAL/migrations/0024_remove_detector_calib_detectorcalib_created_and_more.py @@ -0,0 +1,36 @@ +# Generated by Django 4.2.11 on 2024-04-06 22:14 + +from django.db import migrations, models +import django.db.models.deletion +import django.utils.timezone + + +class Migration(migrations.Migration): + + dependencies = [ + ('DOSPORTAL', '0023_rename_calibration_detector_calib'), + ] + + operations = [ + migrations.RemoveField( + model_name='detector', + name='calib', + ), + migrations.AddField( + model_name='detectorcalib', + name='created', + field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now), + preserve_default=False, + ), + migrations.CreateModel( + name='DetectorToCalibration', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('calib', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='DOSPORTAL.detectorcalib')), + ('detector', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='DOSPORTAL.detector')), + ], + options={ + 'ordering': ['calib__created'], + }, + ), + ] diff --git a/DOSPORTAL/migrations/0025_detector_calib.py b/DOSPORTAL/migrations/0025_detector_calib.py new file mode 100644 index 0000000..fb22337 --- /dev/null +++ b/DOSPORTAL/migrations/0025_detector_calib.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.11 on 2024-04-06 22:14 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('DOSPORTAL', '0024_remove_detector_calib_detectorcalib_created_and_more'), + ] + + operations = [ + migrations.AddField( + model_name='detector', + name='calib', + field=models.ManyToManyField(blank=True, help_text='Detector calibration', through='DOSPORTAL.DetectorToCalibration', to='DOSPORTAL.detectorcalib'), + ), + ] diff --git a/DOSPORTAL/migrations/0026_remove_detector_calib_delete_detectortocalibration.py b/DOSPORTAL/migrations/0026_remove_detector_calib_delete_detectortocalibration.py new file mode 100644 index 0000000..60d27f0 --- /dev/null +++ b/DOSPORTAL/migrations/0026_remove_detector_calib_delete_detectortocalibration.py @@ -0,0 +1,20 @@ +# Generated by Django 4.2.11 on 2024-04-06 22:26 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('DOSPORTAL', '0025_detector_calib'), + ] + + operations = [ + migrations.RemoveField( + model_name='detector', + name='calib', + ), + migrations.DeleteModel( + name='DetectorToCalibration', + ), + ] diff --git a/DOSPORTAL/migrations/0027_detector_calib.py b/DOSPORTAL/migrations/0027_detector_calib.py new file mode 100644 index 0000000..c41697d --- /dev/null +++ b/DOSPORTAL/migrations/0027_detector_calib.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.11 on 2024-04-06 22:26 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('DOSPORTAL', '0026_remove_detector_calib_delete_detectortocalibration'), + ] + + operations = [ + migrations.AddField( + model_name='detector', + name='calib', + field=models.ManyToManyField(blank=True, help_text='Detector calibration', to='DOSPORTAL.detectorcalib'), + ), + ] diff --git a/DOSPORTAL/migrations/0028_record_time_of_interenst_end_and_more.py b/DOSPORTAL/migrations/0028_record_time_of_interenst_end_and_more.py new file mode 100644 index 0000000..fcc2802 --- /dev/null +++ b/DOSPORTAL/migrations/0028_record_time_of_interenst_end_and_more.py @@ -0,0 +1,23 @@ +# Generated by Django 4.2.11 on 2024-04-06 23:38 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('DOSPORTAL', '0027_detector_calib'), + ] + + operations = [ + migrations.AddField( + model_name='record', + name='time_of_interenst_end', + field=models.DurationField(blank=True, default=None, null=True, verbose_name='Time of interest end'), + ), + migrations.AddField( + model_name='record', + name='time_of_interenst_start', + field=models.DurationField(blank=True, default=None, null=True, verbose_name='Time of interest start'), + ), + ] diff --git a/DOSPORTAL/migrations/0029_rename_time_of_interenst_end_record_time_of_interest_end_and_more.py b/DOSPORTAL/migrations/0029_rename_time_of_interenst_end_record_time_of_interest_end_and_more.py new file mode 100644 index 0000000..2c825dc --- /dev/null +++ b/DOSPORTAL/migrations/0029_rename_time_of_interenst_end_record_time_of_interest_end_and_more.py @@ -0,0 +1,23 @@ +# Generated by Django 4.2.11 on 2024-04-06 23:50 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('DOSPORTAL', '0028_record_time_of_interenst_end_and_more'), + ] + + operations = [ + migrations.RenameField( + model_name='record', + old_name='time_of_interenst_end', + new_name='time_of_interest_end', + ), + migrations.RenameField( + model_name='record', + old_name='time_of_interenst_start', + new_name='time_of_interest_start', + ), + ] diff --git a/DOSPORTAL/models.py b/DOSPORTAL/models.py index 42978cc..28ce26e 100644 --- a/DOSPORTAL/models.py +++ b/DOSPORTAL/models.py @@ -14,6 +14,10 @@ from django.contrib.contenttypes.models import ContentType from django.db import models from django.contrib.gis.db import models as geomodels +from django.contrib.gis.geos import Point +from django.contrib.gis.measure import Distance +from django import forms + from markdownx.models import MarkdownxField import json from markdownx.utils import markdownify @@ -261,6 +265,8 @@ class DetectorCalib(UUIDMixin): _("Calibration name") ) + created = models.DateTimeField(auto_now_add=True) + description = models.TextField( _("Description of calibration status") ) @@ -288,13 +294,10 @@ class DetectorCalib(UUIDMixin): # on_delete=models.CASCADE, # ) + def __str__(self) -> str: + return f"Calibration '{self.name}' ({self.coef0/1000:.2f}, {self.coef1/1000:.2f}, {self.coef2/1000:.2f} KeV), {self.date}, {self.description}" - cabib = models.JSONField( - _("Slozky kalibrace, json"), - null=True - ) - - + class Detector(UUIDMixin): @@ -312,12 +315,13 @@ class Detector(UUIDMixin): on_delete=models.CASCADE ) - calib = models.ForeignKey( + calib = models.ManyToManyField( DetectorCalib, - on_delete=models.CASCADE, - null=True, blank=True, - related_name="detectors" + #name=_("Detector calibration"), + #related_name="detectors", + help_text=_("Detector calibration"), + #limit_choices_to=, ) manufactured_date = models.DateField( @@ -356,8 +360,8 @@ def __str__(self) -> str: @property def formatted_label(self): - return f""" {self.type.name} - {self.name} ({self.sn})""" + return f""" {self.type.name} + {self.name} ({self.sn})""" class DetectorLogbook(UUIDMixin): @@ -560,6 +564,20 @@ def user_directory_path_data(instance, extension='pk'): default=datetime.datetime(2000, 1, 1, 0, 0, 0) ) + time_of_interest_start = models.DurationField( + verbose_name = _("Time of interest start"), + null=True, + blank=True, + default=None + ) + + time_of_interest_end = models.DurationField( + verbose_name = _("Time of interest end"), + null=True, + blank=True, + default=None + ) + created = models.DateTimeField( verbose_name = _("Time of creation"), null=False, @@ -597,6 +615,14 @@ def user_directory_path_data(instance, extension='pk'): ) + calib = models.ForeignKey( + DetectorCalib, + on_delete=models.CASCADE, + null=True, + blank=True, + related_name='records' + ) + belongs = models.ForeignKey( Organization, on_delete=models.DO_NOTHING, @@ -624,6 +650,22 @@ def __str__(self) -> str: # return "Record ({}, {})".format(get_enum_dsc(self.RECORD_TYPES, self.record_type), self.time_start.strftime("%Y-%m-%d_%H:%M")) + def calibration_select_form(self): + class selectCalibForm(forms.ModelForm): + class Meta: + model = Record + #fields = ['calib'] + exclude = [] + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + detector = self.instance.detector + if detector: + self.fields['calib'].queryset = detector.calib.all() + else: + self.fields['calib'].queryset = DetectorCalib.objects.none() + return selectCalibForm + @property def formatted_markdown(self): return markdownify(self.description) @@ -702,15 +744,6 @@ class SpectrumData(UUIDMixin): blank=True, ) - - # metadata = HStoreField( - # _("Metadata"), - # help_text=_("Additional metadata for the spectrum data"), - # null=True, - # blank=True, - # default=dict() - # ) - location = models.ForeignKey( TrajectoryPoint, on_delete=models.CASCADE, @@ -723,7 +756,6 @@ class SpectrumData(UUIDMixin): help_text=_("Time difference from the start of the measurement"), null=True ) - # Add any other fields that you think might be useful def __str__(self) -> str: return f"Spectrum data {self.record.id}" diff --git a/DOSPORTAL/templates/detectors/detectors_detail.html b/DOSPORTAL/templates/detectors/detectors_detail.html index 5039f97..6721bca 100644 --- a/DOSPORTAL/templates/detectors/detectors_detail.html +++ b/DOSPORTAL/templates/detectors/detectors_detail.html @@ -29,6 +29,12 @@