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
7 from newtagging.models import TagBase
8 from newtagging import managers
12 ('author', _('author')),
13 ('epoch', _('epoch')),
15 ('genre', _('genre')),
16 ('theme', _('theme')),
21 class TagSubcategoryManager(models.Manager):
22 def __init__(self, subcategory):
23 super(TagSubcategoryManager, self).__init__()
24 self.subcategory = subcategory
26 def get_query_set(self):
27 return super(TagSubcategoryManager, self).get_query_set().filter(category=self.subcategory)
31 name = models.CharField(_('name'), max_length=50, unique=True, db_index=True)
32 slug = models.SlugField(_('slug'), unique=True, db_index=True)
33 sort_key = models.SlugField(_('sort key'), db_index=True)
34 category = models.CharField(_('category'), max_length=50, blank=False, null=False,
35 db_index=True, choices=TAG_CATEGORIES)
36 description = models.TextField(blank=True)
38 user = models.ForeignKey(User, blank=True, null=True)
40 def has_description(self):
41 return len(self.description) > 0
42 has_description.short_description = _('Has description')
43 has_description.boolean = True
46 def get_absolute_url(self):
47 return ('catalogue.views.tagged_book_list', [self.slug])
50 ordering = ('sort_key',)
51 verbose_name = _('tag')
52 verbose_name_plural = _('tags')
54 def __unicode__(self):
58 def get_tag_list(tags):
59 if isinstance(tags, basestring):
60 tag_slugs = tags.split('/')
61 return [Tag.objects.get(slug=slug) for slug in tag_slugs]
63 return TagBase.get_tag_list(tags)
66 class Book(models.Model):
67 title = models.CharField(_('title'), max_length=120)
68 slug = models.SlugField(_('slug'), unique=True, db_index=True)
69 description = models.TextField(_('description'), blank=True)
70 created_at = models.DateTimeField(_('creation date'), auto_now=True)
73 pdf_file = models.FileField(_('PDF file'), upload_to='books/pdf', blank=True)
74 odt_file = models.FileField(_('ODT file'), upload_to='books/odt', blank=True)
75 html_file = models.FileField(_('HTML file'), upload_to='books/html', blank=True)
77 objects = managers.ModelTaggedItemManager(Tag)
78 tags = managers.TagDescriptor(Tag)
80 def has_description(self):
81 return len(self.description) > 0
82 has_description.short_description = _('Has description')
83 has_description.boolean = True
85 def has_pdf_file(self):
86 return bool(self.pdf_file)
87 has_pdf_file.short_description = 'PDF'
88 has_pdf_file.boolean = True
90 def has_odt_file(self):
91 return bool(self.odt_file)
92 has_odt_file.short_description = 'ODT'
93 has_odt_file.boolean = True
95 def has_html_file(self):
96 return bool(self.html_file)
97 has_html_file.short_description = 'HTML'
98 has_html_file.boolean = True
101 def get_absolute_url(self):
102 return ('catalogue.views.book_detail', [self.slug])
105 ordering = ('title',)
106 verbose_name = _('book')
107 verbose_name_plural = _('books')
109 def __unicode__(self):
113 # class Fragment(models.Model):
114 # id = models.IntegerField(primary_key=True)
115 # text = models.TextField(blank=True)
116 # start_paragraph = models.IntegerField(null=True, blank=True)
117 # book_id = models.IntegerField(null=True, blank=True)
119 # db_table = u'fragment'
122 # class Inflections(models.Model):
123 # word = models.CharField(max_length=120, primary_key=True)
124 # cases = models.TextField() # This field type is a guess.
126 # db_table = u'inflections'
129 # class Paragraph(models.Model):
130 # id = models.IntegerField(primary_key=True)
131 # number = models.IntegerField(null=True, blank=True)
132 # text = models.TextField(blank=True)
133 # book_id = models.IntegerField(null=True, blank=True)
135 # db_table = u'paragraph'