-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
52 lines (38 loc) · 2.07 KB
/
model.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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import sqlalchemy
import datetime
from sqlalchemy.orm import mapper, sessionmaker
engine = sqlalchemy.create_engine('sqlite:///.db/test.db', encoding='utf8', echo=True)
metadata = sqlalchemy.MetaData()
timesession_table = sqlalchemy.Table('sessions', metadata,
sqlalchemy.Column('id', sqlalchemy.Integer, primary_key=True),
sqlalchemy.Column('start_time', sqlalchemy.DateTime),
sqlalchemy.Column('end_time', sqlalchemy.DateTime),
sqlalchemy.Column('unit', sqlalchemy.Integer),
sqlalchemy.Column('description', sqlalchemy.String),
sqlalchemy.Column('tags', sqlalchemy.String)
)
tags_table = sqlalchemy.Table('tags', metadata,
sqlalchemy.Column('id', sqlalchemy.Integer, primary_key=True),
sqlalchemy.Column('name', sqlalchemy.String)
)
metadata.create_all(engine)
class TimeSession(object):
def __init__(self, start_time, end_time, unit, description, tags):
self.start_time = start_time
self.end_time = end_time
self.unit = unit
self.description = description
self.tags = tags
def __repr__(self):
return "<TimeSession('%s','%s','%s','%s','%s')>" % (self.start_time, self.end_time, self.unit, self.description, self.tags)
mapper(TimeSession, timesession_table)
new_timesession = TimeSession(datetime.datetime.now(), datetime.datetime.now()+datetime.timedelta(days=1), 1, u"Leírás", u"cimkék")
SessionMaker = sessionmaker(bind=engine)
session = SessionMaker()
session.add(new_timesession)
new_timesession = TimeSession(datetime.datetime.now(), datetime.datetime.now()+datetime.timedelta(days=1), 1, u"Leírás2", u"cimkék")
session.add(new_timesession)
our_timesession = session.query(TimeSession).filter_by(tags=u'cimkék').all()
print(our_timesession)