def addTrend(dict, klid): session = Session() stock = session.query(Stock).filter(Stock.klid == klid).first() treData = session.query(Trend).filter(and_(Trend.stock_id == stock.id, Trend.time == dict['time'])).first() #check that there are no data from that day and time if not treData: with transaction.manager: trend = Trend(stock_id = stock.id, time = dict['time'], value = dict['value']) session.add(trend) session.commit()
def stockData(list, id): session = Session() stock = session.query(Stock).filter(Stock.klid == id).first() time = datetime.strptime(list[0], "%d.%m.%Y").date() #make it a date object stockData = session.query(Stock_data).filter(and_(Stock_data.stock_id == stock.id, Stock_data.date == time)).first() #if there is a record from that day we can exit this class if stockData: return False with transaction.manager: newData = Stock_data(stock_id = stock.id, date = time, open = float(list[1]), high_sell = float(list[2]), lowest_sell = float(list[3]), close = float(list[4]), volume = float(list[5])) session.add(newData) session.commit() return True
def addStock(dict, cat): ''' What is given to this class {'name': name, "rise": change, 'price': price, 'buy': buy, 'sell': sell, 'lowest sell': ssell, 'high': high, 'volume': volume, 'exchange': exchange, 'time': time, 'klid': klid} ''' session = Session() stock = session.query(Stock).filter(Stock.klid == dict['klid']).all() #if stock isn't in the db lets add it there if not stock: #because python didn't like Finnish letters, we can change them back if "'O'" in cat: cat = cat.replace("'O'", u"Ö") if "'o'" in cat: cat = cat.replace("'o'", u"ö") #lets see if there is already a same named category category = session.query(Category).filter(Category.category == cat).first() #if not lets create a category if not category: with transaction.manager: cate = Category(category = cat) session.add(cate) session.commit() #get category's id cateid = cate.id else: #get category's id cateid = category.id with transaction.manager: name = dict['name'] #because python didn't like finnish letters, we can change them back if "'a'" in name: name = name.replace("'a'", u"ä") if "'o'" in name: name = name.replace("'o'", u"ö") if "'OA'" in name: name = name.replace("'OA'", u"Å") #create new stock record newStock = Stock(name = name, klid = dict['klid'], category_id = cateid) session.add(newStock) session.commit() #add exchange information for the stock exc = Exchange(stock_id = newStock.id, exchange = dict['exchange']) session.add(exc) session.commit()