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.
5 from __future__ import print_function, unicode_literals
7 from django.core.management.base import BaseCommand
10 class Command(BaseCommand):
11 help = 'Checks for dead links.'
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
20 domain = Site.objects.get_current().domain
26 ('wiki_link', lambda b: b.wiki_link),
27 ('źródło', lambda b: b.extra_info.get('source_url')),
29 'admin:catalogue_book_change'
34 ('wiki_link', lambda p: p.wiki_link),
35 ('źródło', lambda p: p.extra_info.get('source_url')),
37 'admin:pictures_picture_change'
41 for model, model_fields, admin_name in fields:
42 for obj in model.objects.all():
44 for name, get in model_fields:
49 except (HTTPError, URLError, ValueError), e:
52 print(unicode(obj).encode('utf-8'))
53 print(('Na stronie: https://%s%s' % (domain, obj.get_absolute_url())).encode('utf-8'))
55 ('Administracja: https://%s%s' % (domain, reverse(admin_name, args=[obj.pk])))
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'))