# Path: blockexplorer/settings.py
# BLOCKCYPHER_PUBLIC_KEY = '31c49f33f35c85a8f4d9845a754f7c8e'
# 
# BLOCKCYPHER_API_KEY = os.getenv('BLOCKCYPHER_API_KEY')
# 
# Path: homepage/forms.py
# class SearchForm(forms.Form):
#     search_string = forms.CharField(
#         required=True,
#         min_length=1,
#         max_length=128,
#     )
# 
#     coin_symbol = forms.ChoiceField(
#         required=False,
#         choices=COIN_CHOICES,
#     )
# 
#     def clean_search_string(self):
#         search_string = self.cleaned_data['search_string'].strip()
#         # process possible Wallet Names
#         if is_valid_wallet_name(search_string):
#             return search_string
# 
#         # get rid of non alphanumerics
#         if search_string.lower().startswith("0x"):
#             search_string = search_string[2:].lower()
#         search_string = re.sub(r'[^a-zA-Z0-9]+', '', search_string)
#         if is_valid_hash(search_string) or is_valid_address(search_string) or is_valid_eth_address(search_string) or is_valid_block_num(search_string):
#             return search_string
#         else:
#             err_msg = _('Not a valid address, transaction hash, or block number')
#             raise forms.ValidationError(err_msg)
# 
# class UnitChoiceForm(forms.Form):
#     unit_choice = forms.ChoiceField(
#         label='',
#         required=True,
#         choices=UNIT_CHOICES_DJANGO,
#         widget=forms.Select(attrs={'onchange': 'this.form.submit()'}),
#     )


from django.http import HttpResponseRedirect
from django.urls import reverse
from django.contrib import messages
from django.utils.translation import ugettext_lazy as _
from annoying.decorators import render_to
from blockexplorer.decorators import assert_valid_coin_symbol
from blockexplorer.settings import BLOCKCYPHER_PUBLIC_KEY, BLOCKCYPHER_API_KEY
from blockexplorer.walletname import lookup_wallet_name, is_valid_wallet_name
from homepage.forms import SearchForm, UnitChoiceForm
from blockcypher.api import get_transaction_details, get_block_overview, get_blocks_overview, get_latest_block_height, get_broadcast_transactions, get_blockchain_fee_estimates
from blockcypher.utils import is_valid_hash, is_valid_ethash_block_hash, is_valid_block_num, is_valid_sha_block_hash, is_valid_address, is_valid_eth_address
from blockcypher.constants import ETHASH_COINS, SHA_COINS, SCRYPT_COINS, COIN_SYMBOL_MAPPINGS
from operator import itemgetter
                    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

# Path: blockexplorer/settings.py
# BLOCKCYPHER_PUBLIC_KEY = '31c49f33f35c85a8f4d9845a754f7c8e'
# 
# BLOCKCYPHER_API_KEY = os.getenv('BLOCKCYPHER_API_KEY')
# 
# Path: homepage/forms.py
# class SearchForm(forms.Form):
#     search_string = forms.CharField(
#         required=True,
#         min_length=1,
#         max_length=128,
#     )
# 
#     coin_symbol = forms.ChoiceField(
#         required=False,
#         choices=COIN_CHOICES,
#     )
# 
#     def clean_search_string(self):
#         search_string = self.cleaned_data['search_string'].strip()
#         # process possible Wallet Names
#         if is_valid_wallet_name(search_string):
#             return search_string
# 
#         # get rid of non alphanumerics
#         if search_string.lower().startswith("0x"):
#             search_string = search_string[2:].lower()
#         search_string = re.sub(r'[^a-zA-Z0-9]+', '', search_string)
#         if is_valid_hash(search_string) or is_valid_address(search_string) or is_valid_eth_address(search_string) or is_valid_block_num(search_string):
#             return search_string
#         else:
#             err_msg = _('Not a valid address, transaction hash, or block number')
#             raise forms.ValidationError(err_msg)
# 
# class UnitChoiceForm(forms.Form):
#     unit_choice = forms.ChoiceField(
#         label='',
#         required=True,
#         choices=UNIT_CHOICES_DJANGO,
#         widget=forms.Select(attrs={'onchange': 'this.form.submit()'}),
#     )


# Path: homepage/views.py
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.contrib import messages
from django.utils.translation import ugettext_lazy as _
from annoying.decorators import render_to
from blockexplorer.decorators import assert_valid_coin_symbol
from blockexplorer.settings import BLOCKCYPHER_PUBLIC_KEY, BLOCKCYPHER_API_KEY
from blockexplorer.walletname import lookup_wallet_name, is_valid_wallet_name
from homepage.forms import SearchForm, UnitChoiceForm
from blockcypher.api import get_transaction_details, get_block_overview, get_blocks_overview, get_latest_block_height, get_broadcast_transactions, get_blockchain_fee_estimates
from blockcypher.utils import is_valid_hash, is_valid_ethash_block_hash, is_valid_block_num, is_valid_sha_block_hash, is_valid_address, is_valid_eth_address
from blockcypher.constants import ETHASH_COINS, SHA_COINS, SCRYPT_COINS, COIN_SYMBOL_MAPPINGS
from operator import itemgetter

                    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)