Skip to content

Commit

Permalink
Add Email reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
po5i committed Jun 11, 2018
1 parent 1383a74 commit 289a385
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
3 changes: 1 addition & 2 deletions wagtaillinkchecker/management/commands/linkcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ def handle(self, *args, **kwargs):
site = Site.objects.filter(is_default_site=True).first()
pages = site.root_page.get_descendants(inclusive=True).live().public()
print(f'Scanning {len(pages)} pages...')
scan = broken_link_scan(site)
broken_links = ScanLink.objects.filter(scan=scan, crawled=True)
broken_link_scan(site)
print('Links enqueued on Redis')
32 changes: 32 additions & 0 deletions wagtaillinkchecker/models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import django_rq

from django.core import mail
from django.conf import settings
from django.db import models
from django.db.models.signals import pre_delete
from django.dispatch import receiver
from django.template.loader import render_to_string
from django.utils.translation import ugettext_lazy as _
from wagtail import __version__ as WAGTAIL_VERSION

Expand Down Expand Up @@ -45,6 +47,36 @@ def result(self):
def __str__(self):
return 'Scan - {0}'.format(self.scan_started.strftime('%d/%m/%Y'))

def reporting(self):
email_message = ''
pages = self.site.root_page.get_descendants(
inclusive=True).live().public()
broken_links = self.links.broken_links()
for page in pages:
page_broken_links = []
for link in broken_links:
if link.page == page:
page_broken_links.append(link)

if page_broken_links:
email_message += render_to_string(
'wagtaillinkchecker/emails/broken_links.html', {
'page_broken_links': page_broken_links,
'user': '',
'page': page,
'base_url': self.site.root_url,
'site_name': settings.WAGTAIL_SITE_NAME,
})

with mail.get_connection() as connection:
email = mail.EmailMessage(
'Broken links for {}'.format(self),
email_message,
settings.DEFAULT_FROM_EMAIL,
[settings.DEFAULT_FROM_EMAIL])
email.content_subtype = 'html'
email.send()


class ScanLinkQuerySet(models.QuerySet):

Expand Down
1 change: 1 addition & 0 deletions wagtaillinkchecker/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ def check_link(link_pk):
scan = link.scan
scan.scan_finished = timezone.now()
scan.save()
scan.reporting()

return True

0 comments on commit 289a385

Please sign in to comment.