-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #196 from 2077-Collective/fix/category-url
Fix/category url
- Loading branch information
Showing
6 changed files
with
72 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# 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), | ||
), | ||
] |
18 changes: 18 additions & 0 deletions
18
server/apps/research/migrations/0016_alter_category_slug.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# 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), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,39 @@ | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters