1
from django.shortcuts import render_to_response, get_object_or_404
2
from django.template import RequestContext
3
from django.http import Http404
4
#from django.views.generic import date_based, list_detail
5
from django.db.models import Q
6
from news.models import *
7
from django.views import generic
8
from django.views.generic.dates import DateDetailView
15
def post_list(request, page=0, **kwargs):
16
return list_detail.object_list(
18
queryset = Post.objects.published(),
22
#post_list.__doc__ = list_detail.object_list.__doc__
24
def post_archive_year(request, year, **kwargs):
25
return date_based.archive_year(
28
date_field = 'publish',
29
queryset = Post.objects.published(),
30
make_object_list = True,
33
#post_archive_year.__doc__ = date_based.archive_year.__doc__
36
def post_archive_month(request, year, month, **kwargs):
37
return date_based.archive_month(
42
date_field = 'publish',
43
queryset = Post.objects.published(),
46
#post_archive_month.__doc__ = date_based.archive_month.__doc__
49
def post_archive_day(request, year, month, day, **kwargs):
50
return date_based.archive_day(
56
date_field = 'publish',
57
queryset = Post.objects.published(),
60
#post_archive_day.__doc__ = date_based.archive_day.__doc__
63
def post_detail(request, slug, year, month, day, **kwargs):
64
return date_based.object_detail(
70
date_field = 'publish',
72
queryset = Post.objects.published(),
75
#post_detail.__doc__ = date_based.object_detail.__doc__
78
def category_list(request, template_name = 'news/category_list.html', **kwargs):
82
Template: ``news/category_list.html``
87
return list_detail.object_list(
89
queryset = Category.objects.all(),
90
template_name = template_name,
94
def category_detail(request, slug, template_name = 'news/category_detail.html', **kwargs):
98
Template: ``news/category_detail.html``
101
List of posts specific to the given category.
105
category = get_object_or_404(Category, slug__iexact=slug)
107
return list_detail.object_list(
109
queryset = category.post_set.published(),
110
extra_context = {'category': category},
111
template_name = template_name,
116
# Stop Words courtesy of http://www.dcs.gla.ac.uk/idom/ir_resources/linguistic_utils/stop_words
117
STOP_WORDS = r"""\b(a|about|above|across|after|afterwards|again|against|all|almost|alone|along|already|also|
118
although|always|am|among|amongst|amoungst|amount|an|and|another|any|anyhow|anyone|anything|anyway|anywhere|are|
119
around|as|at|back|be|became|because|become|becomes|becoming|been|before|beforehand|behind|being|below|beside|
120
besides|between|beyond|bill|both|bottom|but|by|call|can|cannot|cant|co|computer|con|could|couldnt|cry|de|describe|
121
detail|do|done|down|due|during|each|eg|eight|either|eleven|else|elsewhere|empty|enough|etc|even|ever|every|everyone|
122
everything|everywhere|except|few|fifteen|fify|fill|find|fire|first|five|for|former|formerly|forty|found|four|from|
123
front|full|further|get|give|go|had|has|hasnt|have|he|hence|her|here|hereafter|hereby|herein|hereupon|hers|herself|
124
him|himself|his|how|however|hundred|i|ie|if|in|inc|indeed|interest|into|is|it|its|itself|keep|last|latter|latterly|
125
least|less|ltd|made|many|may|me|meanwhile|might|mill|mine|more|moreover|most|mostly|move|much|must|my|myself|name|
126
namely|neither|never|nevertheless|next|nine|no|nobody|none|noone|nor|not|nothing|now|nowhere|of|off|often|on|once|
127
one|only|onto|or|other|others|otherwise|our|ours|ourselves|out|over|own|part|per|perhaps|please|put|rather|re|same|
128
see|seem|seemed|seeming|seems|serious|several|she|should|show|side|since|sincere|six|sixty|so|some|somehow|someone|
129
something|sometime|sometimes|somewhere|still|such|system|take|ten|than|that|the|their|them|themselves|then|thence|
130
there|thereafter|thereby|therefore|therein|thereupon|these|they|thick|thin|third|this|those|though|three|through|
131
throughout|thru|thus|to|together|too|top|toward|towards|twelve|twenty|two|un|under|until|up|upon|us|very|via|was|
132
we|well|were|what|whatever|when|whence|whenever|where|whereafter|whereas|whereby|wherein|whereupon|wherever|whether|
133
which|while|whither|who|whoever|whole|whom|whose|why|will|with|within|without|would|yet|you|your|yours|yourself|
137
def search(request, template_name='news/post_search.html'):
139
Search for news posts.
141
This template will allow you to setup a simple search form that will try to return results based on
142
given search strings. The queries will be put through a stop words filter to remove words like
143
'the', 'a', or 'have' to help imporve the result set.
145
Template: ``news/post_search.html``
148
List of news posts that match given search term(s).
154
stop_word_list = re.compile(STOP_WORDS, re.IGNORECASE)
155
search_term = '%s' % request.GET['q']
156
cleaned_search_term = stop_word_list.sub('', search_term)
157
cleaned_search_term = cleaned_search_term.strip()
158
if len(cleaned_search_term) != 0:
159
post_list = Post.objects.published().filter(Q(body__icontains=cleaned_search_term) | Q(tags__icontains=cleaned_search_term) | Q(categories__title__icontains=cleaned_search_term))
160
context = {'object_list': post_list, 'search_term':search_term}
162
message = 'Search term was too vague. Please try again.'
163
context = {'message':message}
164
return render_to_response(template_name, context, context_instance=RequestContext(request))