Changed all imports of slughifi and dcparser to new imports.
[wolnelektury.git] / catalogue / models.py
index b49747b..71c89a2 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.core.files import File
 
 from newtagging.models import TagBase
 from newtagging import managers
@@ -54,6 +55,14 @@ class Tag(TagBase):
     def __unicode__(self):
         return self.name
 
+    @staticmethod
+    def get_tag_list(tags):
+        if isinstance(tags, basestring):
+            tag_slugs = tags.split('/')
+            return [Tag.objects.get(slug=slug) for slug in tag_slugs]
+        else:
+            return TagBase.get_tag_list(tags)
+
 
 class Book(models.Model):
     title = models.CharField(_('title'), max_length=120)
@@ -62,6 +71,7 @@ class Book(models.Model):
     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)
@@ -89,6 +99,22 @@ class Book(models.Model):
     has_html_file.short_description = 'HTML'
     has_html_file.boolean = True
     
+    def save(self, **kwargs):
+        try:
+            from bin import book2html
+            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])
@@ -102,27 +128,17 @@ class Book(models.Model):
         return self.title
 
 
-# class Fragment(models.Model):
-#     id = models.IntegerField(primary_key=True)
-#     text = models.TextField(blank=True)
-#     start_paragraph = models.IntegerField(null=True, blank=True)
-#     book_id = models.IntegerField(null=True, blank=True)
-#     class Meta:
-#         db_table = u'fragment'
-
-
-# class Inflections(models.Model):
-#     word = models.CharField(max_length=120, primary_key=True)
-#     cases = models.TextField() # This field type is a guess.
-#     class Meta:
-#         db_table = u'inflections'
-
-
-# class Paragraph(models.Model):
-#     id = models.IntegerField(primary_key=True)
-#     number = models.IntegerField(null=True, blank=True)
-#     text = models.TextField(blank=True)
-#     book_id = models.IntegerField(null=True, blank=True)
-#     class Meta:
-#         db_table = u'paragraph'
+class Fragment(models.Model):
+    text = models.TextField()
+    short_text = models.TextField()
+    anchor = models.IntegerField()
+    book = models.ForeignKey(Book)
+    
+    objects = managers.ModelTaggedItemManager(Tag)
+    tags = managers.TagDescriptor(Tag)
+    
+    class Meta:
+        ordering = ('book', 'anchor',)
+        verbose_name = _('fragment')
+        verbose_name_plural = _('fragment')