X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/d454277a61e2f34e34ee87777b0d35724edf877e..2c0eb88678075e583641f4d1debe3bec2bfd0bf1:/apps/catalogue/models/source.py?ds=inline diff --git a/apps/catalogue/models/source.py b/apps/catalogue/models/source.py index f530e07f4..9aff4efec 100644 --- a/apps/catalogue/models/source.py +++ b/apps/catalogue/models/source.py @@ -9,7 +9,7 @@ from django.utils.translation import ugettext_lazy as _ class Source(models.Model): """A collection of books, which might be defined before publishing them.""" netloc = models.CharField(_('network location'), max_length=120, primary_key=True) - name = models.CharField(_('name'), max_length=120) + name = models.CharField(_('name'), max_length=120, blank=True) class Meta: ordering = ('netloc',) @@ -19,3 +19,27 @@ class Source(models.Model): def __unicode__(self): return self.netloc + + def save(self, *args, **kwargs): + from catalogue.models import Book + try: + str(self.pk) + old_self = type(self).objects.get(pk=self) + except type(self).DoesNotExist: + old_name = u'' + old_netloc = self.netloc + else: + old_name = old_self.name + old_netloc = old_self.netloc + + ret = super(Source, self).save(*args, **kwargs) + + # If something really changed here, find relevant books + # and invalidate their cached includes. + if old_name != self.name or old_netloc != self.netloc: + for book in Book.objects.all(): + source = book.extra_info.get('source_url', '') + if self.netloc in source or (old_netloc != self.netloc + and old_netloc in source): + book.flush_includes() + return ret