# HG changeset patch # User Brian Neal # Date 1284509694 0 # Node ID 1246a4f1ab4f016d56452f43eda68689ad0a7900 # Parent dcc929973bba0ac4005dfb3ee134ed3fcd768b77 For #93: fix url scheme for the news application. diff -r dcc929973bba -r 1246a4f1ab4f gpp/news/admin.py --- 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) diff -r dcc929973bba -r 1246a4f1ab4f gpp/news/fixtures/news_categories.json --- 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" } } diff -r dcc929973bba -r 1246a4f1ab4f gpp/news/forms.py --- 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) diff -r dcc929973bba -r 1246a4f1ab4f gpp/news/models.py --- 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): diff -r dcc929973bba -r 1246a4f1ab4f gpp/news/urls.py --- 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\d+)/$', 'index', name='news-index_page'), - url(r'^archive/$', 'archive_index', name='news-archive_index'), - url(r'^archive/(?P\d{4})/(?P\d\d?)/page/(?P\d+)/$', - 'archive', - name='news-archive_page'), - (r'^categories/$', 'category_index'), - (r'^category/(?P\d+)/page/(?P\d+)/$', 'category'), - (r'^email/(\d+)/$', 'email_story'), - (r'^email/thanks/$', 'email_thanks'), - url(r'^search/page/(?P\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[^/]+)/page/(?P\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\d{4})/(?P\d\d?)/$', + 'archive', + name='news-archive_page'), + url(r'^categories/$', 'category_index', name='news-category_index'), + url(r'^category/(?P[\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='news-tag_page'), ) - -urlpatterns += patterns('django.views.generic.simple', - (r'^$', 'redirect_to', {'url': 'page/1/'}), -) diff -r dcc929973bba -r 1246a4f1ab4f gpp/news/views.py --- 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)) diff -r dcc929973bba -r 1246a4f1ab4f gpp/templates/base.html --- 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 @@

- + {{ story.category.title }} {{ story.short_text|safe }} @@ -27,7 +27,7 @@

- Category: {{ story.category.title }} + Category: {{ story.category.title }} Story Permalink {% if user.is_authenticated %} @@ -41,7 +41,7 @@ Tags Tags:

diff -r dcc929973bba -r 1246a4f1ab4f gpp/templates/news/story_summary.html --- 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 @@
Submitted by {{ story.submitter.username }} on {{ story.date_submitted|date:"F d, Y" }}.
- + {{ story.category.title }}
@@ -24,7 +24,7 @@ {% endif %}

-Category: {{ story.category.title }} +Category: {{ story.category.title }} Comments {{ comment_count }} comment{{ comment_count|pluralize }} Permalink @@ -39,7 +39,7 @@ Tags Tags:

diff -r dcc929973bba -r 1246a4f1ab4f gpp/templates/news/submit_news.html --- 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 @@ {{ add_form.as_table }} +  Cancel
  -  Cancel
{% else %} diff -r dcc929973bba -r 1246a4f1ab4f gpp/templates/news/tag_index.html --- 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 @@