From 648bee599ed9555158f930e8724d3b21b8230f01 Mon Sep 17 00:00:00 2001 From: Greg Taylor Date: Mon, 30 Jul 2007 20:30:16 +0000 Subject: [PATCH] Improvements on the news app. It's not nearly done yet, don't bother playing with it for a while unless you're really curious and want to see ugly, un-refined code :) --- apps/news/urls.py | 4 +- apps/news/views.py | 84 ++++++++++++++++++++- apps/website/views.py | 1 + webtemplates/prosimii/base.html | 4 +- webtemplates/prosimii/index.html | 2 +- webtemplates/prosimii/news/archive.html | 51 +++++++++++++ webtemplates/prosimii/news/search_form.html | 19 +++++ webtemplates/prosimii/news/show_entry.html | 8 +- 8 files changed, 160 insertions(+), 13 deletions(-) create mode 100644 webtemplates/prosimii/news/archive.html create mode 100644 webtemplates/prosimii/news/search_form.html diff --git a/apps/news/urls.py b/apps/news/urls.py index 902f9cafd..1eefa185d 100755 --- a/apps/news/urls.py +++ b/apps/news/urls.py @@ -2,5 +2,7 @@ from django.conf.urls.defaults import * urlpatterns = patterns('apps.news.views', (r'^show/(?P\d+)/$', 'show_news'), -# (r'^news/categories/list/$', 'recent_kills'), + (r'^archive/$', 'news_archive'), + (r'^search/$', 'search_form'), + (r'^search/results/$', 'search_results'), ) diff --git a/apps/news/views.py b/apps/news/views.py index 02dc65ea5..99485b4f8 100755 --- a/apps/news/views.py +++ b/apps/news/views.py @@ -3,11 +3,29 @@ # from django.shortcuts import render_to_response, get_object_or_404 from django.template import RequestContext -import django.views.generic.list_detail as list_detail +import django.views.generic.list_detail as gv_list_detail +from django.http import HttpResponseRedirect from django.contrib.auth.models import User +from django import newforms as forms from apps.news.models import NewsTopic, NewsEntry +# The sidebar text to be included as a variable on each page. There's got to +# be a better, cleaner way to include this on every page. +sidebar = """ +

This page’s menu:

+ +""" + +class SearchForm(forms.Form): + """ + Class to represent a news search form under Django's newforms. + """ + search_terms = forms.CharField(max_length=100, min_length=3, required=True) + def show_news(request, entry_id): """ Show an individual news entry. @@ -16,8 +34,70 @@ def show_news(request, entry_id): pagevars = { "page_title": "News Entry", - "news_entry": news_entry + "news_entry": news_entry, + "sidebar": sidebar } context_instance = RequestContext(request) return render_to_response('news/show_entry.html', pagevars, context_instance) + +def news_archive(request): + """ + Shows an archive of news entries. + """ + news_entries = NewsEntry.objects.all().order_by('-date_posted') + # TODO: Move this to either settings.py or the SQL configuration. + entries_per_page = 15 + + pagevars = { + "page_title": "News Archive", + "sidebar": sidebar + } + + return gv_list_detail.object_list(request, news_entries, template_name='news/archive.html', extra_context=pagevars, paginate_by=entries_per_page) + +def search_form(request): + """ + Render the news search form. + """ + debug ="" + + if request.method == 'GET': + debug = "GET" + search_form = SearchForm(request.GET) + if search_form.is_valid(): + return HttpResponseRedirect('/news/search/results/?search_terms='+ search_form.cleaned_data['search_terms']) + else: + debug = "NOTHING" + search_form = SearchForm() + + pagevars = { + "page_title": "Search News", + "search_form": search_form, + "debug": debug, + "sidebar": sidebar + } + + context_instance = RequestContext(request) + return render_to_response('news/search_form.html', pagevars, context_instance) + +def search_results(request): + """ + Shows an archive of news entries. + """ + # TODO: Move this to either settings.py or the SQL configuration. + entries_per_page = 15 + + search_form = SearchForm(request.GET) + valid_search = search_form.is_valid() + cleaned_get = search_form.cleaned_data + + news_entries = NewsEntry.objects.filter(title__contains=cleaned_get['search_terms']) + + pagevars = { + "page_title": "Search Results", + "searchtext": "search_terms="+ cleaned_get['search_terms'], + "sidebar": sidebar + } + + return gv_list_detail.object_list(request, news_entries, template_name='news/archive.html', extra_context=pagevars, paginate_by=entries_per_page) \ No newline at end of file diff --git a/apps/website/views.py b/apps/website/views.py index 4f519485a..5e21eb38c 100644 --- a/apps/website/views.py +++ b/apps/website/views.py @@ -15,6 +15,7 @@ def page_index(request): Main root page. """ # Some misc. configurable stuff. + # TODO: Move this to either SQL or settings.py based configuration. fpage_player_limit = 4 fpage_news_entries = 2 diff --git a/webtemplates/prosimii/base.html b/webtemplates/prosimii/base.html index 7f19aad17..7cafbab7d 100644 --- a/webtemplates/prosimii/base.html +++ b/webtemplates/prosimii/base.html @@ -18,7 +18,7 @@ {% block header_ext %} {% endblock %} - {{game_name}} + {{game_name}} - {{page_title}} @@ -31,7 +31,7 @@
diff --git a/webtemplates/prosimii/index.html b/webtemplates/prosimii/index.html index 10187bfa4..4e5676e71 100644 --- a/webtemplates/prosimii/index.html +++ b/webtemplates/prosimii/index.html @@ -27,7 +27,7 @@

