From 3fdb3b89490e7d2ad13c4f52d4f54fca4eb2b281 Mon Sep 17 00:00:00 2001 From: Timo Ludwig Date: Fri, 14 Apr 2023 00:46:24 +0200 Subject: [PATCH] Deprecate `report` view --- CHANGELOG | 1 + linkcheck/templates/linkcheck/coverage.html | 1 - linkcheck/templates/linkcheck/paginator.html | 32 ---- linkcheck/templates/linkcheck/report.html | 186 ------------------- linkcheck/urls.py | 1 - linkcheck/views.py | 163 ---------------- 6 files changed, 1 insertion(+), 383 deletions(-) delete mode 100644 linkcheck/templates/linkcheck/paginator.html delete mode 100644 linkcheck/templates/linkcheck/report.html diff --git a/CHANGELOG b/CHANGELOG index fc2f991..f7d66a0 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,6 +2,7 @@ Unreleased * Migrate linkcheck views to `ModelAdmin` (Timo Ludwig, #186) * Add `ModelAdmin` for Url and Link models + * Deprecate `report` view * Fix internal redirect checker (Timo Ludwig, #180) * Fix SSL status of unreachable domains (Timo Ludwig, #184) * Fix URL message for internal server errorrs (Timo Ludwig, #182) diff --git a/linkcheck/templates/linkcheck/coverage.html b/linkcheck/templates/linkcheck/coverage.html index 0f1c4eb..5bfd53b 100644 --- a/linkcheck/templates/linkcheck/coverage.html +++ b/linkcheck/templates/linkcheck/coverage.html @@ -11,7 +11,6 @@ {% block breadcrumbs %} {% endblock %} diff --git a/linkcheck/templates/linkcheck/paginator.html b/linkcheck/templates/linkcheck/paginator.html deleted file mode 100644 index d48eb0d..0000000 --- a/linkcheck/templates/linkcheck/paginator.html +++ /dev/null @@ -1,32 +0,0 @@ -{% load i18n %} -
- - {% if pages.number > 1 %} - < First - {% else %} - < {% translate "First" %} - {% endif %} - - {% if pages.has_previous %} - << {% translate "Previous" %} - {% else %} - << {% translate "Previous" %} - {% endif %} - - - {% blocktrans with current=pages.number max=pages.paginator.num_pages %}Page {{ current }} of {{ max }}{% endblocktrans %} - - - {% if pages.has_next %} - {% translate "Next" %} >> - {% else %} - {% translate "Next" %} >> - {% endif %} - - {% if pages.number != pages.paginator.num_pages %} - {% translate "Last" %} > - {% else %} - {% translate "Last" %} > - {% endif %} - -
diff --git a/linkcheck/templates/linkcheck/report.html b/linkcheck/templates/linkcheck/report.html deleted file mode 100644 index 161a968..0000000 --- a/linkcheck/templates/linkcheck/report.html +++ /dev/null @@ -1,186 +0,0 @@ -{% extends "linkcheck/base_linkcheck.html" %} -{% load i18n %} -{% load linkcheck_model_tags %} -{% block extrahead %} -{{ block.super }} - - - -{% endblock %} - -{% block innercontent %} - -
- {% translate "Show" %}:   - {% if filter == 'show_valid' %}{% translate "Valid links" %}{% else %}{% translate "Valid links" %}{% endif %}   - {% if filter == 'show_invalid' %}{% translate "Broken links" %}{% else %}{% translate "Broken links" %}{% endif %}   - {% if filter == 'show_unchecked' %}{% translate "Untested links" %}{% else %}{% translate "Untested links" %}{% endif %}   - {% if filter == 'ignored' %}{% translate "Ignored links" %}{% else %}{% translate "Ignored links" %}{% endif %} - ({{ ignored_count }}) -
-
- - {% for content_type in content_types_list %} - - -

{{content_type.content_type|get_verbose_name_plural}}

- {% for object in content_type.object_list %} -
-

{{report_type}} in '{{object.object}}'

   - {% blocktrans with content_type_name=content_type.content_type.name %}View {{ content_type_name }}{% endblocktrans %}   - {% if object.admin_url %}{% blocktrans with content_type_name=content_type.content_type.name %}Edit {{ content_type_name }}{% endblocktrans %}{% endif %} - - - - - - - - {% for link in object.link_list %} - - - - - - - - - {% if link.url.redirect_to %} - - {% endif %} - {% endfor %} - -
- {% endfor %} -
- {% endfor %} - {% csrf_token %} - {% if content_types_list %} - {% include "linkcheck/paginator.html" %} - {% endif %} -{% endblock %} diff --git a/linkcheck/urls.py b/linkcheck/urls.py index 3106cc2..7eb7c6a 100644 --- a/linkcheck/urls.py +++ b/linkcheck/urls.py @@ -4,5 +4,4 @@ urlpatterns = [ path('coverage/', views.coverage, name='linkcheck_coverage'), - path('', views.report, name='linkcheck_report'), ] diff --git a/linkcheck/views.py b/linkcheck/views.py index 41de93e..7033034 100644 --- a/linkcheck/views.py +++ b/linkcheck/views.py @@ -1,20 +1,6 @@ -from itertools import groupby -from operator import itemgetter - -from django import forms from django.contrib.admin.views.decorators import staff_member_required -from django.contrib.contenttypes.models import ContentType -from django.core.exceptions import ObjectDoesNotExist -from django.core.paginator import Paginator -from django.http import JsonResponse from django.shortcuts import render -from django.templatetags.static import static -from django.urls import NoReverseMatch, reverse -from django.utils.translation import gettext as _ -from linkcheck import update_lock -from linkcheck.linkcheck_settings import RESULTS_PER_PAGE -from linkcheck.models import Link from linkcheck.utils import get_coverage_data @@ -33,152 +19,3 @@ def coverage(request): context = {'coverage_data': coverage_data} return render(request, template, context) - - -@staff_member_required -def report(request): - - outerkeyfunc = itemgetter('content_type_id') - content_types_list = [] - - if request.method == 'POST': - - ignore_link_id = request.GET.get('ignore', None) - if ignore_link_id is not None: - link = Link.objects.get(id=ignore_link_id) - link.ignore = True - link.save() - if is_ajax(request): - json_data = {'link': link.pk} - return JsonResponse(json_data) - - unignore_link_id = request.GET.get('unignore', None) - if unignore_link_id is not None: - link = Link.objects.get(id=unignore_link_id) - link.ignore = False - link.save() - if is_ajax(request): - json_data = {'link': link.pk} - return JsonResponse(json_data) - - recheck_link_id = request.GET.get('recheck', None) - if recheck_link_id is not None: - link = Link.objects.get(id=recheck_link_id) - url = link.url - url.check_url(external_recheck_interval=0) - links = [x[0] for x in url.links.values_list('id')] - if is_ajax(request): - json_data = ({ - 'links': links, - 'message': url.message, - 'colour': url.colour, - }) - return JsonResponse(json_data) - - link_filter = request.GET.get('filters', 'show_invalid') - - qset = Link.objects.order_by('-url__last_checked') - if link_filter == 'show_valid': - qset = qset.filter(ignore=False, url__status__exact=True) - report_type = _('Valid links') - elif link_filter == 'show_unchecked': - qset = qset.filter(ignore=False, url__last_checked__exact=None) - report_type = _('Untested links') - elif link_filter == 'ignored': - qset = qset.filter(ignore=True) - report_type = _('Ignored links') - else: - qset = qset.filter(ignore=False, url__status__exact=False) - report_type = _('Broken links') - - paginated_links = Paginator(qset, RESULTS_PER_PAGE, 0, True) - - try: - page = int(request.GET.get("page", "1")) - except ValueError: - page = 0 - # offset = (page - 1) * RESULTS_PER_PAGE - links = paginated_links.page(page) - - # This code groups links into nested lists by content type and object id - # It's a bit nasty but we can't use groupby unless be get values() - # instead of a queryset because of the 'Object is not subscriptable' error - - t = sorted(links.object_list.values(), key=outerkeyfunc) - for tk, tg in groupby(t, outerkeyfunc): - innerkeyfunc = itemgetter('object_id') - objects = [] - tg = sorted(tg, key=innerkeyfunc) - for ok, og in groupby(tg, innerkeyfunc): - content_type = ContentType.objects.get(pk=tk) - og = list(og) - try: - object = None - if content_type.model_class(): - object = content_type.model_class().objects.get(pk=ok) - except ObjectDoesNotExist: - pass - try: - admin_url = object.get_admin_url() # TODO allow method name to be configurable - except AttributeError: - try: - admin_url = reverse(f'admin:{content_type.app_label}_{content_type.model}_change', args=[ok]) - except NoReverseMatch: - admin_url = None - - objects.append({ - 'object': object, - # Convert values_list back to queryset. Do we need to get values() or do we just need a list of ids? - 'link_list': Link.objects.in_bulk([x['id'] for x in og]).values(), - 'admin_url': admin_url, - }) - content_types_list.append({ - 'content_type': content_type, - 'object_list': objects - }) - - # Pass any querystring data back to the form minus page - rqst = request.GET.copy() - if 'page' in rqst: - del rqst['page'] - - return render(request, 'linkcheck/report.html', { - 'content_types_list': content_types_list, - 'pages': links, - 'filter': link_filter, - 'media': forms.Media(js=[static(get_jquery_min_js())]), - 'qry_data': rqst.urlencode(), - 'report_type': report_type, - 'ignored_count': Link.objects.filter(ignore=True).count(), - }, - ) - - -def get_jquery_min_js(): - """ - Return the location of jquery.min.js. It's an entry point to adapt the path - when it changes in Django. - """ - return 'admin/js/vendor/jquery/jquery.min.js' - - -def get_status_message(): - if update_lock.locked(): - return "Still checking. Please refresh this page in a short while. " - else: - broken_links = Link.objects.filter(ignore=False, url__status=False).count() - if broken_links: - return ( - "We've found {} broken link{}.
" - "View/fix broken links".format( - broken_links, - "s" if broken_links > 1 else "", - reverse('linkcheck_report'), - ) - ) - else: - return '' - - -def is_ajax(request): - return request.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'