Skip to content

Commit

Permalink
refactor to refresh assets list
Browse files Browse the repository at this point in the history
  • Loading branch information
sissbruecker committed Apr 14, 2024
1 parent f513335 commit baea9a0
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 31 deletions.
65 changes: 35 additions & 30 deletions bookmarks/templates/bookmarks/details/assets.html
Original file line number Diff line number Diff line change
@@ -1,37 +1,42 @@
{% if details.assets %}
<div class="assets">
{% for asset in details.assets %}
<div class="asset" data-asset-id="{{ asset.id }}">
<div class="asset-icon {{ asset.icon_classes }}">
{% include 'bookmarks/details/asset_icon.html' %}
</div>
<div class="asset-text truncate {{ asset.text_classes }}">
<div {% if details.has_pending_assets %}
ld-fetch="{% url 'bookmarks:details_assets' details.bookmark.id %}"
ld-interval="5" ld-target="self|outerHTML"
{% endif %}>
{% if details.assets %}
<div class="assets">
{% for asset in details.assets %}
<div class="asset" data-asset-id="{{ asset.id }}">
<div class="asset-icon {{ asset.icon_classes }}">
{% include 'bookmarks/details/asset_icon.html' %}
</div>
<div class="asset-text truncate {{ asset.text_classes }}">
<span>
{{ asset.display_name }}
{% if asset.status == 'pending' %}(queued){% endif %}
{% if asset.status == 'failure' %}(failed){% endif %}
</span>
{% if asset.file_size %}
<span class="filesize">{{ asset.file_size|filesizeformat }}</span>
{% endif %}
</div>
<div class="asset-actions">
{% if asset.file %}
<a class="btn btn-link" href="{% url 'bookmarks:assets.view' asset.id %}" target="_blank">View</a>
{% endif %}
{% if details.is_editable %}
<button ld-confirm-button type="submit" name="remove_asset" value="{{ asset.id }}" class="btn btn-link">
Remove
</button>
{% endif %}
{% if asset.file_size %}
<span class="filesize">{{ asset.file_size|filesizeformat }}</span>
{% endif %}
</div>
<div class="asset-actions">
{% if asset.file %}
<a class="btn btn-link" href="{% url 'bookmarks:assets.view' asset.id %}" target="_blank">View</a>
{% endif %}
{% if details.is_editable %}
<button ld-confirm-button type="submit" name="remove_asset" value="{{ asset.id }}" class="btn btn-link">
Remove
</button>
{% endif %}
</div>
</div>
</div>
{% endfor %}
</div>
{% endif %}
{% endfor %}
</div>
{% endif %}

{% if details.is_editable %}
<div class="assets-actions">
<button type="submit" name="create_snapshot" class="btn btn-link">Create HTML snapshot</button>
</div>
{% endif %}
{% if details.is_editable %}
<div class="assets-actions">
<button type="submit" name="create_snapshot" class="btn btn-link">Create HTML snapshot</button>
</div>
{% endif %}
</div>
1 change: 0 additions & 1 deletion bookmarks/templates/bookmarks/details_modal.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<div ld-modal
ld-fetch="{% url 'bookmarks:details_modal' details.bookmark.id %}" ld-on="refresh-details"
ld-select=".content" ld-target=".modal.bookmark-details .content|outerHTML"
ld-interval="5"
class="modal active bookmark-details">
<div class="modal-overlay" aria-label="Close"></div>
<div class="modal-container">
Expand Down
27 changes: 27 additions & 0 deletions bookmarks/tests/test_bookmark_details_modal.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ def test_access(self):
def test_access_with_sharing(self):
self.details_route_sharing_access_test(self.get_view_name(), True)

def test_assets_access(self):
self.details_route_access_test("bookmarks:details_assets", True)

def test_assets_access_with_sharing(self):
self.details_route_sharing_access_test("bookmarks:details_assets", True)

def test_displays_title(self):
# with title
bookmark = self.setup_bookmark(title="Test title")
Expand Down Expand Up @@ -753,6 +759,27 @@ def test_remove_asset(self):
self.assertEqual(response.status_code, 404)
self.assertTrue(BookmarkAsset.objects.filter(id=asset.id).exists())

@override_settings(LD_ENABLE_SNAPSHOTS=True)
def test_assets_refresh_when_having_pending_asset(self):
bookmark = self.setup_bookmark()
asset = self.setup_asset(bookmark, status=BookmarkAsset.STATUS_COMPLETE)
fetch_url = reverse("bookmarks:details_assets", args=[bookmark.id])

# no pending asset
soup = self.get_details(bookmark)
files_section = self.find_section(soup, "Files")
assets_wrapper = files_section.find("div", {"ld-fetch": fetch_url})
self.assertIsNone(assets_wrapper)

# with pending asset
asset.status = BookmarkAsset.STATUS_PENDING
asset.save()

soup = self.get_details(bookmark)
files_section = self.find_section(soup, "Files")
assets_wrapper = files_section.find("div", {"ld-fetch": fetch_url})
self.assertIsNotNone(assets_wrapper)

@override_settings(LD_ENABLE_SNAPSHOTS=True)
def test_create_snapshot(self):
with patch.object(
Expand Down
5 changes: 5 additions & 0 deletions bookmarks/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
views.bookmarks.details_modal,
name="details_modal",
),
path(
"bookmarks/<int:bookmark_id>/details_assets",
views.bookmarks.details_assets,
name="details_assets",
),
# Assets
path(
"assets/<int:asset_id>",
Expand Down
4 changes: 4 additions & 0 deletions bookmarks/views/bookmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ def details_modal(request, bookmark_id: int):
return _details(request, bookmark_id, "bookmarks/details_modal.html")


def details_assets(request, bookmark_id: int):
return _details(request, bookmark_id, "bookmarks/details/assets.html")


def convert_tag_string(tag_string: str):
# Tag strings coming from inputs are space-separated, however services.bookmarks functions expect comma-separated
# strings
Expand Down
3 changes: 3 additions & 0 deletions bookmarks/views/partials/contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,6 @@ def __init__(self, request: WSGIRequest, bookmark: Bookmark):
self.assets = [
BookmarkAssetItem(asset) for asset in bookmark.bookmarkasset_set.all()
]
self.has_pending_assets = any(
asset.status == BookmarkAsset.STATUS_PENDING for asset in self.assets
)
14 changes: 14 additions & 0 deletions web-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@
"required": false
}
},
{
"name": "ld-select",
"description": "The content element(s) to select from the fetched content, for example `#main-content`",
"value": {
"required": false
}
},
{
"name": "ld-interval",
"description": "Automatically fetches the content of the given URL at the given interval, in seconds",
"value": {
"required": false
}
},
{
"name": "ld-fire",
"description": "Fires one or more events once a behavior, such as ld-fetch or ld-form, is finished",
Expand Down

0 comments on commit baea9a0

Please sign in to comment.