launch a thread to check querysets in admin

main
eric 2023-11-25 16:55:41 -05:00
parent dfd2d8fdf2
commit 40313c0834
2 changed files with 14 additions and 5 deletions

View File

@ -5,7 +5,7 @@ from django.utils.safestring import mark_safe
# Register your models here. # Register your models here.
from . import models from . import models
from .check import check_link from .check import start_check_links
@admin.register(models.Check) @admin.register(models.Check)
@ -42,8 +42,8 @@ class LinkAdmin(admin.ModelAdmin):
@admin.action(description="Recheck the links") @admin.action(description="Recheck the links")
def recheck(self, request, queryset): def recheck(self, request, queryset):
for link in queryset: msg = start_check_links(queryset)
check_link(link) self.message_user(request, msg)
@admin.display(description="URL") @admin.display(description="URL")
def link_display(self, obj): def link_display(self, obj):

View File

@ -3,6 +3,7 @@
import logging import logging
import re import re
import threading
import time import time
from urllib.parse import urlparse from urllib.parse import urlparse
@ -128,7 +129,7 @@ def type_for_url(url, response=None):
return code, f'other; {ct}' return code, f'other; {ct}'
def check_link(link): def check_link(link):
''' given a Link object, check it's URL, put the result in a Check object ''' ''' given a Link object, check its URL, put the result in a Check object '''
check = Check(link=link) check = Check(link=link)
code, ct = type_for_url(link.url) code, ct = type_for_url(link.url)
check.return_code = code check.return_code = code
@ -138,4 +139,12 @@ def check_link(link):
check.link.save() check.link.save()
return check return check
def check_links(queryset):
for link in queryset:
check = check_link(link)
def start_check_links(queryset):
checker = threading.Thread(target=check_links, args=(queryset,), daemon=True)
checker.start()
return 'checker has started checking'