# Path: multiexplorer/pricetick/models.py
# class PriceTick(models.Model):
#     currency = models.CharField(max_length=8) # btc/ltc/doge/etc.
#     exchange = models.CharField(max_length=128) # kraken/bitstamp/etc.
#     base_fiat = models.CharField(max_length=8) # USD/EUR/GBP/etc.
#     date = models.DateTimeField(db_index=True)
#     price = models.FloatField()
# 
#     def __unicode__(self):
#         return "%s %s %s->%s" % (self.date, self.price, self.currency, self.base_fiat)
# 
#     @classmethod
#     def nearest(cls, crypto, fiat, date):
#         """
#         Find the tick nearest the passed in date. May raise DoesNotExist
#         if a value can't be found.
#         """
#         try:
#             match = cls.objects.filter(
#                 currency__iexact=crypto, base_fiat__iexact=fiat, date__lt=date
#             ).latest()
#             return {
#                 'price': match.price, 'source': match.exchange,
#                 'time': match.date, 'fiat': fiat
#             }
#         except cls.DoesNotExist:
#             fiat_btc = cls.objects.filter(
#                 currency__iexact=crypto, base_fiat__iexact='BTC', date__lt=date
#             ).latest()
#             crypto_btc = cls.objects.filter(
#                 currency__iexact='btc', base_fiat__iexact=fiat, date__lt=date
#             ).latest()
#             return {
#                 'price': fiat_btc.price * crypto_btc.price,
#                 'source': "%s->%s" % (fiat_btc.exchange, crypto_btc.exchange),
#                 'time': crypto_btc.date,
#                 'fiat': fiat
#             }
# 
#     @classmethod
#     def get_current_price(cls, crypto, fiat, verbose=False):
#         price = cls.get_non_stale_price(crypto, fiat)
#         if price:
#             return price
#         sources, price = get_current_price(crypto, fiat, report_services=True, verbose=verbose)
#         tick = cls.record_price(price, crypto, fiat, sources[0].name)
#         return tick
# 
#     @classmethod
#     def record_price(cls, price, crypto, fiat, source_name, at_time=None, interval=None):
#         """
#         If you already have the price data and want to record that, use this method.
#         WIll only record if the current price is within PRICE_INTERVAL_SECONDS.
#         """
#         at_time = at_time or timezone.now()
#         if cls.needs_update(crypto, fiat, at_time, interval):
#             obj = cls.objects.create(
#                 date=at_time,
#                 price=price,
#                 base_fiat=fiat.upper(),
#                 currency=crypto.upper(),
#                 exchange=source_name
#             )
#             return obj
#         return False
# 
#     @classmethod
#     def get_non_stale_price(cls, crypto, fiat, at_time=None, interval=None):
#         at_time = at_time or timezone.now()
#         try:
#             kwargs = {
#                 'currency__iexact': crypto,
#                 'base_fiat__iexact': fiat,
#                 'date__lte': at_time
#             }
#             if interval:
#                 kwargs['date__gte'] = at_time - interval
#             price = cls.objects.filter(**kwargs).latest()
#         except cls.DoesNotExist:
#             return None
# 
#         if price.is_stale(at_time, interval):
#             return None
# 
#         return price
# 
# 
#     @classmethod
#     def needs_update(cls, crypto, fiat, at_time=None, interval=None):
#         at_time = at_time or timezone.now()
#         price = cls.get_non_stale_price(crypto, fiat, at_time, interval)
#         return price is None
# 
#     def is_stale(self, at_time=None, interval=None):
#         interval = interval or datetime.timedelta(seconds=settings.PRICE_INTERVAL_SECONDS)
#         at_time = at_time or timezone.now()
#         interval_ago = at_time - interval
#         return self.date < interval_ago
# 
#     class Meta:
#         get_latest_by = 'date'


from django.contrib import admin
from .models import PriceTick

class PriceTickAdmin(admin.ModelAdmin):
    list_display = ['date', 'base_fiat', 'currency', 'exchange', 'price']
    ordering = ('-date', )

