From bac7d9ac9d4dfff5894835c3cb726d8d0938d21b Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Wed, 26 Jul 2023 16:37:59 -0400 Subject: [PATCH] files, parsing, updates --- benchtopdevices/sheets/admin.py | 15 ++++- benchtopdevices/sheets/forms.py | 42 +++++--------- .../sheets/migrations/0001_initial.py | 30 ++++++++++ .../sheets/migrations/0002_sheet.py | 56 +++++++++++++++++++ ...t_as_found_alter_sheet_as_left_and_more.py | 36 ++++++++++++ benchtopdevices/sheets/models.py | 31 +++++++++- benchtopdevices/sheets/parsers/hardware.py | 2 +- benchtopdevices/sheets/views.py | 21 +++++-- 8 files changed, 198 insertions(+), 35 deletions(-) create mode 100644 benchtopdevices/sheets/migrations/0001_initial.py create mode 100644 benchtopdevices/sheets/migrations/0002_sheet.py create mode 100644 benchtopdevices/sheets/migrations/0003_alter_sheet_as_found_alter_sheet_as_left_and_more.py diff --git a/benchtopdevices/sheets/admin.py b/benchtopdevices/sheets/admin.py index 8c38f3f..3084a27 100644 --- a/benchtopdevices/sheets/admin.py +++ b/benchtopdevices/sheets/admin.py @@ -1,3 +1,16 @@ from django.contrib import admin +from sheets.models import ( + Configuration, + Sheet +) -# Register your models here. +class ConfigurationAdmin(admin.ModelAdmin): + pass + +admin.site.register(Configuration, ConfigurationAdmin) + + +class SheetAdmin(admin.ModelAdmin): + pass + +admin.site.register(Sheet, SheetAdmin) diff --git a/benchtopdevices/sheets/forms.py b/benchtopdevices/sheets/forms.py index 09abf08..313f841 100644 --- a/benchtopdevices/sheets/forms.py +++ b/benchtopdevices/sheets/forms.py @@ -3,35 +3,22 @@ from django.forms.widgets import ClearableFileInput from django.db import models from django.utils.translation import gettext_lazy as _ - -class UploadFileForm(forms.Form): - instrument = forms.CharField() - customer_name = forms.CharField() - customer_address = forms.CharField() - control_number = forms.CharField() - serial_number = forms.CharField() - accuracy = forms.FloatField(widget=forms.NumberInput(attrs={'step': 0.01})) - barometric_pressure = forms.FloatField(widget=forms.NumberInput(attrs={'step': 0.01, 'max': 1100, 'min': 800})) - temperature = forms.FloatField(widget=forms.NumberInput(attrs={'step': 0.01, 'max': 1000.0, 'min': -459.67})) - humidity = forms.FloatField(widget=forms.NumberInput(attrs={'step': 0.01, 'max': 100.0, 'min': 0.0})) +from sheets.models import Configuration, Sheet - CHOICES = [ - ('TV', _('Transducer Verify')), - ('HC', _('Hardware Calibration')) - ] - - report_type = forms.ChoiceField( - widget=forms.RadioSelect, - choices=CHOICES, - ) - - as_found = forms.FileField(required=False, widget=ClearableFileInput(attrs={'placeholder': ...})) - as_left = forms.FileField(required=False, widget=ClearableFileInput(attrs={'placeholder': ...})) - both = forms.FileField(required=False, widget=ClearableFileInput(attrs={'placeholder': ...})) - +class SheetForm(forms.ModelForm): + class Meta: + model = Sheet + fields = ["instrument", "customer_name", "customer_address", "control_number", "serial_number", "accuracy", "barometric_pressure", "temperature", "humidity", "report_type", "as_found", "as_left", "both", "configuration",] + widgets = { + "accuracy": forms.NumberInput(attrs={'step': 0.01}), + "barometric_pressure": forms.NumberInput(attrs={'step': 0.01, 'max': 1100, 'min': 800}), + "temperature": forms.NumberInput(attrs={'step': 0.01, 'max': 1000.0, 'min': -459.67}), + "humidity": forms.NumberInput(attrs={'step': 0.01, 'max': 100.0, 'min': 0.0}), + } + def __init__(self, *args, **kwargs): - super(UploadFileForm, self).__init__(*args, **kwargs) + super(SheetForm, self).__init__(*args, **kwargs) for name, field in self.fields.items(): # add v-model to each model field @@ -42,4 +29,5 @@ class UploadFileForm(forms.Form): 'v-if': f"show_{name}" }) else: - field.widget.attrs.update({'v-model': name}) \ No newline at end of file + field.widget.attrs.update({'v-model': name}) + diff --git a/benchtopdevices/sheets/migrations/0001_initial.py b/benchtopdevices/sheets/migrations/0001_initial.py new file mode 100644 index 0000000..7decb71 --- /dev/null +++ b/benchtopdevices/sheets/migrations/0001_initial.py @@ -0,0 +1,30 @@ +# Generated by Django 4.2.3 on 2023-07-26 18:12 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + initial = True + + dependencies = [] + + operations = [ + migrations.CreateModel( + name="Configuration", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("serial", models.CharField(max_length=100)), + ("device", models.CharField(max_length=100)), + ("calibration_date", models.DateField()), + ("calibration_due_date", models.DateField()), + ], + ), + ] diff --git a/benchtopdevices/sheets/migrations/0002_sheet.py b/benchtopdevices/sheets/migrations/0002_sheet.py new file mode 100644 index 0000000..623bdf4 --- /dev/null +++ b/benchtopdevices/sheets/migrations/0002_sheet.py @@ -0,0 +1,56 @@ +# Generated by Django 4.2.3 on 2023-07-26 19:33 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + dependencies = [ + ("sheets", "0001_initial"), + ] + + operations = [ + migrations.CreateModel( + name="Sheet", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("instrument", models.CharField(max_length=256)), + ("customer_name", models.CharField(max_length=256)), + ("customer_address", models.CharField(max_length=256)), + ("control_number", models.CharField(max_length=256)), + ("serial_number", models.CharField(max_length=256)), + ("accuracy", models.FloatField()), + ("barometric_pressure", models.FloatField()), + ("temperature", models.FloatField()), + ("humidity", models.FloatField()), + ( + "report_type", + models.CharField( + choices=[ + ("TV", "Transducer Verify"), + ("HC", "Hardware Calibration"), + ], + max_length=256, + ), + ), + ("as_found", models.FileField(upload_to="")), + ("as_left", models.FileField(upload_to="")), + ("both", models.FileField(upload_to="")), + ( + "configuration", + models.ForeignKey( + on_delete=django.db.models.deletion.PROTECT, + to="sheets.configuration", + ), + ), + ], + ), + ] diff --git a/benchtopdevices/sheets/migrations/0003_alter_sheet_as_found_alter_sheet_as_left_and_more.py b/benchtopdevices/sheets/migrations/0003_alter_sheet_as_found_alter_sheet_as_left_and_more.py new file mode 100644 index 0000000..bb57e67 --- /dev/null +++ b/benchtopdevices/sheets/migrations/0003_alter_sheet_as_found_alter_sheet_as_left_and_more.py @@ -0,0 +1,36 @@ +# Generated by Django 4.2.3 on 2023-07-26 20:34 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("sheets", "0002_sheet"), + ] + + operations = [ + migrations.AlterField( + model_name="sheet", + name="as_found", + field=models.FileField(blank=True, upload_to=""), + ), + migrations.AlterField( + model_name="sheet", + name="as_left", + field=models.FileField(blank=True, upload_to=""), + ), + migrations.AlterField( + model_name="sheet", + name="both", + field=models.FileField(blank=True, upload_to=""), + ), + migrations.RemoveField( + model_name="sheet", + name="configuration", + ), + migrations.AddField( + model_name="sheet", + name="configuration", + field=models.ManyToManyField(to="sheets.configuration"), + ), + ] diff --git a/benchtopdevices/sheets/models.py b/benchtopdevices/sheets/models.py index b9b5208..7e89537 100644 --- a/benchtopdevices/sheets/models.py +++ b/benchtopdevices/sheets/models.py @@ -1,4 +1,5 @@ from django.db import models +from django.utils.translation import gettext_lazy as _ # Create your models here. @@ -9,4 +10,32 @@ class Configuration(models.Model): calibration_due_date = models.DateField() def __str__(self): - return f"{self.serial} {self.description}" \ No newline at end of file + return f"{self.serial} {self.device} [{self.calibration_date} to {self.calibration_due_date}]" + + +class Sheet(models.Model): + instrument = models.CharField(max_length=256) + customer_name = models.CharField(max_length=256) + customer_address = models.CharField(max_length=256) + control_number = models.CharField(max_length=256) + serial_number = models.CharField(max_length=256) + accuracy = models.FloatField() + barometric_pressure = models.FloatField() + temperature = models.FloatField() + humidity = models.FloatField() + + + CHOICES = [ + ('TV', _('Transducer Verify')), + ('HC', _('Hardware Calibration')) + ] + + report_type = models.CharField(max_length=256, + choices=CHOICES, + ) + + as_found = models.FileField(blank=True) + as_left = models.FileField(blank=True) + both = models.FileField(blank=True) + + configuration = models.ManyToManyField("Configuration") \ No newline at end of file diff --git a/benchtopdevices/sheets/parsers/hardware.py b/benchtopdevices/sheets/parsers/hardware.py index f015f9f..dd3dbbd 100644 --- a/benchtopdevices/sheets/parsers/hardware.py +++ b/benchtopdevices/sheets/parsers/hardware.py @@ -49,7 +49,7 @@ def parse_calibration_data(text): return calibration_data -def parse_hardware_calibration(content): +def parse_hardware_calibration(content, accuracy): # Split the content into instrument info and calibration data sections info_section, calibration_section = content.split( "|| Hardware Calibration Report ||" diff --git a/benchtopdevices/sheets/views.py b/benchtopdevices/sheets/views.py index 44114a1..456473d 100644 --- a/benchtopdevices/sheets/views.py +++ b/benchtopdevices/sheets/views.py @@ -1,18 +1,29 @@ from django.http import HttpResponseRedirect from django.shortcuts import render -from .forms import UploadFileForm +from .forms import SheetForm, SheetForm +from django.core.exceptions import ValidationError from .parsers import parse_transducer, parse_hardware_calibration def upload_file(request): if request.method == "POST": - breakpoint() - f = UploadFileForm(request.POST, request.FILES) + f = SheetForm(request.POST, request.FILES) if f.is_valid(): data = f.clean() - content = request.FILES['file'].read() + as_found = request.FILES.get('as_found') + as_left = request.FILES.get('as_left') + both = request.FILES.get('both') + if both: + if data['report_type'] == "TV": + tv = parse_transducer(both.read().decode(), data['accuracy']) + else: + hc = parse_hardware_calibration(both.read().decode(), data['accuracy']) + elif as_found and as_left: + pass + else: + raise ValidationError("Please provide proper files") return HttpResponseRedirect("/success/url/") else: - form = UploadFileForm() + form = SheetForm() return render(request, "sheets/upload.html", {"form": form}) \ No newline at end of file