launch a thread to check querysets in admin
parent
dfd2d8fdf2
commit
40313c0834
|
@ -5,7 +5,7 @@ from django.utils.safestring import mark_safe
|
|||
# Register your models here.
|
||||
|
||||
from . import models
|
||||
from .check import check_link
|
||||
from .check import start_check_links
|
||||
|
||||
|
||||
@admin.register(models.Check)
|
||||
|
@ -42,8 +42,8 @@ class LinkAdmin(admin.ModelAdmin):
|
|||
|
||||
@admin.action(description="Recheck the links")
|
||||
def recheck(self, request, queryset):
|
||||
for link in queryset:
|
||||
check_link(link)
|
||||
msg = start_check_links(queryset)
|
||||
self.message_user(request, msg)
|
||||
|
||||
@admin.display(description="URL")
|
||||
def link_display(self, obj):
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
import logging
|
||||
import re
|
||||
import threading
|
||||
import time
|
||||
from urllib.parse import urlparse
|
||||
|
||||
|
@ -128,7 +129,7 @@ def type_for_url(url, response=None):
|
|||
return code, f'other; {ct}'
|
||||
|
||||
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)
|
||||
code, ct = type_for_url(link.url)
|
||||
check.return_code = code
|
||||
|
@ -138,4 +139,12 @@ def check_link(link):
|
|||
check.link.save()
|
||||
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'
|
||||
|
||||
|
|
Loading…
Reference in New Issue