Mercurial > public > sg101
changeset 240:1246a4f1ab4f
For #93: fix url scheme for the news application.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Wed, 15 Sep 2010 00:14:54 +0000 (2010-09-15) |
parents | dcc929973bba |
children | 27bee3ac85e6 |
files | gpp/news/admin.py gpp/news/fixtures/news_categories.json gpp/news/forms.py gpp/news/models.py gpp/news/urls.py gpp/news/views.py gpp/templates/base.html gpp/templates/core/pagination.html gpp/templates/news/archive_index.html gpp/templates/news/base.html gpp/templates/news/category_index.html gpp/templates/news/current_news.html gpp/templates/news/index.html gpp/templates/news/story.html gpp/templates/news/story_summary.html gpp/templates/news/submit_news.html gpp/templates/news/tag_index.html |
diffstat | 17 files changed, 84 insertions(+), 147 deletions(-) [+] |
line wrap: on
line diff
--- a/gpp/news/admin.py Sun Sep 12 18:30:23 2010 +0000 +++ b/gpp/news/admin.py Wed Sep 15 00:14:54 2010 +0000 @@ -10,6 +10,12 @@ from news.models import Story from news.models import Category + +class CategoryAdmin(admin.ModelAdmin): + prepopulated_fields = {'slug': ("title", )} + list_display = ('title', 'slug') + + class PendingStoryAdmin(admin.ModelAdmin): list_display = ('title', 'date_submitted', 'submitter') list_filter = ('date_submitted', ) @@ -48,6 +54,6 @@ js = settings.GPP_THIRD_PARTY_JS['tiny_mce'] -admin.site.register(Category) +admin.site.register(Category, CategoryAdmin) admin.site.register(Story, StoryAdmin) admin.site.register(PendingStory, PendingStoryAdmin)
--- a/gpp/news/fixtures/news_categories.json Sun Sep 12 18:30:23 2010 +0000 +++ b/gpp/news/fixtures/news_categories.json Wed Sep 15 00:14:54 2010 +0000 @@ -4,6 +4,7 @@ "model": "news.category", "fields": { "icon": "news/categories/nch_articles.jpg", + "slug": "articles", "title": "Articles" } }, @@ -12,6 +13,7 @@ "model": "news.category", "fields": { "icon": "news/categories/nch_bands.jpg", + "slug": "bands", "title": "Bands" } }, @@ -20,6 +22,7 @@ "model": "news.category", "fields": { "icon": "news/categories/video.gif", + "slug": "featured-videos", "title": "Featured Videos" } }, @@ -28,6 +31,7 @@ "model": "news.category", "fields": { "icon": "news/categories/nch_gear.jpg", + "slug": "gear", "title": "Gear" } }, @@ -36,6 +40,7 @@ "model": "news.category", "fields": { "icon": "news/categories/nch_interviews.jpg", + "slug": "interviews", "title": "Interviews" } }, @@ -44,6 +49,7 @@ "model": "news.category", "fields": { "icon": "news/categories/nch_reviews.jpg", + "slug": "reviews", "title": "Reviews" } }, @@ -52,6 +58,7 @@ "model": "news.category", "fields": { "icon": "news/categories/nch_showannoun.jpg", + "slug": "show-announcements", "title": "Show Announcements" } }, @@ -60,6 +67,7 @@ "model": "news.category", "fields": { "icon": "news/categories/nch_showreport.jpg", + "slug": "show-reports", "title": "Show Reports" } }, @@ -68,6 +76,7 @@ "model": "news.category", "fields": { "icon": "news/categories/nch_sitenews.jpg", + "slug": "site-news", "title": "Site News" } }, @@ -76,6 +85,7 @@ "model": "news.category", "fields": { "icon": "news/categories/nch_surfscene.jpg", + "slug": "surf-scene-news", "title": "Surf Scene News" } }, @@ -84,6 +94,7 @@ "model": "news.category", "fields": { "icon": "news/categories/nch_tab.jpg", + "slug": "tablature", "title": "Tablature" } }
--- a/gpp/news/forms.py Sun Sep 12 18:30:23 2010 +0000 +++ b/gpp/news/forms.py Wed Sep 15 00:14:54 2010 +0000 @@ -23,24 +23,6 @@ js = settings.GPP_THIRD_PARTY_JS['tiny_mce'] -class SearchNewsForm(forms.Form): - """Form for a user to search news stories.""" - text = forms.CharField(max_length=30) - category = forms.ModelChoiceField(label='', - required=False, - empty_label='(All Categories)', - queryset=Category.objects.all()) - - def get_query(self): - return self.cleaned_data['text'] - - def get_category(self): - cat = self.cleaned_data['category'] - if cat: - return cat - return None - - class SendStoryForm(forms.Form): """Form for sending a news story via email to a friend.""" friend_name = forms.CharField(label="Friend's Name", max_length=64)
--- a/gpp/news/models.py Sun Sep 12 18:30:23 2010 +0000 +++ b/gpp/news/models.py Wed Sep 15 00:14:54 2010 +0000 @@ -10,7 +10,8 @@ class Category(models.Model): """News stories belong to categories""" - title = models.CharField(max_length = 64) + title = models.CharField(max_length=64) + slug = models.SlugField(max_length=64) icon = models.ImageField(upload_to='news/categories/', blank=True) def __unicode__(self):
--- a/gpp/news/urls.py Sun Sep 12 18:30:23 2010 +0000 +++ b/gpp/news/urls.py Wed Sep 15 00:14:54 2010 +0000 @@ -2,25 +2,18 @@ from django.conf.urls.defaults import * urlpatterns = patterns('news.views', - url(r'^page/(?P<page>\d+)/$', 'index', name='news-index_page'), - url(r'^archive/$', 'archive_index', name='news-archive_index'), - url(r'^archive/(?P<year>\d{4})/(?P<month>\d\d?)/page/(?P<page>\d+)/$', - 'archive', - name='news-archive_page'), - (r'^categories/$', 'category_index'), - (r'^category/(?P<category>\d+)/page/(?P<page>\d+)/$', 'category'), - (r'^email/(\d+)/$', 'email_story'), - (r'^email/thanks/$', 'email_thanks'), - url(r'^search/page/(?P<page>\d+)/$', 'search', name='news-search_page'), - (r'^story/(\d+)/$', 'story'), - (r'^submit/$', 'submit'), - (r'^submit/thanks/$', 'submit_thanks'), - url(r'^tags/$', 'tags', name='news-tag_index'), - url(r'^tag/(?P<tag_name>[^/]+)/page/(?P<page>\d+)/$', - 'tag', - name='news-tag_page'), + url(r'^date/$', 'index', name='news-index_page'), + url(r'^archive/$', 'archive_index', name='news-archive_index'), + url(r'^archive/(?P<year>\d{4})/(?P<month>\d\d?)/$', + 'archive', + name='news-archive_page'), + url(r'^categories/$', 'category_index', name='news-category_index'), + url(r'^category/(?P<slug>[\w\d-]+)/$', 'category', name='news-category'), + url(r'^email/(\d+)/$', 'email_story', name='news-email_story'), + url(r'^email/thanks/$', 'email_thanks', name='news-email_thanks'), + url(r'^story/(\d+)/$', 'story', name='news-story'), + url(r'^submit/$', 'submit', name='news-submit'), + url(r'^submit/thanks/$', 'submit_thanks', name='news-submit_thanks'), + url(r'^tags/$', 'tags', name='news-tag_index'), + url(r'^tag/(?P<tag_name>[^/]+)/$', 'tag', name='news-tag_page'), ) - -urlpatterns += patterns('django.views.generic.simple', - (r'^$', 'redirect_to', {'url': 'page/1/'}), -)
--- a/gpp/news/views.py Sun Sep 12 18:30:23 2010 +0000 +++ b/gpp/news/views.py Wed Sep 15 00:14:54 2010 +0000 @@ -27,7 +27,6 @@ from news.models import PendingStory from news.models import Story from news.forms import AddNewsForm -from news.forms import SearchNewsForm from news.forms import SendStoryForm NEWS_PER_PAGE = 5 @@ -39,18 +38,33 @@ ####################################################################### -def index(request, page=1): +def _get_page(qdict): + """Attempts to retrieve the value for "page" from the given query dict and + return it as an integer. If the key cannot be found or converted to an + integer, 1 is returned. + """ + n = qdict.get('page', 1) + try: + n = int(n) + except ValueError: + n = 1 + return n + +####################################################################### + +def index(request): stories = Story.objects.all().select_related() paginator = create_paginator(stories) + + page = _get_page(request.GET) try: - the_page = paginator.page(int(page)) + the_page = paginator.page(page) except InvalidPage: raise Http404 return render_to_response('news/index.html', { 'title': 'Main Index', 'page': the_page, - 'search_form': SearchNewsForm(), }, context_instance = RequestContext(request)) @@ -61,17 +75,17 @@ return render_to_response('news/archive_index.html', { 'title': 'News Archive', 'dates': dates, - 'search_form': SearchNewsForm(), }, context_instance = RequestContext(request)) ####################################################################### -def archive(request, year, month, page=1): +def archive(request, year, month): stories = Story.objects.filter(date_submitted__year=year, date_submitted__month=month) paginator = create_paginator(stories) + page = _get_page(request.GET) try: - the_page = paginator.page(int(page)) + the_page = paginator.page(page) except InvalidPage: raise Http404 @@ -80,7 +94,6 @@ return render_to_response('news/index.html', { 'title': 'Archive for %s, %s' % (month_name, year), 'page': the_page, - 'search_form': SearchNewsForm(), }, context_instance = RequestContext(request)) @@ -94,69 +107,24 @@ return render_to_response('news/category_index.html', { 'cat_list': cat_list, - 'search_form': SearchNewsForm(), }, context_instance = RequestContext(request)) ####################################################################### -def category(request, category, page=1): - category = get_object_or_404(Category, pk=category) +def category(request, slug): + category = get_object_or_404(Category, slug=slug) stories = Story.objects.filter(category=category) paginator = create_paginator(stories) + page = _get_page(request.GET) try: - the_page = paginator.page(int(page)) + the_page = paginator.page(page) except InvalidPage: raise Http404 return render_to_response('news/index.html', { 'title': 'Category: ' + category.title, 'page': the_page, - 'search_form': SearchNewsForm(), - }, - context_instance = RequestContext(request)) - -####################################################################### - -def search(request, page=1): - if request.method == 'POST': - form = SearchNewsForm(request.POST) - if form.is_valid(): - query_text = form.get_query() - category = form.get_category() - page = 1 - else: - return HttpResponseRedirect(reverse('news.views.index')) - else: - if 'query' in request.GET: - query_text = request.GET['query'] - category = request.GET.get('category', None) - else: - return HttpResponseRedirect(reverse('news.views.index')) - - if category is not None: - stories = Story.objects.filter(category=category) - cat_qual = ' in category "%s"' % category.title - else: - stories = Story.objects.all() - cat_qual = '' - - stories = stories.filter( - Q(title__icontains=query_text) | - Q(short_text__icontains=query_text) | - Q(long_text__icontains=query_text)).order_by('-date_submitted') - - paginator = create_paginator(stories) - try: - the_page = paginator.page(int(page)) - except InvalidPage: - raise Http404 - - return render_to_response('news/index.html', { - 'title': 'Search Results for "%s"%s' % (query_text, cat_qual), - 'query': query_text, - 'page': the_page, - 'search_form': SearchNewsForm(), }, context_instance = RequestContext(request)) @@ -166,7 +134,6 @@ story = get_object_or_404(Story, pk=story_id) return render_to_response('news/story.html', { 'story': story, - 'search_form': SearchNewsForm(), }, context_instance=RequestContext(request)) @@ -188,7 +155,6 @@ return render_to_response('news/submit_news.html', { 'add_form': add_form, - 'search_form': SearchNewsForm(), }, context_instance = RequestContext(request)) @@ -197,7 +163,6 @@ @login_required def submit_thanks(request): return render_to_response('news/submit_news.html', { - 'search_form': SearchNewsForm(), }, context_instance = RequestContext(request)) @@ -207,25 +172,24 @@ tags = Tag.objects.cloud_for_model(Story) return render_to_response('news/tag_index.html', { 'tags': tags, - 'search_form': SearchNewsForm(), }, context_instance = RequestContext(request)) ####################################################################### -def tag(request, tag_name, page=1): +def tag(request, tag_name): tag = get_object_or_404(Tag, name=tag_name) stories = TaggedItem.objects.get_by_model(Story.objects.all().select_related(), tag) paginator = create_paginator(stories) + page = _get_page(request.GET) try: - the_page = paginator.page(int(page)) + the_page = paginator.page(page) except InvalidPage: raise Http404 return render_to_response('news/index.html', { 'title': 'Stories with tag: "%s"' % tag_name, 'page': the_page, - 'search_form': SearchNewsForm(), }, context_instance=RequestContext(request))
--- a/gpp/templates/base.html Sun Sep 12 18:30:23 2010 +0000 +++ b/gpp/templates/base.html Wed Sep 15 00:14:54 2010 +0000 @@ -53,7 +53,7 @@ <div id="content-secondary" class="span-4 append-1"> <ul class="nav-left"> <li><a href="{% url home %}">Home</a></li> - <li><a href="{% url news-index_page page=1 %}">News</a></li> + <li><a href="{% url news-index_page %}">News</a></li> <li><a href="{% url gcalendar-index %}">Calendar</a></li> <li><a href="{% url contact-form %}">Contact</a></li> <li><a href="{% url donations-index %}">Donations</a></li>
--- a/gpp/templates/core/pagination.html Sun Sep 12 18:30:23 2010 +0000 +++ b/gpp/templates/core/pagination.html Wed Sep 15 00:14:54 2010 +0000 @@ -1,21 +1,21 @@ <div class="pagination"> <ul> {% if page.has_previous %} -<li class="prev"><a href="../{{ page.previous_page_number }}/" title="Go to page {{ page.previous_page_number }}">« Previous</a></li> +<li class="prev"><a href="./?page={{ page.previous_page_number }}" title="Go to page {{ page.previous_page_number }}">« Previous</a></li> {% endif %} {% for num in page.page_range %} {% if num %} {% ifequal num page.number %} <li class="current">{{ num }}</li> {% else %} -<li class="page"><a href="../{{ num }}/" title="Go to page {{ num }}">{{ num }}</a></li> +<li class="page"><a href="./?page={{ num }}" title="Go to page {{ num }}">{{ num }}</a></li> {% endifequal %} {% else %} <li>…</li> {% endif %} {% endfor %} {% if page.has_next %} -<li class="next"><a href="../{{ page.next_page_number }}/" title="Go to page {{ page.next_page_number }}">Next »</a></li> +<li class="next"><a href="./?page={{ page.next_page_number }}" title="Go to page {{ page.next_page_number }}">Next »</a></li> {% endif %} </ul> </div>
--- a/gpp/templates/news/archive_index.html Sun Sep 12 18:30:23 2010 +0000 +++ b/gpp/templates/news/archive_index.html Wed Sep 15 00:14:54 2010 +0000 @@ -10,7 +10,7 @@ {% if dates %} <ul> {% for date in dates %} - <li><a href="{% url news-archive_page year=date.year month=date.month page=1 %}"> + <li><a href="{% url news-archive_page year=date.year month=date.month %}"> {{ date|date:"F, Y" }}</a></li> {% endfor %} </ul>
--- a/gpp/templates/news/base.html Sun Sep 12 18:30:23 2010 +0000 +++ b/gpp/templates/news/base.html Wed Sep 15 00:14:54 2010 +0000 @@ -8,19 +8,13 @@ {% endblock %} {% block content %} <h2>SurfGuitar101 News & Articles <a href="{% url feeds-news %}"><img src="{{ MEDIA_URL }}icons/feed.png" alt="News Feed" title="News Feed" /></a></h2> -{% if search_form %} -<div class="news-search"> -<form action="{% url news-search_page page=1 %}" method="post">{% csrf_token %} - <p>{{ search_form.text }} {{ search_form.category }} <input type="submit" value="Search" /></p> -</form> -</div> -{% endif %} + <ul class="app-menu"> -<li><a href="{% url news-index_page page=1 %}">News Main</a></li> +<li><a href="{% url news-index_page %}">News Main</a></li> <li><a href="{% url news-archive_index %}">Archive</a></li> -<li><a href="{% url news.views.category_index %}">Categories</a></li> +<li><a href="{% url news-category_index %}">Categories</a></li> <li><a href="{% url news-tag_index %}">Tags</a></li> -<li><a href="{% url news.views.submit %}">Submit News</a></li> +<li><a href="{% url news-submit %}">Submit News</a></li> </ul> {% block news_content %} {% endblock %}
--- a/gpp/templates/news/category_index.html Sun Sep 12 18:30:23 2010 +0000 +++ b/gpp/templates/news/category_index.html Wed Sep 15 00:14:54 2010 +0000 @@ -12,7 +12,7 @@ {% for category, story_set in cat_list %} <h3>{{ category.title }}</h3> - <p><a href="{% url news.views.category category=category.id page=1 %}"> + <p><a href="{% url news-category slug=category.slug %}"> <img src="{{ category.icon.url }}" alt="{{ category.title }}" title="{{ category.title }}" /> </a> </p>
--- a/gpp/templates/news/current_news.html Sun Sep 12 18:30:23 2010 +0000 +++ b/gpp/templates/news/current_news.html Wed Sep 15 00:14:54 2010 +0000 @@ -4,5 +4,5 @@ {% include 'news/story_summary.html' %} {% endfor %} <hr /> -<p>For more news stories, check out our <a href="{% url news-index_page page=1 %}">news archive</a>.</p> +<p>For more news stories, check out our <a href="{% url news-index_page %}">news archive</a>.</p> {% endif %}
--- a/gpp/templates/news/index.html Sun Sep 12 18:30:23 2010 +0000 +++ b/gpp/templates/news/index.html Wed Sep 15 00:14:54 2010 +0000 @@ -6,29 +6,15 @@ {% block news_content %} <h3>{{ title }}</h3> -{% if query %} -{% include 'core/pagination_query.html' %} -{% else %} +{% if page.object_list %} {% include 'core/pagination.html' %} -{% endif %} - -{% if page.object_list %} {% for story in page.object_list %} {% include 'news/story_summary.html' %} {% endfor %} +{% include 'core/pagination.html' %} <div style="clear:right;"></div> {% else %} - {% if query %} - <p>No results found.</p> - {% else %} - <p>No news at this time.</p> - {% endif %} -{% endif %} - -{% if query %} -{% include 'core/pagination_query.html' %} -{% else %} -{% include 'core/pagination.html' %} + <p>No news at this time.</p> {% endif %} {% endblock %}
--- a/gpp/templates/news/story.html Sun Sep 12 18:30:23 2010 +0000 +++ b/gpp/templates/news/story.html Wed Sep 15 00:14:54 2010 +0000 @@ -19,7 +19,7 @@ </div> <hr /> <div class="news-content"> - <a href="{% url news.views.category category=story.category.id page=1 %}"> + <a href="{% url news-category slug=story.category.slug %}"> <img src="{{ story.category.icon.url }}" alt="{{ story.category.title }}" title="{{ story.category.title }}" class="news-icon" /></a> {{ story.short_text|safe }} @@ -27,7 +27,7 @@ <br clear="all" /> <hr /> <p> - Category: <a href="{% url news.views.category category=story.category.id page=1 %}">{{ story.category.title }}</a> + Category: <a href="{% url news-category slug=story.category.slug %}">{{ story.category.title }}</a> <a href="{{ story.get_absolute_url }}"><img src="{{ MEDIA_URL }}icons/link.png" alt="Story Permalink" title="Story Permalink" /></a> {% if user.is_authenticated %} @@ -41,7 +41,7 @@ <img src="{{ MEDIA_URL }}icons/tag_blue.png" alt="Tags" title="Tags" /> Tags: <ul> {% for tag in story_tags %} - <li><a href="{% url news-tag_page tag_name=tag.name page=1 %}">{{ tag.name }}</a></li> + <li><a href="{% url news-tag_page tag_name=tag.name %}">{{ tag.name }}</a></li> {% endfor %} </ul> </div>
--- a/gpp/templates/news/story_summary.html Sun Sep 12 18:30:23 2010 +0000 +++ b/gpp/templates/news/story_summary.html Wed Sep 15 00:14:54 2010 +0000 @@ -10,7 +10,7 @@ <div class="news-details"> Submitted by {{ story.submitter.username }} on {{ story.date_submitted|date:"F d, Y" }}. </div> -<a href="{% url news.views.category category=story.category.id page=1 %}"> +<a href="{% url news-category slug=story.category.slug %}"> <img src="{{ story.category.icon.url }}" alt="{{ story.category.title }}" title="{{ story.category.title }}" class="news-icon" /></a> <div class="news-content"> @@ -24,7 +24,7 @@ {% endif %} <hr /> <p> -Category: <a href="{% url news.views.category category=story.category.id page=1 %}">{{ story.category.title }}</a> +Category: <a href="{% url news-category slug=story.category.slug %}">{{ story.category.title }}</a> <img src="{{ MEDIA_URL }}icons/comments.png" alt="Comments" title="Comments" /> <a href="{{ story.get_absolute_url }}">{{ comment_count }} comment{{ comment_count|pluralize }}</a> <a href="{{ story.get_absolute_url }}"><img src="{{ MEDIA_URL }}icons/link.png" alt="Permalink" title="Permalink" /></a> @@ -39,7 +39,7 @@ <img src="{{ MEDIA_URL }}icons/tag_blue.png" alt="Tags" title="Tags" /> Tags: <ul> {% for tag in story_tags %} - <li><a href="{% url news-tag_page tag_name=tag page=1 %}">{{ tag }}</a></li> + <li><a href="{% url news-tag_page tag_name=tag %}">{{ tag }}</a></li> {% endfor %} </ul> </div>
--- a/gpp/templates/news/submit_news.html Sun Sep 12 18:30:23 2010 +0000 +++ b/gpp/templates/news/submit_news.html Wed Sep 15 00:14:54 2010 +0000 @@ -13,7 +13,7 @@ <table> {{ add_form.as_table }} <tr><td> </td><td><input type="submit" value="Submit" /> - <a href="{% url news-index_page page=1 %}">Cancel</a></td></tr> + <a href="{% url news-index_page %}">Cancel</a></td></tr> </table> </form> {% else %}
--- a/gpp/templates/news/tag_index.html Sun Sep 12 18:30:23 2010 +0000 +++ b/gpp/templates/news/tag_index.html Wed Sep 15 00:14:54 2010 +0000 @@ -12,7 +12,7 @@ <div class="news-tag-cloud"> <ul> {% for tag in tags %} - <li><a href="{% url news-tag_page tag_name=tag.name page=1 %}"> + <li><a href="{% url news-tag_page tag_name=tag.name %}"> <font size="{{ tag.font_size }}">{{ tag.name }}</font></a></li> {% endfor %} </ul>