2013-02-09 08:55:06 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-02-07 18:50:54 +01:00
|
|
|
"""
|
2014-01-04 13:55:30 +01:00
|
|
|
Copyright 2012-2014 Grégory Soutadé
|
2013-02-07 18:50:54 +01:00
|
|
|
|
|
|
|
This file is part of Dynastie.
|
|
|
|
|
|
|
|
Dynastie is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Dynastie is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Dynastie. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
"""
|
2012-08-25 22:33:34 +02:00
|
|
|
import os
|
|
|
|
import datetime
|
|
|
|
import xml
|
|
|
|
from dynastie.generators.rss import RSS
|
|
|
|
from xml.dom.minidom import getDOMImplementation
|
|
|
|
from django.db import models
|
2014-01-05 22:19:19 +01:00
|
|
|
from dynastie.generators import markdown2
|
2012-08-25 22:33:34 +02:00
|
|
|
|
|
|
|
class Atom(RSS):
|
|
|
|
|
|
|
|
def generate(self, blog, src, output):
|
2012-08-28 09:09:14 +02:00
|
|
|
from dynastie.models import Post, Blog
|
2012-08-25 22:33:34 +02:00
|
|
|
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
|
|
|
|
impl = getDOMImplementation()
|
|
|
|
|
|
|
|
dom = impl.createDocument(None, "feed", None)
|
|
|
|
root = dom.documentElement
|
|
|
|
root.setAttributeNS('xml', 'xml:lang', 'en-gb')
|
|
|
|
root.setAttribute('xmlns', 'http://www.w3.org/2005/Atom')
|
|
|
|
|
|
|
|
self.appendElement(dom, root, 'title', blog.title, {'type':'text'})
|
|
|
|
self.appendElement(dom, root, 'subtitle', blog.description, {'type':'text'})
|
|
|
|
address = 'http://' + blog.name
|
|
|
|
self.appendElement(dom, root, 'link', '', {'rel':'alternate', 'type':'text/html', 'href' : address})
|
|
|
|
self.appendElement(dom, root, 'id', address)
|
|
|
|
|
|
|
|
builddate = now.strftime('%Y-%m-%dT%H:%M:%SZ')
|
|
|
|
self.appendElement(dom, root, 'updated', builddate)
|
|
|
|
self.appendElement(dom, root, 'generator', 'The Dynastie project', {'uri':'http://indefero.soutade.fr/p/dynastie', 'version':'0.1'})
|
|
|
|
self.appendElement(dom, root, 'link', '', {'rel':'self', 'type':'application/atom+xml', 'href':address + '/atom.xml'})
|
|
|
|
|
2012-09-08 12:35:52 +02:00
|
|
|
posts = Post.objects.filter(published=True).order_by('-modification_date')[:10]
|
2012-08-25 22:33:34 +02:00
|
|
|
|
2012-08-28 09:09:14 +02:00
|
|
|
for post in posts:
|
2012-08-25 22:33:34 +02:00
|
|
|
item = dom.createElement('entry')
|
2012-08-28 09:09:14 +02:00
|
|
|
self.appendElement(dom, item, 'title', post.title)
|
|
|
|
path = 'http://' + blog.name + post.getPath()
|
2012-08-25 22:33:34 +02:00
|
|
|
self.appendElement(dom, item, 'link', '', {'rel':'alternate', 'type':'text/html', 'href':path})
|
|
|
|
|
2012-08-28 09:09:14 +02:00
|
|
|
creationDate = post.creation_date.strftime('%Y-%m-%dT%H:%M:%SZ')
|
2012-08-25 22:33:34 +02:00
|
|
|
self.appendElement(dom, item, 'published', creationDate)
|
2012-09-08 12:35:52 +02:00
|
|
|
modificationDate = post.modification_date.strftime('%Y-%m-%dT%H:%M:%SZ')
|
|
|
|
self.appendElement(dom, item, 'updated', modificationDate)
|
2012-08-25 22:33:34 +02:00
|
|
|
self.appendElement(dom, item, 'id', path)
|
2013-01-29 18:51:23 +01:00
|
|
|
try:
|
|
|
|
author = dom.createElement('author')
|
|
|
|
self.appendElement(dom, author, 'name', post.author.first_name + ' ' + post.author.last_name)
|
|
|
|
self.appendElement(dom, author, 'email', post.author.email)
|
|
|
|
item.appendChild(author)
|
|
|
|
except:
|
|
|
|
pass
|
2012-08-28 09:09:14 +02:00
|
|
|
filename = blog.src_path + '/_post/' + str(post.id)
|
2012-08-25 22:33:34 +02:00
|
|
|
|
|
|
|
if not os.path.exists(filename):
|
|
|
|
self.addError('File does not exists ' + filename)
|
|
|
|
return
|
|
|
|
|
|
|
|
f = open(filename, 'rb')
|
2022-06-19 10:12:51 +02:00
|
|
|
post_content = '<![CDATA[' + f.read().decode('utf-8') + ']]>'
|
2012-08-25 22:33:34 +02:00
|
|
|
f.close()
|
|
|
|
|
2014-01-05 22:19:19 +01:00
|
|
|
if post.content_format == Post.CONTENT_TEXT:
|
|
|
|
post_content = markdown2.markdown(post_content)
|
|
|
|
|
2012-08-28 09:09:14 +02:00
|
|
|
self.appendElement(dom, item, 'summary', post_content, {'type':'html'})
|
|
|
|
self.appendElement(dom, item, 'content', post_content, {'type':'html'})
|
2012-08-25 22:33:34 +02:00
|
|
|
root.appendChild(item)
|
|
|
|
|
|
|
|
|
|
|
|
filename = 'atom.xml'
|
|
|
|
self.writeIfNotTheSame(output + '/' + filename, root)
|
|
|
|
|
|
|
|
if not self.somethingWrote:
|
|
|
|
self.addReport('Nothing changed')
|
|
|
|
|
|
|
|
return self.report
|
|
|
|
|