# Path: multiexplorer/wallet/models.py
# class WalletMasterKeys(models.Model):
#     """
#     Stores the BIP32 HD master seed for the user's wallet.
#     The seed is stored encrypted with the user's password.
#     """
#     user = models.ForeignKey('auth.User', on_delete=models.CASCADE)
#     encrypted_mnemonic = models.CharField(max_length=172)
# 
#     auto_logout = models.IntegerField(choices=AUTO_LOGOUT_CHOICES, default=0)
#     display_fiat = models.CharField(max_length=5, default='usd')
#     show_wallet_list = models.TextField(default='btc,ltc,doge,dash') # comma seperated
# 
#     def __unicode__(self):
#         return self.user.username
# 
#     def get_show_wallet_list(self):
#         return self.show_wallet_list.split(",")
# 
#     def get_settings(self):
#         if self.display_fiat in ['cad', 'usd']:
#             symbol = '$'
#         elif self.display_fiat == 'eur':
#             symbol = '€'
#         elif self.display_fiat == 'gbp':
#             symbol = '£'
#         elif self.display_fiat in ['jpy', 'cny']:
#             symbol = '¥'
#         elif self.display_fiat == 'idr':
#             symbol = "Rp"
#         else:
#             symbol = ''
# 
#         return {
#             'show_wallet_list': self.get_show_wallet_list(),
#             'display_fiat_unit': self.display_fiat,
#             'display_fiat_symbol': symbol,
#             'auto_logout': self.auto_logout
#         }
# 
# class FailedLogin(models.Model):
#     username = models.CharField(max_length=64, db_index=True)
#     time = models.DateTimeField(default=datetime.datetime.now)
# 
#     class Meta:
#         get_latest_by = 'time'
# 
# AUTO_LOGOUT_CHOICES = (
#     (5, "5 minutes of inactivity"),
#     (30, "30 minutes of inactivity"),
#     (0, "Never"),
# )


import datetime
import json
from django.views.decorators.csrf import ensure_csrf_cookie, csrf_exempt
from django.contrib.auth import authenticate, login as init_login
from django.contrib.auth.models import User
from django.conf import settings
from django.utils import timezone
from django.template.response import TemplateResponse
from django import http
from .models import WalletMasterKeys, FailedLogin, AUTO_LOGOUT_CHOICES
from pricetick.models import PriceTick
from multiexplorer.utils import get_wallet_currencies
from moneywagon.core import NoService
from multiexplorer.views import _cached_fetch
        'wallet_settings': wal.get_settings(),
        'exchange_rates': get_rates(wal.display_fiat, wal.show_wallet_list),
    })


def login(request):
    """
    Authenticate the user. On failed attempts, record the event, and limit
    5 failed attempts every 15 minutes.
    """
    username = request.POST['username']
    password = request.POST['password']

    fithteen_minutes_ago = datetime.datetime.now() - datetime.timedelta(minutes=15)
    tries = FailedLogin.objects.filter(username=username, time__gt=fithteen_minutes_ago)
    try_count = tries.count()

    if try_count < settings.LOGIN_TRIES:
        user = authenticate(username=username, password=password)

        if user and user.is_authenticated():
            init_login(request, user)
            wal = WalletMasterKeys.objects.get(user=request.user)

            return http.JsonResponse({
                'encrypted_mnemonic': wal.encrypted_mnemonic,
                'wallet_settings': wal.get_settings(),
                'exchange_rates': get_rates(wal.display_fiat, wal.show_wallet_list),
            })
        else:

# Path: multiexplorer/wallet/models.py
# class WalletMasterKeys(models.Model):
#     """
#     Stores the BIP32 HD master seed for the user's wallet.
#     The seed is stored encrypted with the user's password.
#     """
#     user = models.ForeignKey('auth.User', on_delete=models.CASCADE)
#     encrypted_mnemonic = models.CharField(max_length=172)
# 
#     auto_logout = models.IntegerField(choices=AUTO_LOGOUT_CHOICES, default=0)
#     display_fiat = models.CharField(max_length=5, default='usd')
#     show_wallet_list = models.TextField(default='btc,ltc,doge,dash') # comma seperated
# 
#     def __unicode__(self):
#         return self.user.username
# 
#     def get_show_wallet_list(self):
#         return self.show_wallet_list.split(",")
# 
#     def get_settings(self):
#         if self.display_fiat in ['cad', 'usd']:
#             symbol = '$'
#         elif self.display_fiat == 'eur':
#             symbol = '€'
#         elif self.display_fiat == 'gbp':
#             symbol = '£'
#         elif self.display_fiat in ['jpy', 'cny']:
#             symbol = '¥'
#         elif self.display_fiat == 'idr':
#             symbol = "Rp"
#         else:
#             symbol = ''
# 
#         return {
#             'show_wallet_list': self.get_show_wallet_list(),
#             'display_fiat_unit': self.display_fiat,
#             'display_fiat_symbol': symbol,
#             'auto_logout': self.auto_logout
#         }
# 
# class FailedLogin(models.Model):
#     username = models.CharField(max_length=64, db_index=True)
#     time = models.DateTimeField(default=datetime.datetime.now)
# 
#     class Meta:
#         get_latest_by = 'time'
# 
# AUTO_LOGOUT_CHOICES = (
#     (5, "5 minutes of inactivity"),
#     (30, "30 minutes of inactivity"),
#     (0, "Never"),
# )


# Path: multiexplorer/wallet/views.py
import datetime
import json
from django.views.decorators.csrf import ensure_csrf_cookie, csrf_exempt
from django.contrib.auth import authenticate, login as init_login
from django.contrib.auth.models import User
from django.conf import settings
from django.utils import timezone
from django.template.response import TemplateResponse
from django import http
from .models import WalletMasterKeys, FailedLogin, AUTO_LOGOUT_CHOICES
from pricetick.models import PriceTick
from multiexplorer.utils import get_wallet_currencies
from moneywagon.core import NoService
from multiexplorer.views import _cached_fetch

        'wallet_settings': wal.get_settings(),
        'exchange_rates': get_rates(wal.display_fiat, wal.show_wallet_list),
    })


def login(request):
    """
    Authenticate the user. On failed attempts, record the event, and limit
    5 failed attempts every 15 minutes.
    """
    username = request.POST['username']
    password = request.POST['password']

    fithteen_minutes_ago = datetime.datetime.now() - datetime.timedelta(minutes=15)
    tries = FailedLogin.objects.filter(username=username, time__gt=fithteen_minutes_ago)
    try_count = tries.count()

    if try_count < settings.LOGIN_TRIES:
        user = authenticate(username=username, password=password)

        if user and user.is_authenticated():
            init_login(request, user)
            wal = WalletMasterKeys.objects.get(user=request.user)

            return http.JsonResponse({
                'encrypted_mnemonic': wal.encrypted_mnemonic,
                'wallet_settings': wal.get_settings(),
                'exchange_rates': get_rates(wal.display_fiat, wal.show_wallet_list),
            })
        else:

            FailedLogin.objects.create(username=username)