1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from django.db import models
6 from django.utils.translation import ugettext_lazy as _
9 class Source(models.Model):
10 """A collection of books, which might be defined before publishing them."""
11 netloc = models.CharField(_('network location'), max_length=120, primary_key=True)
12 name = models.CharField(_('name'), max_length=120, blank=True)
15 ordering = ('netloc',)
16 verbose_name = _('source')
17 verbose_name_plural = _('sources')
18 app_label = 'catalogue'
20 def __unicode__(self):
23 def save(self, *args, **kwargs):
24 from catalogue.models import Book
27 old_self = type(self).objects.get(pk=self)
28 except type(self).DoesNotExist:
30 old_netloc = self.netloc
32 old_name = old_self.name
33 old_netloc = old_self.netloc
35 ret = super(Source, self).save(*args, **kwargs)
37 # If something really changed here, find relevant books
38 # and invalidate their cached includes.
39 if old_name != self.name or old_netloc != self.netloc:
40 for book in Book.objects.all():
41 source = book.extra_info.get('source_url', '')
42 if self.netloc in source or (old_netloc != self.netloc and old_netloc in source):