Beispiel #1
0
 def test_compat_properties(self):
     x = AsyncResult("1")
     self.assertEqual(x.task_id, x.id)
     x.task_id = "2"
     self.assertEqual(x.id, "2")
Beispiel #2
0
 def stop(self, message: str = None) -> None:
     if self.is_stoppable:
         task = AsyncResult(self.celery_task_id)
         task.revoke(terminate=True, signal='SIGKILL')
     self.on_stop(message=message)
Beispiel #3
0
from celery.result import AsyncResult
from celery_tasks.celery import cel

async_result = AsyncResult(id="583f00af-a6ad-40d7-a942-587a24562b79", app=cel)

if async_result.successful():
    result = async_result.get()
    print(result)
    # result.forget() # 将结果删除,执行完成,结果不会自动删除
    # async.revoke(terminate=True)  # 无论现在是什么时候,都要终止
    # async.revoke(terminate=False) # 如果任务还没有开始执行呢,那么就可以终止。
elif async_result.failed():
    print('执行失败')
elif async_result.status == 'PENDING':
    print('任务等待中被执行')
elif async_result.status == 'RETRY':
    print('任务异常后正在重试')
elif async_result.status == 'STARTED':
    print('任务已经开始被执行')
Beispiel #4
0
from tasks.worker import celery
from celery.result import AsyncResult
from os import environ

if __name__ == '__main__':
    with open(f'{environ["HOME"]}/tasks_ids', 'r') as ids_file:
        ids = ids_file.readline().split(',')
        for id in ids:
            ar = AsyncResult(id)
            if ar.status == 'FAILURE':
                exit(1)
