Fundraising in PDF.
[wolnelektury.git] / src / catalogue / models / source.py
1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
3 #
4 from django.db import models
5
6
7 class Source(models.Model):
8     """A collection of books, which might be defined before publishing them."""
9     netloc = models.CharField('położenie sieciowe', max_length=120, primary_key=True)
10     name = models.CharField('nazwa', max_length=120, blank=True)
11
12     class Meta:
13         ordering = ('netloc',)
14         verbose_name = 'źródło'
15         verbose_name_plural = 'źródła'
16         app_label = 'catalogue'
17
18     def __str__(self):
19         return self.netloc
20
21     def save(self, *args, **kwargs):
22         from catalogue.models import Book
23         try:
24             str(self.pk)
25             old_self = type(self).objects.get(pk=self)
26         except type(self).DoesNotExist:
27             old_name = ''
28             old_netloc = self.netloc
29         else:
30             old_name = old_self.name
31             old_netloc = old_self.netloc
32
33         ret = super(Source, self).save(*args, **kwargs)
34
35         # If something really changed here, find relevant books
36         # and invalidate their cached includes.
37         if old_name != self.name or old_netloc != self.netloc:
38             for book in Book.objects.all():
39                 source = book.get_extra_info_json().get('source_url', '')
40                 if self.netloc in source or (old_netloc != self.netloc and old_netloc in source):
41                     book.clear_cache()
42         return ret