From 98c4b6ccb2a1dbfa59dd630628c078c05aaa7ead Mon Sep 17 00:00:00 2001 From: posativ Date: Tue, 16 Oct 2012 21:07:29 +0200 Subject: [PATCH] move Comment class to isso.models --- isso/comments.py | 60 ------------------------------------------------ isso/db.py | 2 +- isso/models.py | 55 ++++++++++++++++++++++++++++++++++++++++++++ specs/test_db.py | 2 +- 4 files changed, 57 insertions(+), 62 deletions(-) create mode 100644 isso/models.py diff --git a/isso/comments.py b/isso/comments.py index acd4291..b39a802 100644 --- a/isso/comments.py +++ b/isso/comments.py @@ -1,67 +1,7 @@ -import time -import json - from werkzeug.wrappers import Response from werkzeug.exceptions import abort -class Comment(object): - """This class represents a regular comment. It needs at least a text - field, all other fields are optional (or automatically set by the - database driver. - - The field `mode` has a special meaning: - - 0: normal - 1: in moderation queue - 2: deleted - """ - - protected = ['id', 'mode', 'created', 'modified'] - fields = ['text', 'author', 'email', 'website', 'parent'] - - def __init__(self, **kw): - - 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): - return '' - - @property - def pending(self): - return self.mode == 1 - - @property - def deleted(self): - return self.mode == 2 - - def comment(app, environ, request, path, id=None): return Response('', 200) diff --git a/isso/db.py b/isso/db.py index f57fa58..9cc689f 100644 --- a/isso/db.py +++ b/isso/db.py @@ -5,7 +5,7 @@ import sqlite3 from os.path import join -from isso.comments import Comment +from isso.models import Comment class Abstract: diff --git a/isso/models.py b/isso/models.py new file mode 100644 index 0000000..be72633 --- /dev/null +++ b/isso/models.py @@ -0,0 +1,55 @@ + +import json +import time + +class Comment(object): + """This class represents a regular comment. It needs at least a text + field, all other fields are optional (or automatically set by the + database driver. + + The field `mode` has a special meaning: + + 0: normal + 1: in moderation queue + 2: deleted + """ + + protected = ['id', 'mode', 'created', 'modified'] + fields = ['text', 'author', 'email', 'website', 'parent'] + + def __init__(self, **kw): + + 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): + + data = json.loads(data) + 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): + return '' + + @property + def pending(self): + return self.mode == 1 + + @property + def deleted(self): + return self.mode == 2 diff --git a/specs/test_db.py b/specs/test_db.py index a0fdb02..8098390 100644 --- a/specs/test_db.py +++ b/specs/test_db.py @@ -5,7 +5,7 @@ import time import tempfile import unittest -from isso.comments import Comment +from isso.models import Comment from isso.db import SQLite