def proxy(request):
    from deck import current_deck

    deck = current_deck(request)
    if deck['success']:
        decklist = []
        for card in deck['cards']:
            decklist.extend([card['id']]*card['quantity'])

    # Convert our list of card ids in decklist to a list of card models.
    cards = [Card.objects.filter(pk=cid)[0] for cid in decklist]

    return_value = {'cards': cards}
    return render(request, 'proxies.html', return_value)
def analyze_data_js(request):
    from analyze.analyzer import get_mana_curve, get_analyzer_test_deck_list
    from deck import current_deck

    jsonp_prefix = request.GET['jsonp']

    deck = current_deck(request)
    if deck['success']:
        decklist = []
        for card in deck['cards']:
            decklist.extend([card['id']]*card['quantity'])

    # Convert our list of card ids in decklist to a list of card models.
    cards = [Card.objects.filter(pk=cid)[0] for cid in decklist]

    mana_curve = get_mana_curve(cards)
    # Treat None as 0.
    if None in mana_curve:
        mana_curve[0] = mana_curve.pop(None)
    # Interpolate 0's into values for keys up to 9, or the maximum
    # individual card mana in the deck.
    mana_keys = mana_curve.keys()
    max_mana_interpolate = min([9] + [key for key in mana_keys if key > 9])
    for mana in xrange(0, max_mana_interpolate + 1):
        if mana not in mana_curve:
            mana_curve[mana] = 0
    # Transform from a dict to a sorted list.
    mana_curve = sorted(mana_curve.items())

    # Package it up for the client.
    mana_keys, mana_values = zip(*mana_curve) # unzip
    analysis_data_json = json.dumps(dict(
        mana_curve=mana_curve,
        mana_keys=mana_keys,
        mana_values=mana_values,
    ))

    jsonp_script = """
        $(function () {{
            {jsonp_prefix}({analysis_data_json});
        }});
    """.format(**locals())

    return HttpResponse(jsonp_script, mimetype='text/javascript')
def analyze(request):
    return_value = {}

    from analyze.analyzer import get_mana_curve
    from deck import current_deck

    deck = current_deck(request)
    if deck['success']:
        decklist = []
        for card in deck['cards']:
            decklist.extend([card['id']]*card['quantity'])

    # Convert our list of card ids in decklist to a list of card models.
    cards = [Card.objects.filter(pk=cid)[0] for cid in decklist]

    return_value['card_count'] = len(cards)
    return_value['mana_curve'] = get_mana_curve(cards)
    return_value['js_files'] = JAVASCRIPT_FILES
    return render(request, 'analyze.html', return_value)