~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to news/views.py

  • Committer: Holger Rapp
  • Date: 2009-02-21 18:24:02 UTC
  • Revision ID: sirver@kallisto.local-20090221182402-k3tuf5c4gjwslbjf
Main Page contains now the same informations as before

Show diffs side-by-side

added added

removed removed

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