Beispiel #5
0
    def trace_task(uuid, args, kwargs, request=None):
        # R      - is the possibly prepared return value.
        # I      - is the Info object.
        # T      - runtime
        # Rstr   - textual representation of return value
        # retval - is the always unmodified return value.
        # state  - is the resulting task state.

        # This function is very long because we've unrolled all the calls
        # for performance reasons, and because the function is so long
        # we want the main variables (I, and R) to stand out visually from the
        # the rest of the variables, so breaking PEP8 is worth it ;)
        R = I = T = Rstr = retval = state = None
        task_request = None
        time_start = monotonic()
        try:
            try:
                kwargs.items
            except AttributeError:
                raise InvalidTaskError(
                    'Task keyword arguments is not a mapping')

            task_request = Context(request or {},
                                   args=args,
                                   called_directly=False,
                                   kwargs=kwargs)

            redelivered = (task_request.delivery_info
                           and task_request.delivery_info.get(
                               'redelivered', False))
            if deduplicate_successful_tasks and redelivered:
                if task_request.id in successful_requests:
                    return trace_ok_t(R, I, T, Rstr)
                r = AsyncResult(task_request.id, app=app)

                try:
                    state = r.state
                except BackendGetMetaError:
                    pass
                else:
                    if state == SUCCESS:
                        info(
                            LOG_IGNORED, {
                                'id':
                                task_request.id,
                                'name':
                                get_task_name(task_request, name),
                                'description':
                                'Task already completed successfully.'
                            })
                        return trace_ok_t(R, I, T, Rstr)

            push_task(task)
            root_id = task_request.root_id or uuid
            task_priority = task_request.delivery_info.get('priority') if \
                inherit_parent_priority else None
            push_request(task_request)
            try:
                # -*- PRE -*-
                if prerun_receivers:
                    send_prerun(sender=task,
                                task_id=uuid,
                                task=task,
                                args=args,
                                kwargs=kwargs)
                loader_task_init(uuid, task)
                if track_started:
                    task.backend.store_result(
                        uuid,
                        {
                            'pid': pid,
                            'hostname': hostname
                        },
                        STARTED,
                        request=task_request,
                    )

                # -*- TRACE -*-
                try:
                    if task_before_start:
                        task_before_start(uuid, args, kwargs)

                    R = retval = fun(*args, **kwargs)
                    state = SUCCESS
                except Reject as exc:
                    I, R = Info(REJECTED, exc), ExceptionInfo(internal=True)
                    state, retval = I.state, I.retval
                    I.handle_reject(task, task_request)
                    traceback_clear(exc)
                except Ignore as exc:
                    I, R = Info(IGNORED, exc), ExceptionInfo(internal=True)
                    state, retval = I.state, I.retval
                    I.handle_ignore(task, task_request)
                    traceback_clear(exc)
                except Retry as exc:
                    I, R, state, retval = on_error(task_request,
                                                   exc,
                                                   uuid,
                                                   RETRY,
                                                   call_errbacks=False)
                    traceback_clear(exc)
                except Exception as exc:
                    I, R, state, retval = on_error(task_request, exc, uuid)
                    traceback_clear(exc)
                except BaseException:
                    raise
                else:
                    try:
                        # callback tasks must be applied before the result is
                        # stored, so that result.children is populated.

                        # groups are called inline and will store trail
                        # separately, so need to call them separately
                        # so that the trail's not added multiple times :(
                        # (Issue #1936)
                        callbacks = task.request.callbacks
                        if callbacks:
                            if len(task.request.callbacks) > 1:
                                sigs, groups = [], []
                                for sig in callbacks:
                                    sig = signature(sig, app=app)
                                    if isinstance(sig, group):
                                        groups.append(sig)
                                    else:
                                        sigs.append(sig)
                                for group_ in groups:
                                    group_.apply_async((retval, ),
                                                       parent_id=uuid,
                                                       root_id=root_id,
                                                       priority=task_priority)
                                if sigs:
                                    group(sigs, app=app).apply_async(
                                        (retval, ),
                                        parent_id=uuid,
                                        root_id=root_id,
                                        priority=task_priority)
                            else:
                                signature(callbacks[0], app=app).apply_async(
                                    (retval, ),
                                    parent_id=uuid,
                                    root_id=root_id,
                                    priority=task_priority)

                        # execute first task in chain
                        chain = task_request.chain
                        if chain:
                            _chsig = signature(chain.pop(), app=app)
                            _chsig.apply_async((retval, ),
                                               chain=chain,
                                               parent_id=uuid,
                                               root_id=root_id,
                                               priority=task_priority)
                        task.backend.mark_as_done(
                            uuid,
                            retval,
                            task_request,
                            publish_result,
                        )
                    except EncodeError as exc:
                        I, R, state, retval = on_error(task_request, exc, uuid)
                    else:
                        Rstr = saferepr(R, resultrepr_maxsize)
                        T = monotonic() - time_start
                        if task_on_success:
                            task_on_success(retval, uuid, args, kwargs)
                        if success_receivers:
                            send_success(sender=task, result=retval)
                        if _does_info:
                            info(
                                LOG_SUCCESS, {
                                    'id': uuid,
                                    'name': get_task_name(task_request, name),
                                    'return_value': Rstr,
                                    'runtime': T,
                                    'args': safe_repr(args),
                                    'kwargs': safe_repr(kwargs),
                                })

                # -* POST *-
                if state not in IGNORE_STATES:
                    if task_after_return:
                        task_after_return(
                            state,
                            retval,
                            uuid,
                            args,
                            kwargs,
                            None,
                        )
            finally:
                try:
                    if postrun_receivers:
                        send_postrun(sender=task,
                                     task_id=uuid,
                                     task=task,
                                     args=args,
                                     kwargs=kwargs,
                                     retval=retval,
                                     state=state)
                finally:
                    pop_task()
                    pop_request()
                    if not eager:
                        try:
                            task.backend.process_cleanup()
                            loader_cleanup()
                        except (KeyboardInterrupt, SystemExit, MemoryError):
                            raise
                        except Exception as exc:
                            logger.error('Process cleanup failed: %r',
                                         exc,
                                         exc_info=True)
        except MemoryError:
            raise
        except Exception as exc:
            _signal_internal_error(task, uuid, args, kwargs, request, exc)
            if eager:
                raise
            R = report_internal_error(task, exc)
            if task_request is not None:
                I, _, _, _ = on_error(task_request, exc, uuid)
        return trace_ok_t(R, I, T, Rstr)
# from test_nfs_task import TestNfsTask

test ="amqp://*****:*****@172.16.4.134:5672/cabbage_vhost"
app = Celery('cabbage',broker=test)
app.config_from_object("cabbage.cabbage_celery.celeryconfig")
# import celeryconfig
# app.config_from_object(celeryconfig)


for k,v in app.conf.items():
    print k,v
