class CreateProjectWorkorderView(views.APIView): permissionclasses =…
- User
class CreateProjectWorkorderView(views.APIView): permission_classes = [permissions.IsAuthenticated]
def post(self, request, project_title): user = request.user try: project_sites = ProjectSiteModel.objects.filter(title=project_title) except Exception as e: return response.Response({"detail": "Project title not found"}, status=status.HTTP_404_NOT_FOUND) try: project_tickets = ProjectWorkorderModel.objects.filter(project_title=project_title) if project_tickets.exists(): return response.Response({"detail": "Project tickets already exist"}, status=status.HTTP_400_BAD_REQUEST) except Exception as e: return response.Response({"detail": "Error while filtering project ticket model"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) if project_sites.exists(): data_list = project_sites.values_list('data', flat=True) cpg_data_list = project_sites.values_list('cpg_data', flat=True) work_orders_to_create = { 'SA': [], 'ROAD': [], 'POWER': [], 'TI': [], 'CW': [], } for data, cpg_data in zip(data_list, cpg_data_list): data_dict = json.loads(data) cpg_data_dict = json.loads(cpg_data) for key in cpg_data_dict: if key in data_dict: data_dict[key] = cpg_data_dict[key] site_title = data_dict.get('Site ID') pm = data_dict.get('MTNI PM/PC') parent_work_order_id = create_parent_work_order_id(site_title, project_title) project_ticket = ProjectWorkorderModel.objects.create( site_title=site_title, status='pending', created_by=user, pm=pm, work_order_id=parent_work_order_id, project_title=project_title, data=data_dict ) contractors = { 'SA Contractor Name': 'SA', 'CW Contractor Name': 'CW', 'TI Contractor Name': 'TI', 'Road Contractor Name': 'ROAD', 'Power Contractor Name': 'POWER', } for contractor_type, domain in contractors.items(): contractor_name = data_dict.get(contractor_type) company = None extra_contractor = None if contractor_name in ['TBD', 'NTBD']: extra_contractor = contractor_name if contractor_name == 'NTBD': continue else: try: company = Company.objects.get(name=contractor_name) except Company.DoesNotExist: return response.Response({"detail": f"Contractor name {contractor_name} not found"}, status=status.HTTP_404_NOT_FOUND) work_order_id = create_contractor_work_order_id(domain, site_title, project_title) work_order_model_class = { 'SA': SAWorkOrdersModel, 'CW': CWWorkOrdersModel, 'TI': TIWorkOrdersModel, 'ROAD': RoadWorkOrdersModel, 'POWER': PowerWorkOrdersModel, }[domain] if contractor_name == 'TBD': wo_status = 'TBD' else: if domain == 'SA': wo_status = 'pending' else: wo_status = 'locked' work_orders_to_create[domain].append( work_order_model_class( parent_work_order=project_ticket, work_order_id=work_order_id, contractor=company, extra_contractor=extra_contractor, status=wo_status, created_by=user ) ) for domain, work_orders in work_orders_to_create.items(): if work_orders: work_order_model_class = { 'SA': SAWorkOrdersModel, 'ROAD': RoadWorkOrdersModel, 'POWER': PowerWorkOrdersModel, 'TI': TIWorkOrdersModel, 'CW': CWWorkOrdersModel, }[domain] work_order_model_class.objects.bulk_create(work_orders) project_sites.update(status='Done') return response.Response({"detail": "Workorders created successfully"}, status=status.HTTP_201_CREATED) else: return response.Response({"detail": "No project sites found for the given title"}, status=status.HTTP_404_NOT_FOUND)Consider above view. It works fine. Now i want to have another view to able create workorders for partially updated ProjectSiteModel instances. I coded a portion of that view, you should now complete the v2 view based on the first view. For overall description you need to check cpg_data of projectsitemodel instance, if that field was blank or none it means you should skip, other wise you should check if the corresponding workorder of site id exists in ProjectWorkorderModel so again you need to skip them. Actually you should check all site ids in projectsitemodel instances with the corresponding site id in projectworkordermodel, if there are site id with filled cpg_data in projectsitemodel and not exists in projectworkordermodel you should create proper workorders. class CreateProjectWorkorderV2View(views.APIView): permission_classes = [permissions.IsAuthenticated]
def post(self, request, project_title): user = request.user try: project_sites = ProjectSiteModel.objects.filter(title=project_title) except ProjectSiteModel.DoesNotExist: return response.Response({"detail": "No project sites found for the given title"}, status=status.HTTP_404_NOT_FOUND) not_updated_site_ids = [] for project_site in project_sites: if project_site.cpg_data == None: site_id = project_site.site_title not_updated_site_ids.append(site_id) try: workorders = ProjectWorkorderModel.objects.filter(project_title=project_title) except ProjectWorkorderModel.DoesNotExist as e: updated_site_ids=[] for project_site in project_sites: data=json.loads(project_site) existing_cpg_data=data.cpg_data if existing_cpg_data is not None: instance_site_id=data.site_id updated_site_ids.append(instance_site_id)...
class ProjectWorkorderModel(models.Model): id = models.UUIDField(default=uuid.uuid4, primary_key=True, editable=False, unique=True) site_title = models.CharField(max_length=255, null=True, blank=True) work_order_id = models.CharField(max_length=255) # parent_ticket_id = models.CharField(max_length=255, null=True, blank=True) # type = models.CharField(max_length=255,null=True) # cw,road,ti,... data = models.JSONField(null=True, blank=True) status = models.CharField(choices=WORK_ORDER_STATUS, default=[0][0], max_length=255) project_title = models.CharField(max_length=255, null=True, blank=True) pm = models.CharField(max_length=255, null=True, blank=True) created_at = models.DateTimeField(auto_now_add=True) created_by = models.ForeignKey(User, on_delete=models.DO_NOTHING) updated_at = models.DateTimeField(auto_now=True)
class Meta: ordering = ['-created_at'] def __str__(self): return f'{self.work_order_id}'class ProjectSiteModel(models.Model): PROJECT_STATUS = ( ('pending', 'pending'), ('in-progress', 'in-progress'), ('Done', 'Done'), ('drafted', 'drafted'), ('return', 'returned'), ('partially_done','Partially Done') ) PROJECT_TYPE = ( ('new_site_rollout', 'new site rollout'), ('relocation', 'relocation'), ('modernization', 'modernization'), ('new_site_uso', 'new site uso'), ) id = models.UUIDField(default=uuid.uuid4, primary_key=True) site_id = models.ForeignKey(Site, on_delete=models.CASCADE, null=True) site_title = models.CharField(max_length=50, null=True) status = models.CharField(choices=PROJECT_STATUS, default=PROJECT_STATUS[0][0]) type = models.CharField(choices=PROJECT_TYPE) title = models.CharField(max_length=255) data = models.JSONField() cpg_data = models.JSONField(null=True) atoll_existence = models.BooleanField(default=True) created_date = models.DateTimeField(auto_now_add=True) updated_date = models.DateTimeField(auto_now=True) created_by = models.CharField(max_length=100) updated_by = models.CharField(max_length=100) temp_version = models.ForeignKey(ProjectSiteTemplateModel, on_delete=models.SET_NULL, null=True)
class Meta: db_table = 'project_site' permissions = [ ('can_upload_cpg_file', 'Can upload cpg file'), ('can_upload_project_file', 'Can upload project file'), ('can_download_project_file', 'Can download project file'), ('total_wo_show', 'Can view all workorders'), ('pm_wo_show', 'Can view all pm-associated workorders'), ('subcon_wo_show', 'Can view all subcon-associated workorders'), ] @classmethod def get_deduplicated_records(cls): latest_records = cls.objects.values('title').annotate( latest_created_date=Max('created_date') ).values('latest_created_date') result = cls.objects.filter(created_date__in=latest_records).order_by('-created_date') return resultAlso when workorders created based on the complete wo or partially you should update status of all project instances in projectsitemodel. If all site ids have their workorders should be “Done” other wise should “partially_done”. It is possible that in a project that has a bunch of 10 site ids user first update 5 of them so after creation of workorder the status of project in projectsitemodel should be partially_done, in second turn it updates 2 then it should be partially_done and after that with a 3 site id updating when created the 10 workorders it should be updated to “Done”
- Assistant
Certainly! Below is the complete implementation of the
CreateProjectWorkorderV2Viewbased on your originalCreateProjectWorkorderView. This new view handles the creation of work orders for partially updatedProjectSiteModelinstances by:-
Filtering
ProjectSiteModelInstances:- Selects
ProjectSiteModelinstances with the givenproject_title. - Filters out instances where
cpg_dataisNoneor empty. - Excludes sites that already have associated work orders in
ProjectWorkorderModel.
- Selects
-
Creating Work Orders:
- For each eligible
ProjectSiteModel, it mergesdataandcpg_data. - Creates a parent work order in
ProjectWorkorderModel. - Creates associated contractor work orders (
SA,ROAD,POWER,TI,CW) using bulk operations for efficiency.
- For each eligible
-
Updating Statuses:
- Updates the status of the processed
ProjectSiteModelinstances to'Done'. - Determines the overall project status:
- If all relevant sites have work orders, the project status is set to
'Done'. - Otherwise, it's set to
'partially_done'.
- If all relevant sites have work orders, the project status is set to
- Updates the status of the processed
-
Error Handling:
- Comprehensive error handling ensures meaningful responses for different failure scenarios.
Here’s the complete code for
CreateProjectWorkorderV2View:from rest_framework import views, permissions, response, status from django.db.models import Max import json # Assuming the necessary imports for models and helper functions like create_parent_work_order_id and create_contractor_work_order_id class CreateProjectWorkorderV2View(views.APIView): permission_classes = [permissions.IsAuthenticated] def post(self, request, project_title): user = request.user try: # Retrieve all ProjectSiteModel instances with the given project_title project_sites = ProjectSiteModel.objects.filter(title=project_title) except Exception: return response.Response( {"detail": "Error retrieving project sites."}, status=status.HTTP_500_INTERNAL_SERVER_ERROR ) if not project_sites.exists(): return response.Response( {"detail": "No project sites found for the given title."}, status=status.HTTP_404_NOT_FOUND ) # Filter sites with non-empty cpg_data project_sites_to_process = project_sites.filter( cpg_data__isnull=False ).exclude( cpg_data='' ) if not project_sites_to_process.exists(): return response.Response( {"detail": "No project sites with cpg_data to create workorders."}, status=status.HTTP_400_BAD_REQUEST ) # Identify sites that already have workorders existing_workorders_site_titles = ProjectWorkorderModel.objects.filter( project_title=project_title ).values_list('site_title', flat=True) # Exclude sites that already have workorders project_sites_to_create = project_sites_to_process.exclude( site_title__in=existing_workorders_site_titles ) if not project_sites_to_create.exists(): return response.Response( {"detail": "All project sites with cpg_data already have workorders."}, status=status.HTTP_400_BAD_REQUEST ) # Prepare data for bulk creation data_list = project_sites_to_create.values_list('data', flat=True) cpg_data_list = project_sites_to_create.values_list('cpg_data', flat=True) work_orders_to_create = { 'SA': [], 'ROAD': [], 'POWER': [], 'TI': [], 'CW': [], } try: for data, cpg_data in zip(data_list, cpg_data_list): data_dict = json.loads(data) cpg_data_dict = json.loads(cpg_data) # Merge cpg_data into data_dict for key, value in cpg_data_dict.items(): if value is not None: data_dict[key] = value site_title = data_dict.get('Site ID') pm = data_dict.get('MTNI PM/PC') parent_work_order_id = create_parent_work_order_id(site_title, project_title) # Create parent work order project_ticket = ProjectWorkorderModel.objects.create( site_title=site_title, status='pending', created_by=user, pm=pm, work_order_id=parent_work_order_id, project_title=project_title, data=data_dict ) contractors = { 'SA Contractor Name': 'SA', 'CW Contractor Name': 'CW', 'TI Contractor Name': 'TI', 'Road Contractor Name': 'ROAD', 'Power Contractor Name': 'POWER', } for contractor_type, domain in contractors.items(): contractor_name = data_dict.get(contractor_type) company = None extra_contractor = None if contractor_name in ['TBD', 'NTBD']: extra_contractor = contractor_name if contractor_name == 'NTBD': continue # Skip if contractor is 'NTBD' else: try: company = Company.objects.get(name=contractor_name) except Company.DoesNotExist: return response.Response( {"detail": f"Contractor name '{contractor_name}' not found."}, status=status.HTTP_404_NOT_FOUND ) work_order_id = create_contractor_work_order_id(domain, site_title, project_title) work_order_model_class = { 'SA': SAWorkOrdersModel, 'CW': CWWorkOrdersModel, 'TI': TIWorkOrdersModel, 'ROAD': RoadWorkOrdersModel, 'POWER': PowerWorkOrdersModel, }[domain] wo_status = 'TBD' if contractor_name == 'TBD' else ('pending' if domain == 'SA' else 'locked') work_orders_to_create[domain].append( work_order_model_class( parent_work_order=project_ticket, work_order_id=work_order_id, contractor=company, extra_contractor=extra_contractor, status=wo_status, created_by=user ) ) except json.JSONDecodeError: return response.Response( {"detail": "Invalid JSON format in data or cpg_data."}, status=status.HTTP_400_BAD_REQUEST ) except Exception as e: return response.Response( {"detail": f"Error while creating workorders: {str(e)}"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR ) # Bulk create contractor work orders try: for domain, work_orders in work_orders_to_create.items(): if work_orders: work_order_model_class = { 'SA': SAWorkOrdersModel, 'ROAD': RoadWorkOrdersModel, 'POWER': PowerWorkOrdersModel, 'TI': TIWorkOrdersModel, 'CW': CWWorkOrdersModel, }[domain] work_order_model_class.objects.bulk_create(work_orders) except Exception as e: return response.Response( {"detail": f"Error during bulk creation of contractor workorders: {str(e)}"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR ) # Update the status of the ProjectSiteModel instances that had workorders created project_sites_to_create.update(status='Done') # Determine the overall status of the project total_sites_with_cpg = project_sites_to_process.count() done_sites = project_sites_to_process.filter(status='Done').count() overall_status = 'Done' if done_sites == total_sites_with_cpg else 'partially_done' # Update all relevant ProjectSiteModel instances to reflect the overall status project_sites_to_process.update(status=overall_status) return response.Response( {"detail": "Workorders created successfully.", "project_status": overall_status}, status=status.HTTP_201_CREATED )Explanation of Key Components:
-
Filtering Project Sites:
project_sites = ProjectSiteModel.objects.filter(title=project_title) project_sites_to_process = project_sites.filter(cpg_data__isnull=False).exclude(cpg_data='')- Retrieves all sites associated with the
project_title. - Filters out sites where
cpg_dataisNoneor empty, ensuring only relevant sites are processed.
- Retrieves all sites associated with the
-
Excluding Existing Work Orders:
existing_workorders_site_titles = ProjectWorkorderModel.objects.filter( project_title=project_title ).values_list('site_title', flat=True) project_sites_to_create = project_sites_to_process.exclude( site_title__in=existing_workorders_site_titles )- Identifies sites that already have work orders to avoid duplication.
- Only sites without existing work orders are selected for processing.
-
Creating Work Orders:
- Parent Work Order:
project_ticket = ProjectWorkorderModel.objects.create( site_title=site_title, status='pending', created_by=user, pm=pm, work_order_id=parent_work_order_id, project_title=project_title, data=data_dict ) - Contractor Work Orders:
for contractor_type, domain in contractors.items(): # ... (logic to handle contractors) work_orders_to_create[domain].append( work_order_model_class( parent_work_order=project_ticket, work_order_id=work_order_id, contractor=company, extra_contractor=extra_contractor, status=wo_status, created_by=user ) )- Iterates through each contractor type, creates corresponding work orders, and appends them to the
work_orders_to_createdictionary for bulk creation.
- Iterates through each contractor type, creates corresponding work orders, and appends them to the
- Parent Work Order:
-
Bulk Creating Contractor Work Orders:
for domain, work_orders in work_orders_to_create.items(): if work_orders: work_order_model_class = { ... }[domain] work_order_model_class.objects.bulk_create(work_orders)- Performs bulk creation for each contractor domain to optimize database operations.
-
Updating Statuses:
project_sites_to_create.update(status='Done') total_sites_with_cpg = project_sites_to_process.count() done_sites = project_sites_to_process.filter(status='Done').count() overall_status = 'Done' if done_sites == total_sites_with_cpg else 'partially_done' project_sites_to_process.update(status=overall_status)- Sets the status of newly processed sites to
'Done'. - Determines the overall project status:
- If all relevant sites are
'Done', sets the overall status to'Done'. - Otherwise, sets it to
'partially_done'.
- If all relevant sites are
- Updates all relevant
ProjectSiteModelinstances to reflect the overall status.
- Sets the status of newly processed sites to
-
Error Handling:
- Comprehensive error handling ensures that any issues during processing are caught and appropriate error messages are returned to the client.
Notes & Recommendations:
-
Helper Functions:
- Ensure that
create_parent_work_order_idandcreate_contractor_work_order_idare defined and handle ID generation correctly.
- Ensure that
-
Optimizations:
- Consider using transactions to ensure atomicity. If any part of the operation fails, the entire transaction can be rolled back to maintain data integrity.
from django.db import transaction # Wrap the critical section with a transaction with transaction.atomic(): # Creation and updating logic -
Project-Level Status:
- If you have an overarching
Projectmodel, it might be beneficial to manage the project's status there instead of updating allProjectSiteModelinstances. This can provide a clearer representation of the project's state.
- If you have an overarching
-
Logging:
- Implement logging to track operations, especially for debugging purposes.
-
Validation:
- Additional validation can be added to ensure data consistency and integrity before performing database operations.
This implementation ensures that work orders are only created for sites that have been partially updated (i.e., have
cpg_data) and do not already have associated work orders. It also accurately reflects the project's status based on the completion of work orders across all relevant sites. -