1
0
Fork 0
mirror of synced 2024-06-29 03:20:58 +12:00

tweak snapshot asset serving logic to show multiple choices in case of conflict

This commit is contained in:
Nick Sweeting 2021-02-16 15:51:56 -05:00
parent a6c64f2560
commit 28e50c5e49

View file

@ -62,19 +62,21 @@ class SnapshotView(View):
try: try:
try: try:
snapshot = Snapshot.objects.get(timestamp=slug) snapshot = Snapshot.objects.get(timestamp=slug)
response = static.serve(request, archivefile, document_root=snapshot.link_dir, show_indexes=True)
response["Link"] = f'<{snapshot.url}>; rel="canonical"'
return response
except Snapshot.DoesNotExist: except Snapshot.DoesNotExist:
if Snapshot.objects.filter(timestamp__startswith=slug).exists(): if Snapshot.objects.filter(timestamp__startswith=slug).exists():
raise Snapshot.MultipleObjectsReturned raise Snapshot.MultipleObjectsReturned
response = static.serve(request, archivefile, document_root=snapshot.link_dir, show_indexes=True) else:
response["Link"] = f'<{snapshot.url}>; rel="canonical"' raise
return response
except Snapshot.DoesNotExist: except Snapshot.DoesNotExist:
# Snapshot does not exist # Snapshot does not exist
return HttpResponse( return HttpResponse(
format_html( format_html(
( (
'<center><br/><br/><br/>' '<center><br/><br/><br/>'
'No Snapshots match the given timestamp: <code>{}</code><br/><br/>' 'No Snapshots match the given timestamp or UUID: <code>{}</code><br/><br/>'
'You can <a href="/add/" target="_top">add a new Snapshot</a>, or return to the <a href="/" target="_top">Main Index</a>' 'You can <a href="/add/" target="_top">add a new Snapshot</a>, or return to the <a href="/" target="_top">Main Index</a>'
'</center>' '</center>'
), ),
@ -99,7 +101,7 @@ class SnapshotView(View):
return HttpResponse( return HttpResponse(
format_html( format_html(
( (
'Multiple Snapshots match the given timestamp <code>{}</code><br/><pre>' 'Multiple Snapshots match the given timestamp/UUID <code>{}</code><br/><pre>'
), ),
slug, slug,
) + snapshot_hrefs + format_html( ) + snapshot_hrefs + format_html(
@ -134,70 +136,69 @@ class SnapshotView(View):
status=404, status=404,
) )
# slug is a URL # slug is a URL
else: try:
try: try:
# try exact match on full url first
snapshot = Snapshot.objects.get(
Q(url='http://' + path) | Q(url='https://' + path)
)
except Snapshot.DoesNotExist:
# fall back to match on exact base_url
try: try:
# try exact match on full url first
snapshot = Snapshot.objects.get( snapshot = Snapshot.objects.get(
Q(url='http://' + path) | Q(url='https://' + path) Q(url='http://' + base_url(path)) | Q(url='https://' + base_url(path))
) )
except Snapshot.DoesNotExist: except Snapshot.DoesNotExist:
# fall back to match on exact base_url # fall back to matching base_url as prefix
try: snapshot = Snapshot.objects.get(
snapshot = Snapshot.objects.get(
Q(url='http://' + base_url(path)) | Q(url='https://' + base_url(path))
)
except Snapshot.DoesNotExist:
# fall back to matching base_url as prefix
snapshot = Snapshot.objects.get(
Q(url__startswith='http://' + base_url(path)) | Q(url__startswith='https://' + base_url(path))
)
return redirect(f'/archive/{snapshot.timestamp}/index.html')
except Snapshot.DoesNotExist:
return HttpResponse(
format_html(
(
'<center><br/><br/><br/>'
'No Snapshots match the given url: <code>{}</code><br/><br/>'
'You can <a href="/add/?url=https://{}" target="_top">add a new Snapshot</a>, or return to the <a href="/" target="_top">Main Index</a>'
'</center>'
),
base_url(path),
path,
),
content_type="text/html",
status=404,
)
except Snapshot.MultipleObjectsReturned:
snapshot_hrefs = mark_safe('<br/>').join(
format_html(
'{} <a href="/archive/{}/index.html"><b><code>{}</code></b></a> {} <b>{}</b>',
snap.added.strftime('%Y-%m-%d %H:%M:%S'),
snap.timestamp,
snap.timestamp,
snap.url,
snap.title or '',
)
for snap in Snapshot.objects.filter(
Q(url__startswith='http://' + base_url(path)) | Q(url__startswith='https://' + base_url(path)) Q(url__startswith='http://' + base_url(path)) | Q(url__startswith='https://' + base_url(path))
).only('url', 'timestamp', 'title', 'added').order_by('-added') )
) return redirect(f'/archive/{snapshot.timestamp}/index.html')
return HttpResponse( except Snapshot.DoesNotExist:
format_html( return HttpResponse(
( format_html(
'Multiple Snapshots match the given URL <code>{}</code><br/><pre>' (
), '<center><br/><br/><br/>'
base_url(path), 'No Snapshots match the given url: <code>{}</code><br/><br/>'
) + snapshot_hrefs + format_html( 'You can <a href="/add/?url=https://{}" target="_top">add a new Snapshot</a>, or return to the <a href="/" target="_top">Main Index</a>'
( '</center>'
'</pre><br/>'
'Choose a Snapshot to proceed or go back to the <a href="/" target="_top">Main Index</a>'
)
), ),
content_type="text/html", base_url(path),
status=404, path,
),
content_type="text/html",
status=404,
)
except Snapshot.MultipleObjectsReturned:
snapshot_hrefs = mark_safe('<br/>').join(
format_html(
'{} <a href="/archive/{}/index.html"><b><code>{}</code></b></a> {} <b>{}</b>',
snap.added.strftime('%Y-%m-%d %H:%M:%S'),
snap.timestamp,
snap.timestamp,
snap.url,
snap.title or '',
) )
for snap in Snapshot.objects.filter(
Q(url__startswith='http://' + base_url(path)) | Q(url__startswith='https://' + base_url(path))
).only('url', 'timestamp', 'title', 'added').order_by('-added')
)
return HttpResponse(
format_html(
(
'Multiple Snapshots match the given URL <code>{}</code><br/><pre>'
),
base_url(path),
) + snapshot_hrefs + format_html(
(
'</pre><br/>'
'Choose a Snapshot to proceed or go back to the <a href="/" target="_top">Main Index</a>'
)
),
content_type="text/html",
status=404,
)
class PublicIndexView(ListView): class PublicIndexView(ListView):
template_name = 'public_index.html' template_name = 'public_index.html'