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