from rest_framework.views import APIView
from api.models import Mission , User
from api.utils import *

class TaskSelectAPI(APIView):
        authentication_classes = []
        permission_classes = []

        def post(self , request):
            print(request.data.get('username'))
            user = User.objects.get(username = request.data.get('username'))
            tasks = []
            if(user.identity == 'admin'):
                tasklist = Mission.objects.all()
                for file in tasklist:
                    tasks.append({
                        "id": file.id,
                        "name": file.name,
                        "nodeFileId": file.nodeFile_id,
                        "createTime": file.create_time,
                        "updateTime": file.update_time,
                        "state": file.state,
                        "userId": file.user_id,
                        "edgeFileId": file.edgeFile_id 
                    })
            else:
                tasklist = Mission.objects.filter(user_id = user.id) 
                for file in tasklist:
                    tasks.append({
                        "id": file.id,
                        "name": file.name,
                        "nodeFileId": file.nodeFile_id,
                        "createTime": file.create_time,
                        "updateTime": file.update_time,
                        "userId": file.user_id,
                        "edgeFileId": file.edgeFile_id 
                    })
            return success(message = "任务数据查询成功" , 
                           data = tasks , code = 200)
    
