forked from blockcypher/explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforms.py
100 lines (82 loc) · 2.96 KB
/
forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from django import forms
from django.utils.translation import ugettext_lazy as _
from blockcypher.constants import COIN_CHOICES
from blockcypher.utils import uses_only_hash_chars
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Submit
class RawTXForm(forms.Form):
'''
Used for both pushing and decoding
'''
tx_hex = forms.CharField(
label=_('Transaction Hex'),
required=True,
widget=forms.Textarea(attrs={'class': 'input-lg', 'autofocus': 'autofocus'}),
min_length=10, # TODO: accurate min
)
coin_symbol = forms.ChoiceField(
label=_('Network'),
required=True,
choices=COIN_CHOICES,
)
def clean_tx_hex(self):
tx_hex = self.cleaned_data['tx_hex'].strip().upper()
if len(tx_hex) % 2 == 1:
err_msg = _('Please enter a valid transaction hex')
raise forms.ValidationError(err_msg)
return tx_hex
class EmbedDataForm(forms.Form):
'''
Used for both pushing and decoding
'''
data_to_embed = forms.CharField(
label=_('Data to Embed'),
required=True,
widget=forms.Textarea(attrs={'class': 'input-lg', 'autofocus': 'autofocus'}),
min_length=1,
max_length=400,
)
encoding_is_hex = forms.BooleanField(
label=_('Data is Hexadecimal'),
initial=True,
required=False,
)
coin_symbol = forms.ChoiceField(
label=_('Network'),
required=True,
choices=(
('btc', 'Bitcoin'),
('bcy', 'BlockCypher Testnet'),
),
help_text=_('Currently only the Bitcoin Mainnet and Blockcypher Testnets are supported'),
)
def clean_data_to_embed(self):
data_to_embed = self.cleaned_data['data_to_embed'].strip()
if not data_to_embed:
err_msg = _('Data to embed cannot be blank')
raise forms.ValidationError(err_msg)
return data_to_embed
def clean(self):
encoding_is_hex = self.cleaned_data.get('encoding_is_hex')
data_to_embed = self.cleaned_data.get('data_to_embed')
if not data_to_embed:
return self.cleaned_data
if encoding_is_hex:
if not uses_only_hash_chars(data_to_embed):
msg = _('Sorry, that string contains non-hex characters. Uncheck "Data is Hexadecimal" to instead embed the data as a string.')
raise forms.ValidationError(msg)
return self.cleaned_data
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.form_method = 'post'
self.helper.layout = Layout(
'data_to_embed',
'encoding_is_hex',
'coin_symbol',
Submit(
'submit',
'Embed Data',
css_class='btn btn-primary btn-lg',
),
)
super(EmbedDataForm, self).__init__(*args, **kwargs)