+
+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'
+
+ @property
+ def author(self):
+ if not self.loaded:
+ self.load()
+ return self._author
+
+ @property
+ def link(self):
+ if not self.loaded:
+ self.load()
+ return self._link
+
+ @property
+ def raw_content(self):
+ if not self.loaded:
+ self.load()
+ return self._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)
+ # if comments were enabled after the article was added, we
+ # will need to create the directory
+ if not os.path.exists(self.path):
+ os.mkdir(self.path, 0777)
+
+ 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)