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 a 'permanent' option to Link content type. #193

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## Next release
* The "Link" page type now has a "permanent" option to change the kind of redirect it will use.

## 4.3.0 - 2019-11-12
* Importing `cms.sitemaps` at the top level of a module containing an app's AppConfig no longer raises `AppRegistryNotReady`.
* `PageBase`'s help text for the `slug` field now makes sense.
Expand Down
20 changes: 20 additions & 0 deletions cms/apps/links/migrations/0003_link_permanent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.24 on 2019-11-11 20:11
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('links', '0002_auto_20171020_1320'),
]

operations = [
migrations.AddField(
model_name='link',
name='permanent',
field=models.BooleanField(default=False, help_text='By default this entry will use a temporary (302) redirect. Check this to make it permanent (301).'),
),
]
8 changes: 8 additions & 0 deletions cms/apps/links/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,13 @@ class Link(ContentBase):
default=False,
)

permanent = models.BooleanField(
default=False,
help_text=(
'By default this entry will use a temporary (302) redirect. '
'Check this to make it permanent (301).'
),
)

def __str__(self):
return self.page.title
21 changes: 21 additions & 0 deletions cms/apps/links/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def test_index_redirect(self):
Link.objects.create(
page=page,
link_url='http://www.example.com/',
permanent=False,
)

factory = RequestFactory()
Expand All @@ -33,3 +34,23 @@ class Object:

self.assertEquals(view.status_code, 302)
self.assertEquals(view['Location'], 'http://www.example.com/')

with update_index():
page = Page.objects.create(
title='Permanent redirect',
slug='permanent-redirect',
content_type=ContentType.objects.get_for_model(Link),
parent=page,
)

link = Link.objects.create(
page=page,
link_url='http://www.example.com/',
permanent=True,
)

request = factory.get('/permanent-redirect/')

setattr(request, 'pages', Object)
request.pages.current = page
view = index(request)
6 changes: 5 additions & 1 deletion cms/apps/links/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@

def index(request):
'''Redirects to a new page.'''
return redirect(request.pages.current.content.get_link_url_resolved())
content = request.pages.current.content
return redirect(
content.get_link_url_resolved(),
permanent=content.permanent
)