Generating HTML file dynamically from XML file on book save.
authorMarek Stępniowski <marek@stepniowski.com>
Thu, 28 Aug 2008 16:28:56 +0000 (18:28 +0200)
committerMarek Stępniowski <marek@stepniowski.com>
Thu, 28 Aug 2008 16:28:56 +0000 (18:28 +0200)
catalogue/models.py

index 483741b..f4d2803 100644 (file)
@@ -3,6 +3,7 @@ from django.db import models
 from django.db.models import permalink
 from django.utils.translation import ugettext_lazy as _
 from django.contrib.auth.models import User
 from django.db.models import permalink
 from django.utils.translation import ugettext_lazy as _
 from django.contrib.auth.models import User
+from django.core.files import File
 
 from newtagging.models import TagBase
 from newtagging import managers
 
 from newtagging.models import TagBase
 from newtagging import managers
@@ -70,6 +71,7 @@ class Book(models.Model):
     created_at = models.DateTimeField(_('creation date'), auto_now=True)
     
     # Formats
     created_at = models.DateTimeField(_('creation date'), auto_now=True)
     
     # Formats
+    xml_file = models.FileField(_('XML file'), upload_to='books/xml', blank=True)
     pdf_file = models.FileField(_('PDF file'), upload_to='books/pdf', blank=True)
     odt_file = models.FileField(_('ODT file'), upload_to='books/odt', blank=True)
     html_file = models.FileField(_('HTML file'), upload_to='books/html', blank=True)
     pdf_file = models.FileField(_('PDF file'), upload_to='books/pdf', blank=True)
     odt_file = models.FileField(_('ODT file'), upload_to='books/odt', blank=True)
     html_file = models.FileField(_('HTML file'), upload_to='books/html', blank=True)
@@ -97,6 +99,23 @@ class Book(models.Model):
     has_html_file.short_description = 'HTML'
     has_html_file.boolean = True
     
     has_html_file.short_description = 'HTML'
     has_html_file.boolean = True
     
+    def save(self, **kwargs):
+        try:
+            from bin import book2html
+            from django.conf import settings
+            from os.path import splitext, basename
+            from tempfile import NamedTemporaryFile
+            
+            html_file = NamedTemporaryFile()
+            book2html.transform(self.xml_file.path, html_file)
+            
+            html_filename = '%s.html' % splitext(basename(self.xml_file.path))[0]
+            self.html_file.save(html_filename, File(html_file), save=False)
+        except ValueError:
+            pass
+
+        book = super(Book, self).save(**kwargs)
+    
     @permalink
     def get_absolute_url(self):
         return ('catalogue.views.book_detail', [self.slug])
     @permalink
     def get_absolute_url(self):
         return ('catalogue.views.book_detail', [self.slug])