diff --git a/api/wx/admin.py b/api/wx/admin.py index 9c96d8b..b461560 100644 --- a/api/wx/admin.py +++ b/api/wx/admin.py @@ -345,12 +345,13 @@ class ManufacturerAdmin(admin.ModelAdmin): class EquipmentTypeAdmin(admin.ModelAdmin): list_display = ("name",) -@admin.register(models.FoundingSource) -class FoundingSourceAdmin(admin.ModelAdmin): +@admin.register(models.FundingSource) +class FundingSourceAdmin(admin.ModelAdmin): list_display = ("name",) @admin.register(models.Equipment) class EquipmentAdmin(admin.ModelAdmin): list_display = ("equipment_type", "manufacturer", "model", "serial_number", "acquisition_date", "first_deploy_date", "last_calibration_date") + readonly_fields=('location',) diff --git a/api/wx/migrations/0024_equipment_equipmenttype_foundingsource_manufacturer.py b/api/wx/migrations/0024_equipment_equipmenttype_foundingsource_manufacturer.py index 3f37d2f..a798242 100644 --- a/api/wx/migrations/0024_equipment_equipmenttype_foundingsource_manufacturer.py +++ b/api/wx/migrations/0024_equipment_equipmenttype_foundingsource_manufacturer.py @@ -1,4 +1,4 @@ -# Generated by Django 3.2.13 on 2023-06-20 14:33 +# Generated by Django 3.2.13 on 2023-06-20 21:31 import ckeditor.fields from django.db import migrations, models @@ -63,7 +63,7 @@ class Migration(migrations.Migration): ('first_deploy_date', models.DateField(blank=True, null=True)), ('last_calibration_date', models.DateField(blank=True, null=True)), ('next_calibration_date', models.DateField(blank=True, null=True)), - ('decomission_date', models.DateField(blank=True, null=True)), + ('decommission_date', models.DateField(blank=True, null=True)), ('equipment_type', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='wx.equipmenttype')), ('founding_source', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='wx.foundingsource')), ('manufacturer', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='wx.manufacturer')), diff --git a/api/wx/migrations/0025_auto_20230621_1744.py b/api/wx/migrations/0025_auto_20230621_1744.py new file mode 100644 index 0000000..74220ac --- /dev/null +++ b/api/wx/migrations/0025_auto_20230621_1744.py @@ -0,0 +1,42 @@ +# Generated by Django 3.2.13 on 2023-06-21 17:44 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('wx', '0024_equipment_equipmenttype_foundingsource_manufacturer'), + ] + + operations = [ + migrations.RenameModel( + old_name='FoundingSource', + new_name='FundingSource', + ), + migrations.AlterModelOptions( + name='equipment', + options={'verbose_name': 'equipment', 'verbose_name_plural': 'equipment'}, + ), + migrations.RenameField( + model_name='equipment', + old_name='founding_source', + new_name='funding_source', + ), + migrations.AddField( + model_name='equipment', + name='condition', + field=models.CharField(choices=[('F', 'Fully Functional'), ('P', 'Partially Functional'), ('N', 'Not Functional')], max_length=1, null=True), + ), + migrations.AddField( + model_name='equipment', + name='last_deploy_date', + field=models.DateField(blank=True, null=True), + ), + migrations.AddField( + model_name='equipment', + name='location', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.DO_NOTHING, to='wx.station'), + ), + ] diff --git a/api/wx/models.py b/api/wx/models.py index 922485d..2c0406c 100644 --- a/api/wx/models.py +++ b/api/wx/models.py @@ -1394,22 +1394,33 @@ class Meta: def __str__(self): return self.name -class FoundingSource(BaseModel): +class FundingSource(BaseModel): name = models.CharField(max_length=128) + def __str__(self): + return self.name + class Equipment(BaseModel): + class EquipmentCondition(models.TextChoices): + FULLY_FUNCTIONAL = 'F', gettext_lazy('Fully Functional') + PARTIALLY_FUNCTIONAL = 'P', gettext_lazy('Partially Functional') + NOT_FUNCTIONAL = 'N', gettext_lazy('Not Functional') + equipment_type = models.ForeignKey(EquipmentType, on_delete=models.DO_NOTHING) manufacturer = models.ForeignKey(Manufacturer, on_delete=models.DO_NOTHING) - founding_source = models.ForeignKey(FoundingSource, on_delete=models.DO_NOTHING) + funding_source = models.ForeignKey(FundingSource, on_delete=models.DO_NOTHING) model = models.CharField(max_length=64) serial_number = models.CharField(max_length=64) acquisition_date = models.DateField() first_deploy_date = models.DateField(blank=True, null=True) + last_deploy_date = models.DateField(blank=True, null=True) last_calibration_date = models.DateField(blank=True, null=True) next_calibration_date = models.DateField(blank=True, null=True) - decomission_date = models.DateField(blank=True, null=True) + decommission_date = models.DateField(blank=True, null=True) + location = models.ForeignKey(Station, on_delete=models.DO_NOTHING, null=True) + condition = models.CharField(max_length=1, choices=EquipmentCondition.choices, null=True) class Meta: unique_together = ("equipment_type", "serial_number") verbose_name = "equipment" - verbose_name_plural = "equipments" \ No newline at end of file + verbose_name_plural = "equipment" \ No newline at end of file diff --git a/api/wx/templates/wx/maintenance_reports/equipment_inventory.html b/api/wx/templates/wx/maintenance_reports/equipment_inventory.html index 2a388d5..c82da91 100644 --- a/api/wx/templates/wx/maintenance_reports/equipment_inventory.html +++ b/api/wx/templates/wx/maintenance_reports/equipment_inventory.html @@ -83,11 +83,11 @@ @@ -220,12 +220,76 @@ + + + + + + + + + + + + + + + + + + + - + Create Equipment - + Update Equipment @@ -336,6 +400,16 @@ el: "#app", vuetify: new Vuetify(), delimiters: ["[[", "]]"], + validations: { + equipment: { + equipment_type_id: { required }, + funding_source_id: { required }, + manufacturer_id: { required }, + model: { required }, + serial_number: { required }, + acquisition_date: { required }, + }, + }, data:{ search: '', loading: false, @@ -346,14 +420,16 @@ equipments: [], equipment_types: [], manufacturers: [], - founding_sources: [], + funding_sources: [], + stations: [], + equipment_conditions: [], is_new_equipment: true, equipment: { equipment_id: null, equipment_type: null, equipment_type_id: null, - founding_source: null, - founding_source_id: null, + funding_source: null, + funding_source_id: null, manufacturer: null, manufacturer_id: null, model: null, @@ -366,8 +442,14 @@ last_calibration_date_menu: false, next_calibration_date: moment().format('YYYY-MM-DD'), next_calibration_date_menu: false, - decomission_date: moment().format('YYYY-MM-DD'), - decomission_date_menu: false, + decommission_date: moment().format('YYYY-MM-DD'), + decommission_date_menu: false, + location: null, + location_id: null, + condition: null, + condition_id: null, + last_deploy_date: moment().format('YYYY-MM-DD'), + last_deploy_date_menu: false }, headers: [ { text: 'Equipment Type', align: 'left', value: 'equipment_type' }, @@ -376,7 +458,7 @@ { text: 'Serial Number', align: 'center', value: 'serial_number' }, { text: 'Acquired', align: 'center', value: 'acquisition_date' }, { text: 'First Deployed', align: 'center', value: 'first_deploy_date' }, - { text: 'Last Deployed', align: 'center', value: 'last_deployed' }, + { text: 'Last Deployed', align: 'center', value: 'last_deploy_date' }, { text: 'Location', align: 'center', value: 'location' }, { text: 'Condition', align: 'center', value: 'condition' }, { text: 'Actions', align: 'center', value: 'actions', sortable: false }, @@ -395,8 +477,8 @@ equipment_id: null, equipment_type: null, equipment_type_id: null, - founding_source: null, - founding_source_id: null, + funding_source: null, + funding_source_id: null, manufacturer: null, manufacturer_id: null, model: null, @@ -409,8 +491,14 @@ last_calibration_date_menu: false, next_calibration_date: moment().format('YYYY-MM-DD'), next_calibration_date_menu: false, - decomission_date: moment().format('YYYY-MM-DD'), - decomission_date_menu: false, + decommission_date: moment().format('YYYY-MM-DD'), + decommission_date_menu: false, + location: null, + location_id: null, + condition: null, + condition_id: null, + last_deploy_date: moment().format('YYYY-MM-DD'), + last_deploy_date_menu: false }; } } @@ -426,7 +514,9 @@ this.equipments = res.data['equipments']; this.equipment_types = res.data['equipment_types']; this.manufacturers = res.data['manufacturers']; - this.founding_sources = res.data['founding_sources'] + this.funding_sources = res.data['funding_sources']; + this.stations = res.data['stations']; + this.equipment_conditions = res.data['equipment_conditions']; }); }, createEquipment(){ @@ -437,14 +527,17 @@ params: { 'equipment_type': this.equipment.equipment_type_id, 'manufacturer': this.equipment.manufacturer_id, - 'founding_source': this.equipment.founding_source_id, + 'funding_source': this.equipment.funding_source_id, 'model': this.equipment.model, 'serial_number': this.equipment.serial_number, 'acquisition_date': this.equipment.acquisition_date, 'first_deploy_date': this.equipment.first_deploy_date, 'last_calibration_date': this.equipment.last_calibration_date, 'next_calibration_date': this.equipment.next_calibration_date, - 'decomission_date': this.equipment.decomission_date, + 'decommission_date': this.equipment.decommission_date, + 'location': this.equipment.location_id, + 'condition': this.equipment.condition_id, + 'last_deploy_date': this.equipment.last_deploy_date, }, }) .then( @@ -469,14 +562,17 @@ 'equipment_id': this.equipment.equipment_id, 'equipment_type': this.equipment.equipment_type_id, 'manufacturer': this.equipment.manufacturer_id, - 'founding_source': this.equipment.founding_source_id, + 'funding_source': this.equipment.funding_source_id, 'model': this.equipment.model, 'serial_number': this.equipment.serial_number, 'acquisition_date': this.equipment.acquisition_date, 'first_deploy_date': this.equipment.first_deploy_date, 'last_calibration_date': this.equipment.last_calibration_date, 'next_calibration_date': this.equipment.next_calibration_date, - 'decomission_date': this.decomission_date + 'decommission_date': this.equipment.decommission_date, + 'location': this.equipment.location_id, + 'condition': this.equipment.condition_id, + 'last_deploy_date': this.equipment.last_deploy_date, }, }) .then( diff --git a/api/wx/views.py b/api/wx/views.py index 098c95a..cd5cff5 100644 --- a/api/wx/views.py +++ b/api/wx/views.py @@ -3985,7 +3985,7 @@ def get_wave_data(request): return JsonResponse(charts) -from wx.models import Equipment, EquipmentType, Manufacturer, FoundingSource +from wx.models import Equipment, EquipmentType, Manufacturer, FundingSource from django.core.serializers import serialize @@ -3997,41 +3997,80 @@ def get_equipment_inventory(request): return HttpResponse(template.render(context, request)) +def get_value(variable): + if variable is None: + return '---' + return variable + +def equipment_condition(condition): + if condition == 'F': + return 'Fully Functional' + elif condition == 'P': + return 'Partially Functional' + elif condition == 'N': + return 'Not Functional' + return None + @require_http_methods(["GET"]) def get_equipment_inventory_data(request): equipment_types = EquipmentType.objects.all() manufacturers = Manufacturer.objects.all() equipments = Equipment.objects.all() - founding_sources = FoundingSource.objects.all() + funding_sources = FundingSource.objects.all() + stations = Station.objects.all() equipment_list = [] for equipment in equipments: - equipment_dict = { - 'equipment_id': equipment.id, - 'equipment_type': equipment_types.get(id=equipment.equipment_type_id).name, - 'equipment_type_id': equipment.equipment_type_id, - 'founding_source': founding_sources.get(id=equipment.founding_source_id).name, - 'founding_source_id': equipment.founding_source_id, - 'manufacturer': manufacturers.get(id=equipment.manufacturer_id).name, - 'manufacturer_id': equipment.manufacturer_id, - 'model': equipment.model, - 'serial_number': equipment.serial_number, - 'acquisition_date': equipment.acquisition_date, - 'first_deploy_date': equipment.first_deploy_date, - 'last_calibration_date': equipment.last_calibration_date, - 'next_calibration_date': equipment.next_calibration_date, - 'decomission_date': equipment.decomission_date, - 'last_deployed': '---', - 'location': 'Office', - 'condition': '---', - } - equipment_list.append(equipment_dict) + try: + equipment_type = equipment_types.get(id=equipment.equipment_type_id) + funding_source = funding_sources.get(id=equipment.funding_source_id) + manufacturer = manufacturers.get(id=equipment.manufacturer_id) + + station = None + if equipment.location_id: + station = stations.get(id=equipment.location_id) + + equipment_dict = { + 'equipment_id': equipment.id, + 'equipment_type': equipment_type.name, + 'equipment_type_id': equipment_type.id, + 'funding_source': funding_source.name, + 'funding_source_id': funding_source.id, + 'manufacturer': manufacturer.name, + 'manufacturer_id': manufacturer.id, + 'model': equipment.model, + 'serial_number': equipment.serial_number, + 'acquisition_date': equipment.acquisition_date, + 'first_deploy_date': equipment.first_deploy_date, + 'last_calibration_date': equipment.last_calibration_date, + 'next_calibration_date': equipment.next_calibration_date, + 'decommission_date': equipment.decommission_date, + 'last_deploy_date': equipment.last_deploy_date, + 'location': f"{station.name} - {station.code}" if station else 'Office', + 'location_id': station.id if station else None, + 'condition': equipment_condition(equipment.condition), + 'condition_id': equipment.condition, + } + equipment_list.append(equipment_dict) + except ObjectDoesNotExist: + pass + + equipment_conditions = [ + {'name': 'Fully Functional', 'id': 'F'}, + {'name': 'Partially Functional', 'id': 'P'}, + {'name': 'Not Functional', 'id': 'N'}, + ] + + station_list = [{'name': f"{station.name} - {station.code}", + 'id': station.id} for station in stations] response = { 'equipments': equipment_list, 'equipment_types': list(equipment_types.values()), 'manufacturers': list(manufacturers.values()), - 'founding_sources': list(founding_sources.values()) + 'funding_sources': list(funding_sources.values()), + 'stations': station_list, + 'equipment_conditions': equipment_conditions, } return JsonResponse(response, status=status.HTTP_200_OK) @@ -4040,18 +4079,25 @@ def get_equipment_inventory_data(request): def create_equipment(request): equipment_type_id = request.GET.get('equipment_type', None) manufacturer_id = request.GET.get('manufacturer', None) - founding_source_id = request.GET.get('founding_source', None) + funding_source_id = request.GET.get('funding_source', None) model = request.GET.get('model', None) serial_number = request.GET.get('serial_number', None) acquisition_date = request.GET.get('acquisition_date', None) first_deploy_date = request.GET.get('first_deploy_date', None) last_calibration_date = request.GET.get('last_calibration_date', None) next_calibration_date = request.GET.get('next_calibration_date', None) - decomission_date = request.GET.get('decomission_date', None) + decommission_date = request.GET.get('decommission_date', None) + location_id = request.GET.get('location', None) + condition_id = request.GET.get('condition', None) + last_deploy_date = request.GET.get('last_deploy_date', None) equipment_type = EquipmentType.objects.get(id=equipment_type_id) - manufacturer = Manufacturer.objects.get(id=manufacturer_id) - founding_source = FoundingSource.objects.get(id=founding_source_id) + manufacturer = Manufacturer.objects.get(id=manufacturer_id) + funding_source = FundingSource.objects.get(id=funding_source_id) + + location = None + if location_id: + location = Station.objects.get(id=location_id) try: equipment = Equipment.objects.get( @@ -4076,68 +4122,85 @@ def create_equipment(request): updated_at = now, equipment_type = equipment_type, manufacturer = manufacturer, - founding_source = founding_source, + funding_source = funding_source, model = model, serial_number = serial_number, acquisition_date = acquisition_date, first_deploy_date = first_deploy_date, last_calibration_date = last_calibration_date, next_calibration_date = next_calibration_date, - decomission_date = decomission_date, + decommission_date = decommission_date, + location = location, + condition = condition, + last_deploy_date = last_deploy_date, ) response = {'equipment_id': equipment.id} return JsonResponse(response, status=status.HTTP_200_OK) - @require_http_methods(["POST"]) def update_equipment(request): equipment_id = request.GET.get('equipment_id', None) equipment_type_id = request.GET.get('equipment_type', None) manufacturer_id = request.GET.get('manufacturer', None) - founding_source_id = request.GET.get('founding_source', None) + funding_source_id = request.GET.get('funding_source', None) model = request.GET.get('model', None) serial_number = request.GET.get('serial_number', None) acquisition_date = request.GET.get('acquisition_date', None) first_deploy_date = request.GET.get('first_deploy_date', None) last_calibration_date = request.GET.get('last_calibration_date', None) next_calibration_date = request.GET.get('next_calibration_date', None) - decomission_date = request.GET.get('decomission_date', None) + decommission_date = request.GET.get('decommission_date', None) + location_id = request.GET.get('location', None) + condition = request.GET.get('condition', None) + last_deploy_date = request.GET.get('last_deploy_date', None) equipment_type = EquipmentType.objects.get(id=equipment_type_id) manufacturer = Manufacturer.objects.get(id=manufacturer_id) - founding_source = FoundingSource.objects.get(id=founding_source_id) + funding_source = FundingSource.objects.get(id=funding_source_id) + + location = None + if location_id: + location = Station.objects.get(id=location_id) try: equipment = Equipment.objects.get(equipment_type=equipment_type, serial_number=serial_number) if int(equipment_id) != equipment.id: - message = f'''Could not update. Already exist an equipment of \ + message = f"Could not update. Already exist an equipment of \ equipment type {equipment_type.name} and serial \ - number {equipment.serial_number}''' + number {equipment.serial_number}" response = {'message': message} return JsonResponse(response, status=status.HTTP_400_BAD_REQUEST) - else: - now = datetime.datetime.now() - - equipment.updated_at = now - equipment.equipment_type = equipment_type - equipment.manufacturer = manufacturer - equipment.founding_source = founding_source - equipment.model = model - equipment.serial_number = serial_number - equipment.acquisition_date = acquisition_date - equipment.first_deploy_date = first_deploy_date - equipment.last_calibration_date = last_calibration_date - equipment.next_calibration_date = next_calibration_date - equipment.decomission_date = decomission_date - equipment.save() - - response = {} - return JsonResponse(response, status=status.HTTP_200_OK) + except ObjectDoesNotExist: + pass + + try: + equipment = Equipment.objects.get(id=equipment_id) + + now = datetime.datetime.now() + + equipment.updated_at = now + equipment.equipment_type = equipment_type + equipment.manufacturer = manufacturer + equipment.funding_source = funding_source + equipment.model = model + equipment.serial_number = serial_number + equipment.acquisition_date = acquisition_date + equipment.first_deploy_date = first_deploy_date + equipment.last_calibration_date = last_calibration_date + equipment.next_calibration_date = next_calibration_date + equipment.decommission_date = decommission_date + equipment.location = location + equipment.condition = condition + equipment.last_deploy_date = last_deploy_date + equipment.save() + + response = {} + return JsonResponse(response, status=status.HTTP_200_OK) except ObjectDoesNotExist: message = "Object not found"