-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
173 lines (135 loc) · 6.17 KB
/
database.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
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
'''
Created on 8 avr. 2014
@author: fcs
'''
import os
from sqlalchemy.engine import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, column_property, backref
from sqlalchemy.orm.session import sessionmaker
from sqlalchemy.sql.schema import Column, ForeignKey, Table
from sqlalchemy.sql.sqltypes import Integer, String, Boolean
Base = declarative_base()
class Location(Base):
__tablename__ = 'location'
id = Column(Integer, primary_key=True)
name = Column(String(50))
code = Column(String(50))
type = Column(Integer)
parent_id = Column(Integer, ForeignKey('location.id'))
children = relationship("Location",
backref=backref('parent', remote_side=[id]))
criteria_location = Table('criteria_location', Base.metadata,
Column('location_id', Integer, ForeignKey('location.id')),
Column('criteria_id', Integer, ForeignKey('criteria.id'))
)
class Action(Base):
__tablename__ = 'action'
id = Column(Integer, primary_key=True)
name = Column(String(50))
config = Column(String(250))
type = Column(String(50))
success = Column(Integer, ForeignKey('action.id'))
failure = Column(Integer, ForeignKey('action.id'))
criteria_id = Column(Integer, ForeignKey('criteria.id'))
class Site(Base):
__tablename__ = 'site'
id = Column(Integer, primary_key=True)
name = Column(String(20))
criteria_site = Table('criteria_site', Base.metadata,
Column('site_id', Integer, ForeignKey('site.id')),
Column('criteria_id', Integer, ForeignKey('criteria.id'))
)
class Category(Base):
__tablename__ = 'category'
id = Column(Integer, primary_key=True)
name = Column(String(20))
criteria_category = Table('criteria_category', Base.metadata,
Column('category_id', Integer, ForeignKey('category.id')),
Column('criteria_id', Integer, ForeignKey('criteria.id'))
)
class Criteria(Base):
__tablename__ = 'criteria'
id = Column(Integer, primary_key=True)
active = Column(Boolean)
name = Column(String(50))
query = Column(String(120))
#post_code = Column(String(250), nullable=False)
#prices = column_property()
#person = relationship(Person)
locations = relationship("Location", secondary=criteria_location)
sites = relationship("Site", secondary=criteria_site)
categories = relationship("Category", secondary=criteria_category)
categories = relationship("Category", secondary=criteria_category)
action = relationship("Action", uselist=False, backref="criteria")
#TODO : deplacer vers la table Search
#site = Column(String(50))
#site = Column('user_id', Integer, ForeignKey('users.user_id')),
#self.actions=[]
price_min = Column(Integer)
price_max = Column(Integer)
@property
def prices(self):
return {'min':self.price_min,'max':self.price_max}
def createCategories(session):
session.add(Category(id=0,name="velo"))
session.add(Category(id=1,name="venteImmo"))
session.add(Category(id=2,name="electromenager"))
session.add(Category(id=3,name="jeux"))
session.add(Category(id=4,name="equipementBebe"))
session.add(Category(id=5,name="equipementMoto"))
session.add(Category(id=6,name="ameublement"))
session.add(Category(id=7,name="equipementAuto"))
session.add(Category(id=8,name="deco"))
session.add(Category(id=9,name="jardinage"))
session.add(Category(id=10,name="photo"))
session.add(Category(id=11,name="informatique"))
session.commit()
def createLocation(session):
pibrac = Location(name='Pibrac', type='town')
gragnague = Location(name='Gragnague', type='town')
haute_garonne = Location(name='haute_garonne',code='haute_garonne', type='departement', children=[pibrac, gragnague])
MI_PI = Location(name='midi_pyrenees',code='midi_pyrenees', type='region',children=[haute_garonne])
session.add(MI_PI)
def createSites(session):
LBC = Site(name='LBC')
session.add(LBC)
def createTestDatas(session):
mail = Action(config="{'mailadress':'awikatchikaen@free.fr'}", type="Mailer")
push = Action(type="Pushover")
c1 = Criteria(name="test search",
sites=[session.query(Site).filter_by(name='LBC').one()],
#locations=[session.query(Location).filter_by(name='Pibrac').one(),session.query(Location).filter_by(name='Gragnague').one()],
locations=[session.query(Location).filter_by(name='Pibrac').one()],
price_min=10,
price_max=90000,
categories=[session.query(Category).filter_by(name='venteImmo').one()],
query="Terrain",
action=push
);
session.add(c1)
if __name__ == '__main__':
# Create an engine that stores data in the local directory's
# sqlalchemy_example.db file.
if os.path.exists("C:\Windows\Temp\sqlalchemy_example.db"):
os.remove("C:\Windows\Temp\sqlalchemy_example.db")
db = create_engine('sqlite:///C:\Windows\Temp\sqlalchemy_example.db')
#engine = create_engine('sqlite', opts={'filename': 'C:\\Windows\\Temp\\sqlalchemy_example.db'})
# Create all tables in the engine. This is equivalent to "Create Table"
# statements in raw SQL.
Base.metadata.create_all(db)
Base.metadata.bind = db
DBSession = sessionmaker(bind=db)
# A DBSession() instance establishes all conversations with the database
# and represents a "staging zone" for all the objects loaded into the
# database session object. Any change made against the objects in the
# session won't be persisted into the database until you call
# session.commit(). If you're not happy about the changes, you can
# revert all of them back to the last commit by calling
# session.rollback()
session = DBSession()
createCategories(session)
createLocation(session)
createSites(session)
createTestDatas(session)
session.commit()