- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmodels.py
More file actions
Latest commit
48 lines (40 loc) · 1.52 KB
/
Copy pathmodels.py
File metadata and controls
48 lines (40 loc) · 1.52 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
fromsqlalchemyimportcreate_engine, Column, Integer, String
fromsqlalchemy.ext.declarativeimportdeclarative_base
fromsqlalchemy.ormimportscoped_session, sessionmaker
fromzope.sqlalchemyimportZopeTransactionExtension
# DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
DB_URI='sqlite:///stuff.db'
Session=sessionmaker(autocommit=False,
autoflush=False,
bind=create_engine(DB_URI))
session=scoped_session(Session)
Base=declarative_base()
# Note Model
classNote(Base):
__tablename__='notes'
id=Column(Integer, primary_key=True)
title=Column(String(50))
description=Column(String(50))
created_at=Column(String(50))
created_by=Column(String(50))
priority=Column(Integer)
def__init__(self, title, description, created_at ,created_by, priority):
self.title=title
self.description=description
self.created_at=created_at
self.created_by=created_by
self.priority=priority
@classmethod
deffrom_json(cls, data):
returncls(**data)
defto_json(self):
to_serialize= ['id', 'title', 'description', 'created_at', 'created_by', 'priority']
d= {}
forattr_nameinto_serialize:
d[attr_name] =getattr(self, attr_name)
returnd
# creates database
if__name__=="__main__":
engine=create_engine(DB_URI)
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)