示例#1
0
def home(request):
    form = SearchForm(
        initial={
            'search_string': '16Fg2yjwrbtC6fZp61EV9mNVKmwCzGasw5',
            'coin_symbol': 'btc',
        })
    if request.method == 'POST':
        form = SearchForm(data=request.POST)
        if form.is_valid():
            redirect_url = None
            search_string = form.cleaned_data['search_string']
            coin_symbol = form.cleaned_data['coin_symbol']
            kwargs = {'coin_symbol': coin_symbol}
            if is_valid_block_num(search_string):
                kwargs['block_representation'] = search_string
                redirect_url = reverse('block_overview', kwargs=kwargs)
            elif is_valid_hash(search_string):
                if coin_symbol in SHA_COINS:
                    if is_valid_sha_block_hash(search_string):
                        kwargs['block_representation'] = search_string
                        redirect_url = reverse('block_overview', kwargs=kwargs)
                    else:
                        kwargs['tx_hash'] = search_string
                        redirect_url = reverse('transaction_overview',
                                               kwargs=kwargs)
                elif coin_symbol in SCRYPT_COINS:
                    # Try to see if it's a valid TX hash
                    tx_details = get_transaction_details(
                        tx_hash=search_string,
                        coin_symbol=coin_symbol,
                        limit=1,
                        api_key=BLOCKCYPHER_API_KEY,
                    )
                    if 'error' in tx_details:
                        # Not a valid TX hash, see if it's a block hash by checking blockchain
                        block_details = get_block_overview(
                            block_representation=search_string,
                            coin_symbol=coin_symbol,
                            txn_limit=1,
                            api_key=BLOCKCYPHER_API_KEY,
                        )
                        if 'error' in block_details:
                            msg = _(
                                "Sorry, '%(search_string)s' is not a valid transaction or block hash for %(currency)s"
                                % {
                                    'currency': coin_symbol,
                                    'search_string': search_string,
                                })
                            messages.error(request, msg)
                        else:
                            kwargs['block_representation'] = search_string
                            redirect_url = reverse('block_overview',
                                                   kwargs=kwargs)
                    else:
                        kwargs['tx_hash'] = search_string
                        redirect_url = reverse('transaction_overview',
                                               kwargs=kwargs)

            elif is_valid_address(search_string):
                # It's an address
                kwargs['address'] = search_string
                first_char = search_string[0]
                # Override coin_symbol if we can infer it from the blockchain
                # There is now generic constants in the python library (constants.py)
                # Not migrating because this is custom (those constants have overlap/ambiguity)
                if first_char in ('1', ):
                    # Do not force addresses starting with 3 to be BTC because that's also used by LTC
                    kwargs['coin_symbol'] = 'btc'
                elif first_char in ('m', 'n', '2'):
                    # Note that addresses starting in 2 can be LTC testnet, but since we don't support that it's okay to include
                    kwargs['coin_symbol'] = 'btc-testnet'
                elif first_char in ('9', 'A'):
                    kwargs['coin_symbol'] = 'doge'
                elif first_char in ('L', ):
                    # Do not force addresses starting with 3 to be LTC because that's also used by BTC
                    kwargs['coin_symbol'] = 'ltc'
                elif first_char in ('B', 'C'):
                    kwargs['coin_symbol'] = 'bcy'

                redirect_url = reverse('address_overview', kwargs=kwargs)

            elif is_valid_wallet_name(search_string):
                addr = lookup_wallet_name(search_string, kwargs['coin_symbol'])
                if addr:
                    kwargs['address'] = addr
                    kwargs['wallet_name'] = search_string
                    redirect_url = reverse('address_overview', kwargs=kwargs)
                else:
                    msg = _("Sorry, that's not a valid wallet name")
                    messages.error(request, msg)

            if redirect_url:
                return HttpResponseRedirect(redirect_url)

        else:
            currency = COIN_SYMBOL_MAPPINGS[
                request.POST['coin_symbol']]['display_shortname']
            msg = _(
                "Sorry, '%(search_string)s' is not a valid %(currency)s address, wallet name, transaction or block"
                % {
                    'currency': currency,
                    'search_string': request.POST['search_string'],
                })
            messages.error(request, msg)

    return {'is_home': True, 'form': form}
