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
11 from newtagging.models import TagBase
12 from newtagging import managers
13 from catalogue.fields import JSONField
15 from librarian import html, dcparser
19 ('author', _('author')),
20 ('epoch', _('epoch')),
22 ('genre', _('genre')),
23 ('theme', _('theme')),
29 class TagSubcategoryManager(models.Manager):
30 def __init__(self, subcategory):
31 super(TagSubcategoryManager, self).__init__()
32 self.subcategory = subcategory
34 def get_query_set(self):
35 return super(TagSubcategoryManager, self).get_query_set().filter(category=self.subcategory)
39 name = models.CharField(_('name'), max_length=50, db_index=True)
40 slug = models.SlugField(_('slug'), max_length=120, unique=True, db_index=True)
41 sort_key = models.SlugField(_('sort key'), max_length=120, db_index=True)
42 category = models.CharField(_('category'), max_length=50, blank=False, null=False,
43 db_index=True, choices=TAG_CATEGORIES)
44 description = models.TextField(_('description'), blank=True)
45 main_page = models.BooleanField(_('main page'), default=False, db_index=True, help_text=_('Show tag on main page'))
47 user = models.ForeignKey(User, blank=True, null=True)
48 book_count = models.IntegerField(_('book count'), default=0, blank=False, null=False)
50 def has_description(self):
51 return len(self.description) > 0
52 has_description.short_description = _('description')
53 has_description.boolean = True
56 def get_absolute_url(self):
57 return ('catalogue.views.tagged_object_list', [self.slug])
60 ordering = ('sort_key',)
61 verbose_name = _('tag')
62 verbose_name_plural = _('tags')
64 def __unicode__(self):
68 def get_tag_list(tags):
69 if isinstance(tags, basestring):
70 tag_slugs = tags.split('/')
71 return [Tag.objects.get(slug=slug) for slug in tag_slugs]
73 return TagBase.get_tag_list(tags)
76 def book_upload_path(ext):
77 def get_dynamic_path(book, filename):
78 return 'lektura/%s.%s' % (book.slug, ext)
79 return get_dynamic_path
82 class Book(models.Model):
83 title = models.CharField(_('title'), max_length=120)
84 slug = models.SlugField(_('slug'), max_length=120, unique=True, db_index=True)
85 description = models.TextField(_('description'), blank=True)
86 created_at = models.DateTimeField(_('creation date'), auto_now=True)
87 _short_html = models.TextField(_('short HTML'), editable=False)
88 parent_number = models.IntegerField(_('parent number'), default=0)
89 extra_info = JSONField(_('extra information'))
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 mp3_file = models.FileField(_('MP3 file'), upload_to=book_upload_path('mp3'), blank=True)
98 ogg_file = models.FileField(_('OGG file'), upload_to=book_upload_path('ogg'), blank=True)
100 parent = models.ForeignKey('self', blank=True, null=True, related_name='children')
102 objects = models.Manager()
103 tagged = managers.ModelTaggedItemManager(Tag)
104 tags = managers.TagDescriptor(Tag)
111 def short_html(self):
112 if len(self._short_html):
113 return mark_safe(self._short_html)
115 tags = self.tags.filter(~Q(category__in=('set', 'theme', 'book')))
116 tags = [u'<a href="%s">%s</a>' % (tag.get_absolute_url(), tag.name) for tag in tags]
120 formats.append(u'<a href="%s">Czytaj online</a>' % reverse('book_text', kwargs={'slug': self.slug}))
122 formats.append(u'<a href="%s">Plik PDF</a>' % self.pdf_file.url)
124 formats.append(u'<a href="%s">Plik ODT</a>' % self.odt_file.url)
126 formats.append(u'<a href="%s">Plik TXT</a>' % self.txt_file.url)
128 self._short_html = unicode(render_to_string('catalogue/book_short.html',
129 {'book': self, 'tags': tags, 'formats': formats}))
131 return mark_safe(self._short_html)
133 def has_description(self):
134 return len(self.description) > 0
135 has_description.short_description = _('description')
136 has_description.boolean = True
138 def has_pdf_file(self):
139 return bool(self.pdf_file)
140 has_pdf_file.short_description = 'PDF'
141 has_pdf_file.boolean = True
143 def has_odt_file(self):
144 return bool(self.odt_file)
145 has_odt_file.short_description = 'ODT'
146 has_odt_file.boolean = True
148 def has_html_file(self):
149 return bool(self.html_file)
150 has_html_file.short_description = 'HTML'
151 has_html_file.boolean = True
153 class AlreadyExists(Exception):
157 def from_xml_file(xml_file, overwrite=False):
158 from tempfile import NamedTemporaryFile
159 from slughifi import slughifi
160 from markupstring import MarkupString
163 book_info = dcparser.parse(xml_file)
164 book_base, book_slug = book_info.url.rsplit('/', 1)
165 book, created = Book.objects.get_or_create(slug=book_slug)
171 raise Book.AlreadyExists('Book %s already exists' % book_slug)
172 # Save shelves for this book
173 book_shelves = list(book.tags.filter(category='set'))
175 book.title = book_info.title
176 book.extra_info = book_info.to_dict()
177 book._short_html = ''
181 for category in ('kind', 'genre', 'author', 'epoch'):
182 tag_name = getattr(book_info, category)
183 tag_sort_key = tag_name
184 if category == 'author':
185 tag_sort_key = tag_name.last_name
186 tag_name = ' '.join(tag_name.first_names) + ' ' + tag_name.last_name
187 tag, created = Tag.objects.get_or_create(slug=slughifi(tag_name))
190 tag.sort_key = slughifi(tag_sort_key)
191 tag.category = category
193 book_tags.append(tag)
195 book_tag, created = Tag.objects.get_or_create(slug=('l-' + book.slug)[:120])
197 book_tag.name = book.title[:50]
198 book_tag.sort_key = ('l-' + book.slug)[:120]
199 book_tag.category = 'book'
201 book_tags.append(book_tag)
203 book.tags = book_tags
205 if hasattr(book_info, 'parts'):
206 for n, part_url in enumerate(book_info.parts):
207 base, slug = part_url.rsplit('/', 1)
208 child_book = Book.objects.get(slug=slug)
209 child_book.parent = book
210 child_book.parent_number = n
213 book_descendants = list(book.children.all())
214 while len(book_descendants) > 0:
215 child_book = book_descendants.pop(0)
216 for fragment in child_book.fragments.all():
217 fragment.tags = set(list(fragment.tags) + [book_tag])
218 book_descendants += list(child_book.children.all())
220 # Save XML and HTML files
221 book.xml_file.save('%s.xml' % book.slug, File(file(xml_file)), save=False)
223 html_file = NamedTemporaryFile()
224 if html.transform(book.xml_file.path, html_file):
225 book.html_file.save('%s.html' % book.slug, File(html_file), save=False)
228 closed_fragments, open_fragments = html.extract_fragments(book.html_file.path)
230 for fragment in closed_fragments.values():
231 text = fragment.to_string()
233 if (len(MarkupString(text)) > 240):
234 short_text = unicode(MarkupString(text)[:160])
235 new_fragment, created = Fragment.objects.get_or_create(anchor=fragment.id, book=book,
236 defaults={'text': text, 'short_text': short_text})
239 theme_names = [s.strip() for s in fragment.themes.split(',')]
240 except AttributeError:
243 for theme_name in theme_names:
244 tag, created = Tag.objects.get_or_create(slug=slughifi(theme_name))
246 tag.name = theme_name
247 tag.sort_key = slughifi(theme_name)
248 tag.category = 'theme'
252 new_fragment.tags = set(list(book.tags) + themes + [book_tag])
253 book_themes += themes
255 book_themes = set(book_themes)
256 book.tags = list(book.tags) + list(book_themes) + book_shelves
262 def get_absolute_url(self):
263 return ('catalogue.views.book_detail', [self.slug])
266 ordering = ('title',)
267 verbose_name = _('book')
268 verbose_name_plural = _('books')
270 def __unicode__(self):
274 class Fragment(models.Model):
275 text = models.TextField()
276 short_text = models.TextField(editable=False)
277 _short_html = models.TextField(editable=False)
278 anchor = models.CharField(max_length=120)
279 book = models.ForeignKey(Book, related_name='fragments')
281 objects = models.Manager()
282 tagged = managers.ModelTaggedItemManager(Tag)
283 tags = managers.TagDescriptor(Tag)
285 def short_html(self):
286 if len(self._short_html):
287 return mark_safe(self._short_html)
289 book_authors = [u'<a href="%s">%s</a>' % (tag.get_absolute_url(), tag.name)
290 for tag in self.book.tags if tag.category == 'author']
292 self._short_html = unicode(render_to_string('catalogue/fragment_short.html',
293 {'fragment': self, 'book': self.book, 'book_authors': book_authors}))
295 return mark_safe(self._short_html)
297 def get_absolute_url(self):
298 return '%s#m%s' % (reverse('book_text', kwargs={'slug': self.book.slug}), self.anchor)
301 ordering = ('book', 'anchor',)
302 verbose_name = _('fragment')
303 verbose_name_plural = _('fragments')