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
13 ('author', _('author')),
14 ('epoch', _('epoch')),
16 ('genre', _('genre')),
17 ('theme', _('theme')),
22 class TagSubcategoryManager(models.Manager):
23 def __init__(self, subcategory):
24 super(TagSubcategoryManager, self).__init__()
25 self.subcategory = subcategory
27 def get_query_set(self):
28 return super(TagSubcategoryManager, self).get_query_set().filter(category=self.subcategory)
32 name = models.CharField(_('name'), max_length=50, unique=True, db_index=True)
33 slug = models.SlugField(_('slug'), unique=True, db_index=True)
34 sort_key = models.SlugField(_('sort key'), db_index=True)
35 category = models.CharField(_('category'), max_length=50, blank=False, null=False,
36 db_index=True, choices=TAG_CATEGORIES)
37 description = models.TextField(blank=True)
39 user = models.ForeignKey(User, blank=True, null=True)
41 def has_description(self):
42 return len(self.description) > 0
43 has_description.short_description = _('Has description')
44 has_description.boolean = True
47 def get_absolute_url(self):
48 return ('catalogue.views.tagged_book_list', [self.slug])
51 ordering = ('sort_key',)
52 verbose_name = _('tag')
53 verbose_name_plural = _('tags')
55 def __unicode__(self):
59 def get_tag_list(tags):
60 if isinstance(tags, basestring):
61 tag_slugs = tags.split('/')
62 return [Tag.objects.get(slug=slug) for slug in tag_slugs]
64 return TagBase.get_tag_list(tags)
67 class Book(models.Model):
68 title = models.CharField(_('title'), max_length=120)
69 slug = models.SlugField(_('slug'), unique=True, db_index=True)
70 description = models.TextField(_('description'), blank=True)
71 created_at = models.DateTimeField(_('creation date'), auto_now=True)
74 xml_file = models.FileField(_('XML file'), upload_to='books/xml', blank=True)
75 pdf_file = models.FileField(_('PDF file'), upload_to='books/pdf', blank=True)
76 odt_file = models.FileField(_('ODT file'), upload_to='books/odt', blank=True)
77 html_file = models.FileField(_('HTML file'), upload_to='books/html', blank=True)
79 objects = managers.ModelTaggedItemManager(Tag)
80 tags = managers.TagDescriptor(Tag)
82 def has_description(self):
83 return len(self.description) > 0
84 has_description.short_description = _('Has description')
85 has_description.boolean = True
87 def has_pdf_file(self):
88 return bool(self.pdf_file)
89 has_pdf_file.short_description = 'PDF'
90 has_pdf_file.boolean = True
92 def has_odt_file(self):
93 return bool(self.odt_file)
94 has_odt_file.short_description = 'ODT'
95 has_odt_file.boolean = True
97 def has_html_file(self):
98 return bool(self.html_file)
99 has_html_file.short_description = 'HTML'
100 has_html_file.boolean = True
102 def save(self, **kwargs):
104 from bin import book2html
105 from os.path import splitext, basename
106 from tempfile import NamedTemporaryFile
108 html_file = NamedTemporaryFile()
109 book2html.transform(self.xml_file.path, html_file)
111 html_filename = '%s.html' % splitext(basename(self.xml_file.path))[0]
112 self.html_file.save(html_filename, File(html_file), save=False)
116 book = super(Book, self).save(**kwargs)
119 def get_absolute_url(self):
120 return ('catalogue.views.book_detail', [self.slug])
123 ordering = ('title',)
124 verbose_name = _('book')
125 verbose_name_plural = _('books')
127 def __unicode__(self):
131 class Fragment(models.Model):
132 text = models.TextField()
133 short_text = models.TextField()
134 anchor = models.IntegerField()
135 book = models.ForeignKey(Book)
137 objects = managers.ModelTaggedItemManager(Tag)
138 tags = managers.TagDescriptor(Tag)
141 ordering = ('book', 'anchor',)
142 verbose_name = _('fragment')
143 verbose_name_plural = _('fragment')