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
14 from librarian import html, dcparser
18 ('author', _('author')),
19 ('epoch', _('epoch')),
21 ('genre', _('genre')),
22 ('theme', _('theme')),
27 class TagSubcategoryManager(models.Manager):
28 def __init__(self, subcategory):
29 super(TagSubcategoryManager, self).__init__()
30 self.subcategory = subcategory
32 def get_query_set(self):
33 return super(TagSubcategoryManager, self).get_query_set().filter(category=self.subcategory)
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'))
45 user = models.ForeignKey(User, blank=True, null=True)
46 book_count = models.IntegerField(_('book count'), default=0, blank=False, null=False)
48 def has_description(self):
49 return len(self.description) > 0
50 has_description.short_description = _('description')
51 has_description.boolean = True
54 def get_absolute_url(self):
55 return ('catalogue.views.tagged_object_list', [self.slug])
58 ordering = ('sort_key',)
59 verbose_name = _('tag')
60 verbose_name_plural = _('tags')
62 def __unicode__(self):
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]
71 return TagBase.get_tag_list(tags)
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
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)
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)
95 parent = models.ForeignKey('self', blank=True, null=True, related_name='children')
97 objects = models.Manager()
98 tagged = managers.ModelTaggedItemManager(Tag)
99 tags = managers.TagDescriptor(Tag)
106 def short_html(self):
107 if len(self._short_html):
108 return mark_safe(self._short_html)
110 tags = self.tags.filter(~Q(category__in=('set', 'theme')))
111 tags = [u'<a href="%s">%s</a>' % (tag.get_absolute_url(), tag.name) for tag in tags]
115 formats.append(u'<a href="%s">Czytaj online</a>' % reverse('book_text', kwargs={'slug': self.slug}))
117 formats.append(u'<a href="%s">Plik PDF</a>' % self.pdf_file.url)
119 formats.append(u'<a href="%s">Plik ODT</a>' % self.odt_file.url)
121 formats.append(u'<a href="%s">Plik TXT</a>' % self.txt_file.url)
123 self._short_html = unicode(render_to_string('catalogue/book_short.html',
124 {'book': self, 'tags': tags, 'formats': formats}))
126 return mark_safe(self._short_html)
128 def has_description(self):
129 return len(self.description) > 0
130 has_description.short_description = _('description')
131 has_description.boolean = True
133 def has_pdf_file(self):
134 return bool(self.pdf_file)
135 has_pdf_file.short_description = 'PDF'
136 has_pdf_file.boolean = True
138 def has_odt_file(self):
139 return bool(self.odt_file)
140 has_odt_file.short_description = 'ODT'
141 has_odt_file.boolean = True
143 def has_html_file(self):
144 return bool(self.html_file)
145 has_html_file.short_description = 'HTML'
146 has_html_file.boolean = True
148 class AlreadyExists(Exception):
152 def from_xml_file(xml_file, overwrite=False):
153 from tempfile import NamedTemporaryFile
154 from slughifi import slughifi
155 from markupstring import MarkupString
158 book_info = dcparser.parse(xml_file)
159 book_base, book_slug = book_info.url.rsplit('/', 1)
160 book, created = Book.objects.get_or_create(slug=book_slug)
166 raise Book.AlreadyExists('Book %s already exists' % book_slug)
167 # Save shelves for this book
168 book_shelves = list(book.tags.filter(category='set'))
170 book.title = book_info.title
171 book._short_html = ''
175 for category in ('kind', 'genre', 'author', 'epoch'):
176 tag_name = getattr(book_info, category)
177 tag_sort_key = tag_name
178 if category == 'author':
179 tag_sort_key = tag_name.last_name
180 tag_name = ' '.join(tag_name.first_names) + ' ' + tag_name.last_name
181 tag, created = Tag.objects.get_or_create(slug=slughifi(tag_name))
184 tag.sort_key = slughifi(tag_sort_key)
185 tag.category = category
187 book_tags.append(tag)
188 book.tags = book_tags
190 if hasattr(book_info, 'parts'):
191 for n, part_url in enumerate(book_info.parts):
192 base, slug = part_url.rsplit('/', 1)
193 child_book = Book.objects.get(slug=slug)
194 child_book.parent = book
195 child_book.parent_number = n
198 # Save XML and HTML files
199 book.xml_file.save('%s.xml' % book.slug, File(file(xml_file)), save=False)
201 html_file = NamedTemporaryFile()
202 if html.transform(book.xml_file.path, html_file):
203 book.html_file.save('%s.html' % book.slug, File(html_file), save=False)
206 closed_fragments, open_fragments = html.extract_fragments(book.html_file.path)
208 for fragment in closed_fragments.values():
209 text = fragment.to_string()
211 if (len(MarkupString(text)) > 240):
212 short_text = unicode(MarkupString(text)[:160])
213 new_fragment, created = Fragment.objects.get_or_create(anchor=fragment.id, book=book,
214 defaults={'text': text, 'short_text': short_text})
217 theme_names = [s.strip() for s in fragment.themes.split(',')]
218 except AttributeError:
221 for theme_name in theme_names:
222 tag, created = Tag.objects.get_or_create(slug=slughifi(theme_name))
224 tag.name = theme_name
225 tag.sort_key = slughifi(theme_name)
226 tag.category = 'theme'
230 new_fragment.tags = list(book.tags) + themes
231 book_themes += themes
233 book_themes = set(book_themes)
234 book.tags = list(book.tags) + list(book_themes) + book_shelves
240 def get_absolute_url(self):
241 return ('catalogue.views.book_detail', [self.slug])
244 ordering = ('title',)
245 verbose_name = _('book')
246 verbose_name_plural = _('books')
248 def __unicode__(self):
252 class Fragment(models.Model):
253 text = models.TextField()
254 short_text = models.TextField(editable=False)
255 _short_html = models.TextField(editable=False)
256 anchor = models.CharField(max_length=120)
257 book = models.ForeignKey(Book, related_name='fragments')
259 objects = models.Manager()
260 tagged = managers.ModelTaggedItemManager(Tag)
261 tags = managers.TagDescriptor(Tag)
263 def short_html(self):
264 if len(self._short_html):
265 return mark_safe(self._short_html)
267 book_authors = [u'<a href="%s">%s</a>' % (tag.get_absolute_url(), tag.name)
268 for tag in self.book.tags if tag.category == 'author']
270 self._short_html = unicode(render_to_string('catalogue/fragment_short.html',
271 {'fragment': self, 'book': self.book, 'book_authors': book_authors}))
273 return mark_safe(self._short_html)
275 def get_absolute_url(self):
276 return '%s#m%s' % (reverse('book_text', kwargs={'slug': self.book.slug}), self.anchor)
279 ordering = ('book', 'anchor',)
280 verbose_name = _('fragment')
281 verbose_name_plural = _('fragments')