gremmie@1: """ gremmie@1: Views for the News application. gremmie@1: """ gremmie@1: gremmie@1: import datetime bgneal@892: from django.conf import settings gremmie@1: from django.template.loader import render_to_string gremmie@1: from django.http import HttpResponseRedirect gremmie@1: from django.contrib.auth.decorators import login_required gremmie@1: from django.shortcuts import get_object_or_404 bgneal@1028: from django.shortcuts import render bgneal@92: from django.core.paginator import InvalidPage gremmie@1: from django.core.urlresolvers import reverse gremmie@1: from django.contrib.sites.models import Site gremmie@1: from django.http import Http404 gremmie@1: gremmie@1: from tagging.models import Tag gremmie@1: from tagging.models import TaggedItem gremmie@1: gremmie@1: from core.functions import send_mail gremmie@1: from core.functions import get_full_name bgneal@241: from core.functions import get_page gremmie@1: from core.paginator import DiggPaginator gremmie@1: from news.models import Category gremmie@1: from news.models import Story gremmie@1: from news.forms import AddNewsForm gremmie@1: from news.forms import SendStoryForm bgneal@399: from news.utils import attach_extra_attrs gremmie@1: bgneal@399: bgneal@399: NEWS_PER_PAGE = 10 gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: def create_paginator(stories): bgneal@399: return DiggPaginator(stories, NEWS_PER_PAGE, body=5, tail=3, margin=3, padding=2) gremmie@1: gremmie@1: ####################################################################### gremmie@1: bgneal@240: def index(request): bgneal@399: # Defer the tags field because we are going to get all the bgneal@399: # tags out in 1 query later... bgneal@1001: stories = Story.objects.all().defer('tags').select_related( bgneal@1001: 'submitter', 'category', 'forums_topic') bgneal@399: paginator = create_paginator(stories) bgneal@240: bgneal@399: page = get_page(request.GET) bgneal@399: try: bgneal@399: the_page = paginator.page(page) bgneal@399: except InvalidPage: bgneal@399: raise Http404 gremmie@1: bgneal@399: # Go get the tags and comment counts for all these stories in bulk rather bgneal@399: # than one at a time in the template; this saves database queries bgneal@399: attach_extra_attrs(the_page.object_list) bgneal@399: bgneal@1028: return render(request, 'news/index.html', { bgneal@399: 'title': 'Main Index', bgneal@399: 'page': the_page, bgneal@1140: 'section': 'main', bgneal@1140: 'V3_DESIGN': True, bgneal@1028: }) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: def archive_index(request): bgneal@399: dates = Story.objects.dates('date_submitted', 'month', order='DESC') bgneal@1028: return render(request, 'news/archive_index.html', { bgneal@399: 'title': 'News Archive', bgneal@399: 'dates': dates, bgneal@1140: 'section': 'archives', bgneal@1140: 'V3_DESIGN': True, bgneal@1028: }) gremmie@1: gremmie@1: ####################################################################### gremmie@1: bgneal@240: def archive(request, year, month): bgneal@399: stories = Story.objects.defer('tags').filter(date_submitted__year=year, bgneal@1001: date_submitted__month=month).select_related( bgneal@1001: 'submitter', 'category', 'forums_topic') bgneal@399: paginator = create_paginator(stories) bgneal@399: page = get_page(request.GET) bgneal@399: try: bgneal@399: the_page = paginator.page(page) bgneal@399: except InvalidPage: bgneal@399: raise Http404 gremmie@1: bgneal@399: attach_extra_attrs(the_page.object_list) gremmie@1: bgneal@399: month_name = datetime.date(int(year), int(month), 1).strftime('%B') bgneal@399: bgneal@1028: return render(request, 'news/index.html', { bgneal@399: 'title': 'Archive for %s, %s' % (month_name, year), bgneal@399: 'page': the_page, bgneal@1140: 'section': 'archives', bgneal@1140: 'V3_DESIGN': True, bgneal@1028: }) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: def category_index(request): bgneal@399: categories = Category.objects.all().select_related() bgneal@399: cat_list = [] bgneal@399: for cat in categories: bgneal@399: cat_list.append((cat, cat.story_set.defer('tags')[:10])) gremmie@1: bgneal@1028: return render(request, 'news/category_index.html', { bgneal@399: 'cat_list': cat_list, bgneal@1140: 'section': 'categories', bgneal@1140: 'V3_DESIGN': True, bgneal@1028: }) gremmie@1: gremmie@1: ####################################################################### gremmie@1: bgneal@240: def category(request, slug): bgneal@399: category = get_object_or_404(Category, slug=slug) bgneal@1001: stories = Story.objects.defer('tags').filter(category=category).select_related( bgneal@1001: 'submitter', 'category', 'forums_topic') bgneal@399: paginator = create_paginator(stories) bgneal@399: page = get_page(request.GET) bgneal@399: try: bgneal@399: the_page = paginator.page(page) bgneal@399: except InvalidPage: bgneal@399: raise Http404 gremmie@1: bgneal@399: attach_extra_attrs(the_page.object_list) bgneal@399: bgneal@1028: return render(request, 'news/index.html', { bgneal@399: 'title': 'Category: ' + category.title, bgneal@399: 'page': the_page, bgneal@1140: 'section': 'categories', bgneal@1140: 'V3_DESIGN': True, bgneal@1028: }) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: def story(request, story_id): bgneal@1001: story = get_object_or_404(Story.objects.select_related( bgneal@1001: 'submitter', 'category', 'forums_topic'), pk=story_id) bgneal@1028: return render(request, 'news/story.html', { bgneal@399: 'story': story, bgneal@1140: 'V3_DESIGN': True, bgneal@1028: }) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: @login_required gremmie@1: def submit(request): bgneal@399: if request.method == "POST": bgneal@399: add_form = AddNewsForm(request.POST) bgneal@399: if add_form.is_valid(): bgneal@997: add_form.save(request.user) bgneal@998: return HttpResponseRedirect(reverse('news-submit_thanks')) bgneal@399: else: bgneal@399: add_form = AddNewsForm() gremmie@1: bgneal@1028: return render(request, 'news/submit_news.html', { bgneal@399: 'add_form': add_form, bgneal@1140: 'section': 'submit', bgneal@1140: 'V3_DESIGN': True, bgneal@1028: }) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: @login_required gremmie@1: def submit_thanks(request): bgneal@1140: return render(request, 'news/submit_news.html', { bgneal@1140: 'V3_DESIGN': True, bgneal@1140: }) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: def tags(request): bgneal@399: tags = Tag.objects.cloud_for_model(Story) bgneal@1028: return render(request, 'news/tag_index.html', { bgneal@399: 'tags': tags, bgneal@1140: 'section': 'tags', bgneal@1140: 'V3_DESIGN': True, bgneal@1028: }) gremmie@1: gremmie@1: ####################################################################### gremmie@1: bgneal@240: def tag(request, tag_name): bgneal@399: tag = get_object_or_404(Tag, name=tag_name) bgneal@399: stories = TaggedItem.objects.get_by_model( bgneal@1001: Story.objects.defer('tags').select_related( bgneal@1001: 'submitter', 'category', 'forums_topic'), tag) bgneal@399: paginator = create_paginator(stories) bgneal@399: page = get_page(request.GET) bgneal@399: try: bgneal@399: the_page = paginator.page(page) bgneal@399: except InvalidPage: bgneal@399: raise Http404 gremmie@1: bgneal@399: attach_extra_attrs(the_page.object_list) bgneal@399: bgneal@1028: return render(request, 'news/index.html', { bgneal@399: 'title': 'Stories with tag: "%s"' % tag_name, bgneal@399: 'page': the_page, bgneal@1140: 'V3_DESIGN': True, bgneal@1028: }) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: @login_required gremmie@1: def email_story(request, story_id): bgneal@399: story = get_object_or_404(Story, pk=story_id) bgneal@399: if request.method == 'POST': bgneal@399: send_form = SendStoryForm(request.POST) bgneal@399: if send_form.is_valid(): bgneal@892: site = Site.objects.get_current() bgneal@399: to_name = send_form.name() bgneal@399: to_email = send_form.email() bgneal@399: from_name = get_full_name(request.user) bgneal@892: from_email = settings.GPP_NO_REPLY_EMAIL + '@' + site.domain gremmie@1: bgneal@399: msg = render_to_string('news/send_story_email.txt', bgneal@399: { bgneal@399: 'to_name': to_name, bgneal@399: 'sender_name': from_name, bgneal@399: 'site_name' : site.name, bgneal@399: 'site_url' : site.domain, bgneal@399: 'story_title': story.title, bgneal@399: 'story_link': story.get_absolute_url(), bgneal@399: }) gremmie@1: bgneal@399: subject = 'Interesting Story at ' + site.name bgneal@892: send_mail(subject, msg, from_email, [to_email], reply_to=request.user.email) bgneal@1140: return HttpResponseRedirect(reverse('news.views.email_thanks', bgneal@1140: args=[story_id])) bgneal@399: else: bgneal@399: send_form = SendStoryForm() gremmie@1: bgneal@1028: return render(request, 'news/send_story.html', { bgneal@399: 'send_form': send_form, bgneal@399: 'story': story, bgneal@1140: 'V3_DESIGN': True, bgneal@1028: }) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: @login_required bgneal@1140: def email_thanks(request, story_id): bgneal@1140: story = get_object_or_404(Story, pk=story_id) bgneal@1140: return render(request, 'news/send_story.html', { bgneal@1140: 'story': story, bgneal@1140: 'V3_DESIGN': True, bgneal@1140: })