Commit 9149d70c9013773386ff4b630cd8d4e3910e7425

Added some database method stubs.
timApp/tim.py
(43 / 5)
  
99import json
1010import os
1111import sqlite3
12from enum import Enum
1213
14BlockType = Enum('BlockType', 'DocumentBlock Comment Note Answer')
1315app = Flask(__name__)
1416app.config.from_object(__name__)
1517
2626app.config.from_envvar('TIM_SETTINGS', silent=True)
2727STATIC_PATH = "./static/"
2828
29def init_db():
29def initDb():
3030 with app.app_context():
31 db = get_db()
31 db = getDb()
3232 with app.open_resource('schema.sql', mode='r') as f:
3333 db.cursor().executescript(f.read())
3434 db.commit()
3535
36def connect_db():
36def connectDb():
3737 """Connects to the specific database."""
3838 rv = sqlite3.connect(app.config['DATABASE'])
3939 rv.row_factory = sqlite3.Row
4040 return rv
4141
42def get_db():
42def getDb():
4343 """Opens a new database connection if there is none yet for the
4444 current application context.
4545 """
4646 if not hasattr(g, 'sqlite_db'):
47 g.sqlite_db = connect_db()
47 g.sqlite_db = connectDb()
4848 return g.sqlite_db
49
50def createUser(name):
51 """Creates a new user with the specified name."""
52 db = getDb()
53 db.execute('insert into User (name) values (?)', [name])
54 db.commit()
55
56def createDocument(name):
57 """Creates a new document with the specified name."""
58 db = getDb()
59 db.execute('insert into Document (name) values (?)', [name])
60 db.commit()
61 #TODO: Create a file for the document in file system.
62 #TODO: Put the document file under version control (using a Git module maybe?).
63 return
64
65def addMarkDownBlock(document_id, content, previous_block_id):
66 """Adds a new markdown block to the specified document."""
67 db = getDb()
68 db.execute('insert into Block (type_id) values (?)', [BlockType.DocumentBlock])
69 block_id = db.last_insert_rowid()
70 print(block_id)
71 db.commit()
72 #TODO: Create a file for the block using its id as the file name.
73 #TODO: Modify the document file appropriately.
74 return
75
76def modifyMarkDownBlock(block_id, new_content):
77 return
78
79def createDocumentFromBlocks(dir, document_name):
80 """
81 Creates a document from existing blocks in the specified directory.
82 The blocks should be ordered alphabetically.
83 """
84 return
4985
5086@app.teardown_appcontext
5187def close_db(error):