Esempio n. 1
0
def poll_state(request):
    """
    A view to report the progress to the user
    """

    if 'job' in request.GET:
        job_id = request.GET['job']
    else:
        return HttpResponse('No job id given.')

    job = AsyncResult(job_id)
    data = job.result or job.state
    return HttpResponse(Cube.serialize(data), mimetype='application/json')
Esempio n. 2
0
def save_cube_from_file_path(fpath):
    file_name = os.path.basename(fpath)

    try:
        if not os.path.exists(fpath):
            raise CommandError("{} does not exist".format(fpath))
        cube = Cube.objects.get(name=file_name)
    except Cube.DoesNotExist:
        cube = Cube(name=file_name)
        cube.save()

    with open(fpath, "r") as cube_file:
        cards = get_cards_from_names(*[line for line in cube_file.read().splitlines() if line])

    cube.cards = cards
    cube.save()

    return cube
Esempio n. 3
0
def card_contents(request):
    """
    The expect contents of this request is = {
        'card_names': ['card_name', 'card_name'*] # duplicates allowed
    }

    response = {
        'cards': [{serialized card}, {serialized card}*], # duplicates allowed
        'insert_job': 'job_id' # to get the results of async insert
    }
    """
    if not request.is_ajax():
        raise Http404

    try:
        all_card_names = json.loads(request.body)['card_names']
    except ValueError:
        raise ValueError("problem with %r" % request.body)

    fetched_cards, names_to_insert = retrieve_cards_from_names(all_card_names)

    response = {
        'cards': {c.name: c.as_dict() for c in fetched_cards},
        'mismatches': Card.get_mismatched_names_map()
    }

    if names_to_insert:
        insert_job = async_get_cards.delay(names_to_insert,
                                           settings.REDIS_INFORMATION)
        response['insert_job'] = insert_job.id


    return HttpResponse(
        Cube.serialize(response),
        mimetype="application/json"
    )