app.conf.update(CELERY_ROUTES = {     
                 'test_ic_task.TestIcTask': {'queue': 'test2', 'routing_key': 'test2'},     
#                  'product_list_crawler.ProductListCrawlerTask': {'queue': 'celery', 'routing_key': 'celery'}
                  })
# taskId = "de1d0b16-57b1-4128-87bc-3697f78ab6dc"

state = app.events.State()
print app.tasks()

# print state.tasks.get(taskId)

app.send_task("test_ic_task.TestIcTask",kwargs={"jobId"})

res =AsyncResult(taskId)
print res._get_task_meta()
print res.ready()
print res.result
print dir(res)
print res.task_name
Beispiel #7
0
def get_workflow_state():
    threading.Timer(3.0, get_workflow_state).start()
    print("Workflow_1 Status:")
    for task_id in reversed(Workflow_1_tasklist):
        result = AsyncResult(task_id, app=app)
        print({result.id, result.state})
Beispiel #8
0
        taskid = uuid()
        taskid_list.append(taskid)
        with open('dict/' + str(taskid), 'w') as f:
            for i in range(partStartIndex, partEndIndex):
                f.write(content[i])
        partStartIndex = partEndIndex
        partEndIndex = partEndIndex + words
        if partEndIndex > dictLength:
            partEndIndex = dictLength
            dictExceed = True

        files = {'file': open('dict/' + str(taskid), 'rb')}
        requests.post(upload_dict_url, files=files)
        os.remove('dict/' + str(taskid))

        result = fuzzer.delay(taskid, fuzzer_container, fuzzer_start,
                              fuzzer_options)
        result_id_list.append(result.id)

        if (dictExceed):
            break

    while not result.ready():
        time.sleep(1)

    for result_id in result_id_list:
        ares = AsyncResult(result_id, app=fuzzer)
        output = output + ares.get()

    print(output)
    #         if exited_workers_jobId in task_model_map:
    #             del_modelrunId = task_model_map[exited_workers_jobId]
    #             remove_model_run(del_modelrunId)
    #             print "\n REMOVED the model run details"

    #     except ValueError, e:
    #         pass      
    #     # if worker in new_workers:
    #     remove_container(container=worker)
    #     new_workers.remove(celery_worker)
    #     print "\n REMOVED*** the worker container: {}".format(worker)   


   # iterate over jobids to see which all jobs are finished
    for jobId in taskMap.copy():
        if AsyncResult(jobId).state == 'SUCCESS':
            worker_name = taskMap[jobId]
            taskMap.pop(jobId,0)
            if jobId in task_model_map:
                remove_model_run(task_model_map[jobId])
                print "\n REMOVED the model run details"
            if worker_name in new_workers:
                new_workers.remove(worker_name)
                worker_name = worker_name.split('@')[1]
                remove_container(container = worker_name)
                print "\nREMOVED the worker container: {}".format(worker_name),"\n"
            
            sleep(5)
            resp = get_job_status(jobId)
            print 'resp:',resp
            if resp != '':
Beispiel #10
0
 def get_task_result(self):
     """
     :return: celery AsyncResult
     :rtype: celery.result.AsyncResult
     """
     return AsyncResult(self.task_id)
Beispiel #11
0
def get_status(status_id):
    result = AsyncResult(status_id, app=parse)
    return jsonify(status=result.status, data=result.result)
Beispiel #12
0
from datetime import datetime
from celery.result import AsyncResult

from .celery import celery_app


celery_app.conf.update(
    task_routes={"tasks.*": "tasks", "users.*": "users", "mailing.*": "mailing"}
)

print("tasks.say_hi")
celery_app.send_task("tasks.say_hi", args=["Joe"])

print("tasks.talking_clock")
talking_clock_task: AsyncResult = celery_app.send_task("tasks.talking_clock")
talking_clock_task_result = AsyncResult(talking_clock_task.id, app=celery_app)
now: datetime = talking_clock_task_result.get()
print(f"result: {now}")

print("tasks.failing_task")
celery_app.send_task("tasks.failing_task")
Beispiel #13
0
 def __init__(self, task_id):
     self.task_id = task_id
     self.result = AsyncResult(task_id)
Beispiel #14
0
 def get_object(self):
     pk = self.kwargs.get('pk')
     return AsyncResult(pk)
