add Comment.fromjson and update table definitions
This commit is contained in:
parent
3bb830e86c
commit
48cff63d4c
@ -1,5 +1,9 @@
|
|||||||
|
|
||||||
|
import time
|
||||||
|
import json
|
||||||
|
|
||||||
from werkzeug.wrappers import Response
|
from werkzeug.wrappers import Response
|
||||||
|
from werkzeug.exceptions import abort
|
||||||
|
|
||||||
|
|
||||||
class Comment(object):
|
class Comment(object):
|
||||||
@ -14,15 +18,37 @@ class Comment(object):
|
|||||||
2: deleted
|
2: deleted
|
||||||
"""
|
"""
|
||||||
|
|
||||||
fields = ['text', 'author', 'email', 'website', 'id', 'parent', 'timestamp', 'mode']
|
protected = ['id', 'mode', 'created', 'modified']
|
||||||
|
fields = ['text', 'author', 'email', 'website', 'parent']
|
||||||
|
|
||||||
def __init__(self, **kw):
|
def __init__(self, **kw):
|
||||||
|
|
||||||
for field in self.fields:
|
for field in self.protected + self.fields:
|
||||||
if field == 'text' and field not in kw:
|
|
||||||
raise ValueError('Comment needs at least text, but no text was provided.')
|
|
||||||
self.__dict__[field] = kw.get(field)
|
self.__dict__[field] = kw.get(field)
|
||||||
|
|
||||||
|
def iteritems(self, protected=False):
|
||||||
|
for field in self.fields:
|
||||||
|
yield field, getattr(self, field)
|
||||||
|
if protected:
|
||||||
|
for field in self.protected:
|
||||||
|
yield field, getattr(self, field)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def fromjson(self, data):
|
||||||
|
|
||||||
|
try:
|
||||||
|
data = json.loads(data)
|
||||||
|
except ValueError:
|
||||||
|
abort(400)
|
||||||
|
|
||||||
|
comment = Comment(created=time.time())
|
||||||
|
|
||||||
|
for field in self.fields:
|
||||||
|
if field == 'text' and field not in data:
|
||||||
|
raise ValueError('Comment needs at least text, but no text was provided.')
|
||||||
|
comment.__dict__[field] = data.get(field)
|
||||||
|
|
||||||
|
return comment
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def json(self):
|
def json(self):
|
||||||
|
29
isso/db.py
29
isso/db.py
@ -1,6 +1,5 @@
|
|||||||
|
|
||||||
import abc
|
import abc
|
||||||
import time
|
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
|
||||||
from os.path import join
|
from os.path import join
|
||||||
@ -25,15 +24,15 @@ class Abstract:
|
|||||||
return
|
return
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def update(self, path, comment):
|
def update(self, path, id, comment):
|
||||||
return
|
return
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def delete(self, path, comment):
|
def delete(self, path):
|
||||||
return
|
return
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def retrieve(self, path, limit=20):
|
def retrieve(self, path, limit):
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
@ -44,7 +43,7 @@ class SQLite(Abstract):
|
|||||||
post) are ordered by that id."""
|
post) are ordered by that id."""
|
||||||
|
|
||||||
fields = [
|
fields = [
|
||||||
'id', 'path', 'timestamp',
|
'id', 'path', 'created', 'modified',
|
||||||
'text', 'author', 'email', 'website', 'parent', 'mode'
|
'text', 'author', 'email', 'website', 'parent', 'mode'
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -55,9 +54,9 @@ class SQLite(Abstract):
|
|||||||
|
|
||||||
with sqlite3.connect(self.dbpath) as con:
|
with sqlite3.connect(self.dbpath) as con:
|
||||||
sql = ('main.comments (id INTEGER NOT NULL, path VARCHAR(255) NOT NULL,'
|
sql = ('main.comments (id INTEGER NOT NULL, path VARCHAR(255) NOT NULL,'
|
||||||
'timestamp FLOAT NOT NULL, text VARCHAR, author VARCHAR(64),'
|
'created FLOAT NOT NULL, modified FLOAT, text VARCHAR,'
|
||||||
'email VARCHAR(64), website VARCHAR(64), parent INTEGER, mode INTEGER,'
|
'author VARCHAR(64), email VARCHAR(64), website VARCHAR(64),'
|
||||||
'PRIMARY KEY (id, path))')
|
'parent INTEGER, mode INTEGER, PRIMARY KEY (id, path))')
|
||||||
con.execute("CREATE TABLE IF NOT EXISTS %s;" % sql)
|
con.execute("CREATE TABLE IF NOT EXISTS %s;" % sql)
|
||||||
|
|
||||||
# increment id if (id, path) is no longer unique
|
# increment id if (id, path) is no longer unique
|
||||||
@ -74,8 +73,8 @@ class SQLite(Abstract):
|
|||||||
|
|
||||||
def query2comment(self, query):
|
def query2comment(self, query):
|
||||||
return Comment(
|
return Comment(
|
||||||
text=query[3], author=query[4], email=query[5], website=query[6],
|
text=query[4], author=query[5], email=query[6], website=query[7],
|
||||||
parent=query[7], timestamp=query[2], id=query[0], mode=query[8]
|
parent=query[8], mode=query[9], id=query[0], created=query[2], modified=query[3]
|
||||||
)
|
)
|
||||||
|
|
||||||
def add(self, path, c):
|
def add(self, path, c):
|
||||||
@ -83,14 +82,16 @@ class SQLite(Abstract):
|
|||||||
keys = ','.join(self.fields)
|
keys = ','.join(self.fields)
|
||||||
values = ','.join('?'*len(self.fields))
|
values = ','.join('?'*len(self.fields))
|
||||||
con.execute('INSERT INTO comments (%s) VALUES (%s);' % (keys, values), (
|
con.execute('INSERT INTO comments (%s) VALUES (%s);' % (keys, values), (
|
||||||
0, path, time.time(), c.text, c.author, c.email, c.website,
|
0, path, c.created, c.modified, c.text, c.author, c.email, c.website,
|
||||||
c.parent, self.mode)
|
c.parent, self.mode)
|
||||||
)
|
)
|
||||||
|
|
||||||
def update(self, path, comment):
|
def update(self, path, id, comment):
|
||||||
return
|
with sqlite3.connect(self.dbpath) as con:
|
||||||
|
for field, value in comment.iteritems():
|
||||||
|
con.execute('UPDATE comments SET ?=? WHERE id=?;', (field, value, id))
|
||||||
|
|
||||||
def delete(self, path, comment):
|
def delete(self, path, id):
|
||||||
return
|
return
|
||||||
|
|
||||||
def retrieve(self, path, limit=20):
|
def retrieve(self, path, limit=20):
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
|
import json
|
||||||
import tempfile
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
@ -7,6 +8,10 @@ from isso.comments import Comment
|
|||||||
from isso.db import SQLite
|
from isso.db import SQLite
|
||||||
|
|
||||||
|
|
||||||
|
def comment(**kw):
|
||||||
|
return Comment.fromjson(json.dumps(kw))
|
||||||
|
|
||||||
|
|
||||||
class TestSQLite(unittest.TestCase):
|
class TestSQLite(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
@ -17,9 +22,9 @@ class TestSQLite(unittest.TestCase):
|
|||||||
|
|
||||||
def test_add(self):
|
def test_add(self):
|
||||||
|
|
||||||
self.db.add('/', Comment(text='Foo'))
|
self.db.add('/', comment(text='Foo'))
|
||||||
self.db.add('/', Comment(text='Bar'))
|
self.db.add('/', comment(text='Bar'))
|
||||||
self.db.add('/path/', Comment(text='Baz'))
|
self.db.add('/path/', comment(text='Baz'))
|
||||||
|
|
||||||
rv = list(self.db.retrieve('/'))
|
rv = list(self.db.retrieve('/'))
|
||||||
assert rv[0].id == 2
|
assert rv[0].id == 2
|
||||||
|
Loading…
Reference in New Issue
Block a user