{{entry.body|truncatewords:20}}

{% endfor %} - +

diff --git a/webtemplates/prosimii/news/archive.html b/webtemplates/prosimii/news/archive.html new file mode 100644 index 000000000..326e2db75 --- /dev/null +++ b/webtemplates/prosimii/news/archive.html @@ -0,0 +1,51 @@ +{% extends "base.html" %} + +{% block header_ext %} +{% endblock %} + +{% block sidebar %} +{{sidebar}} +{% endblock %} + +{% block content %} +

News Archive

+ +Navigation: First | +{% if has_previous %} + Prev +{% else %} + Prev +{% endif %} + +| {{page}} of {{pages}} pages | + +{% if has_next %} + Next +{% else %} + Next +{% endif %} +| Last + +{% for entry in object_list %} + {{entry.topic.name}}: {{entry.title}} +

By {{entry.author.username}} on {{entry.date_posted|time}}

+

{{entry.body|truncatewords:80}}

+{% endfor %} + +Navigation: First | +{% if has_previous %} + Prev +{% else %} + Prev +{% endif %} + +| {{page}} of {{pages}} pages | + +{% if has_next %} + Next +{% else %} + Next +{% endif %} +| Last + +{% endblock %} \ No newline at end of file diff --git a/webtemplates/prosimii/news/search_form.html b/webtemplates/prosimii/news/search_form.html new file mode 100644 index 000000000..4cb4d7417 --- /dev/null +++ b/webtemplates/prosimii/news/search_form.html @@ -0,0 +1,19 @@ +{% extends "base.html" %} + +{% block header_ext %} +{% endblock %} + +{% block sidebar %} +{{sidebar}} +{% endblock %} + +{% block content %} +

Search News

+

Enter a search term or phrase to search by. Matches will be made against + news titles and their contents.

+
+ {{search_form.as_p}} + +
+ {{debug}} +{% endblock %} \ No newline at end of file diff --git a/webtemplates/prosimii/news/show_entry.html b/webtemplates/prosimii/news/show_entry.html index 2df719a20..1d686ced6 100644 --- a/webtemplates/prosimii/news/show_entry.html +++ b/webtemplates/prosimii/news/show_entry.html @@ -4,13 +4,7 @@ {% endblock %} {% block sidebar %} -

This page’s menu:

- +{{sidebar}} {% endblock %} {% block content %}