Commit 633d2d00 authored by fred's avatar fred
Browse files

create a file suitable for apache rewritemap, for compat with spip URLs

parent 640d2b44
......@@ -10,6 +10,7 @@ from optparse import make_option
from django.core.files import File
from django.core.management.base import BaseCommand, CommandError
from django.core.urlresolvers import reverse
from django.utils.text import slugify
from ...models import Emission, Episode, Diffusion, Category, SoundFile, NewsItem
......@@ -21,18 +22,35 @@ from _spip2html import makeHtmlFromSpip
class Rubric(object):
archived = False
spip_url_marker = '-'
spip_url_object_name = 'rubrique'
def __init__(self):
self.articles = {}
self.rubrics = {}
self.categories = []
@property
def id(self):
return self.id_rubrique
class Article(object):
pass
spip_url_marker = ''
spip_url_object_name = 'article'
@property
def id(self):
return self.id_article
class Breve(object):
pass
spip_url_marker = '+'
spip_url_object_name = 'breve'
@property
def id(self):
return self.id_breve
class KeywordGroup(object):
......@@ -58,10 +76,17 @@ class Command(BaseCommand):
dest='dont_update',
default=False,
help='Only create new objects, do not update existing ones'),
make_option('--rewritemap',
dest='rewritemap_file',
metavar='FILE',
default=None,
help='Create a Apache RewriteMap'),
)
def handle(self, filename, dont_update, **options):
def handle(self, filename, dont_update, rewritemap_file, **options):
self.do_updates = (not dont_update)
if rewritemap_file:
self.rewritemap = []
with open(filename) as fd:
content = fd.read()
# the spip_courriers parts of the spip export are not properly
......@@ -98,16 +123,25 @@ class Command(BaseCommand):
for emission_id in straight_emission_rubric_ids:
rubric = self.rubrics[emission_id]
emission = self.get_or_create_emission(rubric)
if rewritemap_file:
self.add_rewritemap_entries(rubric, emission)
for article in rubric.articles.values():
episode = self.get_or_create_episode(article, emission)
if episode is None:
continue
if rewritemap_file:
self.add_rewritemap_entries(article, episode)
self.set_sound_files(article, episode)
for breve in self.breves.values():
newsitem = self.get_or_create_newsitem(breve)
if rewritemap_file:
self.add_rewritemap_entries(breve, newsitem)
if rewritemap_file:
self.write_rewritemap(rewritemap_file)
def load_keyword_groups(self):
self.keyword_groups = {}
......@@ -437,3 +471,36 @@ class Command(BaseCommand):
if object.image and os.path.basename(object.image.path) == os.path.basename(image_path):
return
object.image = File(file(image_path))
def add_rewritemap_entries(self, spip_object, django_object):
if isinstance(django_object, Emission):
object_url = reverse('emission-view', kwargs={'slug': django_object.slug})
elif isinstance(django_object, Episode):
object_url = reverse('episode-view', kwargs={
'slug': django_object.slug, 'emission_slug': django_object.emission.slug})
elif isinstance(django_object, NewsItem):
object_url = reverse('news-view', kwargs={'slug': django_object.slug})
else:
return
if spip_object.id[0] == '0':
# our hack mapping some articles to newsitems
spip_object.spip_url_object_name = 'article'
spip_object.spip_url_marker = ''
urls = []
urls.append('%s%s' % (spip_object.spip_url_object_name, spip_object.id))
urls.append('spip.php?%s%s' % (spip_object.spip_url_object_name, spip_object.id))
urls.append('%s%s.html' % (spip_object.spip_url_object_name, spip_object.id))
if spip_object.spip_url_object_name == 'article':
urls.append(spip_object.id)
urls.append('%s%s%s' % (spip_object.spip_url_marker, spip_object.url, spip_object.spip_url_marker))
for url in urls:
self.rewritemap.append((url, object_url))
def write_rewritemap(self, rewritemap_file):
fd = file(rewritemap_file, 'w')
for src, dst in self.rewritemap:
print >> fd, src, dst
fd.close()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment