# 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
                messages.success(request, msg, extra_tags='safe')
                redir_url = reverse(
                    'block_overview',
                    kwargs={
                        'coin_symbol': coin_symbol,
                        'block_representation': block_hash,
                        },
                    )
                return HttpResponseRedirect(redir_url)
            elif 'error' in results:
                messages.warning(request, results.get('error'))
            elif 'errors' in results:
                for error in results.get('errors'):
                    messages.warning(request, error)

    elif request.method == 'GET':
        # Preseed tx hex if passed through GET string
        key = request.GET.get('k')
        value = request.GET.get('v')
        if key:
            initial['metadata_key'] = key
        if value:
            initial['metadata_value'] = value
        if key or value:
            form = BaseMetadataForm(initial=initial)
    return {
            'form': form,
            'is_input_page': True,
            'coin_symbol': coin_symbol,
            'block_hash': block_hash,

# 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

                messages.success(request, msg, extra_tags='safe')
                redir_url = reverse(
                    'block_overview',
                    kwargs={
                        'coin_symbol': coin_symbol,
                        'block_representation': block_hash,
                        },
                    )
                return HttpResponseRedirect(redir_url)
            elif 'error' in results:
                messages.warning(request, results.get('error'))
            elif 'errors' in results:
                for error in results.get('errors'):
                    messages.warning(request, error)

    elif request.method == 'GET':
        # Preseed tx hex if passed through GET string
        key = request.GET.get('k')
        value = request.GET.get('v')
        if key:
            initial['metadata_key'] = key
        if value:
            initial['metadata_value'] = value
        if key or value:
            form = BaseMetadataForm(initial=initial)
    return {
            'form': form,
            'is_input_page': True,
            'coin_symbol': coin_symbol,
            'block_hash': block_hash,

            }