annotate news/views.py @ 1067:1868467244d2

Main menu is now 12 columns across in flexbox grid.
author Brian Neal <bgneal@gmail.com>
date Thu, 31 Mar 2016 20:32:46 -0500
parents e932f2ecd4a7
children 97f92a589de7
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@1028 60 })
gremmie@1 61
gremmie@1 62 #######################################################################
gremmie@1 63
gremmie@1 64 def archive_index(request):
bgneal@399 65 dates = Story.objects.dates('date_submitted', 'month', order='DESC')
bgneal@1028 66 return render(request, 'news/archive_index.html', {
bgneal@399 67 'title': 'News Archive',
bgneal@399 68 'dates': dates,
bgneal@1028 69 })
gremmie@1 70
gremmie@1 71 #######################################################################
gremmie@1 72
bgneal@240 73 def archive(request, year, month):
bgneal@399 74 stories = Story.objects.defer('tags').filter(date_submitted__year=year,
bgneal@1001 75 date_submitted__month=month).select_related(
bgneal@1001 76 'submitter', 'category', 'forums_topic')
bgneal@399 77 paginator = create_paginator(stories)
bgneal@399 78 page = get_page(request.GET)
bgneal@399 79 try:
bgneal@399 80 the_page = paginator.page(page)
bgneal@399 81 except InvalidPage:
bgneal@399 82 raise Http404
gremmie@1 83
bgneal@399 84 attach_extra_attrs(the_page.object_list)
gremmie@1 85
bgneal@399 86 month_name = datetime.date(int(year), int(month), 1).strftime('%B')
bgneal@399 87
bgneal@1028 88 return render(request, 'news/index.html', {
bgneal@399 89 'title': 'Archive for %s, %s' % (month_name, year),
bgneal@399 90 'page': the_page,
bgneal@1028 91 })
gremmie@1 92
gremmie@1 93 #######################################################################
gremmie@1 94
gremmie@1 95 def category_index(request):
bgneal@399 96 categories = Category.objects.all().select_related()
bgneal@399 97 cat_list = []
bgneal@399 98 for cat in categories:
bgneal@399 99 cat_list.append((cat, cat.story_set.defer('tags')[:10]))
gremmie@1 100
bgneal@1028 101 return render(request, 'news/category_index.html', {
bgneal@399 102 'cat_list': cat_list,
bgneal@1028 103 })
gremmie@1 104
gremmie@1 105 #######################################################################
gremmie@1 106
bgneal@240 107 def category(request, slug):
bgneal@399 108 category = get_object_or_404(Category, slug=slug)
bgneal@1001 109 stories = Story.objects.defer('tags').filter(category=category).select_related(
bgneal@1001 110 'submitter', 'category', 'forums_topic')
bgneal@399 111 paginator = create_paginator(stories)
bgneal@399 112 page = get_page(request.GET)
bgneal@399 113 try:
bgneal@399 114 the_page = paginator.page(page)
bgneal@399 115 except InvalidPage:
bgneal@399 116 raise Http404
gremmie@1 117
bgneal@399 118 attach_extra_attrs(the_page.object_list)
bgneal@399 119
bgneal@1028 120 return render(request, 'news/index.html', {
bgneal@399 121 'title': 'Category: ' + category.title,
bgneal@399 122 'page': the_page,
bgneal@1028 123 })
gremmie@1 124
gremmie@1 125 #######################################################################
gremmie@1 126
gremmie@1 127 def story(request, story_id):
bgneal@1001 128 story = get_object_or_404(Story.objects.select_related(
bgneal@1001 129 'submitter', 'category', 'forums_topic'), pk=story_id)
bgneal@1028 130 return render(request, 'news/story.html', {
bgneal@399 131 'story': story,
bgneal@1028 132 })
gremmie@1 133
gremmie@1 134 #######################################################################
gremmie@1 135
gremmie@1 136 @login_required
gremmie@1 137 def submit(request):
bgneal@399 138 if request.method == "POST":
bgneal@399 139 add_form = AddNewsForm(request.POST)
bgneal@399 140 if add_form.is_valid():
bgneal@997 141 add_form.save(request.user)
bgneal@998 142 return HttpResponseRedirect(reverse('news-submit_thanks'))
bgneal@399 143 else:
bgneal@399 144 add_form = AddNewsForm()
gremmie@1 145
bgneal@1028 146 return render(request, 'news/submit_news.html', {
bgneal@399 147 'add_form': add_form,
bgneal@1028 148 })
gremmie@1 149
gremmie@1 150 #######################################################################
gremmie@1 151
gremmie@1 152 @login_required
gremmie@1 153 def submit_thanks(request):
bgneal@1028 154 return render(request, 'news/submit_news.html')
gremmie@1 155
gremmie@1 156 #######################################################################
gremmie@1 157
gremmie@1 158 def tags(request):
bgneal@399 159 tags = Tag.objects.cloud_for_model(Story)
bgneal@1028 160 return render(request, 'news/tag_index.html', {
bgneal@399 161 'tags': tags,
bgneal@1028 162 })
gremmie@1 163
gremmie@1 164 #######################################################################
gremmie@1 165
bgneal@240 166 def tag(request, tag_name):
bgneal@399 167 tag = get_object_or_404(Tag, name=tag_name)
bgneal@399 168 stories = TaggedItem.objects.get_by_model(
bgneal@1001 169 Story.objects.defer('tags').select_related(
bgneal@1001 170 'submitter', 'category', 'forums_topic'), tag)
bgneal@399 171 paginator = create_paginator(stories)
bgneal@399 172 page = get_page(request.GET)
bgneal@399 173 try:
bgneal@399 174 the_page = paginator.page(page)
bgneal@399 175 except InvalidPage:
bgneal@399 176 raise Http404
gremmie@1 177
bgneal@399 178 attach_extra_attrs(the_page.object_list)
bgneal@399 179
bgneal@1028 180 return render(request, 'news/index.html', {
bgneal@399 181 'title': 'Stories with tag: "%s"' % tag_name,
bgneal@399 182 'page': the_page,
bgneal@1028 183 })
gremmie@1 184
gremmie@1 185 #######################################################################
gremmie@1 186
gremmie@1 187 @login_required
gremmie@1 188 def email_story(request, story_id):
bgneal@399 189 story = get_object_or_404(Story, pk=story_id)
bgneal@399 190 if request.method == 'POST':
bgneal@399 191 send_form = SendStoryForm(request.POST)
bgneal@399 192 if send_form.is_valid():
bgneal@892 193 site = Site.objects.get_current()
bgneal@399 194 to_name = send_form.name()
bgneal@399 195 to_email = send_form.email()
bgneal@399 196 from_name = get_full_name(request.user)
bgneal@892 197 from_email = settings.GPP_NO_REPLY_EMAIL + '@' + site.domain
gremmie@1 198
bgneal@399 199 msg = render_to_string('news/send_story_email.txt',
bgneal@399 200 {
bgneal@399 201 'to_name': to_name,
bgneal@399 202 'sender_name': from_name,
bgneal@399 203 'site_name' : site.name,
bgneal@399 204 'site_url' : site.domain,
bgneal@399 205 'story_title': story.title,
bgneal@399 206 'story_link': story.get_absolute_url(),
bgneal@399 207 })
gremmie@1 208
bgneal@399 209 subject = 'Interesting Story at ' + site.name
bgneal@892 210 send_mail(subject, msg, from_email, [to_email], reply_to=request.user.email)
bgneal@399 211 return HttpResponseRedirect(reverse('news.views.email_thanks'))
bgneal@399 212 else:
bgneal@399 213 send_form = SendStoryForm()
gremmie@1 214
bgneal@1028 215 return render(request, 'news/send_story.html', {
bgneal@399 216 'send_form': send_form,
bgneal@399 217 'story': story,
bgneal@1028 218 })
gremmie@1 219
gremmie@1 220 #######################################################################
gremmie@1 221
gremmie@1 222 @login_required
gremmie@1 223 def email_thanks(request):
bgneal@1028 224 return render(request, 'news/send_story.html')