|
| 1 | +from django.contrib import admin, messages |
| 2 | +from django.utils.translation import gettext_lazy as _ |
| 3 | +from django.db.models import Count, Exists, OuterRef |
| 4 | + |
| 5 | +from django.template.defaultfilters import yesno |
| 6 | +from ..models import Link, Url, TYPE_CHOICES |
| 7 | +from ..templatetags.linkcheck_admin_tags import ( |
| 8 | + linkcheck_url, |
| 9 | + linkcheck_status_icon, |
| 10 | + linkcheck_ssl_icon, |
| 11 | + linkcheck_anchor_icon, |
| 12 | + linkcheck_status_code, |
| 13 | + linkcheck_links, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +class StatusFilter(admin.BooleanFieldListFilter): |
| 18 | + """ |
| 19 | + A custom status filter to include the ignore-status of links |
| 20 | + """ |
| 21 | + title = _('status') |
| 22 | + parameter_name = 'status' |
| 23 | + ignore_kwarg = 'ignore' |
| 24 | + |
| 25 | + def __init__(self, field, request, params, model, model_admin, field_path): |
| 26 | + self.ignore_val = params.get(self.ignore_kwarg) |
| 27 | + super().__init__(field, request, params, model, model_admin, field_path) |
| 28 | + |
| 29 | + def expected_parameters(self): |
| 30 | + return super().expected_parameters() + [self.ignore_kwarg] |
| 31 | + |
| 32 | + def choices(self, changelist): |
| 33 | + qs = Url.objects.annotate( |
| 34 | + ignore=Exists(Link.objects.filter(url=OuterRef('pk'), ignore=True)) |
| 35 | + ) |
| 36 | + field_choices = dict(self.field.flatchoices) |
| 37 | + return [ |
| 38 | + { |
| 39 | + 'selected': self.lookup_val is None and self.ignore_val is None and not self.lookup_val2, |
| 40 | + 'query_string': changelist.get_query_string( |
| 41 | + {self.lookup_kwarg: None}, |
| 42 | + [self.lookup_kwarg2, self.ignore_kwarg] |
| 43 | + ), |
| 44 | + 'display': _('All') + f' ({Url.objects.count()})', |
| 45 | + }, |
| 46 | + { |
| 47 | + 'selected': self.lookup_val == '1' and self.ignore_val == 'False' and not self.lookup_val2, |
| 48 | + 'query_string': changelist.get_query_string( |
| 49 | + {self.lookup_kwarg: '1', self.ignore_kwarg: 'False'}, |
| 50 | + [self.lookup_kwarg2] |
| 51 | + ), |
| 52 | + 'display': field_choices.get(True) + f' ({qs.filter(status=True, ignore=False).count()})', |
| 53 | + }, |
| 54 | + { |
| 55 | + 'selected': self.lookup_val == '0' and self.ignore_val == 'False' and not self.lookup_val2, |
| 56 | + 'query_string': changelist.get_query_string( |
| 57 | + {self.lookup_kwarg: '0', self.ignore_kwarg: 'False'}, |
| 58 | + [self.lookup_kwarg2] |
| 59 | + ), |
| 60 | + 'display': field_choices.get(False) + f' ({qs.filter(status=False, ignore=False).count()})', |
| 61 | + }, |
| 62 | + { |
| 63 | + 'selected': self.lookup_val2 == 'True' and self.ignore_val == 'False' and not self.lookup_val, |
| 64 | + 'query_string': changelist.get_query_string( |
| 65 | + {self.lookup_kwarg2: 'True', self.ignore_kwarg: 'False'}, |
| 66 | + [self.lookup_kwarg] |
| 67 | + ), |
| 68 | + 'display': field_choices.get(None) + f' ({qs.filter(status=None, ignore=False).count()})', |
| 69 | + }, |
| 70 | + { |
| 71 | + 'selected': self.ignore_val == 'True' and not self.lookup_val and not self.lookup_val2, |
| 72 | + 'query_string': changelist.get_query_string( |
| 73 | + {self.ignore_kwarg: 'True'}, |
| 74 | + [self.lookup_kwarg, self.lookup_kwarg2] |
| 75 | + ), |
| 76 | + 'display': _('Ignored') + f' ({qs.filter(ignore=True).count()})', |
| 77 | + } |
| 78 | + ] |
| 79 | + |
| 80 | + |
| 81 | +class TypeFilter(admin.SimpleListFilter): |
| 82 | + title = _('type') |
| 83 | + parameter_name = 'type' |
| 84 | + |
| 85 | + def lookups(self, request, model_admin): |
| 86 | + return TYPE_CHOICES |
| 87 | + |
| 88 | + def queryset(self, request, queryset): |
| 89 | + if not self.value(): |
| 90 | + return queryset |
| 91 | + urls = [url.pk for url in queryset if url.type == self.value()] |
| 92 | + return queryset.filter(pk__in=urls) |
| 93 | + |
| 94 | + |
| 95 | +class UrlAdmin(admin.ModelAdmin): |
| 96 | + list_display = [ |
| 97 | + 'list_url', |
| 98 | + 'list_status', |
| 99 | + 'list_ssl_status', |
| 100 | + 'list_anchor_status', |
| 101 | + 'list_status_code', |
| 102 | + 'list_get_message', |
| 103 | + 'list_type', |
| 104 | + 'list_links', |
| 105 | + 'list_ignore', |
| 106 | + ] |
| 107 | + list_display_links = None |
| 108 | + list_filter = [('status', StatusFilter), TypeFilter] |
| 109 | + sortable_by = [ |
| 110 | + 'list_url', |
| 111 | + 'list_status', |
| 112 | + 'list_ssl_status', |
| 113 | + 'list_anchor_status', |
| 114 | + 'list_status_code', |
| 115 | + 'list_links', |
| 116 | + ] |
| 117 | + actions = ['recheck', 'ignore', 'unignore'] |
| 118 | + empty_value_display = '' |
| 119 | + |
| 120 | + @admin.display(ordering='url', description=Url._meta.get_field('url').verbose_name) |
| 121 | + def list_url(self, url): |
| 122 | + return linkcheck_url(url) |
| 123 | + |
| 124 | + @admin.display(ordering='status', description=Url._meta.get_field('status').verbose_name) |
| 125 | + def list_status(self, url): |
| 126 | + return linkcheck_status_icon(url) |
| 127 | + |
| 128 | + @admin.display(ordering='ssl_status', description=_('SSL')) |
| 129 | + def list_ssl_status(self, url): |
| 130 | + return linkcheck_ssl_icon(url) |
| 131 | + |
| 132 | + @admin.display(ordering='anchor_status', description=_('Anchor')) |
| 133 | + def list_anchor_status(self, url): |
| 134 | + return linkcheck_anchor_icon(url) |
| 135 | + |
| 136 | + @admin.display(ordering='status_code', description=Url._meta.get_field('status_code').verbose_name) |
| 137 | + def list_status_code(self, url): |
| 138 | + return linkcheck_status_code(url) |
| 139 | + |
| 140 | + @admin.display(description=_('message')) |
| 141 | + def list_get_message(self, url): |
| 142 | + return url.get_message |
| 143 | + |
| 144 | + @admin.display(ordering='type', description=_('type')) |
| 145 | + def list_type(self, url): |
| 146 | + return url.get_type_display() |
| 147 | + |
| 148 | + @admin.display(ordering='links__count', description=Link._meta.verbose_name_plural) |
| 149 | + def list_links(self, url): |
| 150 | + return linkcheck_links(url) |
| 151 | + |
| 152 | + @admin.display(description=Link._meta.get_field('ignore').verbose_name) |
| 153 | + def list_ignore(self, url): |
| 154 | + return yesno(url.ignore) |
| 155 | + |
| 156 | + def has_add_permission(self, request, obj=None): |
| 157 | + return False |
| 158 | + |
| 159 | + def has_change_permission(self, request, obj=None): |
| 160 | + return False |
| 161 | + |
| 162 | + @admin.action(description=_('Recheck selected URLs')) |
| 163 | + def recheck(self, request, queryset): |
| 164 | + for url in queryset: |
| 165 | + url.check_url(external_recheck_interval=0) |
| 166 | + messages.success( |
| 167 | + request, |
| 168 | + _('The selected URLs were rechecked.'), |
| 169 | + ) |
| 170 | + |
| 171 | + @admin.action(description=_('Ignore selected URLs')) |
| 172 | + def ignore(self, request, queryset): |
| 173 | + Link.objects.filter(url__in=queryset).update(ignore=True) |
| 174 | + messages.success( |
| 175 | + request, |
| 176 | + _('The selected URLs are now ignored.'), |
| 177 | + ) |
| 178 | + |
| 179 | + @admin.action(description=_('No longer ignore selected URLs')) |
| 180 | + def unignore(self, request, queryset): |
| 181 | + Link.objects.filter(url__in=queryset).update(ignore=False) |
| 182 | + messages.success( |
| 183 | + request, |
| 184 | + _('The selected URLs are no longer ignored.'), |
| 185 | + ) |
| 186 | + |
| 187 | + def get_queryset(self, request): |
| 188 | + return super().get_queryset(request).annotate( |
| 189 | + Count('links'), |
| 190 | + ignore=Exists(Link.objects.filter(url=OuterRef('pk'), ignore=True)) |
| 191 | + ) |
| 192 | + |
| 193 | + def changelist_view(self, request, extra_context=None): |
| 194 | + extra_context = extra_context or {} |
| 195 | + if request.GET.get('status__exact') == '1': |
| 196 | + title = _('Valid URLs') |
| 197 | + elif request.GET.get('status__exact') == '0': |
| 198 | + title = _('Invalid URLs') |
| 199 | + elif request.GET.get('status__isnull') == 'True': |
| 200 | + title = _('Unchecked URLs') |
| 201 | + elif request.GET.get('ignore') == 'True': |
| 202 | + title = _('Ignored URLs') |
| 203 | + else: |
| 204 | + title = _('URLs') |
| 205 | + extra_context['title'] = title |
| 206 | + return super().changelist_view(request, extra_context=extra_context) |
| 207 | + |
| 208 | + class Media: |
| 209 | + css = { |
| 210 | + 'all': ['linkcheck/css/style.css'], |
| 211 | + } |
0 commit comments