|
| 1 | +from django.contrib import admin, messages |
| 2 | +from .models import Article, Category # Or combine imports |
| 3 | + |
| 4 | + |
| 5 | +@admin.register(Category) |
| 6 | +class CategoryAdmin(admin.ModelAdmin): |
| 7 | + list_display = ('name', 'slug') |
| 8 | + search_fields = ('name',) |
| 9 | + prepopulated_fields = {'slug': ('name',)} # Auto-populate slug from name |
| 10 | + |
| 11 | + |
| 12 | +@admin.action(description="Fetch content, summarize, and translate selected articles") |
| 13 | +def summarize_selected_articles(modeladmin, request, queryset): |
| 14 | + success_count = 0 |
| 15 | + errors = [] |
| 16 | + |
| 17 | + for article in queryset: |
| 18 | + result = article.fetch_and_summarize() |
| 19 | + if result.startswith("Error"): |
| 20 | + errors.append(f"{article.url}: {result}") |
| 21 | + else: |
| 22 | + success_count += 1 |
| 23 | + |
| 24 | + if success_count > 0: |
| 25 | + modeladmin.message_user( |
| 26 | + request, |
| 27 | + f"Successfully processed {success_count} article(s) (fetch, summarize, translate).", |
| 28 | + messages.SUCCESS |
| 29 | + ) |
| 30 | + |
| 31 | + if errors: |
| 32 | + error_message = "Errors encountered:\n" + "\n".join(errors) |
| 33 | + modeladmin.message_user(request, error_message, messages.WARNING) |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +@admin.register(Article) |
| 39 | +class ArticleAdmin(admin.ModelAdmin): |
| 40 | + list_display = ('url', 'title', 'display_categories', 'summary_preview', 'summary_ko_preview', 'reading_time_minutes', 'updated_at', 'created_at') |
| 41 | + list_filter = ('categories', 'created_at', 'updated_at') |
| 42 | + search_fields = ('url', 'title', 'summary', 'summary_ko', 'categories__name') |
| 43 | + readonly_fields = ('created_at', 'updated_at', 'summary', 'summary_ko', 'reading_time_minutes') |
| 44 | + actions = [summarize_selected_articles] |
| 45 | + filter_horizontal = ('categories',) |
| 46 | + |
| 47 | + fieldsets = ( |
| 48 | + ('Article Information', { |
| 49 | + 'fields': ('url', 'title', 'categories') |
| 50 | + }), |
| 51 | + ('Generated Content', { |
| 52 | + 'fields': ('summary', 'summary_ko', 'reading_time_minutes'), |
| 53 | + 'classes': ('collapse',) |
| 54 | + }), |
| 55 | + ('Metadata', { |
| 56 | + 'fields': ('created_at', 'updated_at'), |
| 57 | + 'classes': ('collapse',) |
| 58 | + }), |
| 59 | + ) |
| 60 | + |
| 61 | + @admin.display(description='Categories') |
| 62 | + def display_categories(self, obj): |
| 63 | + """Displays categories as a comma-separated string in the list view.""" |
| 64 | + if obj.categories.exists(): |
| 65 | + return ", ".join([category.name for category in obj.categories.all()]) |
| 66 | + return '-' # Or None, or empty string |
| 67 | + |
| 68 | + def get_readonly_fields(self, request, obj=None): |
| 69 | + # Make 'categories' always read-only as it's set by the LLM |
| 70 | + readonly = list(super().get_readonly_fields(request, obj)) |
| 71 | + if 'categories' not in readonly: |
| 72 | + readonly.append('categories') |
| 73 | + return readonly |
| 74 | + |
| 75 | + @admin.display(description='Summary Preview') |
| 76 | + def summary_preview(self, obj): |
| 77 | + if obj.summary: |
| 78 | + preview = obj.summary[:100] |
| 79 | + return f"{preview}..." if len(obj.summary) > 100 else preview |
| 80 | + return "No summary available" |
| 81 | + |
| 82 | + @admin.display(description='Korean Summary Preview') |
| 83 | + def summary_ko_preview(self, obj): |
| 84 | + if obj.summary_ko: |
| 85 | + if obj.summary_ko.startswith("Translation Error"): return obj.summary_ko |
| 86 | + preview = obj.summary_ko[:50] |
| 87 | + return f"{preview}..." if len(obj.summary_ko) > 50 else preview |
| 88 | + return "No Korean summary" |
0 commit comments