1 # -*- coding: utf-8 -*-
2 from django.db import models
3 from django.db.models import permalink
4 from django.utils.translation import ugettext_lazy as _
5 from django.contrib.auth.models import User
6 from django.core.files import File
8 from newtagging.models import TagBase
9 from newtagging import managers
11 from librarian import html
15 ('author', _('author')),
16 ('epoch', _('epoch')),
18 ('genre', _('genre')),
19 ('theme', _('theme')),
24 class TagSubcategoryManager(models.Manager):
25 def __init__(self, subcategory):
26 super(TagSubcategoryManager, self).__init__()
27 self.subcategory = subcategory
29 def get_query_set(self):
30 return super(TagSubcategoryManager, self).get_query_set().filter(category=self.subcategory)
34 name = models.CharField(_('name'), max_length=50, unique=True, db_index=True)
35 slug = models.SlugField(_('slug'), unique=True, db_index=True)
36 sort_key = models.SlugField(_('sort key'), db_index=True)
37 category = models.CharField(_('category'), max_length=50, blank=False, null=False,
38 db_index=True, choices=TAG_CATEGORIES)
39 description = models.TextField(blank=True)
41 user = models.ForeignKey(User, blank=True, null=True)
43 def has_description(self):
44 return len(self.description) > 0
45 has_description.short_description = _('Has description')
46 has_description.boolean = True
49 def get_absolute_url(self):
50 return ('catalogue.views.tagged_book_list', [self.slug])
53 ordering = ('sort_key',)
54 verbose_name = _('tag')
55 verbose_name_plural = _('tags')
57 def __unicode__(self):
61 def get_tag_list(tags):
62 if isinstance(tags, basestring):
63 tag_slugs = tags.split('/')
64 return [Tag.objects.get(slug=slug) for slug in tag_slugs]
66 return TagBase.get_tag_list(tags)
69 class Book(models.Model):
70 title = models.CharField(_('title'), max_length=120)
71 slug = models.SlugField(_('slug'), unique=True, db_index=True)
72 description = models.TextField(_('description'), blank=True)
73 created_at = models.DateTimeField(_('creation date'), auto_now=True)
76 xml_file = models.FileField(_('XML file'), upload_to='books/xml', blank=True)
77 pdf_file = models.FileField(_('PDF file'), upload_to='books/pdf', blank=True)
78 odt_file = models.FileField(_('ODT file'), upload_to='books/odt', blank=True)
79 html_file = models.FileField(_('HTML file'), upload_to='books/html', blank=True)
81 objects = managers.ModelTaggedItemManager(Tag)
82 tags = managers.TagDescriptor(Tag)
84 def has_description(self):
85 return len(self.description) > 0
86 has_description.short_description = _('Has description')
87 has_description.boolean = True
89 def has_pdf_file(self):
90 return bool(self.pdf_file)
91 has_pdf_file.short_description = 'PDF'
92 has_pdf_file.boolean = True
94 def has_odt_file(self):
95 return bool(self.odt_file)
96 has_odt_file.short_description = 'ODT'
97 has_odt_file.boolean = True
99 def has_html_file(self):
100 return bool(self.html_file)
101 has_html_file.short_description = 'HTML'
102 has_html_file.boolean = True
105 def from_xml_file(xml_file):
106 from tempfile import NamedTemporaryFile
107 from slughifi import slughifi
110 book_info = dcparser.parse(xml_file)
111 book = Book(title=book_info.title, slug=slughifi(book_info.title))
115 for category in ('kind', 'genre', 'author', 'epoch'):
116 tag_name = getattr(book_info, category)
117 tag_sort_key = tag_name
118 if category == 'author':
119 tag_sort_key = tag_name.last_name
120 tag_name = ' '.join(tag_name.first_names) + ' ' + tag_name.last_name
121 tag, created = Tag.objects.get_or_create(name=tag_name,
122 slug=slughifi(tag_name), sort_key=slughifi(tag_sort_key), category=category)
124 book_tags.append(tag)
125 book.tags = book_tags
127 book.xml_file.save('%s.xml' % book.slug, File(file(xml_file)), save=False)
129 html_file = NamedTemporaryFile()
130 html.transform(book.xml_file.path, html_file)
131 book.html_file.save('%s.html' % book.slug, File(html_file), save=False)
136 def get_absolute_url(self):
137 return ('catalogue.views.book_detail', [self.slug])
140 ordering = ('title',)
141 verbose_name = _('book')
142 verbose_name_plural = _('books')
144 def __unicode__(self):
148 class Fragment(models.Model):
149 text = models.TextField()
150 short_text = models.TextField()
151 anchor = models.IntegerField()
152 book = models.ForeignKey(Book)
154 objects = managers.ModelTaggedItemManager(Tag)
155 tags = managers.TagDescriptor(Tag)
158 ordering = ('book', 'anchor',)
159 verbose_name = _('fragment')
160 verbose_name_plural = _('fragment')