Skip to content

Commit

Permalink
Refactor. Model schema moved to assets_helper.py from db.py and queri…
Browse files Browse the repository at this point in the history
…es.py.
  • Loading branch information
mcbuddha committed Mar 3, 2013
1 parent 83900b5 commit 3baafdd
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 30 deletions.
7 changes: 6 additions & 1 deletion assets_helper.py
Expand Up @@ -2,6 +2,11 @@
import queries
import datetime

FIELDS = ["asset_id", "name", "uri", "start_date",
"end_date", "duration", "mimetype", "is_enabled", "nocache"]

create_assets_table = 'CREATE TABLE assets(asset_id text primary key, name text, uri text, md5 text, start_date timestamp, end_date timestamp, duration text, mimetype text, is_enabled integer default 0, nocache integer default 0)'

get_time = datetime.datetime.utcnow


Expand Down Expand Up @@ -49,7 +54,7 @@ def create(conn, asset):
return asset


def read(conn, asset_id=None, keys=db.FIELDS):
def read(conn, asset_id=None, keys=FIELDS):
"""
Fetch one or more assets from the database.
Returns a list of dicts or one dict.
Expand Down
13 changes: 0 additions & 13 deletions db.py
Expand Up @@ -3,14 +3,8 @@

import queries

FIELDS = [
"asset_id", "name", "uri", "start_date",
"end_date", "duration", "mimetype", "is_enabled", "nocache"
]

conn = lambda db: sqlite3.connect(db, detect_types=sqlite3.PARSE_DECLTYPES)


@contextmanager
def cursor(connection):
cur = connection.cursor()
Expand All @@ -24,10 +18,3 @@ def commit(connection):
yield cur
connection.commit()
cur.close()


def create_assets_table(cur):
try:
cur.execute(queries.create_assets_table)
except sqlite3.OperationalError as _:
pass
15 changes: 2 additions & 13 deletions queries.py
@@ -1,20 +1,9 @@
create_assets_table = """
create table assets(
asset_id text primary key,
name text,
uri text,
md5 text,
start_date timestamp,
end_date timestamp,
duration text,
mimetype text,
is_enabled integer default 0,
nocache integer default 0)
"""

comma = ','.join
quest = lambda l: '=?,'.join(l) + '=?'

exists_table = "SELECT name FROM sqlite_master WHERE type='table' AND name='assets'"

read_all = lambda keys: 'select ' + comma(keys) + ' from assets order by name'
read = lambda keys: 'select ' + comma(keys) + ' from assets where asset_id=?'
create = lambda keys: 'insert into assets (' + comma(keys) + ') values (' + comma(['?'] * len(keys)) + ')'
Expand Down
5 changes: 4 additions & 1 deletion server.py
Expand Up @@ -26,6 +26,7 @@
from bottlehaml import haml_template

import db
import queries
import assets_helper

from utils import json_dump
Expand Down Expand Up @@ -354,7 +355,9 @@ def static(path):
global db_conn
db_conn = conn
with db.cursor(db_conn) as c:
db.create_assets_table(c)
c.execute(queries.exists_table)
if c.fetchone() is None:
c.execute(assets_helper.create_assets_table)
run(host=settings.get_listen_ip(),
port=settings.get_listen_port(),
reloader=True)
3 changes: 1 addition & 2 deletions server_test.py
Expand Up @@ -7,7 +7,6 @@

import assets_helper
import db
import queries

# fixtures chronology
#
Expand Down Expand Up @@ -65,7 +64,7 @@ def setUp(self):
self.assertEmpty = functools.partial(self.assertEqual, [])
self.conn = db.conn(':memory:')
with db.commit(self.conn) as cursor:
cursor.execute(queries.create_assets_table)
cursor.execute(assets_helper.create_assets_table)

def tearDown(self):
self.conn.close()
Expand Down

0 comments on commit 3baafdd

Please sign in to comment.