annotate gpp/news/views.py @ 501:0ac39f006eb2

For #240, tweaked the Javascript to be able to submit the form when enter is typed.
author Brian Neal <bgneal@gmail.com>
date Sat, 03 Dec 2011 03:14:13 +0000
parents 24f1230f3ee3
children
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.http import HttpResponseRedirect
gremmie@1 10 from django.contrib.auth.decorators import login_required
gremmie@1 11 from django.shortcuts import get_object_or_404
bgneal@92 12 from django.core.paginator import InvalidPage
gremmie@1 13 from django.core.urlresolvers import reverse
gremmie@1 14 from django.db.models import Q
gremmie@1 15 from django.contrib.sites.models import Site
gremmie@1 16 from django.http import Http404
gremmie@1 17
gremmie@1 18 from tagging.models import Tag
gremmie@1 19 from tagging.models import TaggedItem
gremmie@1 20
bgneal@9 21 from core.html import clean_html
gremmie@1 22 from core.functions import send_mail
gremmie@1 23 from core.functions import get_full_name
bgneal@241 24 from core.functions import get_page
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
bgneal@399 31 from news.utils import attach_extra_attrs
gremmie@1 32
bgneal@399 33
bgneal@399 34 NEWS_PER_PAGE = 10
gremmie@1 35
gremmie@1 36 #######################################################################
gremmie@1 37
gremmie@1 38 def create_paginator(stories):
bgneal@399 39 return DiggPaginator(stories, NEWS_PER_PAGE, body=5, tail=3, margin=3, padding=2)
gremmie@1 40
gremmie@1 41 #######################################################################
gremmie@1 42
bgneal@240 43 def index(request):
bgneal@399 44 # Defer the tags field because we are going to get all the
bgneal@399 45 # tags out in 1 query later...
bgneal@399 46 stories = Story.objects.all().defer('tags').select_related()
bgneal@399 47 paginator = create_paginator(stories)
bgneal@240 48
bgneal@399 49 page = get_page(request.GET)
bgneal@399 50 try:
bgneal@399 51 the_page = paginator.page(page)
bgneal@399 52 except InvalidPage:
bgneal@399 53 raise Http404
gremmie@1 54
bgneal@399 55 # Go get the tags and comment counts for all these stories in bulk rather
bgneal@399 56 # than one at a time in the template; this saves database queries
bgneal@399 57 attach_extra_attrs(the_page.object_list)
bgneal@399 58
bgneal@399 59 return render_to_response('news/index.html', {
bgneal@399 60 'title': 'Main Index',
bgneal@399 61 'page': the_page,
bgneal@399 62 },
bgneal@399 63 context_instance = RequestContext(request))
gremmie@1 64
gremmie@1 65 #######################################################################
gremmie@1 66
gremmie@1 67 def archive_index(request):
bgneal@399 68 dates = Story.objects.dates('date_submitted', 'month', order='DESC')
bgneal@399 69 return render_to_response('news/archive_index.html', {
bgneal@399 70 'title': 'News Archive',
bgneal@399 71 'dates': dates,
bgneal@399 72 },
bgneal@399 73 context_instance = RequestContext(request))
gremmie@1 74
gremmie@1 75 #######################################################################
gremmie@1 76
bgneal@240 77 def archive(request, year, month):
bgneal@399 78 stories = Story.objects.defer('tags').filter(date_submitted__year=year,
bgneal@399 79 date_submitted__month=month).select_related()
bgneal@399 80 paginator = create_paginator(stories)
bgneal@399 81 page = get_page(request.GET)
bgneal@399 82 try:
bgneal@399 83 the_page = paginator.page(page)
bgneal@399 84 except InvalidPage:
bgneal@399 85 raise Http404
gremmie@1 86
bgneal@399 87 attach_extra_attrs(the_page.object_list)
gremmie@1 88
bgneal@399 89 month_name = datetime.date(int(year), int(month), 1).strftime('%B')
bgneal@399 90
bgneal@399 91 return render_to_response('news/index.html', {
bgneal@399 92 'title': 'Archive for %s, %s' % (month_name, year),
bgneal@399 93 'page': the_page,
bgneal@399 94 },
bgneal@399 95 context_instance = RequestContext(request))
gremmie@1 96
gremmie@1 97 #######################################################################
gremmie@1 98
gremmie@1 99 def category_index(request):
bgneal@399 100 categories = Category.objects.all().select_related()
bgneal@399 101 cat_list = []
bgneal@399 102 for cat in categories:
bgneal@399 103 cat_list.append((cat, cat.story_set.defer('tags')[:10]))
gremmie@1 104
bgneal@399 105 return render_to_response('news/category_index.html', {
bgneal@399 106 'cat_list': cat_list,
bgneal@399 107 },
bgneal@399 108 context_instance = RequestContext(request))
gremmie@1 109
gremmie@1 110 #######################################################################
gremmie@1 111
bgneal@240 112 def category(request, slug):
bgneal@399 113 category = get_object_or_404(Category, slug=slug)
bgneal@399 114 stories = Story.objects.defer('tags').filter(category=category).select_related()
bgneal@399 115 paginator = create_paginator(stories)
bgneal@399 116 page = get_page(request.GET)
bgneal@399 117 try:
bgneal@399 118 the_page = paginator.page(page)
bgneal@399 119 except InvalidPage:
bgneal@399 120 raise Http404
gremmie@1 121
bgneal@399 122 attach_extra_attrs(the_page.object_list)
bgneal@399 123
bgneal@399 124 return render_to_response('news/index.html', {
bgneal@399 125 'title': 'Category: ' + category.title,
bgneal@399 126 'page': the_page,
bgneal@399 127 },
bgneal@399 128 context_instance = RequestContext(request))
gremmie@1 129
gremmie@1 130 #######################################################################
gremmie@1 131
gremmie@1 132 def story(request, story_id):
bgneal@399 133 story = get_object_or_404(Story, pk=story_id)
bgneal@399 134 return render_to_response('news/story.html', {
bgneal@399 135 'story': story,
bgneal@399 136 },
bgneal@399 137 context_instance=RequestContext(request))
gremmie@1 138
gremmie@1 139 #######################################################################
gremmie@1 140
gremmie@1 141 @login_required
gremmie@1 142 def submit(request):
bgneal@399 143 if request.method == "POST":
bgneal@399 144 add_form = AddNewsForm(request.POST)
bgneal@399 145 if add_form.is_valid():
bgneal@399 146 pending_story = add_form.save(commit=False)
bgneal@399 147 pending_story.submitter = request.user
bgneal@399 148 pending_story.short_text = clean_html(pending_story.short_text)
bgneal@399 149 pending_story.long_text = clean_html(pending_story.long_text)
bgneal@399 150 pending_story.save()
bgneal@399 151 return HttpResponseRedirect(reverse('news.views.submit_thanks'))
bgneal@399 152 else:
bgneal@399 153 add_form = AddNewsForm()
gremmie@1 154
bgneal@399 155 return render_to_response('news/submit_news.html', {
bgneal@399 156 'add_form': add_form,
bgneal@399 157 },
bgneal@399 158 context_instance = RequestContext(request))
gremmie@1 159
gremmie@1 160 #######################################################################
gremmie@1 161
gremmie@1 162 @login_required
gremmie@1 163 def submit_thanks(request):
bgneal@399 164 return render_to_response('news/submit_news.html', {
bgneal@399 165 },
bgneal@399 166 context_instance = RequestContext(request))
gremmie@1 167
gremmie@1 168 #######################################################################
gremmie@1 169
gremmie@1 170 def tags(request):
bgneal@399 171 tags = Tag.objects.cloud_for_model(Story)
bgneal@399 172 return render_to_response('news/tag_index.html', {
bgneal@399 173 'tags': tags,
bgneal@399 174 },
bgneal@399 175 context_instance = RequestContext(request))
gremmie@1 176
gremmie@1 177 #######################################################################
gremmie@1 178
bgneal@240 179 def tag(request, tag_name):
bgneal@399 180 tag = get_object_or_404(Tag, name=tag_name)
bgneal@399 181 stories = TaggedItem.objects.get_by_model(
bgneal@399 182 Story.objects.defer('tags').select_related(), tag)
bgneal@399 183 paginator = create_paginator(stories)
bgneal@399 184 page = get_page(request.GET)
bgneal@399 185 try:
bgneal@399 186 the_page = paginator.page(page)
bgneal@399 187 except InvalidPage:
bgneal@399 188 raise Http404
gremmie@1 189
bgneal@399 190 attach_extra_attrs(the_page.object_list)
bgneal@399 191
bgneal@399 192 return render_to_response('news/index.html', {
bgneal@399 193 'title': 'Stories with tag: "%s"' % tag_name,
bgneal@399 194 'page': the_page,
bgneal@399 195 },
bgneal@399 196 context_instance=RequestContext(request))
gremmie@1 197
gremmie@1 198 #######################################################################
gremmie@1 199
gremmie@1 200 @login_required
gremmie@1 201 def email_story(request, story_id):
bgneal@399 202 story = get_object_or_404(Story, pk=story_id)
bgneal@399 203 if request.method == 'POST':
bgneal@399 204 send_form = SendStoryForm(request.POST)
bgneal@399 205 if send_form.is_valid():
bgneal@399 206 to_name = send_form.name()
bgneal@399 207 to_email = send_form.email()
bgneal@399 208 from_name = get_full_name(request.user)
bgneal@399 209 from_email = request.user.email
bgneal@399 210 site = Site.objects.get_current()
gremmie@1 211
bgneal@399 212 msg = render_to_string('news/send_story_email.txt',
bgneal@399 213 {
bgneal@399 214 'to_name': to_name,
bgneal@399 215 'sender_name': from_name,
bgneal@399 216 'site_name' : site.name,
bgneal@399 217 'site_url' : site.domain,
bgneal@399 218 'story_title': story.title,
bgneal@399 219 'story_link': story.get_absolute_url(),
bgneal@399 220 })
gremmie@1 221
bgneal@399 222 subject = 'Interesting Story at ' + site.name
bgneal@399 223 send_mail(subject, msg, from_email, [to_email])
bgneal@399 224 return HttpResponseRedirect(reverse('news.views.email_thanks'))
bgneal@399 225 else:
bgneal@399 226 send_form = SendStoryForm()
gremmie@1 227
bgneal@399 228 return render_to_response('news/send_story.html', {
bgneal@399 229 'send_form': send_form,
bgneal@399 230 'story': story,
bgneal@399 231 },
bgneal@399 232 context_instance = RequestContext(request))
gremmie@1 233
gremmie@1 234 #######################################################################
gremmie@1 235
gremmie@1 236 @login_required
gremmie@1 237 def email_thanks(request):
bgneal@399 238 return render_to_response('news/send_story.html', {
bgneal@399 239 },
bgneal@399 240 context_instance = RequestContext(request))
gremmie@1 241