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)
 
  98     parent = models.ForeignKey('self', blank=True, null=True, related_name='children')
 
 100     objects = models.Manager()
 
 101     tagged = managers.ModelTaggedItemManager(Tag)
 
 102     tags = managers.TagDescriptor(Tag)
 
 109     def short_html(self):
 
 110         if len(self._short_html):
 
 111             return mark_safe(self._short_html)
 
 113             tags = self.tags.filter(~Q(category__in=('set', 'theme', 'book')))
 
 114             tags = [u'<a href="%s">%s</a>' % (tag.get_absolute_url(), tag.name) for tag in tags]
 
 118                 formats.append(u'<a href="%s">Czytaj online</a>' % reverse('book_text', kwargs={'slug': self.slug}))
 
 120                 formats.append(u'<a href="%s">Plik PDF</a>' % self.pdf_file.url)
 
 122                 formats.append(u'<a href="%s">Plik ODT</a>' % self.odt_file.url)
 
 124                 formats.append(u'<a href="%s">Plik TXT</a>' % self.txt_file.url)
 
 126             self._short_html = unicode(render_to_string('catalogue/book_short.html',
 
 127                 {'book': self, 'tags': tags, 'formats': formats}))
 
 129             return mark_safe(self._short_html)
 
 131     def has_description(self):
 
 132         return len(self.description) > 0
 
 133     has_description.short_description = _('description')
 
 134     has_description.boolean = True
 
 136     def has_pdf_file(self):
 
 137         return bool(self.pdf_file)
 
 138     has_pdf_file.short_description = 'PDF'
 
 139     has_pdf_file.boolean = True
 
 141     def has_odt_file(self):
 
 142         return bool(self.odt_file)
 
 143     has_odt_file.short_description = 'ODT'
 
 144     has_odt_file.boolean = True
 
 146     def has_html_file(self):
 
 147         return bool(self.html_file)
 
 148     has_html_file.short_description = 'HTML'
 
 149     has_html_file.boolean = True
 
 151     class AlreadyExists(Exception):
 
 155     def from_xml_file(xml_file, overwrite=False):
 
 156         from tempfile import NamedTemporaryFile
 
 157         from slughifi import slughifi
 
 158         from markupstring import MarkupString
 
 161         book_info = dcparser.parse(xml_file)
 
 162         book_base, book_slug = book_info.url.rsplit('/', 1)
 
 163         book, created = Book.objects.get_or_create(slug=book_slug)
 
 169                 raise Book.AlreadyExists('Book %s already exists' % book_slug)
 
 170             # Save shelves for this book
 
 171             book_shelves = list(book.tags.filter(category='set'))
 
 173         book.title = book_info.title
 
 174         book.extra_info = book_info.to_dict()
 
 175         book._short_html = ''
 
 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))
 
 188                 tag.sort_key = slughifi(tag_sort_key)
 
 189                 tag.category = category
 
 191             book_tags.append(tag)
 
 193         book_tag, created = Tag.objects.get_or_create(slug=('l-' + book.slug)[:120])
 
 195             book_tag.name = book.title[:50]
 
 196             book_tag.sort_key = ('l-' + book.slug)[:120]
 
 197             book_tag.category = 'book'
 
 199         book_tags.append(book_tag)
 
 201         book.tags = book_tags
 
 203         if hasattr(book_info, 'parts'):
 
 204             for n, part_url in enumerate(book_info.parts):
 
 205                 base, slug = part_url.rsplit('/', 1)
 
 206                 child_book = Book.objects.get(slug=slug)
 
 207                 child_book.parent = book
 
 208                 child_book.parent_number = n
 
 211         book_descendants = list(book.children.all())
 
 212         while len(book_descendants) > 0:
 
 213             child_book = book_descendants.pop(0)
 
 214             for fragment in child_book.fragments.all():
 
 215                 fragment.tags = set(list(fragment.tags) + [book_tag])
 
 216             book_descendants += list(child_book.children.all())
 
 218         # Save XML and HTML files
 
 219         book.xml_file.save('%s.xml' % book.slug, File(file(xml_file)), save=False)
 
 221         html_file = NamedTemporaryFile()
 
 222         if html.transform(book.xml_file.path, html_file):
 
 223             book.html_file.save('%s.html' % book.slug, File(html_file), save=False)
 
 226             closed_fragments, open_fragments = html.extract_fragments(book.html_file.path)
 
 228             for fragment in closed_fragments.values():
 
 229                 text = fragment.to_string()
 
 231                 if (len(MarkupString(text)) > 240):
 
 232                     short_text = unicode(MarkupString(text)[:160])
 
 233                 new_fragment, created = Fragment.objects.get_or_create(anchor=fragment.id, book=book, 
 
 234                     defaults={'text': text, 'short_text': short_text})
 
 237                     theme_names = [s.strip() for s in fragment.themes.split(',')]
 
 238                 except AttributeError:
 
 241                 for theme_name in theme_names:
 
 242                     tag, created = Tag.objects.get_or_create(slug=slughifi(theme_name))
 
 244                         tag.name = theme_name
 
 245                         tag.sort_key = slughifi(theme_name)
 
 246                         tag.category = 'theme'
 
 250                 new_fragment.tags = set(list(book.tags) + themes + [book_tag])
 
 251                 book_themes += themes
 
 253             book_themes = set(book_themes)
 
 254             book.tags = list(book.tags) + list(book_themes) + book_shelves
 
 260     def get_absolute_url(self):
 
 261         return ('catalogue.views.book_detail', [self.slug])
 
 264         ordering = ('title',)
 
 265         verbose_name = _('book')
 
 266         verbose_name_plural = _('books')
 
 268     def __unicode__(self):
 
 272 class Fragment(models.Model):
 
 273     text = models.TextField()
 
 274     short_text = models.TextField(editable=False)
 
 275     _short_html = models.TextField(editable=False)
 
 276     anchor = models.CharField(max_length=120)
 
 277     book = models.ForeignKey(Book, related_name='fragments')
 
 279     objects = models.Manager()
 
 280     tagged = managers.ModelTaggedItemManager(Tag)
 
 281     tags = managers.TagDescriptor(Tag)
 
 283     def short_html(self):
 
 284         if len(self._short_html):
 
 285             return mark_safe(self._short_html)
 
 287             book_authors = [u'<a href="%s">%s</a>' % (tag.get_absolute_url(), tag.name) 
 
 288                 for tag in self.book.tags if tag.category == 'author']
 
 290             self._short_html = unicode(render_to_string('catalogue/fragment_short.html',
 
 291                 {'fragment': self, 'book': self.book, 'book_authors': book_authors}))
 
 293             return mark_safe(self._short_html)
 
 295     def get_absolute_url(self):
 
 296         return '%s#m%s' % (reverse('book_text', kwargs={'slug': self.book.slug}), self.anchor)
 
 299         ordering = ('book', 'anchor',)
 
 300         verbose_name = _('fragment')
 
 301         verbose_name_plural = _('fragments')