~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to news/views.py.delete

  • Committer: franku
  • Date: 2016-03-02 21:02:38 UTC
  • mto: This revision was merged to the branch mainline in revision 409.
  • Revision ID: somal@arcor.de-20160302210238-kuzuundfpu2fgiab
first partially working version with old data

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 news.models import *
 
7
from django.views import generic
 
8
from django.views.generic.dates import DateDetailView
 
9
 
 
10
import datetime
 
11
import re
 
12
 
 
13
 
 
14
    
 
15
def post_list(request, page=0, **kwargs):
 
16
    return list_detail.object_list(
 
17
        request,
 
18
        queryset = Post.objects.published(),
 
19
        page = page,
 
20
        **kwargs
 
21
    )
 
22
#post_list.__doc__ = list_detail.object_list.__doc__
 
23
 
 
24
def post_archive_year(request, year, **kwargs):
 
25
    return date_based.archive_year(
 
26
        request,
 
27
        year = year,
 
28
        date_field = 'publish',
 
29
        queryset = Post.objects.published(),
 
30
        make_object_list = True,
 
31
        **kwargs
 
32
    )
 
33
#post_archive_year.__doc__ = date_based.archive_year.__doc__
 
34
 
 
35
 
 
36
def post_archive_month(request, year, month, **kwargs):
 
37
    return date_based.archive_month(
 
38
        request,
 
39
        year = year,
 
40
        month = month,
 
41
        month_format = "%m",
 
42
        date_field = 'publish',
 
43
        queryset = Post.objects.published(),
 
44
        **kwargs
 
45
    )
 
46
#post_archive_month.__doc__ = date_based.archive_month.__doc__
 
47
 
 
48
 
 
49
def post_archive_day(request, year, month, day, **kwargs):
 
50
    return date_based.archive_day(
 
51
        request,
 
52
        year = year,
 
53
        month = month,
 
54
        month_format = "%m",
 
55
        day = day,
 
56
        date_field = 'publish',
 
57
        queryset = Post.objects.published(),
 
58
        **kwargs
 
59
    )
 
60
#post_archive_day.__doc__ = date_based.archive_day.__doc__
 
61
 
 
62
 
 
63
def post_detail(request, slug, year, month, day, **kwargs):
 
64
    return date_based.object_detail(
 
65
        request,
 
66
        year = year,
 
67
        month = month,
 
68
        month_format = "%m",
 
69
        day = day,
 
70
        date_field = 'publish',
 
71
        slug = slug,
 
72
        queryset = Post.objects.published(),
 
73
        **kwargs
 
74
    )
 
75
#post_detail.__doc__ = date_based.object_detail.__doc__
 
76
 
 
77
 
 
78
def category_list(request, template_name = 'news/category_list.html', **kwargs):
 
79
    """
 
80
    Category list
 
81
 
 
82
    Template: ``news/category_list.html``
 
83
    Context:
 
84
        object_list
 
85
            List of categories.
 
86
    """
 
87
    return list_detail.object_list(
 
88
        request,
 
89
        queryset = Category.objects.all(),
 
90
        template_name = template_name,
 
91
        **kwargs
 
92
    )
 
93
 
 
94
def category_detail(request, slug, template_name = 'news/category_detail.html', **kwargs):
 
95
    """
 
96
    Category detail
 
97
 
 
98
    Template: ``news/category_detail.html``
 
99
    Context:
 
100
        object_list
 
101
            List of posts specific to the given category.
 
102
        category
 
103
            Given category.
 
104
    """
 
105
    category = get_object_or_404(Category, slug__iexact=slug)
 
106
 
 
107
    return list_detail.object_list(
 
108
        request,
 
109
        queryset = category.post_set.published(),
 
110
        extra_context = {'category': category},
 
111
        template_name = template_name,
 
112
        **kwargs
 
113
    )
 
114
 
 
115
 
 
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|
 
134
yourselves)\b"""
 
135
 
 
136
 
 
137
def search(request, template_name='news/post_search.html'):
 
138
    """
 
139
    Search for news posts.
 
140
 
 
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.
 
144
 
 
145
    Template: ``news/post_search.html``
 
146
    Context:
 
147
        object_list
 
148
            List of news posts that match given search term(s).
 
149
        search_term
 
150
            Given search term.
 
151
    """
 
152
    context = {}
 
153
    if request.GET:
 
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}
 
161
        else:
 
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))