Permalink
Browse files

Add am management command to generate the list of urls of the publish…

…ed Pages
  • Loading branch information...
1 parent ac97fec commit c72cb48491ffbd8c5f45512127d163fece68bb6d @yml yml committed Aug 7, 2011
@@ -0,0 +1,50 @@
+from optparse import make_option
+
+from django.core.management.base import BaseCommand, CommandError
+from django.conf import settings
+from django.contrib.sites.models import Site
+from django.template.loader import render_to_string
+
+
+from cms.models.pagemodel import Page
+
+
+class Command(BaseCommand):
+ help = 'Get the page urls'
+
+ option_list = BaseCommand.option_list + (
+ make_option("--filename", dest="filename", default=""),
+ # default template is template_menu_1placeholder.html
+ make_option("--template", dest="template",
+ default="url_list.txt"),
+ make_option("--domain-name",
+ dest='domain_name',
+ default=(Site.objects.get(id=settings.SITE_ID)
+ .domain))
+ )
+
+ def get_published_pages(self):
+ """Get the queriset of the published Pages"""
+ return Page.objects.published()
+
+ def get_string_urls(self, template=None,
+ extra_context=None):
+ """Create a file containing the urls"""
+ pages = self.get_published_pages()
+ extra_context.update({'pages': pages})
+ return render_to_string(template, extra_context)
+
+ def handle(self, *args, **options):
+ try:
+ filename = options['filename']
+ template = options['template']
+ domain_name = options['domain_name']
+ string_urls = self.get_string_urls(template,
+ extra_context={'domain_name': domain_name})
+ if filename:
+ with open(filename, 'w') as f:
+ f.write(string_urls)
+ else:
+ print(string_urls)
+ except Exception, e:
+ raise CommandError("There was an error running this command. ", e)
@@ -0,0 +1,3 @@
+{% for page in pages %}
+http://{{ domain_name }}{{ page.get_absolute_url }}
+{% endfor %}

0 comments on commit c72cb48

Please sign in to comment.