Skip to content

Commit

Permalink
feat: improve linkcheck command
Browse files Browse the repository at this point in the history
  • Loading branch information
joaquimds committed Sep 17, 2024
1 parent 12e7c2b commit 8f0d81d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
10 changes: 6 additions & 4 deletions wagtaillinkchecker/management/commands/linkcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from wagtaillinkchecker.scanner import broken_link_scan
from wagtaillinkchecker.models import ScanLink

from wagtail.models import PageRevision, Site
from wagtail.models import Revision, Site


class Command(BaseCommand):
Expand All @@ -21,10 +21,10 @@ def add_arguments(self, parser):
def handle(self, *args, **kwargs):
site = Site.objects.filter(is_default_site=True).first()
pages = site.root_page.get_descendants(inclusive=True).live().public()
verbosity = kwargs.get("verbosity") or 1
verbosity = 2

print(f"Scanning {len(pages)} pages...")
scan = broken_link_scan(site, verbosity)
scan = broken_link_scan(site, verbosity, sync=True)
total_links = ScanLink.objects.filter(scan=scan, crawled=True)
broken_links = ScanLink.objects.filter(scan=scan, broken=True)
print(
Expand All @@ -37,7 +37,7 @@ def handle(self, *args, **kwargs):

messages = []
for page in pages:
revisions = PageRevision.objects.filter(page=page)
revisions = page.revisions
user = None
user_email = settings.DEFAULT_FROM_EMAIL
if revisions:
Expand All @@ -48,6 +48,8 @@ def handle(self, *args, **kwargs):
for link in broken_links:
if link.page == page:
page_broken_links.append(link)
if not page_broken_links:
continue
email_message = render_to_string(
"wagtaillinkchecker/emails/broken_links.html",
{
Expand Down
7 changes: 5 additions & 2 deletions wagtaillinkchecker/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,11 @@ def __str__(self):
def page_is_deleted(self):
return self.page_deleted and self.page_slug

def check_link(self, verbosity=1):
from wagtaillinkchecker.tasks import check_link
def check_link(self, verbosity=1, sync=False):
from wagtaillinkchecker.tasks import check_link, check_link_sync

if sync:
return check_link_sync(self.pk, verbosity=verbosity)

check_link(self.pk, verbosity=verbosity)

Expand Down
4 changes: 2 additions & 2 deletions wagtaillinkchecker/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def clean_url(url, site):
return url


def broken_link_scan(site, verbosity=1):
def broken_link_scan(site, verbosity=1, sync=False):
from wagtaillinkchecker.models import Scan, ScanLink

pages = site.root_page.get_descendants(inclusive=True).live().public()
Expand All @@ -123,6 +123,6 @@ def broken_link_scan(site, verbosity=1):
ScanLink.objects.get(url=url, scan=scan)
except ScanLink.DoesNotExist:
link = ScanLink.objects.create(url=page.full_url, page=page, scan=scan)
link.check_link(verbosity=verbosity)
link.check_link(verbosity=verbosity, sync=sync)

return scan
4 changes: 4 additions & 0 deletions wagtaillinkchecker/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def check_link(
link_pk,
verbosity=1,
):
return check_link_sync(link_pk, verbosity=verbosity)


def check_link_sync(link_pk, verbosity=1):
link = ScanLink.objects.get(pk=link_pk)
site = link.scan.site
url = get_url(link.url, link.page, site)
Expand Down

0 comments on commit 8f0d81d

Please sign in to comment.