diff --git a/server/apps/research/admin/article_admin.py b/server/apps/research/admin/article_admin.py index 209c8a4..352ae59 100644 --- a/server/apps/research/admin/article_admin.py +++ b/server/apps/research/admin/article_admin.py @@ -1,8 +1,8 @@ from django.contrib import admin from django import forms -from apps.research.models import Article, Author, ArticleSlugHistory +from apps.research.models import Article, ArticleSlugHistory from tinymce.widgets import TinyMCE -from django.utils.html import format_html +from .slug_history import current_slug_history @@ -20,28 +20,7 @@ class ArticleAdmin(admin.ModelAdmin): """Admin interface for the Article model.""" form = ArticleForm def current_slug_history(self, obj): - """Display the history of URL changes for the article.""" - histories = obj.slug_history.all().order_by('-created_at') - if not histories: - return "No slug changes recorded" - - html = ['
'] - html.append('') - html.append('') - html.append('') - html.append('') - html.append('') - - for history in histories: - html.append('') - html.append(f'') - html.append(f'') - html.append('') - - html.append('
Old SlugChanged At
{history.old_slug}{history.created_at}
') - html.append('
') - - return format_html(''.join(html)) + return current_slug_history(obj) current_slug_history.short_description = 'Slug Change History' fieldsets = [ diff --git a/server/apps/research/admin/slug_history.py b/server/apps/research/admin/slug_history.py new file mode 100644 index 0000000..fc985d4 --- /dev/null +++ b/server/apps/research/admin/slug_history.py @@ -0,0 +1,31 @@ +from django.utils.html import format_html + +def get_slug_history_table(histories): + """Return the HTML table for the slug history.""" + html = [] + html.append('') + html.append('') + html.append('') + html.append('') + html.append('') + for history in histories: + html.append('') + html.append(f'') + html.append(f'') + html.append('') + html.append('
Old SlugChanged At
{history.old_slug}{history.created_at}
') + return ''.join(html) + +def get_slug_history_html(obj): + """Return the HTML for the slug history.""" + histories = obj.slug_history.all().order_by('-created_at') + if not histories: + return "No slug changes recorded" + html = ['
'] + html.append(get_slug_history_table(histories)) + html.append('
') + return format_html(''.join(html)) + +def current_slug_history(obj): + """Display the history of URL changes for the article.""" + return get_slug_history_html(obj) \ No newline at end of file diff --git a/server/apps/research/views.py b/server/apps/research/views.py index d2c2cfd..56109c5 100644 --- a/server/apps/research/views.py +++ b/server/apps/research/views.py @@ -42,7 +42,7 @@ def create(self, request, *args, **kwargs): return Response(serializer.data, status=status.HTTP_201_CREATED) except Exception as e: logger.error(f"Unexpected error during article creation: {e}") - return Response({'error': 'An unexpected error occurred'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + return Response({'error': 'Failed to create a new Article'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) def update(self, request, *args, **kwargs): """Handle article update.""" @@ -54,7 +54,7 @@ def update(self, request, *args, **kwargs): return Response(serializer.data, status=status.HTTP_200_OK) except Exception as e: logger.error(f"Unexpected error during article update: {e}") - return Response({'error': 'An unexpected error occurred'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + return Response({'error': 'Error updating article'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) # Custom action to retrieve articles by slug or UUID @action(detail=False, methods=['get'], url_path=r'(?P[-\w0-9a-fA-F]+)') @@ -89,7 +89,7 @@ def retrieve_by_identifier(self, request, identifier=None): except Exception as e: logger.error(f"Error retrieving article by identifier: {e}") - return Response({'error': 'An unexpected error occurred'}, + return Response({'error': 'Article does not exist'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) # Custom action to retrieve articles by category @@ -102,7 +102,7 @@ def retrieve_by_category(self, request, category=None): return Response({'success': True, 'data': serializer.data}) except Exception as e: logger.error(f"Error retrieving articles by category: {e}") - return Response({'error': 'An unexpected error occurred'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + return Response({'error': 'Category does not exist'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) def is_valid_uuid(self, value): """Check if the value is a valid UUID."""