示例#2
0
文件: views.py 项目: 5mil/explorer
def home(request):
    form = SearchForm(initial={
        'search_string': '16Fg2yjwrbtC6fZp61EV9mNVKmwCzGasw5',
        'coin_symbol': 'btc',
        })
    if request.method == 'POST':
        form = SearchForm(data=request.POST)
        if form.is_valid():
            redirect_url = None
            search_string = form.cleaned_data['search_string']
            coin_symbol = form.cleaned_data['coin_symbol']
            kwargs = {'coin_symbol': coin_symbol}
            if is_valid_block_num(search_string):
                kwargs['block_representation'] = search_string
                redirect_url = reverse('block_overview', kwargs=kwargs)
            elif is_valid_hash(search_string):
                if coin_symbol in SHA_COINS:
                    if is_valid_sha_block_hash(search_string):
                        kwargs['block_representation'] = search_string
                        redirect_url = reverse('block_overview', kwargs=kwargs)
                    else:
                        kwargs['tx_hash'] = search_string
                        redirect_url = reverse('transaction_overview', kwargs=kwargs)
                elif coin_symbol in SCRYPT_COINS:
                    # Try to see if it's a valid TX hash
                    tx_details = get_transaction_details(
                            tx_hash=search_string,
                            coin_symbol=coin_symbol,
                            limit=1,
                            api_key=BLOCKCYPHER_API_KEY,
                            )
                    if 'error' in tx_details:
                        # Not a valid TX hash, see if it's a block hash by checking blockchain
                        block_details = get_block_overview(
                                block_representation=search_string,
                                coin_symbol=coin_symbol,
                                txn_limit=1,
                                api_key=BLOCKCYPHER_API_KEY,
                                )
                        if 'error' in block_details:
                            msg = _("Sorry, that's not a valid transaction or block hash for %(currency)s" % {'currency': coin_symbol})
                            messages.error(request, msg)
                        else:
                            kwargs['block_representation'] = search_string
                            redirect_url = reverse('block_overview', kwargs=kwargs)
                    else:
                        kwargs['tx_hash'] = search_string
                        redirect_url = reverse('transaction_overview', kwargs=kwargs)

            elif is_valid_address(search_string):
                # It's an address
                kwargs['address'] = search_string
                first_char = search_string[0]
                # Override coin_symbol if we can infer it from the blockchain
                # There is now generic constants in the python library (constants.py)
                # Not migrating because this is custom (those constants have overlap/ambiguity)
                if first_char in ('1', ):
                    # Do not force addresses starting with 3 to be BTC because that's also used by LTC
                    kwargs['coin_symbol'] = 'btc'
                elif first_char in ('m', 'n', '2'):
                    # Note that addresses starting in 2 can be LTC testnet, but since we don't support that it's okay to include
                    kwargs['coin_symbol'] = 'btc-testnet'
                elif first_char in ('9', 'A'):
                    kwargs['coin_symbol'] = 'doge'
                elif first_char in ('L', ):
                    # Do not force addresses starting with 3 to be LTC because that's also used by BTC
                    kwargs['coin_symbol'] = 'ltc'
                elif first_char in ('U', ):
                    kwargs['coin_symbol'] = 'uro'
                elif first_char in ('B', 'C'):
                    kwargs['coin_symbol'] = 'bcy'

                redirect_url = reverse('address_overview', kwargs=kwargs)

            elif is_valid_wallet_name(search_string):
                addr = lookup_wallet_name(search_string, kwargs['coin_symbol'])
                if addr:
                    kwargs['address'] = addr
                    kwargs['wallet_name'] = search_string
                    redirect_url = reverse('address_overview', kwargs=kwargs)
                else:
                    msg = _("Sorry, that's not a valid wallet name")
                    messages.error(request, msg)

            if redirect_url:
                return HttpResponseRedirect(redirect_url)

        else:
            currency = COIN_SYMBOL_MAPPINGS[request.POST['coin_symbol']]['display_shortname']
            msg = _("Sorry, that's not a valid %(currency)s address, wallet name, transaction or block" % {
                'currency': currency})
            messages.error(request, msg)

    return {
        'is_home': True,
        'form': form
    }