Skip to content

Commit

Permalink
Uprava modelu, rozsireni views, nove formulare
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-dvorak committed Apr 8, 2024
1 parent 574ba8e commit db736a4
Show file tree
Hide file tree
Showing 11 changed files with 333 additions and 47 deletions.
29 changes: 23 additions & 6 deletions DOSPORTAL/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django import forms
from .models import Detector, Record, Profile, Organization
from .models import Detector, Record, Profile, Organization, DetectorCalib

from django import forms
from django.contrib.auth.models import User
Expand Down Expand Up @@ -83,13 +83,13 @@ def __init__(self,*args, user=None, **kwargs):

description = MarkdownxFormField(
label="Description",
help_text="Detailed description of the record; markdown supported.",
help_text="Detailed description of measurements. You can add additional information about measurement conditions, locations etc.. (Markdown supported)",
required=False,
)

record_type = forms.ChoiceField(
choices=Record.RECORD_TYPES
)
# record_type = forms.ChoiceField(
# choices=Record.RECORD_TYPES
# )

belongs = forms.ModelChoiceField(
queryset=Organization.objects.exclude(user_organizations__user=user),
Expand All @@ -101,11 +101,28 @@ def __init__(self,*args, user=None, **kwargs):
class Meta:
model = Record
exclude = ("time_end", "measurement", "log_original_filename", "metadata", "duration", "record_duration", "author", 'data_file',
"metadata_file", "created", "detector")
"metadata_file", "created", "detector", "time_of_interest_start", "time_of_interest_end", 'calib', 'record_type')


class DetectorEditForm(forms.ModelForm):
class Meta:
model = Detector
fields = ["name", "type", 'sn', "manufactured_date", "data", "owner", "access"]


class DetectorCalibForm(forms.ModelForm):
class Meta:
model = DetectorCalib
#fields = ['name', 'description', 'coef0', 'coef1', 'coef2']
exclude = ['created', 'author']


DetectorCalibFormSet = forms.inlineformset_factory(
Detector,
Detector.calib.through,
#fields= (calib',),
form=DetectorCalibForm,
can_delete=True,
extra=1
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 4.2.11 on 2024-04-07 10:48

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


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('DOSPORTAL', '0029_rename_time_of_interenst_end_record_time_of_interest_end_and_more'),
]

operations = [
migrations.RemoveField(
model_name='detectorcalib',
name='date',
),
migrations.AddField(
model_name='detectorcalib',
name='author',
field=models.ForeignKey(default=None, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='calibrations', to=settings.AUTH_USER_MODEL),
),
]
28 changes: 21 additions & 7 deletions DOSPORTAL/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,19 @@ class DetectorCalib(UUIDMixin):

created = models.DateTimeField(auto_now_add=True)

author = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.DO_NOTHING,
related_name="calibrations",
null=True,
default=None
)

description = models.TextField(
_("Description of calibration status")
)

date = models.DateTimeField(
)
created = models.DateTimeField(auto_now_add=True)

coef0 = models.FloatField(
_("Coefficient 0 (offset)"),
Expand All @@ -295,7 +302,7 @@ class DetectorCalib(UUIDMixin):
# )

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}"
return f"Calibration '{self.name}' ({self.coef0/1000:.2f}+x*{self.coef1/1000:.2f} KeV), {self.created}, {self.description}"



Expand All @@ -319,7 +326,7 @@ class Detector(UUIDMixin):
DetectorCalib,
blank=True,
#name=_("Detector calibration"),
#related_name="detectors",
related_name="detectors",
help_text=_("Detector calibration"),
#limit_choices_to=,
)
Expand Down Expand Up @@ -517,6 +524,7 @@ def user_directory_path_data(instance, extension='pk'):

log_file = models.FileField(
verbose_name=_("File log"),
help_text=_("Upload recorded data file form your detector"),
upload_to=user_directory_path,
blank=True
)
Expand Down Expand Up @@ -554,11 +562,12 @@ def user_directory_path_data(instance, extension='pk'):
time_tracked = models.BooleanField(
verbose_name = _("Is time tracked?"),
default = False,
help_text=_("When time is tracked, 'time_start' must be filled out")
help_text=_("Tick this box if the record is dependent on absolute time. When you need align record to real time.")
)

time_start = models.DateTimeField(
verbose_name = _("Measurement beginning time"),
help_text=("When 'time is tracked', you can set start time of the record beginning. "),
null=True,
blank=True,
default=datetime.datetime(2000, 1, 1, 0, 0, 0)
Expand Down Expand Up @@ -628,10 +637,15 @@ def user_directory_path_data(instance, extension='pk'):
on_delete=models.DO_NOTHING,
null=True,
related_name="records_owning",
help_text=_("Organization, which owns this record")
help_text=_("Organization, which owns this record. If you are the only owner, please leave this field empty.")
)

data_policy = models.CharField(max_length=2, choices= Organization.DATA_POLICY_CHOICES, default='PU')
data_policy = models.CharField(
max_length=2,
choices= Organization.DATA_POLICY_CHOICES,
default='PU',
help_text=_("Data policy of this record. Field can be overridden depending by setting of the organisation, that owns this record."),
)

author = models.ForeignKey(
settings.AUTH_USER_MODEL,
Expand Down
6 changes: 5 additions & 1 deletion DOSPORTAL/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,12 @@ def save_record(sender, instance, created = None, **kwargs):
sn = metadata['log_device_info']['DOS']['hw-sn']
print("Traying to find detector with SN", sn)
det = Detector.objects.get(sn=sn)
print(det)
print("Found detector", det)
instance.detector = det

instance.calib = det.calib.last()


except Exception as e:
print(e)

Expand Down
2 changes: 1 addition & 1 deletion DOSPORTAL/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
</div>
</nav>

<main class="container" style="margin-top:80pt;">
<main class="container" style="margin-top:80pt; margin-bottom: 100pt">
{% block content %}
{% endblock %}
</main>
Expand Down
15 changes: 15 additions & 0 deletions DOSPORTAL/templates/detectors/detectorCalib_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

{% extends "base.html" %}
{% load martortags %}

{% block content %}


Kalibrace

{{calib}}




{% endblock %}
8 changes: 6 additions & 2 deletions DOSPORTAL/templates/detectors/detectors_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
{% extends "base.html" %}

{% block content %}
{% load martortags %}
{% load crispy_forms_tags %}

<nav aria-label="breadcrumb">
<ol class="breadcrumb">
Expand Down Expand Up @@ -86,6 +88,8 @@ <h3>Servisní záznamy: </h3>
{%endif%}




<div class="card mb-5">
<div class="card-header">
<a class="small" type="button" data-bs-toggle="collapse" data-bs-target="#add_record_collapse" aria-expanded="false" aria-controls="collapseExample">
Expand All @@ -96,8 +100,8 @@ <h3>Servisní záznamy: </h3>
<div class="card-body collapse py-2" id="add_record_collapse">
<form method="post" action="new_logbook_record">
{% csrf_token %}
{{ DetectorLogblogForm.as_p }}
<button type="submit">Submit</button>
{{ DetectorLogblogForm | crispy }}
<button type="submit" class="btn btn-primary">Submit</button>
</form>

</div>
Expand Down
Loading

0 comments on commit db736a4

Please sign in to comment.