annotate news/views.py @ 875:d5d8e90d08b5

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