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 django.conf import settings
106 from os.path import splitext, basename
107 from tempfile import NamedTemporaryFile
109 html_file = NamedTemporaryFile()
110 book2html.transform(self.xml_file.path, html_file)
112 html_filename = '%s.html' % splitext(basename(self.xml_file.path))[0]
113 self.html_file.save(html_filename, File(html_file), save=False)
117 book = super(Book, self).save(**kwargs)
120 def get_absolute_url(self):
121 return ('catalogue.views.book_detail', [self.slug])
124 ordering = ('title',)
125 verbose_name = _('book')
126 verbose_name_plural = _('books')
128 def __unicode__(self):
132 # class Fragment(models.Model):
133 # id = models.IntegerField(primary_key=True)
134 # text = models.TextField(blank=True)
135 # start_paragraph = models.IntegerField(null=True, blank=True)
136 # book_id = models.IntegerField(null=True, blank=True)
138 # db_table = u'fragment'
141 # class Inflections(models.Model):
142 # word = models.CharField(max_length=120, primary_key=True)
143 # cases = models.TextField() # This field type is a guess.
145 # db_table = u'inflections'
148 # class Paragraph(models.Model):
149 # id = models.IntegerField(primary_key=True)
150 # number = models.IntegerField(null=True, blank=True)
151 # text = models.TextField(blank=True)
152 # book_id = models.IntegerField(null=True, blank=True)
154 # db_table = u'paragraph'