Commit 3416548640378cbf50e7294cd2a1b38af62e18f2

Fixed errors from timdb.py.
timApp/timdb.py
(14 / 40)
  
33
44BlockType = Enum('BlockType', 'DocumentBlock Comment Note Answer')
55
6class TimDb(object):
7 '''
8 classdocs
9 '''
106
11 def __init__(self, app, g):
12 '''
13 Constructor
14 '''
15 self.app = app
16 self.g = g
7class TimDb(object):
8 def __init__(self, db_path):
9 self.db = sqlite3.connect(db_path)
10 self.db.row_factory = sqlite3.Row
1711
18 def init(self):
19 with self.app.app_context():
20 db = self.getDb()
21 with self.app.open_resource('schema.sql', mode='r') as f:
22 db.cursor().executescript(f.read())
23 db.commit()
24
25 def connect(self):
26 """Connects to the specific database."""
27 rv = sqlite3.connect(self.app.config['DATABASE'])
28 rv.row_factory = sqlite3.Row
29 return rv
12 def init(self, f):
13 self.db.cursor().executescript(f.read())
14 self.db.commit()
3015
31 def getDb(self):
32 """Opens a new database connection if there is none yet for the
33 current application context.
34 """
35 if not hasattr(self.g, 'sqlite_db'):
36 self.g.sqlite_db = connect()
37 return self.g.sqlite_db
38
3916 def createUser(self, name):
4017 """Creates a new user with the specified name."""
41 db = getDb()
42 db.execute('insert into User (name) values (?)', [name])
43 db.commit()
18 self.db.execute('insert into User (name) values (?)', [name])
19 self.db.commit()
4420
4521 def createDocument(self, name):
4622 """Creates a new document with the specified name."""
47 db = getDb()
48 db.execute('insert into Document (name) values (?)', [name])
49 db.commit()
23 self.db.execute('insert into Document (name) values (?)', [name])
24 self.db.commit()
5025 #TODO: Create a file for the document in file system.
5126 #TODO: Put the document file under version control (using a Git module maybe?).
5227 return
5328
5429 def addMarkDownBlock(self, document_id, content, previous_block_id):
5530 """Adds a new markdown block to the specified document."""
56 db = getDb()
57 db.execute('insert into Block (type_id) values (?)', [BlockType.DocumentBlock])
58 block_id = db.last_insert_rowid()
31 self.db.execute('insert into Block (type_id) values (?)', [BlockType.DocumentBlock])
32 block_id = self.db.cursor().rowcount
5933 print(block_id)
60 db.commit()
34 self.db.commit()
6135 #TODO: Create a file for the block using its id as the file name.
6236 #TODO: Modify the document file appropriately.
6337 return