Skip to content

Commit

Permalink
Change method structures
Browse files Browse the repository at this point in the history
  • Loading branch information
ishaansaxena committed Jul 12, 2018
1 parent 873e976 commit ec87124
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
16 changes: 3 additions & 13 deletions server/stocks/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from alpha_vantage.timeseries import TimeSeries

from symbols.views import track_symbol_view, untrack_symbol_view
from symbols.views import track_symbol_view, untrack_symbol_view, is_symbol_tracked
from symbols.models import Symbol
from investor.models import Investor

Expand Down Expand Up @@ -55,16 +55,6 @@ def get_json_data(data):
return data_dict


# Check if symbol is tracked
def is_symbol_tracked(symbol, user):
type, symbol = symbol.split(':')
try:
obj = Symbol.objects.get(type=type, symbol=symbol)
except:
return False
tracker = Investor.objects.get(user=user)
return tracker in obj.trackers.all()

## View methods

# Main dashboard
Expand All @@ -77,8 +67,7 @@ def stock_index_view(request):
@login_required
def stock_timeseries_view(request, symbol, format):
symbol = symbol.upper()
symbol_verbose = 'stock:' + symbol
tracked = is_symbol_tracked(symbol_verbose, request.user)
tracked = is_symbol_tracked("stock", symbol, request.user)

if format not in TS_MAP:
format = DEFAULT_TS_FORMAT
Expand Down Expand Up @@ -109,6 +98,7 @@ def stock_timeseries_view(request, symbol, format):
# Time series default view
@login_required
def stock_timeseries_default(request, symbol):
symbol = symbol.upper()
return stock_timeseries_view(request, symbol, DEFAULT_TS_FORMAT)

# Track stock view
Expand Down
31 changes: 20 additions & 11 deletions server/symbols/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,46 @@

## Helper methods

# Get symbol object from verbose symbol
def get_symbol_object(symbol):
type, symbol, slug = get_symbol_data(symbol)
obj = Symbol.objects.get(
# Get symbol object from symbol and type
def get_symbol_object(type, symbol):
type, symbol, slug = get_symbol_data(type + ":" + symbol)
obj, created = Symbol.objects.get_or_create(
type=type, symbol=symbol, slug=slug
)
return obj

# Get Symbol information
def get_symbol_data(symbol):
type = symbol.split(':')[0]
symbol = symbol.split(':')[1]
def get_symbol_data(symbol_verbose):
type = symbol_verbose.split(':')[0]
symbol = symbol_verbose.split(':')[1]
slug = SLUG_MAP[type].format(symbol)
return type, symbol, slug

# Check if symbol is tracked
def is_symbol_tracked(type, symbol, user):
try:
symbol = get_symbol_object(type, symbol)
except:
return False
tracker = Investor.objects.get(user=user)
return tracker in symbol.trackers.all()


## View methods
## NOTE: These views cannot directly be called, and are only called through
## stocks/views, crypto/views, etc. Thus, login_required is not necessary.

# Track symbol view
def track_symbol_view(request, type, symbol):
symbol_verbose = type + ":" + symbol
symbol = get_symbol_object(symbol_verbose)
symbol = get_symbol_object(type, symbol)
tracker = Investor.objects.get(user=request.user)
symbol.trackers.add(tracker)
data = {}
return JsonResponse(data)

# Track symbol view
def untrack_symbol_view(request, type, symbol):
symbol_verbose = type + ":" + symbol
symbol = get_symbol_object(symbol_verbose)
symbol = get_symbol_object(type, symbol)
tracker = Investor.objects.get(user=request.user)
symbol.trackers.remove(tracker)
data = {}
Expand Down

0 comments on commit ec87124

Please sign in to comment.