1 # Copyright 2016 ZTE Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
16 from rest_framework.response import Response
17 from rest_framework.views import APIView
19 from lcm.jobs.job_get import GetJobInfoService
20 from lcm.pub.utils.jobutil import JobUtil
21 from lcm.pub.utils.values import ignore_case_get
23 logger = logging.getLogger(__name__)
25 class JobView(APIView):
26 def get(self, request, job_id):
27 response_id = ignore_case_get(request.META, 'responseId')
28 ret = GetJobInfoService(job_id, response_id).do_biz()
29 return Response(data=ret)
31 def post(self, request, job_id):
33 logger.debug("Enter JobView:post, %s, %s ", job_id, request.data)
34 jobs = JobUtil.query_job_status(job_id)
35 if len(jobs) > 0 and jobs[-1].errcode == '255':
36 return Response(data={'result': 'ok'})
37 progress = request.data.get('progress')
38 desc = request.data.get('desc', '%s' % progress)
39 errcode = '0' if request.data.get('errcode') in ('true', 'active') else '255'
40 logger.debug("errcode=%s", errcode)
41 JobUtil.add_job_status(job_id, progress, desc, error_code=errcode)
42 return Response(data={'result': 'ok'})
43 except Exception as e:
44 return Response(data={'result': 'error', 'msg': e.message})