Skip to content

Commit

Permalink
Merge branch 'release/0.3.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
suminb committed Aug 30, 2018
2 parents 2d69680 + d4b3a4f commit a49fa1d
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 2,202 deletions.
2 changes: 1 addition & 1 deletion finance/__init__.py
Expand Up @@ -9,7 +9,7 @@
from logbook import Logger, StreamHandler


__version__ = '0.3.2'
__version__ = '0.3.3'
__author__ = 'Sumin Byeon'
__email__ = 'suminb@gmail.com'

Expand Down
53 changes: 34 additions & 19 deletions finance/models.py
@@ -1,7 +1,7 @@
import collections
import functools
import operator
from datetime import datetime
from datetime import datetime, timedelta

import uuid64
from flask_login import UserMixin
Expand Down Expand Up @@ -54,9 +54,9 @@ def create(cls, commit=True, ignore_if_exists=False, **kwargs):
kwargs.update(dict(id=uuid64.issue()))
instance = cls(**kwargs)

if hasattr(instance, 'timestamp') \
and getattr(instance, 'timestamp') is None:
instance.timestamp = datetime.utcnow()
if hasattr(instance, 'created_at') \
and getattr(instance, 'created_at') is None:
instance.created_at = datetime.utcnow()

try:
return instance.save(commit=commit)
Expand Down Expand Up @@ -394,7 +394,7 @@ def get_by_number(cls, institution: str, number: str):
.first()

if account is None:
raise AccountNotFoundException
raise AccountNotFoundException((institution, number))
else:
return account

Expand All @@ -418,9 +418,9 @@ def balance(self, evaluated_at=None):
# Sum all transactions to produce {asset: sum(quantity)} dictionary
bs = {}
rs = [(r.asset, r.quantity, r.type) for r in records]
for asset, quantity, type in rs:
for asset, quantity, type_ in rs:
bs.setdefault(asset, 0)
if type == RecordType.balance_adjustment:
if type_ == RecordType.balance_adjustment:
# Previous records will be ignored when 'balance_adjustment'
# is seen.
bs[asset] = quantity
Expand All @@ -440,16 +440,11 @@ def net_worth(self, evaluated_at=None, granularity=Granularity.day,
if evaluated_at is None:
evaluated_at = datetime.utcnow()

if granularity == Granularity.day:
if isinstance(evaluated_at, datetime):
# NOTE: Any better way to handle this?
date = evaluated_at.date().timetuple()[:6]
evaluated_at = datetime(*date)
else:
raise NotImplementedError
evaluated_from, evaluated_until = \
self.get_bounds(evaluated_at, granularity)

net_asset_value = 0
for asset, quantity in self.balance(evaluated_at).items():
for asset, quantity in self.balance(evaluated_until).items():
if asset == base_asset:
net_asset_value += quantity
continue
Expand All @@ -459,12 +454,13 @@ def net_worth(self, evaluated_at=None, granularity=Granularity.day,
AssetValue.granularity == granularity,
AssetValue.base_asset == base_asset)
if approximation:
asset_value = asset_value.filter(
AssetValue.evaluated_at <= evaluated_at) \
asset_value = asset_value \
.filter(AssetValue.evaluated_at <= evaluated_until) \
.order_by(AssetValue.evaluated_at.desc())
else:
asset_value = asset_value.filter(
AssetValue.evaluated_at == evaluated_at)
asset_value = asset_value \
.filter(AssetValue.evaluated_at >= evaluated_from) \
.filter(AssetValue.evaluated_at <= evaluated_until) \

asset_value = asset_value.first()

Expand All @@ -476,6 +472,25 @@ def net_worth(self, evaluated_at=None, granularity=Granularity.day,

return net_asset_value

# FIXME: We probably want to move this function elsewhere
# FIXME: Think of a better name
@classmethod
def get_bounds(cls, evaluated_at=None, granularity=Granularity.day):
if granularity == Granularity.day:
if isinstance(evaluated_at, datetime):
# Truncate by date
lower_bound = evaluated_at.replace(
hour=0, minute=0, second=0, microsecond=0)

# Fast-forward the time to the end of the day, as
# `evaluated_at` is expected to be inclusive on the upper bound
upper_bound = \
lower_bound + timedelta(days=1) - timedelta(microseconds=1)

return lower_bound, upper_bound
else:
raise NotImplementedError


class Portfolio(CRUDMixin, db.Model): # type: ignore
"""A collection of accounts (= a collection of assets)."""
Expand Down
21 changes: 0 additions & 21 deletions frontend/.gitignore

This file was deleted.

0 comments on commit a49fa1d

Please sign in to comment.