Exemplo n.º 1
0
 def assign_handwerker(request):
     response = {}
     try:
         component_id = request.POST.get('component_id')
         user_id = request.POST.get('user_id')
         BuildingComponents.objects.filter(id=component_id).update(assign_to_id=user_id, assigned_by=request.user)
         component = BuildingComponents.objects.get(id=component_id)
         # handworker = Users.objects.get(id=user_id)
         if component.flat:
             task = Tasks.objects.filter(building_component__component__parent_id=component.component_id).first()
         else:
             task = Tasks.objects.filter(building_component__building_id=component.building_id,
                                         building_component__flat__isnull=True).filter(Q(
                 Q(building_component__component__parent_id=component.component_id) | Q(
                     building_component__component_id=component.component_id))).first()
         task.save()
         handworker = HandWorker.objects.get(user_id=user_id)
         # Send Notification
         message = NotificationText.get_assign_worker_notification_text(request.user.get_full_name(), handworker.company_name,
                                                                        component.component.name)
         NotificationsView.create_notfication(request, 'assign_worker', message, task.id, request.user.id)
         handworker_info = {
             "fullname": handworker.company_name,
             "avatar": handworker.user.avatar.url if handworker.user.avatar else ''
         }
         response['success'] = True
         response['handworker'] = handworker_info
     except Exception as e:
         LogHelper.efail(e)
         response['success'] = False
         response['message'] = "Something went wrong. Please try again"
     return HttpResponse(json.dumps(response), content_type='application/json')
