+ def get_comment_header(self, comment):
+ vars = comment.to_vars()
+ if comment.link:
+ vars['linked_author'] = '<a href="%s">%s</a>' \
+ % (comment.link, comment.author)
+ else:
+ vars['linked_author'] = comment.author
+ return self.get_template(
+ 'com_header', default_comment_header, vars)
+
+ def get_comment_footer(self, comment):
+ return self.get_template(
+ 'com_footer', default_comment_footer, comment.to_vars())
+
+ def get_comment_form(self, article, form_data, captcha_puzzle):
+ vars = article.to_vars()
+ vars.update(form_data.to_vars(self))
+ vars['captcha_puzzle'] = captcha_puzzle
+ return self.get_template(
+ 'com_form', default_comment_form, vars)
+
+ def get_comment_error(self, error):
+ return self.get_template(
+ 'com_error', default_comment_error, dict(error=error))
+
+
+class CommentFormData (object):
+ def __init__(self, author = '', link = '', captcha = '', body = ''):
+ self.author = author
+ self.link = link
+ self.captcha = captcha
+ self.body = body
+ self.author_error = ''
+ self.link_error = ''
+ self.captcha_error = ''
+ self.body_error = ''
+ self.action = ''
+ self.method = 'post'
+
+ def to_vars(self, template):
+ render_error = template.get_comment_error
+ a_error = self.author_error and render_error(self.author_error)
+ l_error = self.link_error and render_error(self.link_error)
+ c_error = self.captcha_error \
+ and render_error(self.captcha_error)
+ b_error = self.body_error and render_error(self.body_error)
+ return {
+ 'form_author': sanitize(self.author),
+ 'form_link': sanitize(self.link),
+ 'form_captcha': sanitize(self.captcha),
+ 'form_body': sanitize(self.body),
+
+ 'form_author_error': a_error,
+ 'form_link_error': l_error,
+ 'form_captcha_error': c_error,
+ 'form_body_error': b_error,
+
+ 'form_action': self.action,
+ 'form_method': self.method,
+ }
+
+
+class Comment (object):
+ def __init__(self, article, number, created = None):
+ self.article = article
+ self.number = number
+ if created is None:
+ self.created = datetime.datetime.now()
+ else:
+ self.created = created
+
+ self.loaded = False
+
+ # loaded on demand
+ self._author = author
+ self._link = ''
+ self._raw_content = 'Removed comment'
+
+
+ def get_author(self):
+ if not self.loaded:
+ self.load()
+ return self._author
+ author = property(fget = get_author)
+
+ def get_link(self):
+ if not self.loaded:
+ self.load()
+ return self._link
+ link = property(fget = get_link)
+
+ def get_raw_content(self):
+ if not self.loaded:
+ self.load()
+ return self._raw_content
+ raw_content = property(fget = get_raw_content)
+
+
+ def set(self, author, raw_content, link = '', created = None):
+ self.loaded = True
+ self._author = author
+ self._raw_content = raw_content
+ self._link = link
+ self.created = created or datetime.datetime.now()
+
+
+ def load(self):
+ filename = os.path.join(comments_path, self.article.uuid,
+ str(self.number))
+ try:
+ raw = open(filename).readlines()
+ except:
+ return
+
+ count = 0
+ for l in raw:
+ if ':' in l:
+ name, value = l.split(':', 1)
+ if name.lower() == 'author':
+ self._author = value.strip()
+ elif name.lower() == 'link':
+ self._link = value.strip()
+ elif l == '\n':
+ # end of header
+ break
+ count += 1
+ self._raw_content = ''.join(raw[count + 1:])
+ self.loaded = True
+
+ def save(self):
+ filename = os.path.join(comments_path, self.article.uuid,
+ str(self.number))
+ try:
+ f = open(filename, 'w')
+ f.write('Author: %s\n' % self.author)
+ f.write('Link: %s\n' % self.link)
+ f.write('\n')
+ f.write(self.raw_content)
+ except:
+ return
+
+
+ def to_html(self):
+ return rst_to_html(self.raw_content)
+
+ def to_vars(self):
+ return {
+ 'number': self.number,
+ 'author': sanitize(self.author),
+ 'link': sanitize(self.link),
+ 'date': self.created.isoformat(' '),
+ 'created': self.created.isoformat(' '),
+
+ 'year': self.created.year,
+ 'month': self.created.month,
+ 'day': self.created.day,
+ 'hour': self.created.hour,
+ 'minute': self.created.minute,
+ 'second': self.created.second,
+ }
+
+class CommentDB (object):
+ def __init__(self, article):
+ self.path = os.path.join(comments_path, article.uuid)
+ self.comments = []
+ self.load(article)
+
+ def load(self, article):
+ try:
+ f = open(os.path.join(self.path, 'db'))
+ except:
+ return
+
+ for l in f:
+ # Each line has the following comma separated format:
+ # number, created (epoch)
+ # Empty lines are meaningful and represent removed
+ # comments (so we can preserve the comment number)
+ l = l.split(',')
+ try:
+ n = int(l[0])
+ d = datetime.datetime.fromtimestamp(float(l[1]))
+ except:
+ # Removed/invalid comment
+ self.comments.append(None)
+ continue
+ self.comments.append(Comment(article, n, d))
+
+ def save(self):
+ old_db = os.path.join(self.path, 'db')
+ new_db = os.path.join(self.path, 'db.tmp')
+ f = open(new_db, 'w')
+ for c in self.comments:
+ s = ''
+ if c is not None:
+ s = ''
+ s += str(c.number) + ', '
+ s += str(time.mktime(c.created.timetuple()))
+ s += '\n'
+ f.write(s)
+ f.close()
+ os.rename(new_db, old_db)
+