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