From 5b6bb4d9f169df634ca60c91559a6e8e62effe4d Mon Sep 17 00:00:00 2001 From: rw-bsi Date: Sun, 24 Nov 2024 18:16:21 +0000 Subject: [PATCH] update Instance Completion task + update admin --- backend/apps/ifc_validation/admin.py | 4 +-- backend/apps/ifc_validation/tasks.py | 42 ++++++++++++++++++++++------ 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/backend/apps/ifc_validation/admin.py b/backend/apps/ifc_validation/admin.py index 915e155..6a869b1 100644 --- a/backend/apps/ifc_validation/admin.py +++ b/backend/apps/ifc_validation/admin.py @@ -298,9 +298,9 @@ def size_text(self, obj): class ModelInstanceAdmin(BaseAdmin, NonAdminAddable): - list_display = ["id", "public_id", "stepfile_id", "model", "ifc_type", "created", "updated"] - + list_display = ["id", "public_id", "model", "model_id", "stepfile_id", "ifc_type", "created", "updated"] search_fields = ('stepfile_id', 'model__file_name', 'ifc_type') + list_filter = ["ifc_type", "model_id", "created", "updated"] class CompanyAdmin(BaseAdmin): diff --git a/backend/apps/ifc_validation/tasks.py b/backend/apps/ifc_validation/tasks.py index a8e6162..e35ba13 100644 --- a/backend/apps/ifc_validation/tasks.py +++ b/backend/apps/ifc_validation/tasks.py @@ -191,27 +191,51 @@ def ifc_file_validation_task(self, id, file_name, *args, **kwargs): @requires_django_user_context def instance_completion_subtask(self, prev_result, id, file_name, *args, **kwargs): + # fetch request info + request = ValidationRequest.objects.get(pk=id) + file_path = get_absolute_file_path(request.file.name) + + # increment overall progress + PROGRESS_INCREMENT = 5 + request.progress += PROGRESS_INCREMENT + request.save() + + # add task + task = ValidationTask.objects.create(request=request, type=ValidationTask.Type.INSTANCE_COMPLETION) + prev_result_succeeded = prev_result is not None and prev_result[0]['is_valid'] is True if prev_result_succeeded: - request = ValidationRequest.objects.get(pk=id) - file_path = get_absolute_file_path(request.file.name) - + task.mark_as_initiated() + try: ifc_file = ifcopenshell.open(file_path) - except: - logger.warning(f'Failed to open {file_path}. Likely previous tasks also failed.') - ifc_file = None + except: + reason = f'Failed to open {file_path}. Likely previous tasks also failed.' + task.mark_as_completed(reason) + return {'is_valid': False, 'reason': reason} if ifc_file: + + # fetch and update ModelInstance records without ifc_type with transaction.atomic(): - for inst in request.model.instances.iterator(): + model_id = request.model.id + model_instances = ModelInstance.objects.filter(model_id=model_id, ifc_type__in=[None, '']) + instance_count = model_instances.count() + logger.info(f'Retrieved {instance_count:,} ModelInstance record(s)') + + for inst in model_instances.iterator(): inst.ifc_type = ifc_file[inst.stepfile_id].is_a() inst.save() + # update Task info and return + reason = f'Updated {instance_count:,} ModelInstance record(s)' + task.mark_as_completed(reason) + return {'is_valid': True, 'reason': reason} + else: reason = f'Skipped as prev_result = {prev_result}.' - #task.mark_as_skipped(reason) + task.mark_as_skipped(reason) return {'is_valid': None, 'reason': reason} @@ -468,7 +492,7 @@ def prerequisites_subtask(self, prev_result, id, file_name, *args, **kwargs): file_path = get_absolute_file_path(request.file.name) # increment overall progress - PROGRESS_INCREMENT = 15 + PROGRESS_INCREMENT = 10 request.progress += PROGRESS_INCREMENT request.save()