Browse Source

fix: Refactor html functionality

Cristian 5 years ago
parent
commit
891dd3b8a9

+ 14 - 2
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

+ 1 - 7
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',

+ 3 - 3
archivebox/themes/default/main_index.html

@@ -227,14 +227,14 @@
             <thead>
                 <tr>
                     <th style="width: 100px;">Bookmarked</th>
-                    <th style="width: 26vw;">Saved Link ({{num_links}})</th>
+                    <th style="width: 26vw;">Saved Link ({{num_snapshots}})</th>
                     <th style="width: 50px">Files</th>
                     <th style="width: 16vw;whitespace:nowrap;overflow-x:hidden;">Original URL</th>
                 </tr>
             </thead>
             <tbody>
-                {% 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 %}
             </tbody>
         </table>

+ 28 - 22
archivebox/themes/default/main_index_minimal.html

@@ -1,24 +1,30 @@
 <!DOCTYPE html>
 <html lang="en">
-    <head>
-        <title>Archived Sites</title>
-        <meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1">
-    </head>
-    <body data-status="{{status}}">
-        <table id="table-bookmarks">
-            <thead>
-                <tr class="thead-tr">
-                    <th style="width: 100px;">Bookmarked</th>
-                    <th style="width: 26vw;">Saved Link ({{num_links}})</th>
-                    <th style="width: 50px">Files</th>
-                    <th style="width: 16vw;whitespace:nowrap;overflow-x:hidden;">Original URL</th>
-                </tr>
-            </thead>
-            <tbody>
-                {% for link in links %}
-                    {% include "main_index_row.html" with link=link %} 
-                {% endfor %}
-            </tbody>
-        </table>
-    </body>
-</html>
+  <head>
+    <title>Archived Sites</title>
+    <meta
+      charset="utf-8"
+      name="viewport"
+      content="width=device-width, initial-scale=1"
+    />
+  </head>
+  <body data-status="{{status}}">
+    <table id="table-bookmarks">
+      <thead>
+        <tr class="thead-tr">
+          <th style="width: 100px">Bookmarked</th>
+          <th style="width: 26vw">Saved Link ({{num_links}})</th>
+          <th style="width: 50px">Files</th>
+          <th style="width: 16vw; whitespace: nowrap; overflow-x: hidden">
+            Original URL
+          </th>
+        </tr>
+      </thead>
+      <tbody>
+        {% for snapshot in snapshots %}
+          {% include "main_index_row.html" with snapshot=snapshot %}
+        {% endfor %}
+      </tbody>
+    </table>
+  </body>
+</html>

+ 11 - 11
archivebox/themes/default/main_index_row.html

@@ -1,22 +1,22 @@
 {% load static %}
 
 <tr>
-    <td title="{{link.timestamp}}"> {% if link.bookmarked_date  %} {{ link.bookmarked_date }} {% else %} {{ link.added }} {% endif %} </td>
+    <td title="{{snapshot.timestamp}}"> {% if snapshot.bookmarked_date  %} {{ snapshot.bookmarked_date }} {% else %} {{ snapshot.added }} {% endif %} </td>
     <td class="title-col">
-        {% if link.is_archived %}
-            <a href="archive/{{link.timestamp}}/index.html"><img src="archive/{{link.timestamp}}/favicon.ico" class="link-favicon" decoding="async"></a>
+        {% if snapshot.is_archived %}
+            <a href="archive/{{snapshot.timestamp}}/index.html"><img src="archive/{{snapshot.timestamp}}/favicon.ico" class="link-favicon" decoding="async"></a>
         {% else %}
-            <a href="archive/{{link.timestamp}}/index.html"><img src="{% static 'spinner.gif' %}" class="link-favicon" decoding="async"></a>
+            <a href="archive/{{snapshot.timestamp}}/index.html"><img src="{% static 'spinner.gif' %}" class="link-favicon" decoding="async"></a>
         {% endif %}
-            <a href="archive/{{link.timestamp}}/{{link.canonical_outputs.wget_path}}" title="{{link.title}}">
-                <span data-title-for="{{link.url}}" data-archived="{{link.is_archived}}">{{link.title|default:'Loading...'}}</span>
-                <small style="float:right">{% if link.tags_str != None %} {{link.tags_str|default:''}} {% else %} {{ link.tags|default:'' }} {% endif %}</small>
+            <a href="archive/{{snapshot.timestamp}}/{{snapshot.canonical_outputs.wget_path}}" title="{{snapshot.title}}">
+                <span data-title-for="{{snapshot.url}}" data-archived="{{snapshot.is_archived}}">{{snapshot.title|default:'Loading...'}}</span>
+                <small style="float:right">{% if snapshot.tags_str != None %} {{snapshot.tags_str|default:''}} {% else %} untagged {% endif %}</small>
             </a>
     </td>
     <td>
-        <a href="archive/{{link.timestamp}}/index.html">📄 
-            <span data-number-for="{{link.url}}" title="Fetching any missing files...">{% if link.icons  %} {{link.icons}} {% else %} {{ link.num_outputs}} {% endif %}<img src="{% static 'spinner.gif' %}" class="files-spinner" decoding="async"/></span>
+        <a href="archive/{{snapshot.timestamp}}/index.html">📄 
+            <span data-number-for="{{snapshot.url}}" title="Fetching any missing files...">{% if snapshot.icons  %} {{snapshot.icons}} {% else %} {{ snapshot.num_outputs}} {% endif %}<img src="{% static 'spinner.gif' %}" class="files-spinner" decoding="async"/></span>
         </a>
     </td>
-   <td style="text-align:left"><a href="{{link.url}}">{{link.url}}</a></td>
-</tr>
+   <td style="text-align:left"><a href="{{snapshot.url}}">{{snapshot.url}}</a></td>
+</tr>