improve monero address validator to ensure the app only accepts correct addresses for the app's context

pull/3/head
lalanza808 5 years ago
parent c6d661dc80
commit e11f670408

@ -1,11 +1,29 @@
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from monero.address import address from monero.address import address
from core.monero import AuctionDaemon
def address_is_valid_monero(value): def address_is_valid_monero(value):
ad = AuctionDaemon()
try: try:
address(value) a = address(value)
if ad.connected:
net_type = ad.daemon.info()['nettype']
if net_type == 'stagenet' and a.is_stagenet():
return True
elif net_type == 'testnet' and a.is_testnet():
return True
elif net_type == 'mainnet' and a.is_mainnet():
return True
else:
raise ValidationError(
_('%(value)s is not a valid %(net_type)s address'),
params={
'value': value,
'net_type': net_type
}
)
return True return True
except ValueError: except ValueError:
raise ValidationError( raise ValidationError(

@ -85,7 +85,6 @@ def create_item(request):
for err in form_errors: for err in form_errors:
err_data = form_errors[err][0] err_data = form_errors[err][0]
messages.error(request, f'{err}: {err_data["message"]}') messages.error(request, f'{err}: {err_data["message"]}')
return HttpResponseRedirect(reverse('create_item'))
context = { context = {
'form': CreateItemForm(request.POST or None), 'form': CreateItemForm(request.POST or None),
@ -128,8 +127,7 @@ def edit_item(request, item_id):
for err in form_errors: for err in form_errors:
err_data = form_errors[err][0] err_data = form_errors[err][0]
messages.error(request, f'{err}: {err_data["message"]}') messages.error(request, f'{err}: {err_data["message"]}')
return HttpResponseRedirect(reverse('get_item', args=[item_id]))
else:
context = { context = {
'form': CreateItemForm(instance=item), 'form': CreateItemForm(instance=item),
'formset': ItemImageFormSet(instance=item) 'formset': ItemImageFormSet(instance=item)