1eb73127b5b28a109bec64c2972666bceef82689
[wolnelektury.git] / src / catalogue / management / commands / report_dead_links.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 __future__ import print_function, unicode_literals
6
7 from django.core.management.base import BaseCommand
8
9
10 class Command(BaseCommand):
11     help = 'Checks for dead links.'
12
13     def handle(self, **options):
14         from catalogue.models import Book
15         from picture.models import Picture
16         from urllib2 import urlopen, HTTPError, URLError
17         from django.core.urlresolvers import reverse
18         from django.contrib.sites.models import Site
19
20         domain = Site.objects.get_current().domain
21
22         fields = [
23             (
24                 Book,
25                 [
26                     ('wiki_link', lambda b: b.wiki_link),
27                     ('źródło', lambda b: b.extra_info.get('source_url')),
28                 ],
29                 'admin:catalogue_book_change'
30             ),
31             (
32                 Picture,
33                 [
34                     ('wiki_link', lambda p: p.wiki_link),
35                     ('źródło', lambda p: p.extra_info.get('source_url')),
36                 ],
37                 'admin:pictures_picture_change'
38             )
39         ]
40
41         for model, model_fields, admin_name in fields:
42             for obj in model.objects.all():
43                 clean = True
44                 for name, get in model_fields:
45                     url = get(obj)
46                     if url:
47                         try:
48                             urlopen(url)
49                         except (HTTPError, URLError, ValueError), e:
50                             if clean:
51                                 clean = False
52                                 print(unicode(obj).encode('utf-8'))
53                                 print(('Na stronie: https://%s%s' % (domain, obj.get_absolute_url())).encode('utf-8'))
54                                 print(
55                                     ('Administracja: https://%s%s' % (domain, reverse(admin_name, args=[obj.pk])))
56                                     .encode('utf-8'))
57                                 if obj.extra_info.get('about'):
58                                     print(('Redakcja: %s' % (obj.extra_info.get('about'),)).encode('utf-8'))
59                             print(('    %s (%s): %s' % (name, getattr(e, 'code', 'błąd'), url)).encode('utf-8'))
60                 if not clean:
61                     print()