From 891dd3b8a97e3ec47d2e4e3cc5413488170df3e0 Mon Sep 17 00:00:00 2001 From: Cristian Date: Mon, 18 Jan 2021 09:38:19 -0500 Subject: [PATCH] fix: Refactor html functionality --- archivebox/core/models.py | 16 +++++- archivebox/index/html.py | 8 +-- archivebox/themes/default/main_index.html | 6 +-- .../themes/default/main_index_minimal.html | 50 +++++++++++-------- archivebox/themes/default/main_index_row.html | 22 ++++---- 5 files changed, 57 insertions(+), 45 deletions(-) diff --git a/archivebox/core/models.py b/archivebox/core/models.py index a225f4d7..2b555e08 100644 --- a/archivebox/core/models.py +++ b/archivebox/core/models.py @@ -3,7 +3,7 @@ __package__ = 'archivebox.core' import uuid from pathlib import Path from typing import Dict, Optional, List -from datetime import datetime +from datetime import datetime, timedelta from collections import defaultdict from django.db import models, transaction @@ -148,7 +148,6 @@ class Snapshot(models.Model): output["history"] = self.get_history() return output - def as_csv(self, cols: Optional[List[str]]=None, separator: str=',', ljust: int=0) -> str: from ..index.csv import to_csv return to_csv(self, cols=cols or self.field_names(), separator=separator, ljust=ljust) @@ -167,6 +166,19 @@ class Snapshot(models.Model): def bookmarked(self): return parse_date(self.timestamp) + @cached_property + def bookmarked_date(self) -> Optional[str]: + from ..util import ts_to_date + + max_ts = (datetime.now() + timedelta(days=30)).timestamp() + + if self.timestamp and self.timestamp.replace('.', '').isdigit(): + if 0 < float(self.timestamp) < max_ts: + return ts_to_date(datetime.fromtimestamp(float(self.timestamp))) + else: + return str(self.timestamp) + return None + @cached_property def is_archived(self) -> bool: from ..config import ARCHIVE_DIR diff --git a/archivebox/index/html.py b/archivebox/index/html.py index 5917af3c..a2a7cb35 100644 --- a/archivebox/index/html.py +++ b/archivebox/index/html.py @@ -84,12 +84,6 @@ def snapshot_details_template(snapshot: Model) -> str: from ..extractors.wget import wget_output_path - tags = snapshot.tags.all() - if len(tags) > 0: - tags = ",".join(list(tags.values_list("name", flat=True))) - else: - tags = "untagged" - return render_django_template(LINK_DETAILS_TEMPLATE, { **snapshot.as_json(), **snapshot.canonical_outputs(), @@ -103,7 +97,7 @@ def snapshot_details_template(snapshot: Model) -> str: or (snapshot.domain if snapshot.is_archived else '') ) or 'about:blank', 'extension': snapshot.extension or 'html', - 'tags': tags, + 'tags': snapshot.tags_str() or "untagged", 'size': printable_filesize(snapshot.archive_size) if snapshot.archive_size else 'pending', 'status': 'archived' if snapshot.is_archived else 'not yet archived', 'status_color': 'success' if snapshot.is_archived else 'danger', diff --git a/archivebox/themes/default/main_index.html b/archivebox/themes/default/main_index.html index 95af1963..42150342 100644 --- a/archivebox/themes/default/main_index.html +++ b/archivebox/themes/default/main_index.html @@ -227,14 +227,14 @@ Bookmarked - Saved Link ({{num_links}}) + Saved Link ({{num_snapshots}}) Files Original URL - {% for link in links %} - {% include 'main_index_row.html' with link=link %} + {% for snapshot in snapshots %} + {% include 'main_index_row.html' with snapshot=snapshot %} {% endfor %} diff --git a/archivebox/themes/default/main_index_minimal.html b/archivebox/themes/default/main_index_minimal.html index dcfaa23f..e7bd6840 100644 --- a/archivebox/themes/default/main_index_minimal.html +++ b/archivebox/themes/default/main_index_minimal.html @@ -1,24 +1,30 @@ - - Archived Sites - - - - - - - - - - - - - - {% for link in links %} - {% include "main_index_row.html" with link=link %} - {% endfor %} - -
BookmarkedSaved Link ({{num_links}})FilesOriginal URL
- - \ No newline at end of file + + Archived Sites + + + + + + + + + + + + + + {% for snapshot in snapshots %} + {% include "main_index_row.html" with snapshot=snapshot %} + {% endfor %} + +
BookmarkedSaved Link ({{num_links}})Files + Original URL +
+ + diff --git a/archivebox/themes/default/main_index_row.html b/archivebox/themes/default/main_index_row.html index 5e21a8c1..66c297a7 100644 --- a/archivebox/themes/default/main_index_row.html +++ b/archivebox/themes/default/main_index_row.html @@ -1,22 +1,22 @@ {% load static %} - {% if link.bookmarked_date %} {{ link.bookmarked_date }} {% else %} {{ link.added }} {% endif %} + {% if snapshot.bookmarked_date %} {{ snapshot.bookmarked_date }} {% else %} {{ snapshot.added }} {% endif %} - {% if link.is_archived %} - + {% if snapshot.is_archived %} + {% else %} - + {% endif %} - - {{link.title|default:'Loading...'}} - {% if link.tags_str != None %} {{link.tags_str|default:''}} {% else %} {{ link.tags|default:'' }} {% endif %} + + {{snapshot.title|default:'Loading...'}} + {% if snapshot.tags_str != None %} {{snapshot.tags_str|default:''}} {% else %} untagged {% endif %} - 📄 - {% if link.icons %} {{link.icons}} {% else %} {{ link.num_outputs}} {% endif %} + 📄 + {% if snapshot.icons %} {{snapshot.icons}} {% else %} {{ snapshot.num_outputs}} {% endif %} - {{link.url}} - \ No newline at end of file + {{snapshot.url}} +