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.
4 from django.core.management.base import BaseCommand
7 class Command(BaseCommand):
8 help = 'Checks for dead links.'
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
17 domain = Site.objects.get_current().domain
23 ('wiki_link', lambda b: b.wiki_link),
24 ('źródło', lambda b: b.get_extra_info_json().get('source_url')),
26 'admin:catalogue_book_change'
31 ('wiki_link', lambda p: p.wiki_link),
32 ('źródło', lambda p: p.get_extra_info_json().get('source_url')),
34 'admin:pictures_picture_change'
38 for model, model_fields, admin_name in fields:
39 for obj in model.objects.all():
41 for name, get in model_fields:
46 except (HTTPError, URLError, ValueError) as e:
49 print(str(obj).encode('utf-8'))
50 print(('Na stronie: https://%s%s' % (domain, obj.get_absolute_url())).encode('utf-8'))
52 ('Administracja: https://%s%s' % (domain, reverse(admin_name, args=[obj.pk])))
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'))