Beispiel #15
0
 def test_without_id(self):
     with pytest.raises(ValueError):
         AsyncResult(None, app=self.app)
Beispiel #16
0
from proj.task import add, mul
from celery.app.control import Control
from proj.celery import app
from celery.result import AsyncResult

# 获取app队列信息
queue_info = app.connection().channel().queue_declare('proj', passive=True)
print('message count:', queue_info.message_count)
# 清空队列
app.connection().channel().queue_purge('proj')

result = add.delay(4, 4)
# 获取task id
print("task id: ", str(result.id))
# 获取task对象
task = AsyncResult(str(result.id))
# 获取task状态,进入开始执行状态
time.sleep(1)
print("task status: ", task.status)

celery_control = Control(app=app)
# 属于强制终止,后台会有报错信息
celery_control.revoke(str(result.id), terminate=True, signal='SIGKILL')
# 进入任务撤销状态
time.sleep(1)
print("task done: ", task.status)

# # 同步阻塞等待结果
# print("result: ", result.get(timeout=1))

# 参数签名
Beispiel #17
0
 def test_reduce_direct(self):
     x = AsyncResult('1', app=self.app)
     fun, args = x.__reduce__()
     assert fun(*args) == x
Beispiel #18
0
 def obj_get(self, bundle, **kwargs):
     task = AsyncResult(kwargs['pk'])
     return ReloadTask(kwargs['pk'], task.status, '{}'.format(task.info))
Beispiel #19
0
    def on_get(self, req, resp, task_id):

        task_result = AsyncResult(task_id)
        result = {'status': task_result.status, 'result': task_result.result}
        resp.status = falcon.HTTP_200
        resp.media = result
Beispiel #20
0
from celery.result import AsyncResult
from demo.celery import cel

async = AsyncResult(id='0c2a2115-f000-4655-b16d-213c0acc5607', app=cel)
print(async .status)
if async .successful():
    x = async .get()
    print(x)

else:
    print("未执行或执行错误")
Beispiel #21
0
 def get_deferred_result(cls, result_id):
     """
     Return the celery result for the given id.
     """
     return AsyncResult(result_id)
Beispiel #22
0
 def get_async_result(self):
     """Returns Celery AsyncResult object belonging to this Task."""
     return AsyncResult(id=str(self.uuid),
                        task_name=amcat_task.name,
                        app=app)
Beispiel #23
0
def is_task_successful(request, task_id):
    """Returns task execute status in JSON format."""
    return JsonResponse({'task': {
        'id': task_id,
        'executed': AsyncResult(task_id).successful(),
    }})
