import os
import datetime
import xml
from dynastie.generators.generator import DynastieGenerator
from dynastie.generators.rss import RSS
from xml.dom.minidom import getDOMImplementation
from django.db import models

class Atom(RSS):

    def generate(self, blog, src, output):
        from dynastie.models import Post, Blog

        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'})

        posts = Post.objects.filter(published=True).order_by('-modification_date')[:10]

        for post in posts:
            item = dom.createElement('entry')
            self.appendElement(dom, item, 'title', post.title)
            path = 'http://' + blog.name + post.getPath()
            self.appendElement(dom, item, 'link', '', {'rel':'alternate', 'type':'text/html', 'href':path})

            creationDate = post.creation_date.strftime('%Y-%m-%dT%H:%M:%SZ')
            self.appendElement(dom, item, 'published', creationDate)
            modificationDate = post.modification_date.strftime('%Y-%m-%dT%H:%M:%SZ')
            self.appendElement(dom, item, 'updated', modificationDate)
            self.appendElement(dom, item, 'id', path)
            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)
            
            filename = blog.src_path + '/_post/' + str(post.id)

            if not os.path.exists(filename):
                self.addError('File does not exists ' + filename)
                return

            f = open(filename, 'rb')
            post_content = '<![CDATA[' + f.read()  + ']]>'
            f.close()

            self.appendElement(dom, item, 'summary', post_content, {'type':'html'})
            self.appendElement(dom, item, 'content', post_content, {'type':'html'})
            root.appendChild(item)
            
        
        filename = 'atom.xml'
        self.writeIfNotTheSame(output + '/' + filename, root)

        if not self.somethingWrote:
            self.addReport('Nothing changed')

        return self.report

