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
10 from newtagging.models import TagBase
11 from newtagging import managers
13 from librarian import html
17 ('author', _('author')),
18 ('epoch', _('epoch')),
20 ('genre', _('genre')),
21 ('theme', _('theme')),
26 class TagSubcategoryManager(models.Manager):
27 def __init__(self, subcategory):
28 super(TagSubcategoryManager, self).__init__()
29 self.subcategory = subcategory
31 def get_query_set(self):
32 return super(TagSubcategoryManager, self).get_query_set().filter(category=self.subcategory)
36 name = models.CharField(_('name'), max_length=50, unique=True, db_index=True)
37 slug = models.SlugField(_('slug'), unique=True, db_index=True)
38 sort_key = models.SlugField(_('sort key'), db_index=True)
39 category = models.CharField(_('category'), max_length=50, blank=False, null=False,
40 db_index=True, choices=TAG_CATEGORIES)
41 description = models.TextField(blank=True)
43 user = models.ForeignKey(User, blank=True, null=True)
45 def has_description(self):
46 return len(self.description) > 0
47 has_description.short_description = _('description')
48 has_description.boolean = True
51 def get_absolute_url(self):
52 return ('catalogue.views.tagged_book_list', [self.slug])
55 ordering = ('sort_key',)
56 verbose_name = _('tag')
57 verbose_name_plural = _('tags')
59 def __unicode__(self):
63 def get_tag_list(tags):
64 if isinstance(tags, basestring):
65 tag_slugs = tags.split('/')
66 return [Tag.objects.get(slug=slug) for slug in tag_slugs]
68 return TagBase.get_tag_list(tags)
71 class Book(models.Model):
72 title = models.CharField(_('title'), max_length=120)
73 slug = models.SlugField(_('slug'), unique=True, db_index=True)
74 description = models.TextField(_('description'), blank=True)
75 created_at = models.DateTimeField(_('creation date'), auto_now=True)
76 _short_html = models.TextField(_('short HTML'), editable=False)
79 xml_file = models.FileField(_('XML file'), upload_to='books/xml', blank=True)
80 pdf_file = models.FileField(_('PDF file'), upload_to='books/pdf', blank=True)
81 odt_file = models.FileField(_('ODT file'), upload_to='books/odt', blank=True)
82 html_file = models.FileField(_('HTML file'), upload_to='books/html', blank=True)
84 objects = managers.ModelTaggedItemManager(Tag)
85 tags = managers.TagDescriptor(Tag)
88 if len(self._short_html):
89 return mark_safe(self._short_html)
91 tags = self.tags.filter(~Q(category__in=('set', 'theme')))
92 tags = [u'<a href="%s">%s</a>' % (tag.get_absolute_url(), tag.name) for tag in tags]
96 formats.append(u'<a href="%s">Czytaj online</a>' % self.html_file.url)
98 formats.append(u'<a href="%s">Plik PDF</a>' % self.pdf_file.url)
100 formats.append(u'<a href="%s">Plik ODT</a>' % self.odt_file.url)
102 self._short_html = render_to_string('catalogue/book_short.html',
103 {'book': self, 'tags': tags, 'formats': formats})
105 return mark_safe(self._short_html)
107 def has_description(self):
108 return len(self.description) > 0
109 has_description.short_description = _('description')
110 has_description.boolean = True
112 def has_pdf_file(self):
113 return bool(self.pdf_file)
114 has_pdf_file.short_description = 'PDF'
115 has_pdf_file.boolean = True
117 def has_odt_file(self):
118 return bool(self.odt_file)
119 has_odt_file.short_description = 'ODT'
120 has_odt_file.boolean = True
122 def has_html_file(self):
123 return bool(self.html_file)
124 has_html_file.short_description = 'HTML'
125 has_html_file.boolean = True
128 def from_xml_file(xml_file):
129 from tempfile import NamedTemporaryFile
130 from slughifi import slughifi
132 from markupstring import MarkupString
136 book_info = dcparser.parse(xml_file)
137 book = Book(title=book_info.title, slug=slughifi(book_info.title))
141 for category in ('kind', 'genre', 'author', 'epoch'):
142 tag_name = getattr(book_info, category)
143 tag_sort_key = tag_name
144 if category == 'author':
145 tag_sort_key = tag_name.last_name
146 tag_name = ' '.join(tag_name.first_names) + ' ' + tag_name.last_name
147 tag, created = Tag.objects.get_or_create(name=tag_name,
148 slug=slughifi(tag_name), sort_key=slughifi(tag_sort_key), category=category)
150 book_tags.append(tag)
151 book.tags = book_tags
153 # Save XML and HTML files
154 book.xml_file.save('%s.xml' % book.slug, File(file(xml_file)), save=False)
156 html_file = NamedTemporaryFile()
157 html.transform(book.xml_file.path, html_file)
158 book.html_file.save('%s.html' % book.slug, File(html_file), save=False)
161 closed_fragments, open_fragments = html.extract_fragments(book.html_file.path)
163 for fragment in closed_fragments.values():
164 text = fragment.to_string()
166 if (len(re.sub(r'</?.*?>', '', text)) > 240):
167 short_text = MarkupString(text)[:160]
168 new_fragment = Fragment(text=text, short_text=short_text, anchor=fragment.id, book=book)
170 theme_names = [s.strip() for s in fragment.themes.split(',')]
172 for theme_name in theme_names:
173 tag, created = Tag.objects.get_or_create(name=theme_name,
174 slug=slughifi(theme_name), sort_key=slughifi(theme_name), category='theme')
178 new_fragment.tags = list(book.tags) + themes
179 book_themes += themes
181 book_themes = set(book_themes)
182 book.tags = list(book.tags) + list(book_themes)
186 def get_absolute_url(self):
187 return ('catalogue.views.book_detail', [self.slug])
190 ordering = ('title',)
191 verbose_name = _('book')
192 verbose_name_plural = _('books')
194 def __unicode__(self):
198 class Fragment(models.Model):
199 text = models.TextField()
200 short_text = models.TextField(editable=False)
201 _short_html = models.TextField(editable=False)
202 anchor = models.IntegerField()
203 book = models.ForeignKey(Book, related_name='fragments')
205 objects = managers.ModelTaggedItemManager(Tag)
206 tags = managers.TagDescriptor(Tag)
208 def short_html(self):
209 if len(self._short_html):
210 return mark_safe(self._short_html)
212 book_authors = [u'<a href="%s">%s</a>' % (tag.get_absolute_url(), tag.name)
213 for tag in self.book.tags if tag.category == 'author']
215 self._short_html = render_to_string('catalogue/fragment_short.html',
216 {'fragment': self, 'book': self.book, 'book_authors': book_authors})
218 return mark_safe(self._short_html)
221 ordering = ('book', 'anchor',)
222 verbose_name = _('fragment')
223 verbose_name_plural = _('fragments')