fix live not being set to false when link removed

main
eric 2023-10-25 17:32:04 -04:00
parent ce34b40399
commit e92fb44365
5 changed files with 44 additions and 15 deletions

View File

@ -101,6 +101,9 @@ def load_doab_record(doab_id, title, publisher_name, item_type, urls, timestamps
linkrel.status = 1 linkrel.status = 1
else: else:
linkrel.status = 0 linkrel.status = 0
linkrel.save()
linkrel.link.save()
return new_item return new_item
@ -115,6 +118,7 @@ def set_deleted(record):
for linkrel in item.related.all(): for linkrel in item.related.all():
linkrel.status = 0 linkrel.status = 0
linkrel.save() linkrel.save()
linkrel.link.save()
return item return item
except Item.DoesNotExist: except Item.DoesNotExist:
logger.warning(f'no item {doab}') logger.warning(f'no item {doab}')

View File

@ -18,7 +18,7 @@ class Command(BaseCommand):
def check_data(self, item): def check_data(self, item):
link_dict = {'doab': item.doab} link_dict = {'doab': item.doab}
for link in item.links.filter(live=True): for link in item.links.filter(related__status=1):
link_dict['url'] = link.url link_dict['url'] = link.url
if link.recent_check: if link.recent_check:
link_dict['checked'] = link.recent_check.created link_dict['checked'] = link.recent_check.created
@ -44,5 +44,5 @@ class Command(BaseCommand):
end_time = datetime.datetime.now() end_time = datetime.datetime.now()
logger.info(f'wrote {num} link checks in {end_time - start_time}') logger.info(f'wrote {num} link checks in {end_time - start_time}')
self.stdout.write(f'wrote {num} link checks in {end_time - start_time}') self.stdout.write(f'wrote link checks for {num} items in {end_time - start_time}')

View File

@ -0,0 +1,19 @@
from django.core.management.base import BaseCommand
from doab_check.models import Link
class Command(BaseCommand):
help = "set live attribute for all links"
def handle(self, **options):
changed = 0
for link in Link.objects.all():
live = link.live
link.save()
if live != link.live:
changed += 1
self.stdout.write(f'changed {changed} links')

View File

@ -52,6 +52,12 @@ class Link(models.Model):
if netloc.startswith('www.'): if netloc.startswith('www.'):
netloc = netloc[4:] netloc = netloc[4:]
self.provider = netloc self.provider = netloc
if self.id:
live = False
for linkrel in self.related.filter(status=1):
live = True
break
self.live = live
super().save(*args, **kwargs) super().save(*args, **kwargs)

View File

@ -15,8 +15,8 @@ class HomepageView(generic.TemplateView):
template_name = 'index.html' template_name = 'index.html'
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
active_links = Link.objects.filter(recent_check__isnull=False).only( active_links = Link.objects.filter(recent_check__isnull=False, live=True
'recent_check').order_by('-recent_check__return_code') ).only('recent_check').order_by('-recent_check__return_code')
codes = active_links.values( codes = active_links.values(
'recent_check__return_code').distinct() 'recent_check__return_code').distinct()
num_checked = active_links.count() num_checked = active_links.count()
@ -40,9 +40,9 @@ class ProblemsView(generic.TemplateView):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
code = kwargs['code'] code = kwargs['code']
problems = Link.objects.exclude(recent_check__isnull=True).filter( problems = Link.objects.exclude(recent_check__isnull=True
recent_check__return_code__exact=code).order_by('provider' ).filter(recent_check__return_code__exact=code, live=True
).annotate(title=F('items__title')) ).order_by('provider').annotate(title=F('items__title'))
providers = problems.values('provider').distinct().annotate(Count('provider')) providers = problems.values('provider').distinct().annotate(Count('provider'))
for provider in providers: for provider in providers:
provider['links'] = problems.filter(provider=provider['provider']) provider['links'] = problems.filter(provider=provider['provider'])
@ -121,7 +121,7 @@ class PublisherView(generic.TemplateView):
if pub == NOPUBNAME: if pub == NOPUBNAME:
pub = '' pub = ''
publisher_links = Link.objects.filter( publisher_links = Link.objects.filter(
items__publisher_name=pub, items__status=1, recent_check__isnull=False items__publisher_name=pub, live=True, recent_check__isnull=False
).annotate(title=F('items__title')) ).annotate(title=F('items__title'))
link_count = publisher_links.distinct().count() link_count = publisher_links.distinct().count()
@ -151,12 +151,12 @@ def link_api_view(request, doab):
data['status'] = 'found' data['status'] = 'found'
links = [] links = []
data['links'] = links data['links'] = links
for link in item.links.filter(live=True): for linkrel in item.related.filter(status=1):
link_dict = {'url': link.url} link_dict = {'url': linkrel.link.url}
if link.recent_check: if linkrel.link.recent_check:
link_dict['checked'] = link.recent_check.created link_dict['checked'] = linkrel.link.recent_check.created
link_dict['return_code'] = link.recent_check.return_code link_dict['return_code'] = linkrel.link.recent_check.return_code
link_dict['content_type'] = link.recent_check.content_type link_dict['content_type'] = linkrel.link.recent_check.content_type
links.append(link_dict) links.append(link_dict)
return JsonResponse(data) return JsonResponse(data)