# Path: blockexplorer/settings.py
# BLOCKCYPHER_API_KEY = os.getenv('BLOCKCYPHER_API_KEY')
# 
# Path: metadata/forms.py
# class BaseMetadataForm(forms.Form):
#     '''
#     Used for uploading public metadata
#     '''
# 
#     metadata_key = forms.CharField(
#         label=_('Metadata Key'),
#         required=True,
#         widget=forms.TextInput(attrs={'class': 'input-lg', 'autofocus': 'autofocus'}),
#         min_length=1,
#         max_length=256,
#         )
# 
#     metadata_value = forms.CharField(
#         label=_('Metadata Value'),
#         required=True,
#         widget=forms.TextInput(attrs={'class': 'input-lg'}),
#         min_length=1,
#         max_length=512,
#         )
# 
#     def __init__(self, *args, **kwargs):
#         self.helper = FormHelper()
#         self.helper.form_method = 'post'
#         self.helper.layout = Layout(
#                 'metadata_key',
#                 'metadata_value',
#                 Submit(
#                     'submit',
#                     'Upload Metadata',
#                     css_class='btn btn-primary btn-lg',
#                     ),
#                 )
#         super(BaseMetadataForm, self).__init__(*args, **kwargs)


from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.core.serializers.json import DjangoJSONEncoder
from django.urls import reverse
from django.contrib import messages
from django.contrib.humanize.templatetags.humanize import intcomma
from django.utils.translation import ugettext_lazy as _
from annoying.decorators import render_to
from blockexplorer.settings import BLOCKCYPHER_API_KEY
from blockexplorer.decorators import assert_valid_coin_symbol
from metadata.forms import BaseMetadataForm
from blockcypher.api import get_metadata, put_metadata, get_latest_block_hash, get_block_overview
from blockcypher.utils import is_valid_address_for_coinsymbol, is_valid_hash
from blockcypher.constants import COIN_SYMBOL_MAPPINGS
from random import choice
import json
                coin_symbol=coin_symbol,
                )
    elif identifier_type == 'block':
        metadata = get_metadata(
                block_hash=identifier,
                api_key=BLOCKCYPHER_API_KEY,
                private=True,
                coin_symbol=coin_symbol,
                )
    else:
        # shouldn't be possible
        raise(Http404)

    json_response = json.dumps(
            {'metadata': metadata},
            cls=DjangoJSONEncoder,
            sort_keys=True,
            )

    return HttpResponse(json_response, content_type='application/json')


@render_to('add_metadata.html')
def add_metadata_to_tx(request, coin_symbol, tx_hash):
    if not is_valid_hash(tx_hash):
        return Http404

    initial = {}
    form = BaseMetadataForm(initial=initial)
    if request.method == 'POST':

# Path: blockexplorer/settings.py
# BLOCKCYPHER_API_KEY = os.getenv('BLOCKCYPHER_API_KEY')
# 
# Path: metadata/forms.py
# class BaseMetadataForm(forms.Form):
#     '''
#     Used for uploading public metadata
#     '''
# 
#     metadata_key = forms.CharField(
#         label=_('Metadata Key'),
#         required=True,
#         widget=forms.TextInput(attrs={'class': 'input-lg', 'autofocus': 'autofocus'}),
#         min_length=1,
#         max_length=256,
#         )
# 
#     metadata_value = forms.CharField(
#         label=_('Metadata Value'),
#         required=True,
#         widget=forms.TextInput(attrs={'class': 'input-lg'}),
#         min_length=1,
#         max_length=512,
#         )
# 
#     def __init__(self, *args, **kwargs):
#         self.helper = FormHelper()
#         self.helper.form_method = 'post'
#         self.helper.layout = Layout(
#                 'metadata_key',
#                 'metadata_value',
#                 Submit(
#                     'submit',
#                     'Upload Metadata',
#                     css_class='btn btn-primary btn-lg',
#                     ),
#                 )
#         super(BaseMetadataForm, self).__init__(*args, **kwargs)


# Path: metadata/views.py
from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.core.serializers.json import DjangoJSONEncoder
from django.urls import reverse
from django.contrib import messages
from django.contrib.humanize.templatetags.humanize import intcomma
from django.utils.translation import ugettext_lazy as _
from annoying.decorators import render_to
from blockexplorer.settings import BLOCKCYPHER_API_KEY
from blockexplorer.decorators import assert_valid_coin_symbol
from metadata.forms import BaseMetadataForm
from blockcypher.api import get_metadata, put_metadata, get_latest_block_hash, get_block_overview
from blockcypher.utils import is_valid_address_for_coinsymbol, is_valid_hash
from blockcypher.constants import COIN_SYMBOL_MAPPINGS
from random import choice
import json

                coin_symbol=coin_symbol,
                )
    elif identifier_type == 'block':
        metadata = get_metadata(
                block_hash=identifier,
                api_key=BLOCKCYPHER_API_KEY,
                private=True,
                coin_symbol=coin_symbol,
                )
    else:
        # shouldn't be possible
        raise(Http404)

    json_response = json.dumps(
            {'metadata': metadata},
            cls=DjangoJSONEncoder,
            sort_keys=True,
            )

    return HttpResponse(json_response, content_type='application/json')


@render_to('add_metadata.html')
def add_metadata_to_tx(request, coin_symbol, tx_hash):
    if not is_valid_hash(tx_hash):
        return Http404

    initial = {}
    form = BaseMetadataForm(initial=initial)
    if request.method == 'POST':

        form = BaseMetadataForm(data=request.POST)