Skip to content

Commit 258103c

Browse files
author
konstantin.gabbasov
committedAug 19, 2016
added command for migrate urls from old catalog with redirects
1 parent b770654 commit 258103c

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed
 

‎catalog/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
default_app_config = 'catalog.apps.CatalogAppConfig'
2-
__version__ = '0.0.5'
2+
__version__ = '0.0.6'
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from optparse import make_option
2+
from django.core.management.base import BaseCommand
3+
from django.conf import settings
4+
from django.db import transaction
5+
from django.contrib.redirects.models import Redirect
6+
from catalog.models import TreeItem
7+
8+
9+
class Command(BaseCommand):
10+
help = ('Create redirects for old catalog urls (/catalog_prefix/model/slug/).')
11+
12+
option_list = BaseCommand.option_list + (
13+
make_option('-p', '--prefix', action='store', type='string', dest='url_prefix',
14+
help='Url conf prefix for old catalog urls. default: catalog'),
15+
)
16+
can_import_settings = True
17+
18+
def handle(self, *args, **options):
19+
items = TreeItem.objects.all()
20+
url_prefix = options.get('url_prefix') if options.get('url_prefix') else 'catalog'
21+
22+
with transaction.atomic():
23+
for item in items:
24+
content_object = item.content_object
25+
if content_object:
26+
old_url = '/{}/{}/{}/'.format(
27+
url_prefix,
28+
content_object.__class__.__name__,
29+
content_object.slug
30+
).lower()
31+
redirect, create= Redirect.objects.get_or_create(
32+
old_path=old_url,
33+
site_id=settings.SITE_ID
34+
)
35+
redirect.new_path = content_object.get_absolute_url()
36+
redirect.save()

0 commit comments

Comments
 (0)
Please sign in to comment.