annotate gpp/news/views.py @ 240:1246a4f1ab4f

For #93: fix url scheme for the news application.
author Brian Neal <bgneal@gmail.com>
date Wed, 15 Sep 2010 00:14:54 +0000
parents da46e77cd804
children 27bee3ac85e6
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 SendStoryForm
gremmie@1 31
bgneal@205 32 NEWS_PER_PAGE = 5
gremmie@1 33
gremmie@1 34 #######################################################################
gremmie@1 35
gremmie@1 36 def create_paginator(stories):
gremmie@1 37 return DiggPaginator(stories, NEWS_PER_PAGE, body=5, tail=3, margin=3, padding=2)
gremmie@1 38
gremmie@1 39 #######################################################################
gremmie@1 40
bgneal@240 41 def _get_page(qdict):
bgneal@240 42 """Attempts to retrieve the value for "page" from the given query dict and
bgneal@240 43 return it as an integer. If the key cannot be found or converted to an
bgneal@240 44 integer, 1 is returned.
bgneal@240 45 """
bgneal@240 46 n = qdict.get('page', 1)
bgneal@240 47 try:
bgneal@240 48 n = int(n)
bgneal@240 49 except ValueError:
bgneal@240 50 n = 1
bgneal@240 51 return n
bgneal@240 52
bgneal@240 53 #######################################################################
bgneal@240 54
bgneal@240 55 def index(request):
bgneal@197 56 stories = Story.objects.all().select_related()
gremmie@1 57 paginator = create_paginator(stories)
bgneal@240 58
bgneal@240 59 page = _get_page(request.GET)
gremmie@1 60 try:
bgneal@240 61 the_page = paginator.page(page)
gremmie@1 62 except InvalidPage:
gremmie@1 63 raise Http404
gremmie@1 64
gremmie@1 65 return render_to_response('news/index.html', {
gremmie@1 66 'title': 'Main Index',
gremmie@1 67 'page': the_page,
gremmie@1 68 },
gremmie@1 69 context_instance = RequestContext(request))
gremmie@1 70
gremmie@1 71 #######################################################################
gremmie@1 72
gremmie@1 73 def archive_index(request):
bgneal@204 74 dates = Story.objects.dates('date_submitted', 'month', order='DESC')
gremmie@1 75 return render_to_response('news/archive_index.html', {
gremmie@1 76 'title': 'News Archive',
gremmie@1 77 'dates': dates,
gremmie@1 78 },
gremmie@1 79 context_instance = RequestContext(request))
gremmie@1 80
gremmie@1 81 #######################################################################
gremmie@1 82
bgneal@240 83 def archive(request, year, month):
bgneal@204 84 stories = Story.objects.filter(date_submitted__year=year, date_submitted__month=month)
gremmie@1 85 paginator = create_paginator(stories)
bgneal@240 86 page = _get_page(request.GET)
gremmie@1 87 try:
bgneal@240 88 the_page = paginator.page(page)
gremmie@1 89 except InvalidPage:
gremmie@1 90 raise Http404
gremmie@1 91
gremmie@1 92 month_name = datetime.date(int(year), int(month), 1).strftime('%B')
gremmie@1 93
gremmie@1 94 return render_to_response('news/index.html', {
gremmie@1 95 'title': 'Archive for %s, %s' % (month_name, year),
gremmie@1 96 'page': the_page,
gremmie@1 97 },
gremmie@1 98 context_instance = RequestContext(request))
gremmie@1 99
gremmie@1 100 #######################################################################
gremmie@1 101
gremmie@1 102 def category_index(request):
bgneal@197 103 categories = Category.objects.all().select_related()
gremmie@1 104 cat_list = []
gremmie@1 105 for cat in categories:
bgneal@197 106 cat_list.append((cat, cat.story_set.defer('tags')[:10]))
gremmie@1 107
gremmie@1 108 return render_to_response('news/category_index.html', {
gremmie@1 109 'cat_list': cat_list,
gremmie@1 110 },
gremmie@1 111 context_instance = RequestContext(request))
gremmie@1 112
gremmie@1 113 #######################################################################
gremmie@1 114
bgneal@240 115 def category(request, slug):
bgneal@240 116 category = get_object_or_404(Category, slug=slug)
gremmie@1 117 stories = Story.objects.filter(category=category)
gremmie@1 118 paginator = create_paginator(stories)
bgneal@240 119 page = _get_page(request.GET)
gremmie@1 120 try:
bgneal@240 121 the_page = paginator.page(page)
gremmie@1 122 except InvalidPage:
gremmie@1 123 raise Http404
gremmie@1 124
gremmie@1 125 return render_to_response('news/index.html', {
gremmie@1 126 'title': 'Category: ' + category.title,
gremmie@1 127 'page': the_page,
gremmie@1 128 },
gremmie@1 129 context_instance = RequestContext(request))
gremmie@1 130
gremmie@1 131 #######################################################################
gremmie@1 132
gremmie@1 133 def story(request, story_id):
gremmie@1 134 story = get_object_or_404(Story, pk=story_id)
gremmie@1 135 return render_to_response('news/story.html', {
gremmie@1 136 'story': story,
gremmie@1 137 },
gremmie@1 138 context_instance=RequestContext(request))
gremmie@1 139
gremmie@1 140 #######################################################################
gremmie@1 141
gremmie@1 142 @login_required
gremmie@1 143 def submit(request):
gremmie@1 144 if request.method == "POST":
gremmie@1 145 add_form = AddNewsForm(request.POST)
gremmie@1 146 if add_form.is_valid():
gremmie@1 147 pending_story = add_form.save(commit=False)
gremmie@1 148 pending_story.submitter = request.user
gremmie@1 149 pending_story.short_text = clean_html(pending_story.short_text)
gremmie@1 150 pending_story.long_text = clean_html(pending_story.long_text)
gremmie@1 151 pending_story.save()
gremmie@1 152 return HttpResponseRedirect(reverse('news.views.submit_thanks'))
gremmie@1 153 else:
gremmie@1 154 add_form = AddNewsForm()
gremmie@1 155
gremmie@1 156 return render_to_response('news/submit_news.html', {
gremmie@1 157 'add_form': add_form,
gremmie@1 158 },
gremmie@1 159 context_instance = RequestContext(request))
gremmie@1 160
gremmie@1 161 #######################################################################
gremmie@1 162
gremmie@1 163 @login_required
gremmie@1 164 def submit_thanks(request):
gremmie@1 165 return render_to_response('news/submit_news.html', {
gremmie@1 166 },
gremmie@1 167 context_instance = RequestContext(request))
gremmie@1 168
gremmie@1 169 #######################################################################
gremmie@1 170
gremmie@1 171 def tags(request):
gremmie@1 172 tags = Tag.objects.cloud_for_model(Story)
gremmie@1 173 return render_to_response('news/tag_index.html', {
gremmie@1 174 'tags': tags,
gremmie@1 175 },
gremmie@1 176 context_instance = RequestContext(request))
gremmie@1 177
gremmie@1 178 #######################################################################
gremmie@1 179
bgneal@240 180 def tag(request, tag_name):
gremmie@1 181 tag = get_object_or_404(Tag, name=tag_name)
bgneal@197 182 stories = TaggedItem.objects.get_by_model(Story.objects.all().select_related(), tag)
gremmie@1 183 paginator = create_paginator(stories)
bgneal@240 184 page = _get_page(request.GET)
gremmie@1 185 try:
bgneal@240 186 the_page = paginator.page(page)
gremmie@1 187 except InvalidPage:
gremmie@1 188 raise Http404
gremmie@1 189
gremmie@1 190 return render_to_response('news/index.html', {
gremmie@1 191 'title': 'Stories with tag: "%s"' % tag_name,
gremmie@1 192 'page': the_page,
gremmie@1 193 },
bgneal@197 194 context_instance=RequestContext(request))
gremmie@1 195
gremmie@1 196 #######################################################################
gremmie@1 197
gremmie@1 198 @login_required
gremmie@1 199 def email_story(request, story_id):
gremmie@1 200 story = get_object_or_404(Story, pk=story_id)
gremmie@1 201 if request.method == 'POST':
gremmie@1 202 send_form = SendStoryForm(request.POST)
gremmie@1 203 if send_form.is_valid():
gremmie@1 204 to_name = send_form.name()
gremmie@1 205 to_email = send_form.email()
gremmie@1 206 from_name = get_full_name(request.user)
gremmie@1 207 from_email = request.user.email
gremmie@1 208 site = Site.objects.get_current()
gremmie@1 209
gremmie@1 210 msg = render_to_string('news/send_story_email.txt',
gremmie@1 211 {
gremmie@1 212 'to_name': to_name,
gremmie@1 213 'sender_name': from_name,
gremmie@1 214 'site_name' : site.name,
gremmie@1 215 'site_url' : site.domain,
gremmie@1 216 'story_title': story.title,
gremmie@1 217 'story_link': story.get_absolute_url(),
gremmie@1 218 })
gremmie@1 219
gremmie@1 220 subject = 'Interesting Story at ' + site.name
gremmie@1 221 send_mail(subject, msg, from_email, [to_email])
gremmie@1 222 return HttpResponseRedirect(reverse('news.views.email_thanks'))
gremmie@1 223 else:
gremmie@1 224 send_form = SendStoryForm()
gremmie@1 225
gremmie@1 226 return render_to_response('news/send_story.html', {
gremmie@1 227 'send_form': send_form,
gremmie@1 228 'story': story,
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_thanks(request):
gremmie@1 236 return render_to_response('news/send_story.html', {
gremmie@1 237 },
gremmie@1 238 context_instance = RequestContext(request))
gremmie@1 239