Skip to content

Commit

Permalink
Merge pull request #35 from hazadus/34-export-fav-entries-as-md
Browse files Browse the repository at this point in the history
Export favorite entries as Markdown file
  • Loading branch information
hazadus authored Nov 3, 2023
2 parents 4b055fb + f3868de commit 49962ab
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
5 changes: 4 additions & 1 deletion feeds/selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@

def get_entry_queryset(user, mode: str) -> QuerySet:
"""
Return Entry queryset for "mode", where mode is one of "Smart Feed" names,
Return Entry queryset for `user` and `mode`, where mode is one of "Smart Feed" names,
i.e. all, today, unread, read, favorites.
:param user: `CustomUser` instance
:param str mode: all | today | unread | read | favorites
"""
mode_querysets = {
"all": Entry.objects.all(),
Expand Down
6 changes: 6 additions & 0 deletions feeds/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
FeedsSettingsView,
FeedUpdateView,
FoldersSettingsView,
entry_export_favorites,
entry_toggle_is_favorite_view,
feed_mark_as_read_view,
)
Expand Down Expand Up @@ -49,6 +50,11 @@
EntryDetailView.as_view(),
name="entry_detail",
),
path(
"entries/export/favorites/",
entry_export_favorites,
name="entry_export_favorites",
),
path(
"entry/toggle/is_favorite/<int:entry_pk>/",
entry_toggle_is_favorite_view,
Expand Down
26 changes: 26 additions & 0 deletions feeds/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,32 @@ def get_context_data(self, **kwargs):
return context


@login_required
def entry_export_favorites(request: HttpRequest) -> HttpResponse:
"""
Export user's favorite entries as Markdown file.
"""
response = HttpResponse(content_type="text/plain")
response["Content-Disposition"] = "attachment; filename=favorites.md"

lines = ["# Your favorite entries from RSS feeds\n\n"]

for entry in get_entry_queryset(user=request.user, mode="favorites"):
lines.append(
"- [{title}]({url}) in [{feed_title}]({site_url}) on {pub_date}\n".format(
title=entry.title,
url=entry.url,
feed_title=entry.feed.title,
site_url=entry.feed.site_url,
pub_date=entry.pub_date.strftime("%d.%m.%Y"),
)
)

lines.append("\nCreated by [rss.hazadus.ru](https://rss.hazadus.ru/)")
response.writelines(lines)
return response


@login_required
@require_POST
def entry_toggle_is_favorite_view(request: HttpRequest, entry_pk: int) -> HttpResponse:
Expand Down
9 changes: 9 additions & 0 deletions templates/feeds/includes/entry_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@
{% include "icons/icon_gear_edit.html" with size="24" color="#6b7280" %}
</div>
</a>
{% elif mode %}
<!-- "Export favorites as Markdown file" button -->
{% if mode == "favorites" %}
<a href="{% url 'feeds:entry_export_favorites' %}">
<div>
{% include "icons/icon_markdown_file.html" with size="24" color="#6b7280" %}
</div>
</a>
{% endif %}
{% endif %}
</div>
</div>
Expand Down
3 changes: 3 additions & 0 deletions templates/icons/icon_markdown_file.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" width="{{ size }}" height="{{ size }}" viewBox="0 0 1024 1024">
<path fill="{{ color }}" d="M854.6 288.6L639.4 73.4c-6-6-14.1-9.4-22.6-9.4H192c-17.7 0-32 14.3-32 32v832c0 17.7 14.3 32 32 32h640c17.7 0 32-14.3 32-32V311.3c0-8.5-3.4-16.7-9.4-22.7zM790.2 326H602V137.8L790.2 326zm1.8 562H232V136h302v216a42 42 0 0 0 42 42h216v494zM429 481.2c-1.9-4.4-6.2-7.2-11-7.2h-35c-6.6 0-12 5.4-12 12v272c0 6.6 5.4 12 12 12h27.1c6.6 0 12-5.4 12-12V582.1l66.8 150.2a12 12 0 0 0 11 7.1H524c4.7 0 9-2.8 11-7.1l66.8-150.6V758c0 6.6 5.4 12 12 12H641c6.6 0 12-5.4 12-12V486c0-6.6-5.4-12-12-12h-34.7c-4.8 0-9.1 2.8-11 7.2l-83.1 191l-83.2-191z"/>
</svg>

0 comments on commit 49962ab

Please sign in to comment.