Skip to content

Commit

Permalink
feat: improve OPML export functionality with error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
versun committed Jun 24, 2024
1 parent 9040dfe commit 73c11c3
Showing 1 changed file with 70 additions and 41 deletions.
111 changes: 70 additions & 41 deletions utils/modelAdmin_utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import logging
from datetime import datetime
from django.conf import settings
from django.urls import reverse
from django.http import HttpResponse
from django.utils.html import format_html
from django.apps import apps


from opyml import OPML, Outline
from opyml import OPML, Outline, Head
from huey.contrib.djhuey import HUEY as huey

# from django.conf import settings
Expand All @@ -23,55 +24,83 @@

class CustomModelActions:
def o_feed_export_as_opml(self, request, queryset):
opml_obj = OPML()

for item in queryset:
category = item.category.name if item.category else 'default'
category_outline = Outline(text=category)
item_outline = Outline(
title=item.name,
text=item.name,
type="rss",
xml_url=item.feed_url,
html_url=item.feed_url,
try:
opml_obj = OPML()
opml_obj.head = Head(
title="Original Feeds | RSS Translator",
date_created=datetime.now().strftime("%a, %d %b %Y %H:%M:%S %z"),
owner_name="RSS Translator",
)
category_outline.outlines.append(item_outline)
opml_obj.body.outlines.append(category_outline)

response = HttpResponse(opml_obj.to_xml(), content_type="application/xml")
response["Content-Disposition"] = (
'attachment; filename="rsstranslator_original_feeds.opml"'
)
return response
categories = {}
for item in queryset:
category = item.category.name if item.category else 'default'
#category_outline = Outline(text=category)
if category not in categories:
categories[category] = Outline(text=category)

item_outline = Outline(
title=item.name,
text=item.name,
type="rss",
xml_url=item.feed_url,
html_url=item.feed_url,
)
categories[category].outlines.append(item_outline)

#category_outline.outlines.append(item_outline)
#opml_obj.body.outlines.append(category_outline)
for category_outline in categories.values():
opml_obj.body.outlines.append(category_outline)

response = HttpResponse(opml_obj.to_xml(), content_type="application/xml")
response["Content-Disposition"] = (
'attachment; filename="rsstranslator_original_feeds.opml"'
)
return response
except Exception as e:
return HttpResponse(f"An error occurred: {str(e)}", status=500)

o_feed_export_as_opml.short_description = _("Export selected feeds as OPML")

def t_feed_export_as_opml(self, request, queryset):
opml_obj = OPML()

for item in queryset:
category = item.o_feed.category.name if item.o_feed.category else 'default'
text = item.o_feed.name or "No Name"
xml_url = request.build_absolute_uri(
reverse("core:rss", kwargs={"feed_sid": item.sid})
try:
opml_obj = OPML()
opml_obj.head = Head(
title="Translated Feeds | RSS Translator",
date_created=datetime.now().strftime("%a, %d %b %Y %H:%M:%S %z"),
owner_name="RSS Translator",
)

category_outline = Outline(text=category)
item_outline = Outline(
title=text,
text=text,
type="rss",
xml_url=xml_url,
html_url=item.o_feed.feed_url,
categories = {}
for item in queryset:
category = item.o_feed.category.name if item.o_feed.category else 'default'
text = item.o_feed.name or "No Name"
xml_url = request.build_absolute_uri(
reverse("core:rss", kwargs={"feed_sid": item.sid})
)

if category not in categories:
categories[category] = Outline(text=category)

item_outline = Outline(
title=text,
text=text,
type="rss",
xml_url=xml_url,
html_url=item.o_feed.feed_url,
)
categories[category].outlines.append(item_outline)
for category_outline in categories.values():
opml_obj.body.outlines.append(category_outline)

response = HttpResponse(opml_obj.to_xml(), content_type="application/xml")
response["Content-Disposition"] = (
'attachment; filename="rsstranslator_translated_feeds.opml"'
)
category_outline.outlines.append(item_outline)
opml_obj.body.outlines.append(category_outline)

response = HttpResponse(opml_obj.to_xml(), content_type="application/xml")
response["Content-Disposition"] = (
'attachment; filename="rsstranslator_translated_feeds.opml"'
)
return response
return response
except Exception as e:
return HttpResponse(f"An error occurred: {str(e)}", status=500)

t_feed_export_as_opml.short_description = _("Export selected feeds as OPML")

Expand Down

0 comments on commit 73c11c3

Please sign in to comment.