Beispiel #24
0
def similar_search_form(request):
    """ A view for similarity search """
    user_db = None
    flt_mw_min = None
    flt_mw_max = None
    flt_atom_num_min = None
    flt_atom_num_max = None
    flt_bond_num_min = None
    flt_bond_num_max = None
    flt_ring_num_min = None
    flt_ring_num_max = None
    flt_rotate_min = None
    flt_rotate_max = None
    flt_formal_min = None
    flt_formal_max = None
    flt_hba_min = None
    flt_hba_max = None
    flt_hbd_min = None
    flt_hbd_max = None
    flt_logP_min = None
    flt_logP_max = None
    cosmo_area_min = None
    cosmo_area_max = None
    cosmo_volume_min = None
    cosmo_volume_max = None
    dimensions_min = None
    dimensions_max = None
    elec_energy_min = None
    elec_energy_max = None
    homo_min = None
    homo_max = None
    lumo_min = None
    lumo_max = None
    total_energy_min = None
    total_energy_max = None

    if 'job' in request.GET:
        job_id = request.GET['job']
        job = AsyncResult(job_id)
        data = job.result
        query_session = SearchSession(request)
        query_stored = query_session.show()

        # Run when user is log-in
        if request.user.is_active:
            user_db = UserDataBase.objects.filter(user_id=request.user.id)

        context = {
            'data': data,
            'task_id': job_id,
            'query_similarity': query_stored['similarity_query'],
            'query_tanimoto': query_stored['tanimoto'],
            'user_db': user_db,
        }
        return render(request, 'result/similar_result_list.html', context)

    elif 'Similar_Search' in request.GET:
        query = request.GET['Similar_Search']
        category = request.GET['Category']
        fp_category = request.GET['fp_category']
        tanimoto = request.GET['tanimoto_min']
        max_result = request.GET['max_result']
        exclude_atoms = request.GET['exclude_atoms']

        try:
            if request.GET['molweight_min2']:
                flt_mw_min = request.GET['molweight_min2']
        except KeyError:
            pass

        try:
            if request.GET['molweight_max2']:
                flt_mw_max = request.GET['molweight_max2']
        except KeyError:
            pass

        try:
            if request.GET['AtomNum_min2']:
                flt_atom_num_min = request.GET['AtomNum_min2']
        except KeyError:
            pass

        try:
            if request.GET['AtomNum_max2']:
                flt_atom_num_max = request.GET['AtomNum_max2']
        except KeyError:
            pass

        try:
            if request.GET['BondNum_min2']:
                flt_bond_num_min = request.GET['BondNum_min2']
        except KeyError:
            pass

        try:
            if request.GET['BondNum_max2']:
                flt_bond_num_max = request.GET['BondNum_max2']
        except KeyError:
            pass

        try:
            if request.GET['ringnum_min2']:
                flt_ring_num_min = request.GET['ringnum_min2']
        except KeyError:
            pass

        try:
            if request.GET['ringnum_max2']:
                flt_ring_num_max = request.GET['ringnum_max2']
        except KeyError:
            pass

        try:
            if request.GET['Rotate_min2']:
                flt_rotate_min = request.GET['Rotate_min2']
        except KeyError:
            pass

        try:
            if request.GET['Rotate_max2']:
                flt_rotate_max = request.GET['Rotate_max2']
        except KeyError:
            pass

        try:
            if request.GET['Formal_min2']:
                flt_formal_min = request.GET['Formal_min2']
        except KeyError:
            pass

        try:
            if request.GET['Formal_max2']:
                flt_formal_max = request.GET['Formal_max2']
        except KeyError:
            pass

        try:
            if request.GET['HBA_min2']:
                flt_hba_min = request.GET['HBA_min2']
        except KeyError:
            pass

        try:
            if request.GET['HBA_max2']:
                flt_hba_max = request.GET['HBA_max2']
        except KeyError:
            pass

        try:
            if request.GET['HBD_min2']:
                flt_hbd_min = request.GET['HBD_min2']

        except KeyError:
            pass

        try:
            if request.GET['HBD_max2']:
                flt_hbd_max = request.GET['HBD_max2']
        except KeyError:
            pass

        try:
            if request.GET['LogP_min2']:
                flt_logP_min = request.GET['LogP_min2']
        except KeyError:
            pass

        try:
            if request.GET['LogP_max2']:
                flt_logP_max = request.GET['LogP_max2']
        except KeyError:
            pass

        try:
            if request.GET['cosmo_area_min']:
                cosmo_area_min = request.GET['cosmo_area_min']
        except KeyError:
            pass

        try:
            if request.GET['cosmo_area_max']:
                cosmo_area_max = request.GET['cosmo_area_max']
        except KeyError:
            pass

        try:
            if request.GET['cosmo_volume_min']:
                cosmo_volume_min = request.GET['cosmo_volume_min']
        except KeyError:
            pass

        try:
            if request.GET['cosmo_volume_max']:
                cosmo_volume_max = request.GET['cosmo_volume_max']
        except KeyError:
            pass

        try:
            if request.GET['dimensions_min']:
                dimensions_min = request.GET['dimensions_min']
        except KeyError:
            pass

        try:
            if request.GET['dimensions_max']:
                dimensions_max = request.GET['dimensions_max']
        except KeyError:
            pass

        try:
            if request.GET['elec_energy_min']:
                elec_energy_min = request.GET['elec_energy_min']
        except KeyError:
            pass

        try:
            if request.GET['elec_energy_max']:
                elec_energy_max = request.GET['elec_energy_max']
        except KeyError:
            pass

        try:
            if request.GET['homo_min']:
                homo_min = request.GET['homo_min']
        except KeyError:
            pass

        try:
            if request.GET['homo_max']:
                homo_max = request.GET['homo_max']
        except KeyError:
            pass

        try:
            if request.GET['lumo_min']:
                lumo_min = request.GET['lumo_min']
        except KeyError:
            pass

        try:
            if request.GET['lumo_max']:
                lumo_max = request.GET['lumo_max']
        except KeyError:
            pass

        try:
            if request.GET['total_energy_min']:
                total_energy_min = request.GET['total_energy_min']
        except KeyError:
            pass

        try:
            if request.GET['total_energy_max']:
                total_energy_max = request.GET['total_energy_max']
        except KeyError:
            pass

        job = similar_search.delay(category,
                                   fp_category,
                                   tanimoto,
                                   max_result,
                                   query,
                                   exclude_atoms,
                                   mw_min=flt_mw_min,
                                   mw_max=flt_mw_max,
                                   atom_min=flt_atom_num_min,
                                   atom_max=flt_atom_num_max,
                                   bond_min=flt_bond_num_min,
                                   bond_max=flt_bond_num_max,
                                   ring_min=flt_ring_num_min,
                                   ring_max=flt_ring_num_max,
                                   rotate_min=flt_rotate_min,
                                   rotate_max=flt_rotate_max,
                                   formal_min=flt_formal_min,
                                   formal_max=flt_formal_max,
                                   hba_min=flt_hba_min,
                                   hba_max=flt_hba_max,
                                   hbd_min=flt_hbd_min,
                                   hbd_max=flt_hbd_max,
                                   logP_min=flt_logP_min,
                                   logP_max=flt_logP_max,
                                   cosmo_area_min=cosmo_area_min,
                                   cosmo_area_max=cosmo_area_max,
                                   cosmo_volume_min=cosmo_volume_min,
                                   cosmo_volume_max=cosmo_volume_max,
                                   dimensions_min=dimensions_min,
                                   dimensions_max=dimensions_max,
                                   elec_energy_min=elec_energy_min,
                                   elec_energy_max=elec_energy_max,
                                   homo_min=homo_min,
                                   homo_max=homo_max,
                                   lumo_min=lumo_min,
                                   lumo_max=lumo_max,
                                   total_energy_min=total_energy_min,
                                   total_energy_max=total_energy_max)

        # Store query to session
        query_session = SearchSession(request)
        query_session.get_similarity_search_query(job.id, query, tanimoto)

        return HttpResponseRedirect(reverse('C3DB:similar') + '?job=' + job.id)

    else:
        # Reset the Session
        query_session = SearchSession(request)
        query_stored = query_session.show()

        # If user get back to search ui during the job searchin, terminate the job.
        try:
            if query_stored['task_id']:
                app.control.revoke(
                    str(query_stored['task_id']), terminate=True
                )  # => This line only works on UINX type OS !!!
                query_session.clear()
                return HttpResponseRedirect(reverse('C3DB:similar'))

        except KeyError as err:
            print err
            pass

        query_session.clear()

        form = SimilaritySearchForm()
        categories = Category.objects.all()
        fp_categories = fpCategory.objects.all()
        context = {
            'form': form,
            'categories': categories,
            'fp_categories': fp_categories
        }
        return render(request, 'search/similar_search.html', context)
