Fix pep8 error for vfc-nfvo-lcm/jobs
[vfc/nfvo/lcm.git] / lcm / jobs / views.py
1 # Copyright 2016 ZTE Corporation.
2 #
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
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14 import logging
15
16 from rest_framework.response import Response
17 from rest_framework.views import APIView
18
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
22
23 logger = logging.getLogger(__name__)
24
25
26 class JobView(APIView):
27     def get(self, request, job_id):
28         response_id = ignore_case_get(request.META, 'responseId')
29         ret = GetJobInfoService(job_id, response_id).do_biz()
30         return Response(data=ret)
31
32     def post(self, request, job_id):
33         try:
34             logger.debug("Enter JobView:post, %s, %s ", job_id, request.data)
35             jobs = JobUtil.query_job_status(job_id)
36             if len(jobs) > 0 and jobs[-1].errcode == '255':
37                 return Response(data={'result': 'ok'})
38             progress = request.data.get('progress')
39             desc = request.data.get('desc', '%s' % progress)
40             errcode = '0' if request.data.get('errcode') in ('true', 'active') else '255'
41             logger.debug("errcode=%s", errcode)
42             JobUtil.add_job_status(job_id, progress, desc, error_code=errcode)
43             return Response(data={'result': 'ok'})
44         except Exception as e:
45             return Response(data={'result': 'error', 'msg': e.message})