# Path: crumpet/profiles/forms.py
# class UserAccountForm(forms.Form):
# 
# Path: crumpet/profiles/models.py
# class UserAccount(models.Model):
#     class Meta:
#     def __str__(self):
# 
# Path: crumpet/profiles/constants.py
# CRYPTO_EXCHANGES = (
#     ("poloniex", _("Poloniex")),
#     ("bittrex", _("Bittrex")),
#     ("bitstamp", _("Bitstamp"))
# )
# EXCHANGE_PERIODS = (
#     (300, "5-min"),
#     (900, "15-min"),
#     (1800, "30-min"),
#     (7200, "2-hr"),
#     (14400, "4-hr"),
#     (86400, "1-day")
# )
# STRATEGIES = (
#     ("To The Moon", "toTheMoon"),
#     ("Strategy 2", "strategy 2"),
# )
# INSTRUMENTS = (
#     ("ETH", _("Ethereum")),
#     ("DGB", _("DigiByte")),
#     ("XRP", _("Ripple")),
#     ("XMR", _("Monero")),
#     ("LTC", _("LiteCoin")),
#     ("LSK", _("LISK")),
#     ("STEEM", _("STEEM"))
# )
# MODES = (
#     ("backtest", _("Backtest")),
#     ("livetest", _("Live Backtest")),
#     ("live", _("Live"))
# )
#     BUY = 0
#     SELL = 1
# ORDER_TYPES = (
#     (OrderType.BUY, 'buy'),
#     (OrderType.SELL, 'sell')
# )
# OPEN, CANCELLED, PROCESSED = 'open', 'cancelled', 'processed'
# ORDER_STATES = (
#     (OPEN, _('open')),
#     (CANCELLED, _('cancelled')),
#     (PROCESSED, _('processed'))
# )
# SMA, EMA, RSI = 'sma', 'ema', 'rsi'
# INDICATORS = (
#     (SMA, _('sma')),
#     (EMA, _('ema')),
#     (RSI, _('rsi'))
# )
# class OrderType(object):
# 
# Path: crumpet/models.py
# class Order(models.Model):
#     type = models.IntegerField(
#         choices=constants.ORDER_TYPES,
#         max_length=255,
#         db_index=True
#     )
#     order_number = models.CharField(
#         null=True,
#         blank=True,
#         max_length=50,
#         verbose_name="Order Number"
#     )
#     amount = AmountField()
#     price = AmountField()
#     date = models.DateTimeField()
#     created = models.DateTimeField(auto_now_add=True)
#     updated = models.DateTimeField(auto_now=True)
#     user_account = models.ForeignKey(UserAccount, related_name='orders')
#     ticker = models.CharField(verbose_name='Ticker Symbol', max_length=30)
# 
#     status = models.CharField(
#         default=None,
#         choices=constants.ORDER_STATES,
#         max_length=255,
#         db_index=True
#     )
# 
#     class Meta:
#         ordering = ['-date']
#         get_latest_by = 'date'
#         db_table = 'bot_order'
# 
#     def __str__(self):
#         return '{type} {amount} BTC at {price} US$'.format(
#             type=self.get_type_display(),
#             amount=self.amount,
#             price=self.price
#         )


import json
from django.contrib import messages
from django.shortcuts import redirect, render
from django.urls import reverse
from django.views.generic import TemplateView, FormView
from poloniex import Poloniex
from crumpet.profiles import forms, models, constants
from crumpet.models import Order
        ).first()

        form.fields["api_key"].initial = user_account.api_key
        form.fields["api_secret"].initial = user_account.api_secret

        return context

    def form_valid(self, form):
        context = self.get_context_data()
        exchange = form.cleaned_data["exchange"]
        api_key = form.cleaned_data["api_key"]
        api_secret = form.cleaned_data["api_secret"]

        user_account = models.UserAccount.objects.filter(
            user=self.request.user
        ).first()

        if user_account:
            user_account.exchange = exchange
            user_account.api_key = api_key
            user_account.api_secret = api_secret
            user_account.save()
            messages.success(self.request, 'Profile details updated.')

            if user_account.api_key and user_account.api_secret:
                polo = Poloniex(
                    key=user_account.api_key,
                    secret=user_account.api_secret)

                trade_history = polo.returnTradeHistory()