Beispiel #25
0
 def on_time(self, task_id):
     revoke(task_id)
     result = AsyncResult(task_id, app=self.application.celery_app)
     self.write({'task-id': task_id, 'state': result.state})
     self.finish()
Beispiel #26
0
def get_task(task_id):
    """
    task_id의 결과를 얻는다.
    """
    return AsyncResult(task_id, app=celery)
Beispiel #27
0
def index(request):
    """
    index page, user uploads a file and this spawns the task to process the upload
    """
    message = ''
    form = UploadFileForm()

    if 'job' in request.GET:
        """
        If we have a GET request and 'job' in the request then we show the user the status of the job
        """
        task_id = request.GET['job']
        task = AsyncResult(task_id)
        context = {
            'result': task.result,
            'state': task.state,
            'task_id': task_id
        }
        return render(request, 'xmlconversion_app/show_result.html', context)

    elif 'file' in request.GET:
        """
        If we have a GET request and 'file' in the request then we return the zip file to the user
        """
        task_id = request.GET['file']
        task = AsyncResult(task_id)
        if task.result:
            file_and_path = os.path.join(settings.OUTPUT_LOCATION, task.result)
            if ('/' not in task.result) and os.path.isfile(file_and_path):
                response = HttpResponse(content_type='application/zip')
                response[
                    'Content-Disposition'] = 'attachment; filename=' + task.result
                response.write(open(file_and_path, 'rb').read())
                return response
            else:
                message = 'There was a problem with the file download'
        else:
            message = 'There was a problem with the task id'

    elif request.POST.get('upload'):
        """
        If we have a POST request and 'upload' in the request then we handle the file upload form 
        """
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            xmlfile = request.FILES['xmlfile']
            if validate_xml(xmlfile.read()):
                tempdir = tempfile.mkdtemp()
                with open(os.path.join(tempdir, xmlfile.name),
                          'wb+') as destination:
                    for chunk in xmlfile.chunks():
                        destination.write(chunk)
                task = xmlconversion.delay(xmlfile.name, tempdir)
                return HttpResponseRedirect('?job=' + task.id)
            else:
                message = 'The uploaded file is not valid XML'
        else:
            message = 'There was a problem with the file upload'

    # If we had an error above, then 'message' will be set and we show the message and the form
    # If the request was not one of the handled POST or GETs then show the form and 'message' is an empty string
    return render(request, 'xmlconversion_app/index.html', {
        'form': form,
        'message': message
    })
