Skip to content
Browse files

Added the ability to pick a response code of 302 or 301, and added te…

…sts and migrations
  • Loading branch information...
1 parent 50c430a commit b9b3cac16ccf4a7f018de82b14457bd5331baf4d @MegaMark16 MegaMark16 committed Apr 13, 2011
View
4 cms_redirects/admin.py
@@ -2,7 +2,7 @@
from cms_redirects.models import CMSRedirect
class CMSRedirectAdmin(admin.ModelAdmin):
- list_display = ('old_path', 'new_path', 'page', 'page_site',)
+ list_display = ('old_path', 'new_path', 'page', 'page_site', 'actual_response_code',)
list_filter = ('site',)
search_fields = ('old_path', 'new_path', 'page')
radio_fields = {'site': admin.VERTICAL}
@@ -11,7 +11,7 @@ class CMSRedirectAdmin(admin.ModelAdmin):
"fields": ('site','old_path',)
}),
('Destination', {
- "fields": ('new_path','page',)
+ "fields": ('new_path','page', 'response_code',)
}),
]
View
11 cms_redirects/middleware.py
@@ -19,8 +19,15 @@ def process_exception(self, request, exception):
pass
if r is not None:
if r.page:
- return http.HttpResponsePermanentRedirect(r.page.get_absolute_url())
+ if r.response_code == '302':
+ return http.HttpResponseRedirect(r.page.get_absolute_url())
+ else:
+ return http.HttpResponsePermanentRedirect(r.page.get_absolute_url())
if r.new_path == '':
return http.HttpResponseGone()
- return http.HttpResponsePermanentRedirect(r.new_path)
+ if r.response_code == '302':
+ return http.HttpResponseRedirect(r.new_path)
+ else:
+ return http.HttpResponsePermanentRedirect(r.new_path)
+
View
85 cms_redirects/migrations/0001_initial.py
@@ -0,0 +1,85 @@
+# encoding: utf-8
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+class Migration(SchemaMigration):
+
+ def forwards(self, orm):
+
+ # Adding model 'CMSRedirect'
+ db.create_table('cms_redirects_cmsredirect', (
+ ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
+ ('page', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['cms.Page'], null=True, blank=True)),
+ ('site', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['sites.Site'])),
+ ('old_path', self.gf('django.db.models.fields.CharField')(max_length=200, db_index=True)),
+ ('new_path', self.gf('django.db.models.fields.CharField')(max_length=200, blank=True)),
+ ))
+ db.send_create_signal('cms_redirects', ['CMSRedirect'])
+
+ # Adding unique constraint on 'CMSRedirect', fields ['site', 'old_path']
+ db.create_unique('cms_redirects_cmsredirect', ['site_id', 'old_path'])
+
+
+ def backwards(self, orm):
+
+ # Removing unique constraint on 'CMSRedirect', fields ['site', 'old_path']
+ db.delete_unique('cms_redirects_cmsredirect', ['site_id', 'old_path'])
+
+ # Deleting model 'CMSRedirect'
+ db.delete_table('cms_redirects_cmsredirect')
+
+
+ models = {
+ 'cms.page': {
+ 'Meta': {'ordering': "('site', 'tree_id', 'lft')", 'object_name': 'Page'},
+ 'changed_by': ('django.db.models.fields.CharField', [], {'max_length': '70'}),
+ 'created_by': ('django.db.models.fields.CharField', [], {'max_length': '70'}),
+ 'creation_date': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'in_navigation': ('django.db.models.fields.BooleanField', [], {'default': 'True', 'db_index': 'True'}),
+ 'level': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
+ 'lft': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
+ 'limit_visibility_in_menu': ('django.db.models.fields.SmallIntegerField', [], {'default': 'None', 'null': 'True', 'db_index': 'True', 'blank': 'True'}),
+ 'login_required': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'moderator_state': ('django.db.models.fields.SmallIntegerField', [], {'default': '1', 'blank': 'True'}),
+ 'navigation_extenders': ('django.db.models.fields.CharField', [], {'db_index': 'True', 'max_length': '80', 'null': 'True', 'blank': 'True'}),
+ 'parent': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'children'", 'null': 'True', 'to': "orm['cms.Page']"}),
+ 'placeholders': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['cms.Placeholder']", 'symmetrical': 'False'}),
+ 'publication_date': ('django.db.models.fields.DateTimeField', [], {'db_index': 'True', 'null': 'True', 'blank': 'True'}),
+ 'publication_end_date': ('django.db.models.fields.DateTimeField', [], {'db_index': 'True', 'null': 'True', 'blank': 'True'}),
+ 'published': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'publisher_is_draft': ('django.db.models.fields.BooleanField', [], {'default': 'True', 'db_index': 'True'}),
+ 'publisher_public': ('django.db.models.fields.related.OneToOneField', [], {'related_name': "'publisher_draft'", 'unique': 'True', 'null': 'True', 'to': "orm['cms.Page']"}),
+ 'publisher_state': ('django.db.models.fields.SmallIntegerField', [], {'default': '0', 'db_index': 'True'}),
+ 'reverse_id': ('django.db.models.fields.CharField', [], {'db_index': 'True', 'max_length': '40', 'null': 'True', 'blank': 'True'}),
+ 'rght': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
+ 'site': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['sites.Site']"}),
+ 'soft_root': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'db_index': 'True'}),
+ 'template': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'tree_id': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'})
+ },
+ 'cms.placeholder': {
+ 'Meta': {'object_name': 'Placeholder'},
+ 'default_width': ('django.db.models.fields.PositiveSmallIntegerField', [], {'null': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'slot': ('django.db.models.fields.CharField', [], {'max_length': '50', 'db_index': 'True'})
+ },
+ 'cms_redirects.cmsredirect': {
+ 'Meta': {'ordering': "('old_path',)", 'unique_together': "(('site', 'old_path'),)", 'object_name': 'CMSRedirect'},
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'new_path': ('django.db.models.fields.CharField', [], {'max_length': '200', 'blank': 'True'}),
+ 'old_path': ('django.db.models.fields.CharField', [], {'max_length': '200', 'db_index': 'True'}),
+ 'page': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['cms.Page']", 'null': 'True', 'blank': 'True'}),
+ 'site': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['sites.Site']"})
+ },
+ 'sites.site': {
+ 'Meta': {'ordering': "('domain',)", 'object_name': 'Site', 'db_table': "'django_site'"},
+ 'domain': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
+ }
+ }
+
+ complete_apps = ['cms_redirects']
View
73 cms_redirects/migrations/0002_auto__add_field_cmsredirect_response_code.py
@@ -0,0 +1,73 @@
+# encoding: utf-8
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+class Migration(SchemaMigration):
+
+ def forwards(self, orm):
+
+ # Adding field 'CMSRedirect.response_code'
+ db.add_column('cms_redirects_cmsredirect', 'response_code', self.gf('django.db.models.fields.CharField')(default='', max_length=3, blank=True), keep_default=False)
+
+
+ def backwards(self, orm):
+
+ # Deleting field 'CMSRedirect.response_code'
+ db.delete_column('cms_redirects_cmsredirect', 'response_code')
+
+
+ models = {
+ 'cms.page': {
+ 'Meta': {'ordering': "('site', 'tree_id', 'lft')", 'object_name': 'Page'},
+ 'changed_by': ('django.db.models.fields.CharField', [], {'max_length': '70'}),
+ 'created_by': ('django.db.models.fields.CharField', [], {'max_length': '70'}),
+ 'creation_date': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'in_navigation': ('django.db.models.fields.BooleanField', [], {'default': 'True', 'db_index': 'True'}),
+ 'level': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
+ 'lft': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
+ 'limit_visibility_in_menu': ('django.db.models.fields.SmallIntegerField', [], {'default': 'None', 'null': 'True', 'db_index': 'True', 'blank': 'True'}),
+ 'login_required': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'moderator_state': ('django.db.models.fields.SmallIntegerField', [], {'default': '1', 'blank': 'True'}),
+ 'navigation_extenders': ('django.db.models.fields.CharField', [], {'db_index': 'True', 'max_length': '80', 'null': 'True', 'blank': 'True'}),
+ 'parent': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'children'", 'null': 'True', 'to': "orm['cms.Page']"}),
+ 'placeholders': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['cms.Placeholder']", 'symmetrical': 'False'}),
+ 'publication_date': ('django.db.models.fields.DateTimeField', [], {'db_index': 'True', 'null': 'True', 'blank': 'True'}),
+ 'publication_end_date': ('django.db.models.fields.DateTimeField', [], {'db_index': 'True', 'null': 'True', 'blank': 'True'}),
+ 'published': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'publisher_is_draft': ('django.db.models.fields.BooleanField', [], {'default': 'True', 'db_index': 'True'}),
+ 'publisher_public': ('django.db.models.fields.related.OneToOneField', [], {'related_name': "'publisher_draft'", 'unique': 'True', 'null': 'True', 'to': "orm['cms.Page']"}),
+ 'publisher_state': ('django.db.models.fields.SmallIntegerField', [], {'default': '0', 'db_index': 'True'}),
+ 'reverse_id': ('django.db.models.fields.CharField', [], {'db_index': 'True', 'max_length': '40', 'null': 'True', 'blank': 'True'}),
+ 'rght': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
+ 'site': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['sites.Site']"}),
+ 'soft_root': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'db_index': 'True'}),
+ 'template': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'tree_id': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'})
+ },
+ 'cms.placeholder': {
+ 'Meta': {'object_name': 'Placeholder'},
+ 'default_width': ('django.db.models.fields.PositiveSmallIntegerField', [], {'null': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'slot': ('django.db.models.fields.CharField', [], {'max_length': '50', 'db_index': 'True'})
+ },
+ 'cms_redirects.cmsredirect': {
+ 'Meta': {'ordering': "('old_path',)", 'unique_together': "(('site', 'old_path'),)", 'object_name': 'CMSRedirect'},
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'new_path': ('django.db.models.fields.CharField', [], {'max_length': '200', 'blank': 'True'}),
+ 'old_path': ('django.db.models.fields.CharField', [], {'max_length': '200', 'db_index': 'True'}),
+ 'page': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['cms.Page']", 'null': 'True', 'blank': 'True'}),
+ 'response_code': ('django.db.models.fields.CharField', [], {'max_length': '3', 'blank': 'True'}),
+ 'site': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['sites.Site']"})
+ },
+ 'sites.site': {
+ 'Meta': {'ordering': "('domain',)", 'object_name': 'Site', 'db_table': "'django_site'"},
+ 'domain': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
+ }
+ }
+
+ complete_apps = ['cms_redirects']
View
0 cms_redirects/migrations/__init__.py
No changes.
View
13 cms_redirects/models.py
@@ -5,20 +5,33 @@
from cms.models import Page
+RESPONSE_CODES = (
+ ('301', '301'),
+ ('302', '302'),
+)
+
class CMSRedirect(models.Model):
page = PageField(verbose_name=_("page"), blank=True, null=True, help_text=_("A link to a page has priority over a text link."))
site = models.ForeignKey(Site)
old_path = models.CharField(_('redirect from'), max_length=200, db_index=True,
help_text=_("This should be an absolute path, excluding the domain name. Example: '/events/search/'."))
new_path = models.CharField(_('redirect to'), max_length=200, blank=True,
help_text=_("This can be either an absolute path (as above) or a full URL starting with 'http://'."))
+ response_code = models.CharField(_('response code'), max_length=3, choices=RESPONSE_CODES, default=RESPONSE_CODES[0][0],
+ help_text=_("This is the http response code returned if a destination is specified. If no destination is specified the response code will be 410."))
def page_site(self):
if self.page:
return u'%s' % self.page.site
return u''
page_site.short_description = "Page Site"
+ def actual_response_code(self):
+ if self.page or self.new_path:
+ return self.response_code
+ return u'410'
+ actual_response_code.short_description = "Response Code"
+
class Meta:
verbose_name = _('CMS Redirect')
verbose_name_plural = _('CMS Redirects')
View
64 cms_redirects/tests.py
@@ -0,0 +1,64 @@
+import unittest
+from django.test.client import Client
+from cms.models import Page, Title
+from django.contrib.sites.models import Site
+from cms_redirects.models import CMSRedirect
+from django.conf import settings
+
+class TestRedirects(unittest.TestCase):
+ def setUp(self):
+ settings.APPEND_SLASH = False
+
+ self.site = Site.objects.all()[0]
+
+ page = Page()
+ page.site = self.site
+ page.save()
+ page.publish()
+ self.page = page
+
+ title = Title(title="Hello world!")
+ title.page = page
+ title.language = u'en'
+ title.save()
+
+ def test_301_page_redirect(self):
+ r_301_page = CMSRedirect(site=self.site, page=self.page, old_path='/301_page.php')
+ r_301_page.save()
+
+ c = Client()
+ r = c.get('/301_page.php')
+ self.assertEqual(r.status_code, 301)
+
+ def test_302_page_redirect(self):
+ r_302_page = CMSRedirect(site=self.site, page=self.page, old_path='/302_page.php', response_code='302')
+ r_302_page.save()
+
+ c = Client()
+ r = c.get('/302_page.php')
+ self.assertEqual(r.status_code, 302)
+
+ def test_301_path_redirect(self):
+ r_301_path = CMSRedirect(site=self.site, new_path='/', old_path='/301_path.php')
+ r_301_path.save()
+
+ c = Client()
+ r = c.get('/301_path.php')
+ self.assertEqual(r.status_code, 301)
+
+ def test_302_path_redirect(self):
+ r_302_path = CMSRedirect(site=self.site, new_path='/', old_path='/302_path.php', response_code='302')
+ r_302_path.save()
+
+ c = Client()
+ r = c.get('/302_path.php')
+ self.assertEqual(r.status_code, 302)
+
+ def test_410_redirect(self):
+ r_410 = CMSRedirect(site=self.site, old_path='/410.php', response_code='302')
+ r_410.save()
+
+ c = Client()
+ r = c.get('/410.php')
+ self.assertEqual(r.status_code, 410)
+

0 comments on commit b9b3cac

Please sign in to comment.
Something went wrong with that request. Please try again.