Fixed importing books.
[wolnelektury.git] / apps / catalogue / models.py
1 # -*- coding: utf-8 -*-
2 from django.db import models
3 from django.db.models import permalink, Q
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 from django.template.loader import render_to_string
8 from django.utils.safestring import mark_safe
9
10 from newtagging.models import TagBase
11 from newtagging import managers
12
13 from librarian import html, dcparser
14
15
16 TAG_CATEGORIES = (
17     ('author', _('author')),
18     ('epoch', _('epoch')),
19     ('kind', _('kind')),
20     ('genre', _('genre')),
21     ('theme', _('theme')),
22     ('set', _('set')),
23 )
24
25
26 class TagSubcategoryManager(models.Manager):
27     def __init__(self, subcategory):
28         super(TagSubcategoryManager, self).__init__()
29         self.subcategory = subcategory
30         
31     def get_query_set(self):
32         return super(TagSubcategoryManager, self).get_query_set().filter(category=self.subcategory)
33
34
35 class Tag(TagBase):
36     name = models.CharField(_('name'), max_length=50, db_index=True)
37     slug = models.SlugField(_('slug'), max_length=120, unique=True, db_index=True)
38     sort_key = models.SlugField(_('sort key'), max_length=120, db_index=True)
39     category = models.CharField(_('category'), max_length=50, blank=False, null=False, 
40         db_index=True, choices=TAG_CATEGORIES)
41     description = models.TextField(_('description'), blank=True)
42     main_page = models.BooleanField(_('main page'), default=False, db_index=True, help_text=_('Show tag on main page'))
43         
44     user = models.ForeignKey(User, blank=True, null=True)
45     
46     def has_description(self):
47         return len(self.description) > 0
48     has_description.short_description = _('description')
49     has_description.boolean = True
50
51     @permalink
52     def get_absolute_url(self):
53         return ('catalogue.views.tagged_object_list', [self.slug])
54     
55     class Meta:
56         ordering = ('sort_key',)
57         verbose_name = _('tag')
58         verbose_name_plural = _('tags')
59     
60     def __unicode__(self):
61         return self.name
62
63     @staticmethod
64     def get_tag_list(tags):
65         if isinstance(tags, basestring):
66             tag_slugs = tags.split('/')
67             return [Tag.objects.get(slug=slug) for slug in tag_slugs]
68         else:
69             return TagBase.get_tag_list(tags)
70
71
72 def book_upload_path(ext):
73     def get_dynamic_path(book, filename):
74         return 'lektura/%s.%s' % (book.slug, ext)
75     return get_dynamic_path
76
77
78 class Book(models.Model):
79     title = models.CharField(_('title'), max_length=120)
80     slug = models.SlugField(_('slug'), max_length=120, unique=True, db_index=True)
81     description = models.TextField(_('description'), blank=True)
82     created_at = models.DateTimeField(_('creation date'), auto_now=True)
83     _short_html = models.TextField(_('short HTML'), editable=False)
84     
85     # Formats
86     xml_file = models.FileField(_('XML file'), upload_to=book_upload_path('xml'), blank=True)
87     html_file = models.FileField(_('HTML file'), upload_to=book_upload_path('html'), blank=True)
88     pdf_file = models.FileField(_('PDF file'), upload_to=book_upload_path('pdf'), blank=True)
89     odt_file = models.FileField(_('ODT file'), upload_to=book_upload_path('odt'), blank=True)
90     txt_file = models.FileField(_('TXT file'), upload_to=book_upload_path('txt'), blank=True)
91     
92     parent = models.ForeignKey('self', blank=True, null=True, related_name='children')
93     
94     objects = models.Manager()
95     tagged = managers.ModelTaggedItemManager(Tag)
96     tags = managers.TagDescriptor(Tag)
97     
98     def short_html(self):
99         if len(self._short_html):
100             return mark_safe(self._short_html)
101         else:
102             tags = self.tags.filter(~Q(category__in=('set', 'theme')))
103             tags = [u'<a href="%s">%s</a>' % (tag.get_absolute_url(), tag.name) for tag in tags]
104
105             formats = []
106             if self.html_file:
107                 formats.append(u'<a href="%s">Czytaj online</a>' % self.html_file.url)
108             if self.pdf_file:
109                 formats.append(u'<a href="%s">Plik PDF</a>' % self.pdf_file.url)
110             if self.odt_file:
111                 formats.append(u'<a href="%s">Plik ODT</a>' % self.odt_file.url)
112             
113             self._short_html = unicode(render_to_string('catalogue/book_short.html',
114                 {'book': self, 'tags': tags, 'formats': formats}))
115             self.save()
116             return mark_safe(self._short_html)
117     
118     def has_description(self):
119         return len(self.description) > 0
120     has_description.short_description = _('description')
121     has_description.boolean = True
122     
123     def has_pdf_file(self):
124         return bool(self.pdf_file)
125     has_pdf_file.short_description = 'PDF'
126     has_pdf_file.boolean = True
127     
128     def has_odt_file(self):
129         return bool(self.odt_file)
130     has_odt_file.short_description = 'ODT'
131     has_odt_file.boolean = True
132     
133     def has_html_file(self):
134         return bool(self.html_file)
135     has_html_file.short_description = 'HTML'
136     has_html_file.boolean = True
137
138     @staticmethod
139     def from_xml_file(xml_file):
140         from tempfile import NamedTemporaryFile
141         from slughifi import slughifi
142         from markupstring import MarkupString
143         
144         # Read book metadata
145         book_info = dcparser.parse(xml_file)
146         book_base, book_slug = book_info.url.rsplit('/', 1)
147         book = Book(title=book_info.title, slug=book_slug)
148         book.save()
149         
150         book_tags = []
151         for category in ('kind', 'genre', 'author', 'epoch'):    
152             tag_name = getattr(book_info, category)
153             tag_sort_key = tag_name
154             if category == 'author':
155                 tag_sort_key = tag_name.last_name
156                 tag_name = ' '.join(tag_name.first_names) + ' ' + tag_name.last_name
157             tag, created = Tag.objects.get_or_create(slug=slughifi(tag_name))
158             if created:
159                 tag.name = tag_name
160                 tag.sort_key = slughifi(tag_sort_key)
161                 tag.category = category
162                 tag.save()
163             book_tags.append(tag)
164         book.tags = book_tags
165         
166         if hasattr(book_info, 'parts'):
167             for part_url in book_info.parts:
168                 base, slug = part_url.rsplit('/', 1)
169                 child_book = Book.objects.get(slug=slug)
170                 child_book.parent = book
171                 child_book.save()
172         
173         # Save XML and HTML files
174         book.xml_file.save('%s.xml' % book.slug, File(file(xml_file)), save=False)
175         
176         html_file = NamedTemporaryFile()
177         html.transform(book.xml_file.path, html_file)
178         book.html_file.save('%s.html' % book.slug, File(html_file), save=False)
179         
180         # Extract fragments
181         closed_fragments, open_fragments = html.extract_fragments(book.html_file.path)
182         book_themes = []
183         for fragment in closed_fragments.values():
184             text = fragment.to_string()
185             short_text = ''
186             if (len(MarkupString(text)) > 240):
187                 short_text = unicode(MarkupString(text)[:160])
188             new_fragment = Fragment(text=text, short_text=short_text, anchor=fragment.id, book=book)
189             
190             try:
191                 theme_names = [s.strip() for s in fragment.themes.split(',')]
192             except AttributeError:
193                 continue
194             themes = []
195             for theme_name in theme_names:
196                 tag, created = Tag.objects.get_or_create(slug=slughifi(theme_name))
197                 if created:
198                     tag.name = theme_name
199                     tag.sort_key = slughifi(theme_name)
200                     tag.category = 'theme'
201                     tag.save()
202                 themes.append(tag)
203             new_fragment.save()
204             new_fragment.tags = list(book.tags) + themes
205             book_themes += themes
206         
207         book_themes = set(book_themes)
208         book.tags = list(book.tags) + list(book_themes)
209         return book.save()
210     
211     @permalink
212     def get_absolute_url(self):
213         return ('catalogue.views.book_detail', [self.slug])
214         
215     class Meta:
216         ordering = ('title',)
217         verbose_name = _('book')
218         verbose_name_plural = _('books')
219
220     def __unicode__(self):
221         return self.title
222
223
224 class Fragment(models.Model):
225     text = models.TextField()
226     short_text = models.TextField(editable=False)
227     _short_html = models.TextField(editable=False)
228     anchor = models.CharField(max_length=120)
229     book = models.ForeignKey(Book, related_name='fragments')
230
231     objects = models.Manager()
232     tagged = managers.ModelTaggedItemManager(Tag)
233     tags = managers.TagDescriptor(Tag)
234     
235     def short_html(self):
236         if len(self._short_html):
237             return mark_safe(self._short_html)
238         else:
239             book_authors = [u'<a href="%s">%s</a>' % (tag.get_absolute_url(), tag.name) 
240                 for tag in self.book.tags if tag.category == 'author']
241             
242             self._short_html = unicode(render_to_string('catalogue/fragment_short.html',
243                 {'fragment': self, 'book': self.book, 'book_authors': book_authors}))
244             self.save()
245             return mark_safe(self._short_html)
246     
247     def get_absolute_url(self):
248         return '%s#m%s' % (self.book.html_file.url, self.anchor)
249     
250     class Meta:
251         ordering = ('book', 'anchor',)
252         verbose_name = _('fragment')
253         verbose_name_plural = _('fragments')
254