Beispiel #28
0
 def __init__(self, job_id=None, async_result=None):
     if async_result:
         self._async_result = async_result
     else:
         self._async_result = AsyncResult(job_id, app=celery)
Beispiel #29
0
def download_gradebook(req, teacher, results=None):
    """
    Download the wanted gradebook.

    Parameters
    ----------
    req : HttpRequest
        Request with:
            parameters:
                task_id: str
                    Id of the celery task responsible for the gradebook
                    generation sent with the first request for a gradebook
    teacher : Teacher
        Teacher instance returned by `teacher_required` (not used)
    results : Optional[Dict[str, Any]]
        Either
            If group gradebook
                {
                    group: str
                        Title of the group
                    assignments: List[str]
                        Assignment identifier
                    school_id_needed: bool
                        If a school id is needed
                    results: [{
                        school_id: Optional[str]
                            School id if needed
                        email: str
                            Student email
                        assignments: [{
                            n_completed: Optional[int]
                                Number of completed questions
                            n_correct: Optional[int]
                                Number of correct questions
                        }]
                    }]
                }
            If assignment gradebook
                {
                    group: str
                        Title of the group
                    assignment: str
                        Title of the assignment
                    questions: List[str]
                        Question title
                    school_id_needed: bool
                        If a school id is needed
                    results: [{
                        school_id: Optional[str]
                            School id if needed
                        email: str
                            Student email
                        questions: List[Optional[float]]
                            Grade for each question
                    }]
                }

    Returns
    -------
    StreamingHttpResponse
        csv file with the gradebook results
    """
    if results is None:
        args = get_json_params(req, args=["task_id"])
        if isinstance(args, HttpResponse):
            return args
        (task_id, ), _ = args

        result = AsyncResult(task_id)

        try:
            if not result.ready():
                return response_400(
                    req,
                    msg="The gradebook isn't ready.",
                    logger_msg="Not completed gradebook {}".format(task_id) +
                    " accessed by teacher {}".format(teacher.user.username),
                )
        except AttributeError:
            return response_500(
                req,
                msg="There is no gradebook corresponding to this url. "
                "Please ask for a new one.",
                logger_msg="Celery error getting gradebook"
                " for teacher {}".format(teacher.user.username) +
                " and task {}.".format(task_id),
                log=logger.warning,
                use_template=False,
            )

        results = result.result

        if RunningTask.objects.filter(id=task_id):
            RunningTask.objects.get(id=task_id).delete()

    if "assignment" in results:
        filename = "myDALITE_gradebook_{}_{}.csv".format(
            results["group"], results["assignment"])
    else:
        filename = "myDALITE_gradebook_{}.csv".format(results["group"])
    gradebook_gen = convert_gradebook_to_csv(results)
    data = chain(iter((filename + "\n", )), gradebook_gen)
    resp = StreamingHttpResponse(data, content_type="text/csv")
    return resp
Beispiel #30
0
 def test_TaskSetResult(self):
     x = TaskSetResult(uuid(), [AsyncResult(uuid()) for _ in range(10)])
     self.assertEqual(x, from_serializable(x.serializable()))
     self.assertEqual(x, from_serializable(x))