isso/isso/models.py
Martin Zimmermann f6271e5cf6 commit work in progress
completely revamp JS client because JS sucks^W^W^W to feature AMD,
require.js, promises and HTML.js.

The comment listing is now more like Disqus and for now comment
retrieval, comment creation and deletion works. Form validation is
rudimentary implemented as well.

replaced Mako with Jinja2 (because... I forgot.), admin interface will
use Bootstrap™ but is not functional yet.

features a progress indicator in case you're sqlite db performs *really*
bad
2013-09-05 19:31:18 +02:00

67 lines
1.8 KiB
Python

# -*- encoding: utf-8 -*-
#
# Copyright 2012, Martin Zimmermann <info@posativ.org>. All rights reserved.
# License: BSD Style, 2 clauses. see isso/__init__.py
import json
import time
import hashlib
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:
1: normal
2: in moderation queue
4: deleted
You can query for them like with UNIX permission bits, so you get both
normal and queued using MODE=3.
"""
protected = ['path', '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=True):
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 pending(self):
return self.mode == 2
@property
def deleted(self):
return self.mode == 4
@property
def md5(self):
hv = hashlib.md5()
for key in set(self.fields) - set(['parent', ]):
hv.update(getattr(self, key).encode('utf-8', errors="replace") or u'')
return hv.hexdigest()