Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to rename a tag #463

Merged
merged 4 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion blog/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.db.models import F
from django import forms
from xml.etree import ElementTree
from .models import Entry, Tag, Quotation, Blogmark, Comment, Series
from .models import Entry, Tag, Quotation, Blogmark, Comment, Series, PreviousTagName


class BaseAdmin(admin.ModelAdmin):
Expand Down Expand Up @@ -78,6 +78,13 @@ def get_search_results(self, request, queryset, search_term):
else:
return queryset.all(), False

def save_model(self, request, obj, form, change):
if change:
old_obj = Tag.objects.get(pk=obj.pk)
if old_obj.tag != obj.tag:
PreviousTagName.objects.create(tag=obj, previous_name=old_obj.tag)
super().save_model(request, obj, form, change)


admin.site.register(
Comment,
Expand All @@ -103,3 +110,8 @@ def get_search_results(self, request, queryset, search_term):
"slug",
),
)


admin.site.register(
PreviousTagName, raw_id_fields=("tag",), list_display=("previous_name", "tag")
)
27 changes: 27 additions & 0 deletions blog/migrations/0021_previous_tag_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("blog", "0020_tag_description"),
]

operations = [
migrations.CreateModel(
name="PreviousTagName",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("previous_name", models.SlugField()),
("tag", models.ForeignKey(on_delete=models.CASCADE, to="blog.Tag")),
],
),
]
13 changes: 13 additions & 0 deletions blog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ def get_related_tags(self, limit=10):
self._related_tags = [tags_by_name[name] for name in tag_names]
return self._related_tags

def rename_tag(self, new_name):
PreviousTagName.objects.create(tag=self, previous_name=self.tag)
self.tag = new_name
self.save()


class PreviousTagName(models.Model):
tag = models.ForeignKey(Tag, on_delete=models.CASCADE)
previous_name = models.SlugField()

def __str__(self):
return self.previous_name


class Series(models.Model):
created = models.DateTimeField(default=datetime.datetime.utcnow)
Expand Down
21 changes: 21 additions & 0 deletions blog/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
BlogmarkFactory,
QuotationFactory,
)
from blog.models import Tag, PreviousTagName


class BlogTests(TransactionTestCase):
Expand Down Expand Up @@ -106,3 +107,23 @@ def test_do_typography_string(self):
),
):
self.assertEqual(do_typography_string(input), expected)

def test_rename_tag_creates_previous_tag_name(self):
tag = Tag.objects.create(tag="old-name")
tag.entry_set.create(
title="Test entry",
body="Test entry body",
created="2020-01-01",
)
assert self.client.get("/tags/old-name/").status_code == 200
assert self.client.get("/tags/new-name/").status_code == 404
tag.rename_tag("new-name")
self.assertEqual(tag.tag, "new-name")
previous_tag_name = PreviousTagName.objects.get(tag=tag)
self.assertEqual(previous_tag_name.previous_name, "old-name")
assert self.client.get("/tags/old-name/").status_code == 301
assert self.client.get("/tags/new-name/").status_code == 200

def test_tag_with_hyphen(self):
tag = Tag.objects.create(tag="tag-with-hyphen")
self.assertEqual(tag.tag, "tag-with-hyphen")
23 changes: 17 additions & 6 deletions blog/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# coding=utf8
from django.http.response import HttpResponseRedirect
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, JsonResponse
from django.contrib.admin.views.decorators import staff_member_required
Expand All @@ -23,6 +22,7 @@
Photoset,
Series,
Tag,
PreviousTagName,
load_mixed_objects,
)
import requests
Expand Down Expand Up @@ -354,9 +354,20 @@ def tag_index(request):
def archive_tag(request, tags, atom=False):
from .feeds import EverythingTagged

tags = Tag.objects.filter(tag__in=tags.split("+")).values_list("tag", flat=True)[:3]
if not tags:
tags_ = Tag.objects.filter(tag__in=tags.split("+")).values_list("tag", flat=True)[
:3
]
if not tags_:
# Try for a previous tag name
if "+" not in tags:
try:
previous = PreviousTagName.objects.get(previous_name=tags)
except PreviousTagName.DoesNotExist:
raise Http404
return Redirect("/tag/%s/" % previous.tag.tag)

raise Http404
tags = tags_
items = []
from django.db import connection

Expand Down Expand Up @@ -770,15 +781,15 @@ def user_from_cookies(request):


def redirect_entry(request, pk):
return HttpResponseRedirect(get_object_or_404(Entry, pk=pk).get_absolute_url())
return Redirect(get_object_or_404(Entry, pk=pk).get_absolute_url())


def redirect_blogmark(request, pk):
return HttpResponseRedirect(get_object_or_404(Blogmark, pk=pk).get_absolute_url())
return Redirect(get_object_or_404(Blogmark, pk=pk).get_absolute_url())


def redirect_quotation(request, pk):
return HttpResponseRedirect(get_object_or_404(Quotation, pk=pk).get_absolute_url())
return Redirect(get_object_or_404(Quotation, pk=pk).get_absolute_url())


def about(request):
Expand Down