From 938925daa4f8731c6b926c8f689f5d2bb3b51fa2 Mon Sep 17 00:00:00 2001 From: ndu Date: Wed, 11 Dec 2024 05:13:29 +0100 Subject: [PATCH] Revert "Fix/category url" --- server/apps/research/admin/category_admin.py | 3 +-- .../research/migrations/0015_category_slug.py | 18 ------------- .../migrations/0016_alter_category_slug.py | 18 ------------- server/apps/research/models/category.py | 27 ------------------- .../serializers/category_serializer.py | 3 +-- server/apps/research/views.py | 8 +++--- 6 files changed, 5 insertions(+), 72 deletions(-) delete mode 100644 server/apps/research/migrations/0015_category_slug.py delete mode 100644 server/apps/research/migrations/0016_alter_category_slug.py diff --git a/server/apps/research/admin/category_admin.py b/server/apps/research/admin/category_admin.py index 0edf8ef..8dcc690 100644 --- a/server/apps/research/admin/category_admin.py +++ b/server/apps/research/admin/category_admin.py @@ -5,9 +5,8 @@ class CategoryAdmin(admin.ModelAdmin): """Admin interface for the Category model.""" - list_display = ('name', 'slug', 'created_at') + list_display = ('name', 'created_at') list_per_page = 25 search_fields = ('name',) list_filter = ('created_at',) ordering = ('name',) - readonly_fields = ('slug',) diff --git a/server/apps/research/migrations/0015_category_slug.py b/server/apps/research/migrations/0015_category_slug.py deleted file mode 100644 index 4ee94d1..0000000 --- a/server/apps/research/migrations/0015_category_slug.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 5.0.8 on 2024-12-09 11:07 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('research', '0014_alter_article_authors'), - ] - - operations = [ - migrations.AddField( - model_name='category', - name='slug', - field=models.SlugField(blank=True, max_length=255), - ), - ] diff --git a/server/apps/research/migrations/0016_alter_category_slug.py b/server/apps/research/migrations/0016_alter_category_slug.py deleted file mode 100644 index 6df3e9e..0000000 --- a/server/apps/research/migrations/0016_alter_category_slug.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 5.0.8 on 2024-12-09 11:19 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('research', '0015_category_slug'), - ] - - operations = [ - migrations.AlterField( - model_name='category', - name='slug', - field=models.SlugField(blank=True, max_length=255, unique=True), - ), - ] diff --git a/server/apps/research/models/category.py b/server/apps/research/models/category.py index 080d673..e86f32c 100644 --- a/server/apps/research/models/category.py +++ b/server/apps/research/models/category.py @@ -1,39 +1,12 @@ from django.db import models from apps.common.models import BaseModel -from django.utils.text import slugify -from django.db import transaction class Category(BaseModel): """Model for categories.""" name = models.CharField(max_length=255) - slug = models.SlugField(max_length=255, unique=True, blank=True) class Meta: verbose_name_plural = 'Categories' - - def save(self, *args, **kwargs): - if not self.slug: - self.slug = self.generate_slug() - super().save(*args, **kwargs) def __str__(self): return self.name - - def generate_slug(self): - - if not self.name: - raise ValueError("Name is required to generate slug") - - base_slug = slugify(self.name) - slug = base_slug - num = 1 - with transaction.atomic(): - while ( - Category.objects.select_for_update() - .filter(slug=slug) - .exclude(id=self.id) # Exclude current instance when updating - .exists() - ): - slug = f"{base_slug}-{num}" - num += 1 - return slug \ No newline at end of file diff --git a/server/apps/research/serializers/category_serializer.py b/server/apps/research/serializers/category_serializer.py index 79cf2b3..45f2ec0 100644 --- a/server/apps/research/serializers/category_serializer.py +++ b/server/apps/research/serializers/category_serializer.py @@ -3,7 +3,6 @@ class CategorySerializer(serializers.ModelSerializer): """Serializer for the Category model.""" - slug = serializers.SlugField(read_only=True, max_length=255, help_text='URL-friendly version of the category name.') class Meta: model = Category - fields = ['id', 'name', 'slug'] \ No newline at end of file + fields = ['id', 'name'] \ No newline at end of file diff --git a/server/apps/research/views.py b/server/apps/research/views.py index dd98ce9..eb355b9 100644 --- a/server/apps/research/views.py +++ b/server/apps/research/views.py @@ -102,13 +102,11 @@ def retrieve_by_identifier(self, request, identifier=None): status=status.HTTP_404_NOT_FOUND) # Custom action to retrieve articles by category - @action(detail=False, methods=['get'], url_path=r'category/(?P[-\w]+)') - def retrieve_by_category(self, request, category_slug=None): + @action(detail=False, methods=['get'], url_path=r'category/(?P[-\w]+)') + def retrieve_by_category(self, request, category=None): """Retrieve article list by category.""" try: - instances = Article.objects.filter(categories__slug=category_slug) - if not instances.exists(): - return Response({'error': 'No articles found for this category'}, status=status.HTTP_404_NOT_FOUND) + instances = Article.objects.filter(categories__name=category) serializer = self.get_serializer(instances, many=True) return Response({'success': True, 'data': serializer.data}) except Exception as e: