forked from oldrev/brickmover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
109 lines (87 loc) · 3.4 KB
/
models.py
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
#encoding: utf-8
import os
import datetime
from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relation, sessionmaker
import config
BaseModel = declarative_base()
class SmsMessage(BaseModel):
__tablename__ = 'sms_messages'
id = Column(Integer, primary_key=True)
arrived_time = Column(DateTime, nullable=False)
mobile = Column(String(32), nullable=True)
content = Column(String(256), nullable=True)
def __repr__(self):
return "SmsMessage(%r, %r, %r)" % (self.arrived_time, self.mobile, self.content)
class TradeLead(BaseModel):
__tablename__ = 'trade_leads'
id = Column(Integer, primary_key=True)
created_time = Column(DateTime, nullable=False)
exchange_to_buy = Column(String(32), nullable=False)
exchange_to_sell = Column(String(32), nullable=False)
buy_price = Column(Float, nullable=False)
sell_price = Column(Float, nullable=False)
def __init__(self, buy_exchange, buy_price, sell_exchange, sell_price):
self.created_time = datetime.datetime.now()
self.exchange_to_buy = buy_exchange
self.buy_price = buy_price
self.exchange_to_sell = sell_exchange
self.sell_price = sell_price
@staticmethod
def last():
session = Session()
o = session.query(TradeLead).order_by(TradeLead.created_time.desc()).first()
return o
class Order(BaseModel):
__tablename__ = 'orders'
id = Column(Integer, primary_key=True)
created_time = Column(DateTime, nullable=False)
sold_time = Column(DateTime, nullable=True)
bought_time = Column(DateTime, nullable=True)
is_bought = Column(Boolean, nullable=False)
is_sold = Column(Boolean, nullable=False)
exchange_to_buy = Column(String(32), nullable=False)
exchange_to_sell = Column(String(32), nullable=False)
buy_price = Column(Float, nullable=False)
sell_price = Column(Float, nullable=False)
quantity = Column(Float, nullable=False)
state = Column(Enum('processing', 'done', 'cancel', 'except'), nullable=False)
#director = relation("Director", backref='movies', lazy=False)
def __init__(self, buy_exchange, buy_price, sell_exchange, sell_price, qty):
self.created_time = datetime.datetime.now()
self.is_sold = False
self.is_bought = False
self.exchange_to_buy = buy_exchange
self.buy_price = buy_price
self.exchange_to_sell = sell_exchange
self.sell_price = sell_price
self.quantity = qty
self.state = 'processing'
def __repr__(self):
return "Order(%r, %r, %r, %r, %r, %r)" % (self.id, self.created_time, self.buy_price, self.sell_price, self.quantity, self.state)
@staticmethod
def last():
session = Session()
o = session.query(Order).order_by(Order.id.desc()).first()
return o
db_path = os.path.join(config.configuration['data_path'], 'brickmover.db')
engine = create_engine('sqlite:///' + db_path)
BaseModel.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
'''
session = Session()
m1 = Movie("Star Trek", 2009)
m1.director = Director("JJ Abrams")
d2 = Director("George Lucas")
d2.movies = [Movie("Star Wars", 1977), Movie("THX 1138", 1971)]
try:
session.add(m1)
session.add(d2)
session.commit()
except:
session.rollback()
alldata = session.query(Movie).all()
for somedata in alldata:
print somedata
'''