# Path: multiexplorer/pricetick/models.py
# class PriceTick(models.Model):
#     currency = models.CharField(max_length=8) # btc/ltc/doge/etc.
#     exchange = models.CharField(max_length=128) # kraken/bitstamp/etc.
#     base_fiat = models.CharField(max_length=8) # USD/EUR/GBP/etc.
#     date = models.DateTimeField(db_index=True)
#     price = models.FloatField()
# 
#     def __unicode__(self):
#         return "%s %s %s->%s" % (self.date, self.price, self.currency, self.base_fiat)
# 
#     @classmethod
#     def nearest(cls, crypto, fiat, date):
#         """
#         Find the tick nearest the passed in date. May raise DoesNotExist
#         if a value can't be found.
#         """
#         try:
#             match = cls.objects.filter(
#                 currency__iexact=crypto, base_fiat__iexact=fiat, date__lt=date
#             ).latest()
#             return {
#                 'price': match.price, 'source': match.exchange,
#                 'time': match.date, 'fiat': fiat
#             }
#         except cls.DoesNotExist:
#             fiat_btc = cls.objects.filter(
#                 currency__iexact=crypto, base_fiat__iexact='BTC', date__lt=date
#             ).latest()
#             crypto_btc = cls.objects.filter(
#                 currency__iexact='btc', base_fiat__iexact=fiat, date__lt=date
#             ).latest()
#             return {
#                 'price': fiat_btc.price * crypto_btc.price,
#                 'source': "%s->%s" % (fiat_btc.exchange, crypto_btc.exchange),
#                 'time': crypto_btc.date,
#                 'fiat': fiat
#             }
# 
#     @classmethod
#     def get_current_price(cls, crypto, fiat, verbose=False):
#         price = cls.get_non_stale_price(crypto, fiat)
#         if price:
#             return price
#         sources, price = get_current_price(crypto, fiat, report_services=True, verbose=verbose)
#         tick = cls.record_price(price, crypto, fiat, sources[0].name)
#         return tick
# 
#     @classmethod
#     def record_price(cls, price, crypto, fiat, source_name, at_time=None, interval=None):
#         """
#         If you already have the price data and want to record that, use this method.
#         WIll only record if the current price is within PRICE_INTERVAL_SECONDS.
#         """
#         at_time = at_time or timezone.now()
#         if cls.needs_update(crypto, fiat, at_time, interval):
#             obj = cls.objects.create(
#                 date=at_time,
#                 price=price,
#                 base_fiat=fiat.upper(),
#                 currency=crypto.upper(),
#                 exchange=source_name
#             )
#             return obj
#         return False
# 
#     @classmethod
#     def get_non_stale_price(cls, crypto, fiat, at_time=None, interval=None):
#         at_time = at_time or timezone.now()
#         try:
#             kwargs = {
#                 'currency__iexact': crypto,
#                 'base_fiat__iexact': fiat,
#                 'date__lte': at_time
#             }
#             if interval:
#                 kwargs['date__gte'] = at_time - interval
#             price = cls.objects.filter(**kwargs).latest()
#         except cls.DoesNotExist:
#             return None
# 
#         if price.is_stale(at_time, interval):
#             return None
# 
#         return price
# 
# 
#     @classmethod
#     def needs_update(cls, crypto, fiat, at_time=None, interval=None):
#         at_time = at_time or timezone.now()
#         price = cls.get_non_stale_price(crypto, fiat, at_time, interval)
#         return price is None
# 
#     def is_stale(self, at_time=None, interval=None):
#         interval = interval or datetime.timedelta(seconds=settings.PRICE_INTERVAL_SECONDS)
#         at_time = at_time or timezone.now()
#         interval_ago = at_time - interval
#         return self.date < interval_ago
# 
#     class Meta:
#         get_latest_by = 'date'


# Path: multiexplorer/pricetick/admin.py
from django.contrib import admin
from .models import PriceTick


class PriceTickAdmin(admin.ModelAdmin):
    list_display = ['date', 'base_fiat', 'currency', 'exchange', 'price']
    ordering = ('-date', )

    list_filter = ('currency', )