Skip to content

Commit

Permalink
refactor(url_redirect): updated url_redirect implementation codebase …
Browse files Browse the repository at this point in the history
…for readability and maintainability
  • Loading branch information
happychuks committed Nov 27, 2024
1 parent b458506 commit 5401547
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 28 deletions.
27 changes: 3 additions & 24 deletions server/apps/research/admin/article_admin.py
Original file line number Diff line number Diff line change
@@ -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



Expand All @@ -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 = ['<div class="slug-history">']
html.append('<table style="width: 100%; border-collapse: collapse;">')
html.append('<tr style="background-color: #f5f5f5;">')
html.append('<th style="padding: 8px; border: 1px solid #ddd;">Old Slug</th>')
html.append('<th style="padding: 8px; border: 1px solid #ddd;">Changed At</th>')
html.append('</tr>')

for history in histories:
html.append('<tr>')
html.append(f'<td style="padding: 8px; border: 1px solid #ddd;">{history.old_slug}</td>')
html.append(f'<td style="padding: 8px; border: 1px solid #ddd;">{history.created_at}</td>')
html.append('</tr>')

html.append('</table>')
html.append('</div>')

return format_html(''.join(html))
return current_slug_history(obj)
current_slug_history.short_description = 'Slug Change History'

fieldsets = [
Expand Down
31 changes: 31 additions & 0 deletions server/apps/research/admin/slug_history.py
Original file line number Diff line number Diff line change
@@ -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('<table style="width: 100%; border-collapse: collapse;">')
html.append('<tr style="background-color: #f5f5f5;">')
html.append('<th style="padding: 8px; border: 1px solid #ddd;">Old Slug</th>')
html.append('<th style="padding: 8px; border: 1px solid #ddd;">Changed At</th>')
html.append('</tr>')
for history in histories:
html.append('<tr>')
html.append(f'<td style="padding: 8px; border: 1px solid #ddd;">{history.old_slug}</td>')
html.append(f'<td style="padding: 8px; border: 1px solid #ddd;">{history.created_at}</td>')
html.append('</tr>')
html.append('</table>')
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 = ['<div class="slug-history">']
html.append(get_slug_history_table(histories))
html.append('</div>')
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)
8 changes: 4 additions & 4 deletions server/apps/research/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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<identifier>[-\w0-9a-fA-F]+)')
Expand Down Expand Up @@ -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
Expand All @@ -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."""
Expand Down

0 comments on commit 5401547

Please sign in to comment.