# Path: crumpet/profiles/forms.py
# class UserAccountForm(forms.Form):
# 
# Path: crumpet/profiles/models.py
# class UserAccount(models.Model):
#     class Meta:
#     def __str__(self):
# 
# Path: crumpet/profiles/constants.py
# CRYPTO_EXCHANGES = (
#     ("poloniex", _("Poloniex")),
#     ("bittrex", _("Bittrex")),
#     ("bitstamp", _("Bitstamp"))
# )
# EXCHANGE_PERIODS = (
#     (300, "5-min"),
#     (900, "15-min"),
#     (1800, "30-min"),
#     (7200, "2-hr"),
#     (14400, "4-hr"),
#     (86400, "1-day")
# )
# STRATEGIES = (
#     ("To The Moon", "toTheMoon"),
#     ("Strategy 2", "strategy 2"),
# )
# INSTRUMENTS = (
#     ("ETH", _("Ethereum")),
#     ("DGB", _("DigiByte")),
#     ("XRP", _("Ripple")),
#     ("XMR", _("Monero")),
#     ("LTC", _("LiteCoin")),
#     ("LSK", _("LISK")),
#     ("STEEM", _("STEEM"))
# )
# MODES = (
#     ("backtest", _("Backtest")),
#     ("livetest", _("Live Backtest")),
#     ("live", _("Live"))
# )
#     BUY = 0
#     SELL = 1
# ORDER_TYPES = (
#     (OrderType.BUY, 'buy'),
#     (OrderType.SELL, 'sell')
# )
# OPEN, CANCELLED, PROCESSED = 'open', 'cancelled', 'processed'
# ORDER_STATES = (
#     (OPEN, _('open')),
#     (CANCELLED, _('cancelled')),
#     (PROCESSED, _('processed'))
# )
# SMA, EMA, RSI = 'sma', 'ema', 'rsi'
# INDICATORS = (
#     (SMA, _('sma')),
#     (EMA, _('ema')),
#     (RSI, _('rsi'))
# )
# class OrderType(object):
# 
# Path: crumpet/models.py
# class Order(models.Model):
#     type = models.IntegerField(
#         choices=constants.ORDER_TYPES,
#         max_length=255,
#         db_index=True
#     )
#     order_number = models.CharField(
#         null=True,
#         blank=True,
#         max_length=50,
#         verbose_name="Order Number"
#     )
#     amount = AmountField()
#     price = AmountField()
#     date = models.DateTimeField()
#     created = models.DateTimeField(auto_now_add=True)
#     updated = models.DateTimeField(auto_now=True)
#     user_account = models.ForeignKey(UserAccount, related_name='orders')
#     ticker = models.CharField(verbose_name='Ticker Symbol', max_length=30)
# 
#     status = models.CharField(
#         default=None,
#         choices=constants.ORDER_STATES,
#         max_length=255,
#         db_index=True
#     )
# 
#     class Meta:
#         ordering = ['-date']
#         get_latest_by = 'date'
#         db_table = 'bot_order'
# 
#     def __str__(self):
#         return '{type} {amount} BTC at {price} US$'.format(
#             type=self.get_type_display(),
#             amount=self.amount,
#             price=self.price
#         )


# Path: crumpet/views.py
import json
from django.contrib import messages
from django.shortcuts import redirect, render
from django.urls import reverse
from django.views.generic import TemplateView, FormView
from poloniex import Poloniex
from crumpet.profiles import forms, models, constants
from crumpet.models import Order

        ).first()

        form.fields["api_key"].initial = user_account.api_key
        form.fields["api_secret"].initial = user_account.api_secret

        return context

    def form_valid(self, form):
        context = self.get_context_data()
        exchange = form.cleaned_data["exchange"]
        api_key = form.cleaned_data["api_key"]
        api_secret = form.cleaned_data["api_secret"]

        user_account = models.UserAccount.objects.filter(
            user=self.request.user
        ).first()

        if user_account:
            user_account.exchange = exchange
            user_account.api_key = api_key
            user_account.api_secret = api_secret
            user_account.save()
            messages.success(self.request, 'Profile details updated.')

            if user_account.api_key and user_account.api_secret:
                polo = Poloniex(
                    key=user_account.api_key,
                    secret=user_account.api_secret)

                trade_history = polo.returnTradeHistory()

                Order.objects.all().delete()