X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/0cae17bec6d31806615fae59a5b3945016285fbe..f062d83d5999fa0540aabef056fa21ef225728fd:/apps/catalogue/models.py diff --git a/apps/catalogue/models.py b/apps/catalogue/models.py index 9786150e6..5f4ec923c 100644 --- a/apps/catalogue/models.py +++ b/apps/catalogue/models.py @@ -34,12 +34,13 @@ class TagSubcategoryManager(models.Manager): class Tag(TagBase): name = models.CharField(_('name'), max_length=50, unique=True, db_index=True) - slug = models.SlugField(_('slug'), unique=True, db_index=True) - sort_key = models.SlugField(_('sort key'), db_index=True) + slug = models.SlugField(_('slug'), max_length=120, unique=True, db_index=True) + sort_key = models.SlugField(_('sort key'), max_length=120, db_index=True) category = models.CharField(_('category'), max_length=50, blank=False, null=False, db_index=True, choices=TAG_CATEGORIES) - description = models.TextField(blank=True) - + description = models.TextField(_('description'), blank=True) + main_page = models.BooleanField(_('main page'), default=False, db_index=True, help_text=_('Show tag on main page')) + user = models.ForeignKey(User, blank=True, null=True) def has_description(self): @@ -68,18 +69,25 @@ class Tag(TagBase): return TagBase.get_tag_list(tags) +def book_upload_path(ext): + def get_dynamic_path(book, filename): + return 'lektura/%s.%s' % (book.slug, ext) + return get_dynamic_path + + class Book(models.Model): title = models.CharField(_('title'), max_length=120) - slug = models.SlugField(_('slug'), unique=True, db_index=True) + slug = models.SlugField(_('slug'), max_length=120, unique=True, db_index=True) description = models.TextField(_('description'), blank=True) created_at = models.DateTimeField(_('creation date'), auto_now=True) _short_html = models.TextField(_('short HTML'), editable=False) # Formats - xml_file = models.FileField(_('XML file'), upload_to='books/xml', blank=True) - pdf_file = models.FileField(_('PDF file'), upload_to='books/pdf', blank=True) - odt_file = models.FileField(_('ODT file'), upload_to='books/odt', blank=True) - html_file = models.FileField(_('HTML file'), upload_to='books/html', blank=True) + xml_file = models.FileField(_('XML file'), upload_to=book_upload_path('xml'), blank=True) + html_file = models.FileField(_('HTML file'), upload_to=book_upload_path('html'), blank=True) + pdf_file = models.FileField(_('PDF file'), upload_to=book_upload_path('pdf'), blank=True) + odt_file = models.FileField(_('ODT file'), upload_to=book_upload_path('odt'), blank=True) + txt_file = models.FileField(_('TXT file'), upload_to=book_upload_path('txt'), blank=True) parent = models.ForeignKey('self', blank=True, null=True, related_name='children') @@ -135,7 +143,8 @@ class Book(models.Model): # Read book metadata book_info = dcparser.parse(xml_file) - book = Book(title=book_info.title, slug=slughifi(book_info.title)) + book_base, book_slug = book_info.url.rsplit('/', 1) + book = Book(title=book_info.title, slug=book_slug) book.save() book_tags = [] @@ -207,7 +216,7 @@ class Fragment(models.Model): text = models.TextField() short_text = models.TextField(editable=False) _short_html = models.TextField(editable=False) - anchor = models.IntegerField() + anchor = models.CharField(max_length=120) book = models.ForeignKey(Book, related_name='fragments') objects = models.Manager() @@ -225,7 +234,10 @@ class Fragment(models.Model): {'fragment': self, 'book': self.book, 'book_authors': book_authors})) self.save() return mark_safe(self._short_html) - + + def get_absolute_url(self): + return '%s#m%s' % (self.book.html_file.url, self.anchor) + class Meta: ordering = ('book', 'anchor',) verbose_name = _('fragment')