from django.shortcuts import render from django.http import HttpResponse, HttpResponseNotFound from django.views.decorators.csrf import csrf_exempt from .calls import * from django.conf import settings from monitorT.task import KillingThread import json import re # Create your views here. @csrf_exempt def wrapper(request, service_str, **kwargs): if request.method == 'POST': data = request.body.decode('utf8') received_json_data = json.loads(u'{}'.format(data)) try: service = eval('{}.post'.format(service_str)) except AttributeError: return HttpResponseNotFound('Service do not exists') try: task_class = eval('{}.TaskCustom'.format(service_str)) except AttributeError: task_class = None task_obj = settings.MASTER_TASK_LIST.add_task(received_json_data['user'], service, received_json_data, task_class) t = KillingThread(target=task_obj.run, name=task_obj.task_id) t.start() if task_obj.CONTROL: result = {'task_id': task_obj.task_id} else: t.join() result = task_obj.result settings.MASTER_TASK_LIST.remove_task(task_obj.task_id) json_result = json.dumps(result) return HttpResponse(json_result) elif request.method == 'GET': try: service = eval('{}.get'.format(service_str)) except AttributeError: return HttpResponseNotFound('Service do not exists') result = service() return HttpResponse(result)