Exemplo n.º 2
0
 def change_task_due_date(request, task_id):
     try:
         with transaction.atomic():
             response = {}
             if not request.user.is_staff:
                 raise ModuleNotFoundError
             due_date = request.data.pop("due_date", '')
             task = Tasks.objects.get(id=task_id)
             task.due_date = due_date
             task.save()
             # Send Notification
             message = NotificationText.get_change_due_date_notification_text(
                 request.user.get_full_name(),
                 task.building_component.component.name, due_date)
             task_thread = threading.Thread(
                 target=NotificationsView.create_notfication,
                 args=(request, 'change_due_date', message, task_id,
                       request.user.id))
             task_thread.start()
             response['success'] = True
             response['message'] = "Deadline Update successfully"
             return Response(response, status=status.HTTP_200_OK)
     except Exception as e:
         LogHelper.efail(e)
         return Response(
             {
                 'status': False,
                 'message': "Something went wrong."
             },
             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Exemplo n.º 3
0
    def change_task_status(request, task_id):
        try:
            with transaction.atomic():
                response = {}
                task_status = request.data.pop("status", '')
                task = Tasks.objects.get(id=task_id)
                task.status = task_status
                task.save()
                # Send Notification
                if request.user.is_staff:
                    user_name = request.user.get_full_name()
                else:
                    user_name = request.user.handworker.company_name
                message = NotificationText.get_change_task_status_notification_text(
                    user_name, task.building_component.component.name,
                    task.status)
                task_thread = threading.Thread(
                    target=NotificationsView.create_notfication,
                    args=(request, 'change_task_status', message, task_id,
                          request.user.id))
                task_thread.start()

                response['success'] = True
                response['message'] = "Task status changed successfully"
                return Response(response, status=status.HTTP_200_OK)
        except Exception as e:
            LogHelper.efail(e)
            return Response(
                {
                    'status': False,
                    'message': "Something went wrong."
                },
                status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Exemplo n.º 4
0
 def add_new_comment(request, *args, **kwargs):
     task_id = kwargs['task_id']
     try:
         comment = request.POST.get('task_comment_text')
         files = request.FILES.getlist('task_comment_files')
         file_list = []
         dir = os.path.join(settings.MEDIA_ROOT, "comments")
         if not os.path.exists(dir):
             os.makedirs(dir)
         for file in files:
             uploaded_file = CommonView.handle_uploaded_file(request, file)
             if uploaded_file != "":
                 file_list.append(uploaded_file)
         if comment != '' or len(file_list) > 0:
             comment_form = {
                 "text": comment,
                 "file_type": file_list if(len(file_list) > 0) else None,
                 "task_id": task_id,
                 "user": request.user,
                 "type": "text"
             }
             new_comment = Comments(**comment_form)
             new_comment.save()
             task = Tasks.objects.get(id=task_id)
             task.save()
             if comment != '':
                 # Send Notification
                 message = NotificationText.get_task_comment_notification_text(request.user.get_full_name(),
                                                                                  task.building_component.component.name)
                 NotificationsView.create_notfication(request, 'task_comment', message, task_id, request.user.id)
             if len(file_list) > 0:
                 # Send Notification
                 message = NotificationText.get_attach_file_notification_text(request.user.get_full_name(),
                                                                               task.building_component.component.name)
                 NotificationsView.create_notfication(request, 'attach_file', message, task_id, request.user.id)
     except Exception as e:
         LogHelper.efail(e)
     return HttpResponseRedirect('/tasks/'+str(task_id)+'/')
Exemplo n.º 5
0
 def change_task_status(request):
     response = {}
     try:
         task_id = request.POST.get('task_id')
         status = request.POST.get('status')
         task = Tasks.objects.get(id=task_id)
         task.status = status
         task.save()
         # Send Notification
         message = NotificationText.get_change_task_status_notification_text(request.user.get_full_name(),
                                                                    task.building_component.component.name, task.status)
         NotificationsView.create_notfication(request, 'change_task_status', message, task_id, request.user.id)
         response['success'] = True
         response['message'] = "Status Update successfully"
     except Exception as e:
         LogHelper.efail(e)
         response['success'] = False
         response['message'] = "Something went wrong. Please try again"
     return HttpResponse(json.dumps(response), content_type='application/json')
Exemplo n.º 6
0
 def post(self, request, **kwargs):
     try:
         task_id = kwargs['task_id']
         comment = ''
         file_list = []
         dir = os.path.join(settings.MEDIA_ROOT, "comments")
         if not os.path.exists(dir):
             os.makedirs(dir)
         if 'files' in request.data:
             files = request.data.getlist('files')
             for file in files:
                 uploaded_file = CommonView.handle_uploaded_file(
                     request, file)
                 if uploaded_file != "":
                     file_list.append(uploaded_file)
         if 'text' in request.data:
             comment = request.data["text"]
         if comment != '' or len(file_list) > 0:
             comment_form = {
                 "text": comment,
                 "file_type": file_list if (len(file_list) > 0) else None,
                 "task_id": task_id,
                 "user": request.user,
                 "type": "text"
             }
             new_comment = Comments(**comment_form)
             new_comment.save()
             task = Tasks.objects.get(id=task_id)
             task.save()
             if request.user.is_staff:
                 user_name = request.user.get_full_name()
             else:
                 user_name = request.user.handworker.company_name
             if comment != '':
                 # Send Notification
                 message = NotificationText.get_task_comment_notification_text(
                     user_name, task.building_component.component.name)
                 task_thread = threading.Thread(
                     target=NotificationsView.create_notfication,
                     args=(request, 'task_comment', message, task_id,
                           request.user.id))
                 task_thread.start()
             if len(file_list) > 0:
                 # Send Notification
                 message = NotificationText.get_attach_file_notification_text(
                     user_name, task.building_component.component.name)
                 task_thread = threading.Thread(
                     target=NotificationsView.create_notfication,
                     args=(request, 'attach_file', message, task_id,
                           request.user.id))
                 task_thread.start()
             paginator = PageNumberPagination()
             paginator.page_size = 10
             comments = Comments.objects.filter(
                 task_id=task_id).order_by('-created_at')
             result_page = paginator.paginate_queryset(comments, request)
             serializer = CommentSerializer(result_page, many=True)
             return paginator.get_paginated_response(data=serializer.data)
         else:
             return Response(
                 {
                     'success': False,
                     'message': "Something went wrong."
                 },
                 status=status.HTTP_404_NOT_FOUND)
     except Exception as e:
         LogHelper.efail(e)
         return Response(
             {
                 'success': False,
                 'message': "Something went wrong."
             },
             status=status.HTTP_500_INTERNAL_SERVER_ERROR)