add Comment.fromjson and update table definitions

This commit is contained in:
posativ 2012-10-16 20:20:29 +02:00
parent 3bb830e86c
commit 48cff63d4c
3 changed files with 53 additions and 21 deletions

View File

@ -1,5 +1,9 @@
import time
import json
from werkzeug.wrappers import Response
from werkzeug.exceptions import abort
class Comment(object):
@ -14,15 +18,37 @@ class Comment(object):
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):
for field in self.fields:
if field == 'text' and field not in kw:
raise ValueError('Comment needs at least text, but no text was provided.')
for field in self.protected + self.fields:
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
def json(self):

View File

@ -1,6 +1,5 @@
import abc
import time
import sqlite3
from os.path import join
@ -25,15 +24,15 @@ class Abstract:
return
@abc.abstractmethod
def update(self, path, comment):
def update(self, path, id, comment):
return
@abc.abstractmethod
def delete(self, path, comment):
def delete(self, path):
return
@abc.abstractmethod
def retrieve(self, path, limit=20):
def retrieve(self, path, limit):
return
@ -44,7 +43,7 @@ class SQLite(Abstract):
post) are ordered by that id."""
fields = [
'id', 'path', 'timestamp',
'id', 'path', 'created', 'modified',
'text', 'author', 'email', 'website', 'parent', 'mode'
]
@ -55,9 +54,9 @@ class SQLite(Abstract):
with sqlite3.connect(self.dbpath) as con:
sql = ('main.comments (id INTEGER NOT NULL, path VARCHAR(255) NOT NULL,'
'timestamp FLOAT NOT NULL, text VARCHAR, author VARCHAR(64),'
'email VARCHAR(64), website VARCHAR(64), parent INTEGER, mode INTEGER,'
'PRIMARY KEY (id, path))')
'created FLOAT NOT NULL, modified FLOAT, text VARCHAR,'
'author VARCHAR(64), email VARCHAR(64), website VARCHAR(64),'
'parent INTEGER, mode INTEGER, PRIMARY KEY (id, path))')
con.execute("CREATE TABLE IF NOT EXISTS %s;" % sql)
# increment id if (id, path) is no longer unique
@ -74,8 +73,8 @@ class SQLite(Abstract):
def query2comment(self, query):
return Comment(
text=query[3], author=query[4], email=query[5], website=query[6],
parent=query[7], timestamp=query[2], id=query[0], mode=query[8]
text=query[4], author=query[5], email=query[6], website=query[7],
parent=query[8], mode=query[9], id=query[0], created=query[2], modified=query[3]
)
def add(self, path, c):
@ -83,14 +82,16 @@ class SQLite(Abstract):
keys = ','.join(self.fields)
values = ','.join('?'*len(self.fields))
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)
)
def update(self, path, comment):
return
def update(self, path, id, comment):
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
def retrieve(self, path, limit=20):

View File

@ -1,5 +1,6 @@
import os
import json
import tempfile
import unittest
@ -7,6 +8,10 @@ from isso.comments import Comment
from isso.db import SQLite
def comment(**kw):
return Comment.fromjson(json.dumps(kw))
class TestSQLite(unittest.TestCase):
def setUp(self):
@ -17,9 +22,9 @@ class TestSQLite(unittest.TestCase):
def test_add(self):
self.db.add('/', Comment(text='Foo'))
self.db.add('/', Comment(text='Bar'))
self.db.add('/path/', Comment(text='Baz'))
self.db.add('/', comment(text='Foo'))
self.db.add('/', comment(text='Bar'))
self.db.add('/path/', comment(text='Baz'))
rv = list(self.db.retrieve('/'))
assert rv[0].id == 2