diff --git a/core/admin.py b/core/admin.py
index 37f9785..b22397c 100644
--- a/core/admin.py
+++ b/core/admin.py
@@ -4,6 +4,7 @@
from django.contrib.auth.models import User, Group
from django.utils.html import format_html
from django.utils.translation import gettext_lazy as _
+from django.urls import path, reverse
from .models import O_Feed, T_Feed
from .custom_admin_site import core_admin_site
@@ -19,6 +20,7 @@
)
from .tasks import update_original_feed, update_translated_feed
from utils.modelAdmin_utils import valid_icon
+from .views import import_opml
class O_FeedAdmin(admin.ModelAdmin):
@@ -40,6 +42,21 @@ class O_FeedAdmin(admin.ModelAdmin):
actions = [o_feed_force_update, o_feed_export_as_opml, o_feed_batch_modify]
list_per_page = 20
+ def get_urls(self):
+ urls = super().get_urls()
+ custom_urls = [
+ path('import_opml/', self.admin_site.admin_view(import_opml), name='core_o_feed_import_opml'),
+ ]
+ return custom_urls + urls
+
+ def changelist_view(self, request, extra_context=None):
+ extra_context = extra_context or {}
+ extra_context['import_opml_button'] = format_html(
+ '导入OPML',
+ reverse('admin:core_o_feed_import_opml')
+ )
+ return super().changelist_view(request, extra_context=extra_context)
+
def save_formset(self, request, form, formset, change):
instances = formset.save(commit=False)
for instance in instances:
diff --git a/core/urls.py b/core/urls.py
index 84caad9..522f373 100644
--- a/core/urls.py
+++ b/core/urls.py
@@ -12,5 +12,6 @@
path("category//", views.category, name="category"),
path("json/", views.rss_json, name="json"),
path("json//", views.rss_json, name="json"),
+ path('core/o_feed/import_opml/', views.import_opml, name='import_opml'),
re_path(r"(?P[^/]+)/?$", views.rss, name="rss"),
]
diff --git a/core/views.py b/core/views.py
index 38ba886..1b30995 100644
--- a/core/views.py
+++ b/core/views.py
@@ -8,9 +8,40 @@
from django.views.decorators.cache import cache_page
from django.views.decorators.http import condition
from .models import T_Feed, O_Feed
+from django.shortcuts import redirect
+from django.contrib import messages
+from django.core.files.uploadedfile import InMemoryUploadedFile
+from opyml import OPML
+from django.utils.translation import gettext_lazy as _
from utils.feed_action import merge_all_atom, check_file_path
+def import_opml(request):
+ if request.method == 'POST':
+ opml_file = request.FILES.get('opml_file')
+ if opml_file and isinstance(opml_file, InMemoryUploadedFile):
+ try:
+ opml_content = opml_file.read().decode('utf-8')
+ opml = OPML.from_xml(opml_content)
+
+ for outline in opml.body.outlines:
+ category = outline.text
+ #category, _ = Category.objects.get_or_create(name=category_name)
+
+ for feed in outline.outlines:
+ O_Feed.objects.create(
+ name=feed.title or feed.text,
+ feed_url=feed.xml_url,
+ category=category
+ )
+
+ messages.success(request, _("OPML file imported successfully."))
+ except Exception as e:
+ messages.error(request, _("Error importing OPML file: {}").format(str(e)))
+ else:
+ messages.error(request, _("Please upload a valid OPML file."))
+
+ return redirect('admin:core_o_feed_changelist')
def get_modified(request, feed_sid):
try:
diff --git a/templates/admin/base_site.html b/templates/admin/base_site.html
index 34d3c7a..7392b4f 100644
--- a/templates/admin/base_site.html
+++ b/templates/admin/base_site.html
@@ -67,7 +67,7 @@