-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathajax.py
More file actions
203 lines (195 loc) · 7.37 KB
/
Copy pathajax.py
File metadata and controls
203 lines (195 loc) · 7.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import webapp2
import urllib
import os
import logging
import json
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext import db
import datetime as datetime
def get_price(symbol):
url = 'http://finance.yahoo.com/d/quotes.csv?s=%s&f=%s' % (symbol, 'l1')
return urllib.urlopen(url).read().strip().strip('"')
class Portfolio(db.Model):
portName = db.StringProperty()
portDesc = db.StringProperty()
class Stock(db.Model):
ticker = db.StringProperty()
port = db.ReferenceProperty(Portfolio)
shares = db.StringProperty()
dtime = db.DateTimeProperty()
price = db.StringProperty()
class stockhandler(webapp2.RequestHandler):
def get(self):
userAction = self.request.url.split("/")[4];
logging.info("URL4 is "+userAction);
if userAction == 'price':
stksymbol = self.request.url.split("/")[5];
price = get_price(stksymbol)
resultDict = {'responseQuote': 'The stock price of ' + stksymbol + ' is: ' + price}
curtime = datetime.datetime.now()
logging.info("current time = " + str(curtime) +". Just looked up "+stksymbol)
#newstock = Stock(dtime = curtime, ticker=stksymbol, price=price)
#newstock.put()
temp = os.path.join(
os.path.dirname(__file__),
'resQuote.html')
outstr = template.render(
temp,
resultDict)
self.response.out.write(outstr)
elif userAction == 'delete':
logging.info("GET delete for stocks")
stksymbol = self.request.url.split("/")[5];
portname = self.request.url.split("/")[6];
stockRes = db.GqlQuery("SELECT * FROM Stock WHERE ticker = :1", stksymbol)
for e in stockRes:
if e.port.portName == portname:
e.delete()
logging.info("Deleted item")
logging.info("In delete object area")
portKey = e
self.response.out.write('Deleted '+stksymbol +' in "'+portname+'" portfolio.')
elif userAction == 'allStocks':
logging.info("GET listAll for stocks")
que = db.Query(Stock)
results = que.fetch(limit=200)
stockTable = []
for eachstk in results:
stockentry = {}
stockentry['ticker'] = eachstk.ticker
stockentry['shares'] = eachstk.shares
portfolio_name = eachstk.port.portName
logging.info(portfolio_name)
stockentry['portfolio'] = portfolio_name
stockTable.append(stockentry)
str_json = json.dumps(stockTable)
self.response.out.write(str_json)
else:
self.response.out.write("hello123")
def post(self):
userAction = self.request.url.split("/")[4];
logging.info("add or update stocks")
stksymbol = self.request.get('ticker')
numshares = self.request.get('shares')
portname = self.request.get('portname')
stockprice = get_price(stksymbol)
logging.info("Ticker: " + str(stksymbol) + " Shares: " + str(numshares))
curtime = datetime.datetime.now()
q = Portfolio.all()
q.filter("portName =", portname)
portRes = q.get()
logging.info("Right before query area")
stockRes = db.GqlQuery("SELECT * FROM Stock WHERE port = :1 AND ticker = :2", portRes, stksymbol)
temp = 0
for e in stockRes:
logging.info("In UPDATE area")
e.ticker = stksymbol
e.shares = numshares
e.port = portRes
e.price = stockprice
e.dtime = curtime
db.put(e)
temp = temp + 1
logging.info("UPDATING: "+stksymbol)
self.response.out.write("Updated: " + str(numshares) + " shares of " + stksymbol +" in "+portname+" portfolio.")
if temp == 0:
logging.info("In ADD area")
newstock = Stock(ticker=stksymbol, shares=numshares, port=portRes, price=stockprice, dtime = curtime)
newstock.put()
logging.info("ADDING: "+stksymbol)
self.response.out.write("Added: " + str(numshares) + " shares of " + stksymbol +" in "+portname+" portfolio.")
class portfoliohandler(webapp2.RequestHandler):
def get(self):
userAction = self.request.url.split("/")[4];
if userAction == 'fullList':
logging.info("GET full list of portfolios")
que = db.Query(Portfolio)
outstr = ""
results = que.fetch(limit=200)
counter = 0
for eachPort in results:
# price = get_price(eachstk.ticker)
# value = float(price) * float(eachstk.shares)
# portTotal = portTotal + value
if counter == 0:
outstr = outstr + str(eachPort.portName)
counter = counter + 1
else:
outstr = outstr + ", " + str(eachPort.portName)
logging.info("Portfolio: " + str(eachPort.portName))
logging.info("Portlist: " + outstr)
self.response.out.write("Portfolio List: " + outstr)
elif userAction == 'portfolioDetails':
portname = self.request.url.split("/")[5];
#portfolio_k = db.Key.from_path('Portfolio', portname)
#portfolio = db.get(portfolio_k)
#portStocks = db.GqlQuery("SELECT * FROM Stock WHERE port = :1", portname)
portRes = db.GqlQuery("SELECT * FROM Portfolio WHERE portName = :1", portname)
stockInfo = ''
logging.info('0')
for e in portRes:
logging.info('1')
stockRes = db.GqlQuery("SELECT * FROM Stock WHERE port = :1", e)
logging.info('2')
portDescription = e.portDesc
logging.info('3')
counter = 0
totalValue = 0
for stock in stockRes:
logging.info('4')
sTicker = stock.ticker
sShares = stock.shares
currentPricePerShare = get_price(sTicker)
currentValue = float(currentPricePerShare) * int(sShares)
totalValue = totalValue + currentValue
if counter == 0:
stockInfo = stockInfo + 'The "'+portname+'" portfolio has the following stocks:<p> <p>- '+sShares+' shares of ' +sTicker +', currently valued overall at $'+str(currentValue)+'<p>'
counter = counter + 1
else:
stockInfo = stockInfo + ' - '+sShares + ' shares of ' +sTicker +', currently valued overall at $'+str(currentValue)+'<p>'
logging.info('5')
outstr = 'The "'+portname+'" portfolio is described as "'+portDescription+'". '+stockInfo+' The total value of this portfolio is currently $'+str(totalValue)
self.response.out.write(outstr)
elif userAction == 'portfolioDelete':
portname=self.request.get('portname')
logging.info("GET delete for a portfolio")
portname = self.request.url.split("/")[5];
portRes = db.GqlQuery("SELECT * FROM Portfolio WHERE portName = :1", portname)
for e in portRes:
stockRes = db.GqlQuery("SELECT * FROM Stock WHERE port = :1", e)
for stock in stockRes:
stock.delete()
e.delete()
logging.info("Deleted item")
logging.info("In delete object area")
self.response.out.write('Deleted "'+portname +'" portfolio.')
def post(self):
logging.info("post for portfolio")
portname=self.request.get('portname')
portdesc=self.request.get('portdesc')
logging.info("portfolio name: " + portname)
newport = Portfolio(key_name=portname, portName=portname, portDesc=portdesc)
newport.put()
logging.info("added portfolio " + portname)
self.response.out.write("Added portfolio: "+portname)
app = webapp2.WSGIApplication([
('/stock/.*', stockhandler),
('/portfolio/.*', portfoliohandler),
], debug=True)