annotate news/views.py @ 1007:4c0360ae01c6

News migrations for previous commit.
author Brian Neal <bgneal@gmail.com>
date Thu, 26 Nov 2015 23:20:12 -0600
parents c6c3ba5cf6eb
children 5ba2508939f7
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
bgneal@892 6 from django.conf import settings
gremmie@1 7 from django.shortcuts import render_to_response
gremmie@1 8 from django.template import RequestContext
gremmie@1 9 from django.template.loader import render_to_string
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.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 Story
gremmie@1 28 from news.forms import AddNewsForm
gremmie@1 29 from news.forms import SendStoryForm
bgneal@399 30 from news.utils import attach_extra_attrs
gremmie@1 31
bgneal@399 32
bgneal@399 33 NEWS_PER_PAGE = 10
gremmie@1 34
gremmie@1 35 #######################################################################
gremmie@1 36
gremmie@1 37 def create_paginator(stories):
bgneal@399 38 return DiggPaginator(stories, NEWS_PER_PAGE, body=5, tail=3, margin=3, padding=2)
gremmie@1 39
gremmie@1 40 #######################################################################
gremmie@1 41
bgneal@240 42 def index(request):
bgneal@399 43 # Defer the tags field because we are going to get all the
bgneal@399 44 # tags out in 1 query later...
bgneal@1001 45 stories = Story.objects.all().defer('tags').select_related(
bgneal@1001 46 'submitter', 'category', 'forums_topic')
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@1001 79 date_submitted__month=month).select_related(
bgneal@1001 80 'submitter', 'category', 'forums_topic')
bgneal@399 81 paginator = create_paginator(stories)
bgneal@399 82 page = get_page(request.GET)
bgneal@399 83 try:
bgneal@399 84 the_page = paginator.page(page)
bgneal@399 85 except InvalidPage:
bgneal@399 86 raise Http404
gremmie@1 87
bgneal@399 88 attach_extra_attrs(the_page.object_list)
gremmie@1 89
bgneal@399 90 month_name = datetime.date(int(year), int(month), 1).strftime('%B')
bgneal@399 91
bgneal@399 92 return render_to_response('news/index.html', {
bgneal@399 93 'title': 'Archive for %s, %s' % (month_name, year),
bgneal@399 94 'page': the_page,
bgneal@399 95 },
bgneal@399 96 context_instance = RequestContext(request))
gremmie@1 97
gremmie@1 98 #######################################################################
gremmie@1 99
gremmie@1 100 def category_index(request):
bgneal@399 101 categories = Category.objects.all().select_related()
bgneal@399 102 cat_list = []
bgneal@399 103 for cat in categories:
bgneal@399 104 cat_list.append((cat, cat.story_set.defer('tags')[:10]))
gremmie@1 105
bgneal@399 106 return render_to_response('news/category_index.html', {
bgneal@399 107 'cat_list': cat_list,
bgneal@399 108 },
bgneal@399 109 context_instance = RequestContext(request))
gremmie@1 110
gremmie@1 111 #######################################################################
gremmie@1 112
bgneal@240 113 def category(request, slug):
bgneal@399 114 category = get_object_or_404(Category, slug=slug)
bgneal@1001 115 stories = Story.objects.defer('tags').filter(category=category).select_related(
bgneal@1001 116 'submitter', 'category', 'forums_topic')
bgneal@399 117 paginator = create_paginator(stories)
bgneal@399 118 page = get_page(request.GET)
bgneal@399 119 try:
bgneal@399 120 the_page = paginator.page(page)
bgneal@399 121 except InvalidPage:
bgneal@399 122 raise Http404
gremmie@1 123
bgneal@399 124 attach_extra_attrs(the_page.object_list)
bgneal@399 125
bgneal@399 126 return render_to_response('news/index.html', {
bgneal@399 127 'title': 'Category: ' + category.title,
bgneal@399 128 'page': the_page,
bgneal@399 129 },
bgneal@399 130 context_instance = RequestContext(request))
gremmie@1 131
gremmie@1 132 #######################################################################
gremmie@1 133
gremmie@1 134 def story(request, story_id):
bgneal@1001 135 story = get_object_or_404(Story.objects.select_related(
bgneal@1001 136 'submitter', 'category', 'forums_topic'), pk=story_id)
bgneal@399 137 return render_to_response('news/story.html', {
bgneal@399 138 'story': story,
bgneal@399 139 },
bgneal@399 140 context_instance=RequestContext(request))
gremmie@1 141
gremmie@1 142 #######################################################################
gremmie@1 143
gremmie@1 144 @login_required
gremmie@1 145 def submit(request):
bgneal@399 146 if request.method == "POST":
bgneal@399 147 add_form = AddNewsForm(request.POST)
bgneal@399 148 if add_form.is_valid():
bgneal@997 149 add_form.save(request.user)
bgneal@998 150 return HttpResponseRedirect(reverse('news-submit_thanks'))
bgneal@399 151 else:
bgneal@399 152 add_form = AddNewsForm()
gremmie@1 153
bgneal@399 154 return render_to_response('news/submit_news.html', {
bgneal@399 155 'add_form': add_form,
bgneal@399 156 },
bgneal@399 157 context_instance = RequestContext(request))
gremmie@1 158
gremmie@1 159 #######################################################################
gremmie@1 160
gremmie@1 161 @login_required
gremmie@1 162 def submit_thanks(request):
bgneal@399 163 return render_to_response('news/submit_news.html', {
bgneal@399 164 },
bgneal@399 165 context_instance = RequestContext(request))
gremmie@1 166
gremmie@1 167 #######################################################################
gremmie@1 168
gremmie@1 169 def tags(request):
bgneal@399 170 tags = Tag.objects.cloud_for_model(Story)
bgneal@399 171 return render_to_response('news/tag_index.html', {
bgneal@399 172 'tags': tags,
bgneal@399 173 },
bgneal@399 174 context_instance = RequestContext(request))
gremmie@1 175
gremmie@1 176 #######################################################################
gremmie@1 177
bgneal@240 178 def tag(request, tag_name):
bgneal@399 179 tag = get_object_or_404(Tag, name=tag_name)
bgneal@399 180 stories = TaggedItem.objects.get_by_model(
bgneal@1001 181 Story.objects.defer('tags').select_related(
bgneal@1001 182 'submitter', 'category', 'forums_topic'), 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@892 206 site = Site.objects.get_current()
bgneal@399 207 to_name = send_form.name()
bgneal@399 208 to_email = send_form.email()
bgneal@399 209 from_name = get_full_name(request.user)
bgneal@892 210 from_email = settings.GPP_NO_REPLY_EMAIL + '@' + site.domain
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@892 223 send_mail(subject, msg, from_email, [to_email], reply_to=request.user.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
bgneal@849 242 #######################################################################
bgneal@849 243
bgneal@849 244 def _clean_html(s):
bgneal@849 245 return clean_html(s, profile='news')