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