annotate news/views.py @ 1171:c855a88f9d78

Merge mainline into V3 dev branch
author Brian Neal <bgneal@gmail.com>
date Sun, 07 Jan 2018 12:00:24 -0600
parents 97f92a589de7
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
bgneal@892 6 from django.conf import settings
gremmie@1 7 from django.template.loader import render_to_string
gremmie@1 8 from django.http import HttpResponseRedirect
gremmie@1 9 from django.contrib.auth.decorators import login_required
gremmie@1 10 from django.shortcuts import get_object_or_404
bgneal@1028 11 from django.shortcuts import render
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
gremmie@1 20 from core.functions import send_mail
gremmie@1 21 from core.functions import get_full_name
bgneal@241 22 from core.functions import get_page
gremmie@1 23 from core.paginator import DiggPaginator
gremmie@1 24 from news.models import Category
gremmie@1 25 from news.models import Story
gremmie@1 26 from news.forms import AddNewsForm
gremmie@1 27 from news.forms import SendStoryForm
bgneal@399 28 from news.utils import attach_extra_attrs
gremmie@1 29
bgneal@399 30
bgneal@399 31 NEWS_PER_PAGE = 10
gremmie@1 32
gremmie@1 33 #######################################################################
gremmie@1 34
gremmie@1 35 def create_paginator(stories):
bgneal@399 36 return DiggPaginator(stories, NEWS_PER_PAGE, body=5, tail=3, margin=3, padding=2)
gremmie@1 37
gremmie@1 38 #######################################################################
gremmie@1 39
bgneal@240 40 def index(request):
bgneal@399 41 # Defer the tags field because we are going to get all the
bgneal@399 42 # tags out in 1 query later...
bgneal@1001 43 stories = Story.objects.all().defer('tags').select_related(
bgneal@1001 44 'submitter', 'category', 'forums_topic')
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@1028 57 return render(request, 'news/index.html', {
bgneal@399 58 'title': 'Main Index',
bgneal@399 59 'page': the_page,
bgneal@1140 60 'section': 'main',
bgneal@1140 61 'V3_DESIGN': True,
bgneal@1028 62 })
gremmie@1 63
gremmie@1 64 #######################################################################
gremmie@1 65
gremmie@1 66 def archive_index(request):
bgneal@399 67 dates = Story.objects.dates('date_submitted', 'month', order='DESC')
bgneal@1028 68 return render(request, 'news/archive_index.html', {
bgneal@399 69 'title': 'News Archive',
bgneal@399 70 'dates': dates,
bgneal@1140 71 'section': 'archives',
bgneal@1140 72 'V3_DESIGN': True,
bgneal@1028 73 })
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@1028 92 return render(request, 'news/index.html', {
bgneal@399 93 'title': 'Archive for %s, %s' % (month_name, year),
bgneal@399 94 'page': the_page,
bgneal@1140 95 'section': 'archives',
bgneal@1140 96 'V3_DESIGN': True,
bgneal@1028 97 })
gremmie@1 98
gremmie@1 99 #######################################################################
gremmie@1 100
gremmie@1 101 def category_index(request):
bgneal@399 102 categories = Category.objects.all().select_related()
bgneal@399 103 cat_list = []
bgneal@399 104 for cat in categories:
bgneal@399 105 cat_list.append((cat, cat.story_set.defer('tags')[:10]))
gremmie@1 106
bgneal@1028 107 return render(request, 'news/category_index.html', {
bgneal@399 108 'cat_list': cat_list,
bgneal@1140 109 'section': 'categories',
bgneal@1140 110 'V3_DESIGN': True,
bgneal@1028 111 })
gremmie@1 112
gremmie@1 113 #######################################################################
gremmie@1 114
bgneal@240 115 def category(request, slug):
bgneal@399 116 category = get_object_or_404(Category, slug=slug)
bgneal@1001 117 stories = Story.objects.defer('tags').filter(category=category).select_related(
bgneal@1001 118 'submitter', 'category', 'forums_topic')
bgneal@399 119 paginator = create_paginator(stories)
bgneal@399 120 page = get_page(request.GET)
bgneal@399 121 try:
bgneal@399 122 the_page = paginator.page(page)
bgneal@399 123 except InvalidPage:
bgneal@399 124 raise Http404
gremmie@1 125
bgneal@399 126 attach_extra_attrs(the_page.object_list)
bgneal@399 127
bgneal@1028 128 return render(request, 'news/index.html', {
bgneal@399 129 'title': 'Category: ' + category.title,
bgneal@399 130 'page': the_page,
bgneal@1140 131 'section': 'categories',
bgneal@1140 132 'V3_DESIGN': True,
bgneal@1028 133 })
gremmie@1 134
gremmie@1 135 #######################################################################
gremmie@1 136
gremmie@1 137 def story(request, story_id):
bgneal@1001 138 story = get_object_or_404(Story.objects.select_related(
bgneal@1001 139 'submitter', 'category', 'forums_topic'), pk=story_id)
bgneal@1028 140 return render(request, 'news/story.html', {
bgneal@399 141 'story': story,
bgneal@1140 142 'V3_DESIGN': True,
bgneal@1028 143 })
gremmie@1 144
gremmie@1 145 #######################################################################
gremmie@1 146
gremmie@1 147 @login_required
gremmie@1 148 def submit(request):
bgneal@399 149 if request.method == "POST":
bgneal@399 150 add_form = AddNewsForm(request.POST)
bgneal@399 151 if add_form.is_valid():
bgneal@997 152 add_form.save(request.user)
bgneal@998 153 return HttpResponseRedirect(reverse('news-submit_thanks'))
bgneal@399 154 else:
bgneal@399 155 add_form = AddNewsForm()
gremmie@1 156
bgneal@1028 157 return render(request, 'news/submit_news.html', {
bgneal@399 158 'add_form': add_form,
bgneal@1140 159 'section': 'submit',
bgneal@1140 160 'V3_DESIGN': True,
bgneal@1028 161 })
gremmie@1 162
gremmie@1 163 #######################################################################
gremmie@1 164
gremmie@1 165 @login_required
gremmie@1 166 def submit_thanks(request):
bgneal@1140 167 return render(request, 'news/submit_news.html', {
bgneal@1140 168 'V3_DESIGN': True,
bgneal@1140 169 })
gremmie@1 170
gremmie@1 171 #######################################################################
gremmie@1 172
gremmie@1 173 def tags(request):
bgneal@399 174 tags = Tag.objects.cloud_for_model(Story)
bgneal@1028 175 return render(request, 'news/tag_index.html', {
bgneal@399 176 'tags': tags,
bgneal@1140 177 'section': 'tags',
bgneal@1140 178 'V3_DESIGN': True,
bgneal@1028 179 })
gremmie@1 180
gremmie@1 181 #######################################################################
gremmie@1 182
bgneal@240 183 def tag(request, tag_name):
bgneal@399 184 tag = get_object_or_404(Tag, name=tag_name)
bgneal@399 185 stories = TaggedItem.objects.get_by_model(
bgneal@1001 186 Story.objects.defer('tags').select_related(
bgneal@1001 187 'submitter', 'category', 'forums_topic'), tag)
bgneal@399 188 paginator = create_paginator(stories)
bgneal@399 189 page = get_page(request.GET)
bgneal@399 190 try:
bgneal@399 191 the_page = paginator.page(page)
bgneal@399 192 except InvalidPage:
bgneal@399 193 raise Http404
gremmie@1 194
bgneal@399 195 attach_extra_attrs(the_page.object_list)
bgneal@399 196
bgneal@1028 197 return render(request, 'news/index.html', {
bgneal@399 198 'title': 'Stories with tag: "%s"' % tag_name,
bgneal@399 199 'page': the_page,
bgneal@1140 200 'V3_DESIGN': True,
bgneal@1028 201 })
gremmie@1 202
gremmie@1 203 #######################################################################
gremmie@1 204
gremmie@1 205 @login_required
gremmie@1 206 def email_story(request, story_id):
bgneal@399 207 story = get_object_or_404(Story, pk=story_id)
bgneal@399 208 if request.method == 'POST':
bgneal@399 209 send_form = SendStoryForm(request.POST)
bgneal@399 210 if send_form.is_valid():
bgneal@892 211 site = Site.objects.get_current()
bgneal@399 212 to_name = send_form.name()
bgneal@399 213 to_email = send_form.email()
bgneal@399 214 from_name = get_full_name(request.user)
bgneal@892 215 from_email = settings.GPP_NO_REPLY_EMAIL + '@' + site.domain
gremmie@1 216
bgneal@399 217 msg = render_to_string('news/send_story_email.txt',
bgneal@399 218 {
bgneal@399 219 'to_name': to_name,
bgneal@399 220 'sender_name': from_name,
bgneal@399 221 'site_name' : site.name,
bgneal@399 222 'site_url' : site.domain,
bgneal@399 223 'story_title': story.title,
bgneal@399 224 'story_link': story.get_absolute_url(),
bgneal@399 225 })
gremmie@1 226
bgneal@399 227 subject = 'Interesting Story at ' + site.name
bgneal@892 228 send_mail(subject, msg, from_email, [to_email], reply_to=request.user.email)
bgneal@1140 229 return HttpResponseRedirect(reverse('news.views.email_thanks',
bgneal@1140 230 args=[story_id]))
bgneal@399 231 else:
bgneal@399 232 send_form = SendStoryForm()
gremmie@1 233
bgneal@1028 234 return render(request, 'news/send_story.html', {
bgneal@399 235 'send_form': send_form,
bgneal@399 236 'story': story,
bgneal@1140 237 'V3_DESIGN': True,
bgneal@1028 238 })
gremmie@1 239
gremmie@1 240 #######################################################################
gremmie@1 241
gremmie@1 242 @login_required
bgneal@1140 243 def email_thanks(request, story_id):
bgneal@1140 244 story = get_object_or_404(Story, pk=story_id)
bgneal@1140 245 return render(request, 'news/send_story.html', {
bgneal@1140 246 'story': story,
bgneal@1140 247 'V3_DESIGN': True,
bgneal@1140 248 })