pep8 and other code-style changes
[wolnelektury.git] / src / catalogue / models / source.py
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.
4 #
5 from django.db import models
6 from django.utils.translation import ugettext_lazy as _
7
8
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)
13
14     class Meta:
15         ordering = ('netloc',)
16         verbose_name = _('source')
17         verbose_name_plural = _('sources')
18         app_label = 'catalogue'
19
20     def __unicode__(self):
21         return self.netloc
22
23     def save(self, *args, **kwargs):
24         from catalogue.models import Book
25         try:
26             str(self.pk)
27             old_self = type(self).objects.get(pk=self)
28         except type(self).DoesNotExist:
29             old_name = u''
30             old_netloc = self.netloc
31         else:
32             old_name = old_self.name
33             old_netloc = old_self.netloc
34
35         ret = super(Source, self).save(*args, **kwargs)
36
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):
43                     book.flush_includes()
44         return ret