-
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 #193 from 2077-Collective/url-redirect
feat: auto-redirect url when slug changes.
- Loading branch information
Showing
12 changed files
with
262 additions
and
30 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,35 @@ | ||
from django.utils.html import format_html, escape | ||
from django.utils.safestring import mark_safe | ||
|
||
def get_slug_history_table(histories): | ||
"""Return the HTML table for the slug history.""" | ||
html = [] | ||
html.append('<table class="slug-history-table" role="grid">') | ||
html.append('<caption class="sr-only">Slug History Table</caption>') | ||
html.append('<thead><tr>') | ||
html.append('<th scope="col">Old Slug</th>') | ||
html.append('<th scope="col">Changed At</th>') | ||
html.append('</tr>') | ||
html.append('</thead><tbody>') | ||
for history in histories: | ||
html.append('<tr>') | ||
html.append(f'<td>{escape(history.old_slug)}</td>') | ||
html.append(f'<td>{history.created_at}</td>') | ||
html.append('</tr>') | ||
html.append('</tbody>') | ||
html.append('</table>') | ||
return mark_safe(''.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) |
28 changes: 28 additions & 0 deletions
28
server/apps/research/migrations/0012_articleslughistory.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,28 @@ | ||
# Generated by Django 5.0.8 on 2024-11-24 18:13 | ||
|
||
import django.db.models.deletion | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('research', '0011_remove_article_sponsor_padding'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='ArticleSlugHistory', | ||
fields=[ | ||
('id', models.AutoField(primary_key=True, serialize=False)), | ||
('old_slug', models.SlugField(max_length=255)), | ||
('created_at', models.DateTimeField(auto_now_add=True)), | ||
('article', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='slug_history', to='research.article')), | ||
], | ||
options={ | ||
'db_table': 'research_articleslughistory', | ||
'indexes': [models.Index(fields=['old_slug'], name='research_ar_old_slu_12bba6_idx')], | ||
'unique_together': {('article', 'old_slug')}, | ||
}, | ||
), | ||
] |
18 changes: 18 additions & 0 deletions
18
server/apps/research/migrations/0013_alter_article_authors.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-11-29 17:04 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('research', '0012_articleslughistory'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='article', | ||
name='authors', | ||
field=models.ManyToManyField(related_name='articles', to='research.author'), | ||
), | ||
] |
18 changes: 18 additions & 0 deletions
18
server/apps/research/migrations/0014_alter_article_authors.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-02 08:20 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('research', '0013_alter_article_authors'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='article', | ||
name='authors', | ||
field=models.ManyToManyField(blank=True, related_name='articles', to='research.author'), | ||
), | ||
] |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from .category import Category | ||
from .author import Author | ||
from .article import Article | ||
from .article import Article, ArticleSlugHistory |
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
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
Oops, something went wrong.