Mercurial > public > sg101
changeset 591:1982996ce365
Created a "fixed page" facility.
Reworked the last few commits. We now generate HTML snippets from
restructured text files. These are {% include'd %} by a fixed
page template. This is for bitbucket issue #8.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 12 May 2012 14:57:45 -0500 |
parents | ce73771b6cd0 |
children | 76ec83b73e18 |
files | core/management/commands/make_fixed_page.py core/management/commands/make_flatpage.py core/views.py sg101/fixed/about.rst sg101/flatpages/about.rst sg101/settings/base.py sg101/templates/fixed/about.html sg101/templates/fixed/base.html sg101/urls.py sg101/views.py |
diffstat | 10 files changed, 208 insertions(+), 175 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/management/commands/make_fixed_page.py Sat May 12 14:57:45 2012 -0500 @@ -0,0 +1,78 @@ +"""make_fixed_page.py + +A management command to generate HTML files that can be {% include'd %} by a +generic flatpage-like system. We currently support restructured text as input. + +I chose "fixed" page since I didn't want to confuse this with Django's flatpage +system. + +This command reads input files from the directory PROJECT_PATH/fixed and writes +the HTML output to PROJECT_PATH/templates/fixed. + +""" +from __future__ import with_statement +import os.path +import glob + +import docutils.core +from django.core.management.base import LabelCommand, CommandError +from django.conf import settings + + +class Command(LabelCommand): + help = "Generate HTML from restructured text files" + args = "<inputfile1> <inputfile2> ... | all" + + def handle_label(self, filename, **kwargs): + """Process input file(s)""" + + if not hasattr(settings, 'PROJECT_PATH'): + raise CommandError("Please add a PROJECT_PATH setting") + + self.src_dir = os.path.join(settings.PROJECT_PATH, 'fixed') + self.dst_dir = os.path.join(settings.PROJECT_PATH, 'templates', 'fixed') + + if filename == 'all': + files = glob.glob("%s%s*.rst" % (self.src_dir, os.path.sep)) + files = [os.path.basename(f) for f in files] + else: + files = [filename] + + for f in files: + self.process_page(f) + + def process_page(self, filename): + """Processes one fixed page""" + + # retrieve source text + src_path = os.path.join(self.src_dir, filename) + try: + with open(src_path, 'r') as f: + src_text = f.read() + except IOError, ex: + raise CommandError(str(ex)) + + # transform text + content = self.transform_input(src_text) + + # write output + basename = os.path.splitext(os.path.basename(filename))[0] + dst_path = os.path.join(self.dst_dir, '%s.html' % basename) + + try: + with open(dst_path, 'w') as f: + f.write(content.encode('utf-8')) + except IOError, ex: + raise CommandError(str(ex)) + + prefix = os.path.commonprefix([src_path, dst_path]) + self.stdout.write("%s -> %s\n" % (filename, dst_path[len(prefix):])) + + def transform_input(self, src_text): + """Transforms input restructured text to HTML""" + + return docutils.core.publish_parts(src_text, writer_name='html', + settings_overrides={ + 'doctitle_xform': False, + 'initial_header_level': 2, + })['html_body']
--- a/core/management/commands/make_flatpage.py Thu May 10 20:32:19 2012 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,81 +0,0 @@ -"""make_flatpage.py - -A management command to update flatpage objects from restructured text files. - -""" -from __future__ import with_statement -import os.path - -import docutils.core -from django.core.management.base import LabelCommand, CommandError -from django.conf import settings -from django.template.loader import render_to_string -from django.template import TemplateDoesNotExist -from django.contrib.flatpages.models import FlatPage - - -class Command(LabelCommand): - help = "Update flatpage objects from restructured text files" - args = "<inputfile1> <inputfile2> ... | all" - - def handle_label(self, filename, **kwargs): - """Process input file(s)""" - - if not hasattr(settings, 'GPP_FLATPAGES'): - raise CommandError("Please add a GPP_FLATPAGES setting") - if not hasattr(settings, 'PROJECT_PATH'): - raise CommandError("Please add a PROJECT_PATH setting") - - self.config = settings.GPP_FLATPAGES - self.src_dir = os.path.join(settings.PROJECT_PATH, 'flatpages') - - if filename == 'all': - files = self.config.keys() - else: - files = [filename] - - for f in files: - self.process_flatpage(f) - - - def process_flatpage(self, filename): - """Processes one flatpage""" - - if filename not in self.config: - raise CommandError("No entry in GPP_FLATPAGES for %s" % filename) - url, template_name = self.config[filename] - - # retrieve source text - src_path = os.path.join(self.src_dir, filename) - with open(src_path, 'r') as f: - src_text = f.read() - - # transform text - content = self.transform_input(src_text) - - # render through a template if requested - if template_name is not None: - try: - content = render_to_string(template_name, dict(content=content)) - except TemplateDoesNotExist: - raise CommandError("template not found: %s" % template_name) - - # update the flatpage object - try: - obj = FlatPage.objects.get(url=url) - except FlatPage.DoesNotExist: - raise CommandError("flatpage not found: %s" % url) - - obj.content = content; - obj.save() - - self.stdout.write("%s -> %s\n" % (filename, url)) - - def transform_input(self, src_text): - """Transforms input restructured text to HTML""" - - return docutils.core.publish_parts(src_text, writer_name='html', - settings_overrides={ - 'doctitle_xform': False, - 'initial_header_level': 3, - })['html_body']
--- a/core/views.py Thu May 10 20:32:19 2012 -0500 +++ b/core/views.py Sat May 12 14:57:45 2012 -0500 @@ -5,9 +5,9 @@ from django.contrib.auth.models import User from django.http import HttpResponse from django.shortcuts import render_to_response -from django.template import RequestContext from django.contrib.auth.decorators import login_required from django.views.decorators.http import require_GET +from django.views.generic import TemplateView import django.utils.simplejson as json @@ -36,3 +36,20 @@ users = User.objects.filter(is_active=True, username__istartswith=q).values_list('username', flat=True)[:limit] return HttpResponse(json.dumps(list(users)), content_type='application/json') + + +class FixedView(TemplateView): + """ + For displaying our "fixed" views generated with the custom command + make_fixed_page. + + """ + template_name = 'fixed/base.html' + title = '' + content_template = '' + + def get_context_data(self, **kwargs): + context = super(FixedView, self).get_context_data(**kwargs) + context['title'] = self.title + context['content_template'] = self.content_template + return context
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sg101/fixed/about.rst Sat May 12 14:57:45 2012 -0500 @@ -0,0 +1,51 @@ +About SurfGuitar101.com +======================= + +SurfGuitar101.com is the premier place on the web for friends and fans of the +world-wide phenomenon known as surf music. Surf music was created in the early +1960's in Southern California by such bands as The Belairs, Dick Dale & His +Deltones, and The Chantays, and popularized further by bands like The Ventures, +The Astronauts, The Pyramids, & The Lively Ones. Surf music was all but +forgotten when The Beatles and the British Invasion landed in America in the mid +to late 1960's. In the late 70's and early 1980's a revival began when bands +like Jon & The Nightriders, The Surf Raiders, and The Halibuts heard the call of +the surf and reintroduced it to hungry audiences. This revival continues today +and has spread world-wide. Today you can find surf bands not only in California, +but all across America, Europe, Australia, Central and South America, and Japan. + +Join us in our forums to discuss this great form of popular music. Discover +great bands old and new. Check out our podcasts as we highlight the classic surf +bands and the bands of today. Meet new friends and learn about the next surf +show in your town. Exchange tips on playing and performing surf music and even +starting your own band! + +Thanks for being part of the greatest online community dedicated to surf music! + +A Short History of SurfGuitar101.com +------------------------------------ + +This site started as a Yahoo Group in late October, 2001. There were several +other surf music Yahoo groups at the time, so we started our focus on the +musician aspect of playing surf music (hence the "guitar 101"). After a short +time we dropped that angle and fully embraced all aspects of surf music. + +After seeing The Surf Coasters (Japan) on their first US tour in the summer of +2004, we needed a place to host our many photos and videos. The domain name +surfguitar101.com was registered, and a simple static website was created to +host media files as a supplement to the Yahoo group. + +Cramped by the confines of the Yahoo Group, in February of 2006 we launched an +interactive version of the website, complete with our now famous forums. This +format was kept until February, 2011 when the website software was rewritten and +a new look was designed. + +The SG101 community held its first annual convention weekend in 2008 in Southern +California, a tradition that continues today. Ever year our members get together +for a surf music packed weekend, and each year has been bigger and better than +the last. In 2010, Germany's The Space Rangers and Italy's (via Antigua) Wadadli +Riders were the first non-US bands to play at the convention. Fans of surf music +get to see, hear, and mingle with musicians from the original 60's bands as well +as the up and coming bands of today. + +| *Surf's Up!* +| *— Brian Neal*
--- a/sg101/flatpages/about.rst Thu May 10 20:32:19 2012 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,48 +0,0 @@ -SurfGuitar101.com is the premier place on the web for friends and fans of the -world-wide phenomenon known as surf music. Surf music was created in the early -1960's in Southern California by such bands as The Belairs, Dick Dale & His -Deltones, and The Chantays, and popularized further by bands like The Ventures, -The Astronauts, The Pyramids, & The Lively Ones. Surf music was all but -forgotten when The Beatles and the British Invasion landed in America in the mid -to late 1960's. In the late 70's and early 1980's a revival began when bands -like Jon & The Nightriders, The Surf Raiders, and The Halibuts heard the call of -the surf and reintroduced it to hungry audiences. This revival continues today -and has spread world-wide. Today you can find surf bands not only in California, -but all across America, Europe, Australia, Central and South America, and Japan. - -Join us in our forums to discuss this great form of popular music. Discover -great bands old and new. Check out our podcasts as we highlight the classic surf -bands and the bands of today. Meet new friends and learn about the next surf -show in your town. Exchange tips on playing and performing surf music and even -starting your own band! - -Thanks for being part of the greatest online community dedicated to surf music! - -A Short History of SurfGuitar101.com -==================================== - -This site started as a Yahoo Group in late October, 2001. There were several -other surf music Yahoo groups at the time, so we started our focus on the -musician aspect of playing surf music (hence the "guitar 101"). After a short -time we dropped that angle and fully embraced all aspects of surf music. - -After seeing The Surf Coasters (Japan) on their first US tour in the summer of -2004, we needed a place to host our many photos and videos. The domain name -surfguitar101.com was registered, and a simple static website was created to -host media files as a supplement to the Yahoo group. - -Cramped by the confines of the Yahoo Group, in February of 2006 we launched an -interactive version of the website, complete with our now famous forums. This -format was kept until February, 2011 when the website software was rewritten and -a new look was designed. - -The SG101 community held its first annual convention weekend in 2008 in Southern -California, a tradition that continues today. Ever year our members get together -for a surf music packed weekend, and each year has been bigger and better than -the last. In 2010, Germany's The Space Rangers and Italy's (via Antigua) Wadadli -Riders were the first non-US bands to play at the convention. Fans of surf music -get to see, hear, and mingle with musicians from the original 60's bands as well -as the up and coming bands of today. - -| *Surf's Up!* -| *— Brian Neal*
--- a/sg101/settings/base.py Thu May 10 20:32:19 2012 -0500 +++ b/sg101/settings/base.py Sat May 12 14:57:45 2012 -0500 @@ -297,35 +297,6 @@ ' SurfGuitar101.com!') ####################################################################### -# Homegrown flatpage system configuration -####################################################################### -# -# GPP_FLATPAGES is a dictionary where each key is a filename that is -# used as input to generate HTML that is inserted into an existing -# django.contrib.flatpages model. The value part of each entry is a -# 2-tuple with the following members, in order: -# -# * 'url' - The url of the flatpage model to update. This object must -# already exist. -# -# * 'template' - The template to be used to generate the flatpage; -# The processed text will be passed to the template in the context -# variable {{ content }}. If template is None, no template will -# be used; the generated text will be inserted directly into the -# flatpage object. -# -# The custom management command make_flatpage is used to run the -# flatpage generation process. E.g. -# -# $ python manage.py make_flatpage about.rst privacy.rst -# or -# $ python manage.py make_flatpage all -# -GPP_FLATPAGES = { - 'about.rst': ('/about/', None), -} - -####################################################################### # URL's of 3rd party Javascript and CSS files. # These dictionaries are used by core/templatetags/script_tags, and # should also be used by developers when creating form media classes.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sg101/templates/fixed/about.html Sat May 12 14:57:45 2012 -0500 @@ -0,0 +1,48 @@ +<div class="document"> +<div class="section" id="about-surfguitar101-com"> +<h2>About SurfGuitar101.com</h2> +<p>SurfGuitar101.com is the premier place on the web for friends and fans of the +world-wide phenomenon known as surf music. Surf music was created in the early +1960's in Southern California by such bands as The Belairs, Dick Dale & His +Deltones, and The Chantays, and popularized further by bands like The Ventures, +The Astronauts, The Pyramids, & The Lively Ones. Surf music was all but +forgotten when The Beatles and the British Invasion landed in America in the mid +to late 1960's. In the late 70's and early 1980's a revival began when bands +like Jon & The Nightriders, The Surf Raiders, and The Halibuts heard the call of +the surf and reintroduced it to hungry audiences. This revival continues today +and has spread world-wide. Today you can find surf bands not only in California, +but all across America, Europe, Australia, Central and South America, and Japan.</p> +<p>Join us in our forums to discuss this great form of popular music. Discover +great bands old and new. Check out our podcasts as we highlight the classic surf +bands and the bands of today. Meet new friends and learn about the next surf +show in your town. Exchange tips on playing and performing surf music and even +starting your own band!</p> +<p>Thanks for being part of the greatest online community dedicated to surf music!</p> +<div class="section" id="a-short-history-of-surfguitar101-com"> +<h3>A Short History of SurfGuitar101.com</h3> +<p>This site started as a Yahoo Group in late October, 2001. There were several +other surf music Yahoo groups at the time, so we started our focus on the +musician aspect of playing surf music (hence the "guitar 101"). After a short +time we dropped that angle and fully embraced all aspects of surf music.</p> +<p>After seeing The Surf Coasters (Japan) on their first US tour in the summer of +2004, we needed a place to host our many photos and videos. The domain name +surfguitar101.com was registered, and a simple static website was created to +host media files as a supplement to the Yahoo group.</p> +<p>Cramped by the confines of the Yahoo Group, in February of 2006 we launched an +interactive version of the website, complete with our now famous forums. This +format was kept until February, 2011 when the website software was rewritten and +a new look was designed.</p> +<p>The SG101 community held its first annual convention weekend in 2008 in Southern +California, a tradition that continues today. Ever year our members get together +for a surf music packed weekend, and each year has been bigger and better than +the last. In 2010, Germany's The Space Rangers and Italy's (via Antigua) Wadadli +Riders were the first non-US bands to play at the convention. Fans of surf music +get to see, hear, and mingle with musicians from the original 60's bands as well +as the up and coming bands of today.</p> +<div class="line-block"> +<div class="line"><em>Surf's Up!</em></div> +<div class="line"><em>— Brian Neal</em></div> +</div> +</div> +</div> +</div>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sg101/templates/fixed/base.html Sat May 12 14:57:45 2012 -0500 @@ -0,0 +1,5 @@ +{% extends 'base.html' %} +{% block title %}{{ title }}{% endblock %} +{% block content %} +{% include content_template %} +{% endblock %}
--- a/sg101/urls.py Thu May 10 20:32:19 2012 -0500 +++ b/sg101/urls.py Sat May 12 14:57:45 2012 -0500 @@ -3,18 +3,25 @@ from django.conf import settings from django.contrib import admin from django.views.decorators.cache import cache_page +from django.views.generic import TemplateView from haystack.views import search_view_factory from news.feeds import LatestNewsFeed from forums.feeds import ForumsFeed from custom_search.forms import CustomModelSearchForm +from core.views import FixedView admin.autodiscover() urlpatterns = patterns('', - url(r'^$', 'sg101.views.home', name='home'), + url(r'^$', + TemplateView.as_view(template_name='home.html'), + name='home'), + url(r'^about/$', + FixedView.as_view(title='About', content_template='fixed/about.html'), + name='about'), (r'^admin/doc/', include('django.contrib.admindocs.urls')), url(r'^admin/password_reset/$', 'django.contrib.auth.views.password_reset', name='admin_password_reset'),
--- a/sg101/views.py Thu May 10 20:32:19 2012 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,15 +0,0 @@ -""" -This file contains views that don't belong to any specific application. -In particular, the home page view. -""" -from django.shortcuts import render_to_response -from django.template import RequestContext - - -def home(request): - """ - The home page view of the site. - """ - return render_to_response('home.html', { - }, - context_instance = RequestContext(request))