Generating HTML file dynamically from XML file on book save.
[wolnelektury.git] / catalogue / models.py
1 # -*- coding: utf-8 -*-
2 from django.db import models
3 from django.db.models import permalink
4 from django.utils.translation import ugettext_lazy as _
5 from django.contrib.auth.models import User
6 from django.core.files import File
7
8 from newtagging.models import TagBase
9 from newtagging import managers
10
11
12 TAG_CATEGORIES = (
13     ('author', _('author')),
14     ('epoch', _('epoch')),
15     ('kind', _('kind')),
16     ('genre', _('genre')),
17     ('theme', _('theme')),
18     ('set', _('set')),
19 )
20
21
22 class TagSubcategoryManager(models.Manager):
23     def __init__(self, subcategory):
24         super(TagSubcategoryManager, self).__init__()
25         self.subcategory = subcategory
26         
27     def get_query_set(self):
28         return super(TagSubcategoryManager, self).get_query_set().filter(category=self.subcategory)
29
30
31 class Tag(TagBase):
32     name = models.CharField(_('name'), max_length=50, unique=True, db_index=True)
33     slug = models.SlugField(_('slug'), unique=True, db_index=True)
34     sort_key = models.SlugField(_('sort key'), db_index=True)
35     category = models.CharField(_('category'), max_length=50, blank=False, null=False, 
36         db_index=True, choices=TAG_CATEGORIES)
37     description = models.TextField(blank=True)
38     
39     user = models.ForeignKey(User, blank=True, null=True)
40     
41     def has_description(self):
42         return len(self.description) > 0
43     has_description.short_description = _('Has description')
44     has_description.boolean = True
45
46     @permalink
47     def get_absolute_url(self):
48         return ('catalogue.views.tagged_book_list', [self.slug])
49     
50     class Meta:
51         ordering = ('sort_key',)
52         verbose_name = _('tag')
53         verbose_name_plural = _('tags')
54     
55     def __unicode__(self):
56         return self.name
57
58     @staticmethod
59     def get_tag_list(tags):
60         if isinstance(tags, basestring):
61             tag_slugs = tags.split('/')
62             return [Tag.objects.get(slug=slug) for slug in tag_slugs]
63         else:
64             return TagBase.get_tag_list(tags)
65
66
67 class Book(models.Model):
68     title = models.CharField(_('title'), max_length=120)
69     slug = models.SlugField(_('slug'), unique=True, db_index=True)
70     description = models.TextField(_('description'), blank=True)
71     created_at = models.DateTimeField(_('creation date'), auto_now=True)
72     
73     # Formats
74     xml_file = models.FileField(_('XML file'), upload_to='books/xml', blank=True)
75     pdf_file = models.FileField(_('PDF file'), upload_to='books/pdf', blank=True)
76     odt_file = models.FileField(_('ODT file'), upload_to='books/odt', blank=True)
77     html_file = models.FileField(_('HTML file'), upload_to='books/html', blank=True)
78     
79     objects = managers.ModelTaggedItemManager(Tag)
80     tags = managers.TagDescriptor(Tag)
81     
82     def has_description(self):
83         return len(self.description) > 0
84     has_description.short_description = _('Has description')
85     has_description.boolean = True
86     
87     def has_pdf_file(self):
88         return bool(self.pdf_file)
89     has_pdf_file.short_description = 'PDF'
90     has_pdf_file.boolean = True
91     
92     def has_odt_file(self):
93         return bool(self.odt_file)
94     has_odt_file.short_description = 'ODT'
95     has_odt_file.boolean = True
96     
97     def has_html_file(self):
98         return bool(self.html_file)
99     has_html_file.short_description = 'HTML'
100     has_html_file.boolean = True
101     
102     def save(self, **kwargs):
103         try:
104             from bin import book2html
105             from django.conf import settings
106             from os.path import splitext, basename
107             from tempfile import NamedTemporaryFile
108             
109             html_file = NamedTemporaryFile()
110             book2html.transform(self.xml_file.path, html_file)
111             
112             html_filename = '%s.html' % splitext(basename(self.xml_file.path))[0]
113             self.html_file.save(html_filename, File(html_file), save=False)
114         except ValueError:
115             pass
116
117         book = super(Book, self).save(**kwargs)
118     
119     @permalink
120     def get_absolute_url(self):
121         return ('catalogue.views.book_detail', [self.slug])
122         
123     class Meta:
124         ordering = ('title',)
125         verbose_name = _('book')
126         verbose_name_plural = _('books')
127
128     def __unicode__(self):
129         return self.title
130
131
132 # class Fragment(models.Model):
133 #     id = models.IntegerField(primary_key=True)
134 #     text = models.TextField(blank=True)
135 #     start_paragraph = models.IntegerField(null=True, blank=True)
136 #     book_id = models.IntegerField(null=True, blank=True)
137 #     class Meta:
138 #         db_table = u'fragment'
139
140
141 # class Inflections(models.Model):
142 #     word = models.CharField(max_length=120, primary_key=True)
143 #     cases = models.TextField() # This field type is a guess.
144 #     class Meta:
145 #         db_table = u'inflections'
146
147
148 # class Paragraph(models.Model):
149 #     id = models.IntegerField(primary_key=True)
150 #     number = models.IntegerField(null=True, blank=True)
151 #     text = models.TextField(blank=True)
152 #     book_id = models.IntegerField(null=True, blank=True)
153 #     class Meta:
154 #         db_table = u'paragraph'
155