annotate gpp/news/views.py @ 171:b1766b1bda45

Add rss news feed link on home page. Add rss news feed image and link on news home.
author Brian Neal <bgneal@gmail.com>
date Fri, 12 Feb 2010 04:24:47 +0000 (2010-02-12)
parents 62eb9cbbcffc
children 2baadae33f2e
rev   line source
gremmie@1 1 """
gremmie@1 2 Views for the News application.
gremmie@1 3 """
gremmie@1 4
gremmie@1 5 import datetime
gremmie@1 6 from django.shortcuts import render_to_response
gremmie@1 7 from django.template import RequestContext
gremmie@1 8 from django.template.loader import render_to_string
gremmie@1 9 from django.contrib import auth
gremmie@1 10 from django.http import HttpResponseRedirect
gremmie@1 11 from django.contrib.auth.decorators import login_required
gremmie@1 12 from django.shortcuts import get_object_or_404
bgneal@92 13 from django.core.paginator import InvalidPage
gremmie@1 14 from django.core.urlresolvers import reverse
gremmie@1 15 from django.db.models import Q
gremmie@1 16 from django.contrib.sites.models import Site
gremmie@1 17 from django.http import Http404
gremmie@1 18
gremmie@1 19 from tagging.models import Tag
gremmie@1 20 from tagging.models import TaggedItem
gremmie@1 21
bgneal@9 22 from core.html import clean_html
gremmie@1 23 from core.functions import send_mail
gremmie@1 24 from core.functions import get_full_name
gremmie@1 25 from core.paginator import DiggPaginator
gremmie@1 26 from news.models import Category
gremmie@1 27 from news.models import PendingStory
gremmie@1 28 from news.models import Story
gremmie@1 29 from news.forms import AddNewsForm
gremmie@1 30 from news.forms import SearchNewsForm
gremmie@1 31 from news.forms import SendStoryForm
gremmie@1 32
gremmie@1 33 NEWS_PER_PAGE = 2
gremmie@1 34
gremmie@1 35 #######################################################################
gremmie@1 36
gremmie@1 37 def create_paginator(stories):
gremmie@1 38 return DiggPaginator(stories, NEWS_PER_PAGE, body=5, tail=3, margin=3, padding=2)
gremmie@1 39
gremmie@1 40 #######################################################################
gremmie@1 41
gremmie@1 42 def index(request, page=1):
gremmie@1 43 stories = Story.objects.all()
gremmie@1 44 paginator = create_paginator(stories)
gremmie@1 45 try:
gremmie@1 46 the_page = paginator.page(int(page))
gremmie@1 47 except InvalidPage:
gremmie@1 48 raise Http404
gremmie@1 49
gremmie@1 50 return render_to_response('news/index.html', {
gremmie@1 51 'title': 'Main Index',
gremmie@1 52 'page': the_page,
gremmie@1 53 'search_form': SearchNewsForm(),
gremmie@1 54 },
gremmie@1 55 context_instance = RequestContext(request))
gremmie@1 56
gremmie@1 57 #######################################################################
gremmie@1 58
gremmie@1 59 def archive_index(request):
gremmie@1 60 dates = Story.objects.dates('date_published', 'month', order='DESC')
gremmie@1 61 return render_to_response('news/archive_index.html', {
gremmie@1 62 'title': 'News Archive',
gremmie@1 63 'dates': dates,
gremmie@1 64 'search_form': SearchNewsForm(),
gremmie@1 65 },
gremmie@1 66 context_instance = RequestContext(request))
gremmie@1 67
gremmie@1 68 #######################################################################
gremmie@1 69
gremmie@1 70 def archive(request, year, month, page=1):
gremmie@1 71 stories = Story.objects.filter(date_published__year=year, date_published__month=month)
gremmie@1 72 paginator = create_paginator(stories)
gremmie@1 73 try:
gremmie@1 74 the_page = paginator.page(int(page))
gremmie@1 75 except InvalidPage:
gremmie@1 76 raise Http404
gremmie@1 77
gremmie@1 78 month_name = datetime.date(int(year), int(month), 1).strftime('%B')
gremmie@1 79
gremmie@1 80 return render_to_response('news/index.html', {
gremmie@1 81 'title': 'Archive for %s, %s' % (month_name, year),
gremmie@1 82 'page': the_page,
gremmie@1 83 'search_form': SearchNewsForm(),
gremmie@1 84 },
gremmie@1 85 context_instance = RequestContext(request))
gremmie@1 86
gremmie@1 87 #######################################################################
gremmie@1 88
gremmie@1 89 def category_index(request):
gremmie@1 90 categories = Category.objects.all()
gremmie@1 91 cat_list = []
gremmie@1 92 for cat in categories:
gremmie@1 93 cat_list.append((cat, cat.story_set.all()[:10]))
gremmie@1 94
gremmie@1 95 return render_to_response('news/category_index.html', {
gremmie@1 96 'cat_list': cat_list,
gremmie@1 97 'search_form': SearchNewsForm(),
gremmie@1 98 },
gremmie@1 99 context_instance = RequestContext(request))
gremmie@1 100
gremmie@1 101 #######################################################################
gremmie@1 102
gremmie@1 103 def category(request, category, page=1):
gremmie@1 104 category = get_object_or_404(Category, pk=category)
gremmie@1 105 stories = Story.objects.filter(category=category)
gremmie@1 106 paginator = create_paginator(stories)
gremmie@1 107 try:
gremmie@1 108 the_page = paginator.page(int(page))
gremmie@1 109 except InvalidPage:
gremmie@1 110 raise Http404
gremmie@1 111
gremmie@1 112 return render_to_response('news/index.html', {
gremmie@1 113 'title': 'Category: ' + category.title,
gremmie@1 114 'page': the_page,
gremmie@1 115 'search_form': SearchNewsForm(),
gremmie@1 116 },
gremmie@1 117 context_instance = RequestContext(request))
gremmie@1 118
gremmie@1 119 #######################################################################
gremmie@1 120
gremmie@1 121 def search(request, page=1):
gremmie@1 122 if request.method == 'POST':
gremmie@1 123 form = SearchNewsForm(request.POST)
gremmie@1 124 if form.is_valid():
gremmie@1 125 query_text = form.get_query()
gremmie@1 126 category = form.get_category()
gremmie@1 127 page = 1
gremmie@1 128 else:
gremmie@1 129 return HttpResponseRedirect(reverse('news.views.index'))
gremmie@1 130 else:
gremmie@1 131 if 'query' in request.GET:
gremmie@1 132 query_text = request.GET['query']
gremmie@1 133 category = request.GET.get('category', None)
gremmie@1 134 else:
gremmie@1 135 return HttpResponseRedirect(reverse('news.views.index'))
gremmie@1 136
gremmie@1 137 if category is not None:
gremmie@1 138 stories = Story.objects.filter(category=category)
gremmie@1 139 cat_qual = ' in category "%s"' % category.title
gremmie@1 140 else:
gremmie@1 141 stories = Story.objects.all()
gremmie@1 142 cat_qual = ''
gremmie@1 143
gremmie@1 144 stories = stories.filter(
gremmie@1 145 Q(title__icontains=query_text) |
gremmie@1 146 Q(short_text__icontains=query_text) |
gremmie@1 147 Q(long_text__icontains=query_text)).order_by('-date_published')
gremmie@1 148
gremmie@1 149 paginator = create_paginator(stories)
gremmie@1 150 try:
gremmie@1 151 the_page = paginator.page(int(page))
gremmie@1 152 except InvalidPage:
gremmie@1 153 raise Http404
gremmie@1 154
gremmie@1 155 return render_to_response('news/index.html', {
gremmie@1 156 'title': 'Search Results for "%s"%s' % (query_text, cat_qual),
gremmie@1 157 'query': query_text,
gremmie@1 158 'page': the_page,
gremmie@1 159 'search_form': SearchNewsForm(),
gremmie@1 160 },
gremmie@1 161 context_instance = RequestContext(request))
gremmie@1 162
gremmie@1 163 #######################################################################
gremmie@1 164
gremmie@1 165 def story(request, story_id):
gremmie@1 166 story = get_object_or_404(Story, pk=story_id)
gremmie@1 167 return render_to_response('news/story.html', {
gremmie@1 168 'story': story,
gremmie@1 169 'search_form': SearchNewsForm(),
gremmie@1 170 },
gremmie@1 171 context_instance=RequestContext(request))
gremmie@1 172
gremmie@1 173 #######################################################################
gremmie@1 174
gremmie@1 175 @login_required
gremmie@1 176 def submit(request):
gremmie@1 177 if request.method == "POST":
gremmie@1 178 add_form = AddNewsForm(request.POST)
gremmie@1 179 if add_form.is_valid():
gremmie@1 180 pending_story = add_form.save(commit=False)
gremmie@1 181 pending_story.submitter = request.user
gremmie@1 182 pending_story.short_text = clean_html(pending_story.short_text)
gremmie@1 183 pending_story.long_text = clean_html(pending_story.long_text)
gremmie@1 184 pending_story.save()
gremmie@1 185 return HttpResponseRedirect(reverse('news.views.submit_thanks'))
gremmie@1 186 else:
gremmie@1 187 add_form = AddNewsForm()
gremmie@1 188
gremmie@1 189 return render_to_response('news/submit_news.html', {
gremmie@1 190 'add_form': add_form,
gremmie@1 191 'search_form': SearchNewsForm(),
gremmie@1 192 },
gremmie@1 193 context_instance = RequestContext(request))
gremmie@1 194
gremmie@1 195 #######################################################################
gremmie@1 196
gremmie@1 197 @login_required
gremmie@1 198 def submit_thanks(request):
gremmie@1 199 return render_to_response('news/submit_news.html', {
gremmie@1 200 'search_form': SearchNewsForm(),
gremmie@1 201 },
gremmie@1 202 context_instance = RequestContext(request))
gremmie@1 203
gremmie@1 204 #######################################################################
gremmie@1 205
gremmie@1 206 def tags(request):
gremmie@1 207 tags = Tag.objects.cloud_for_model(Story)
gremmie@1 208 return render_to_response('news/tag_index.html', {
gremmie@1 209 'tags': tags,
gremmie@1 210 'search_form': SearchNewsForm(),
gremmie@1 211 },
gremmie@1 212 context_instance = RequestContext(request))
gremmie@1 213
gremmie@1 214 #######################################################################
gremmie@1 215
gremmie@1 216 def tag(request, tag_name, page=1):
gremmie@1 217 tag = get_object_or_404(Tag, name=tag_name)
gremmie@1 218 stories = TaggedItem.objects.get_by_model(Story, tag)
gremmie@1 219 paginator = create_paginator(stories)
gremmie@1 220 try:
gremmie@1 221 the_page = paginator.page(int(page))
gremmie@1 222 except InvalidPage:
gremmie@1 223 raise Http404
gremmie@1 224
gremmie@1 225 return render_to_response('news/index.html', {
gremmie@1 226 'title': 'Stories with tag: "%s"' % tag_name,
gremmie@1 227 'page': the_page,
gremmie@1 228 'search_form': SearchNewsForm(),
gremmie@1 229 },
gremmie@1 230 context_instance = RequestContext(request))
gremmie@1 231
gremmie@1 232 #######################################################################
gremmie@1 233
gremmie@1 234 @login_required
gremmie@1 235 def email_story(request, story_id):
gremmie@1 236 story = get_object_or_404(Story, pk=story_id)
gremmie@1 237 if request.method == 'POST':
gremmie@1 238 send_form = SendStoryForm(request.POST)
gremmie@1 239 if send_form.is_valid():
gremmie@1 240 to_name = send_form.name()
gremmie@1 241 to_email = send_form.email()
gremmie@1 242 from_name = get_full_name(request.user)
gremmie@1 243 from_email = request.user.email
gremmie@1 244 site = Site.objects.get_current()
gremmie@1 245
gremmie@1 246 msg = render_to_string('news/send_story_email.txt',
gremmie@1 247 {
gremmie@1 248 'to_name': to_name,
gremmie@1 249 'sender_name': from_name,
gremmie@1 250 'site_name' : site.name,
gremmie@1 251 'site_url' : site.domain,
gremmie@1 252 'story_title': story.title,
gremmie@1 253 'story_link': story.get_absolute_url(),
gremmie@1 254 })
gremmie@1 255
gremmie@1 256 subject = 'Interesting Story at ' + site.name
gremmie@1 257 send_mail(subject, msg, from_email, [to_email])
gremmie@1 258 return HttpResponseRedirect(reverse('news.views.email_thanks'))
gremmie@1 259 else:
gremmie@1 260 send_form = SendStoryForm()
gremmie@1 261
gremmie@1 262 return render_to_response('news/send_story.html', {
gremmie@1 263 'send_form': send_form,
gremmie@1 264 'story': story,
gremmie@1 265 },
gremmie@1 266 context_instance = RequestContext(request))
gremmie@1 267
gremmie@1 268 #######################################################################
gremmie@1 269
gremmie@1 270 @login_required
gremmie@1 271 def email_thanks(request):
gremmie@1 272 return render_to_response('news/send_story.html', {
gremmie@1 273 },
gremmie@1 274 context_instance = RequestContext(request))
gremmie@1 275