gremmie@1: """ gremmie@1: Views for the News application. gremmie@1: """ gremmie@1: gremmie@1: import datetime gremmie@1: from django.shortcuts import render_to_response gremmie@1: from django.template import RequestContext 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@92: from django.core.paginator import InvalidPage gremmie@1: from django.core.urlresolvers import reverse gremmie@1: from django.db.models import Q 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: bgneal@9: from core.html import clean_html 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 PendingStory gremmie@1: from news.models import Story gremmie@1: from news.forms import AddNewsForm gremmie@1: from news.forms import SendStoryForm gremmie@1: bgneal@205: NEWS_PER_PAGE = 5 gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: def create_paginator(stories): gremmie@1: 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@197: stories = Story.objects.all().select_related() gremmie@1: paginator = create_paginator(stories) bgneal@240: bgneal@241: page = get_page(request.GET) gremmie@1: try: bgneal@240: the_page = paginator.page(page) gremmie@1: except InvalidPage: gremmie@1: raise Http404 gremmie@1: gremmie@1: return render_to_response('news/index.html', { gremmie@1: 'title': 'Main Index', gremmie@1: 'page': the_page, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: def archive_index(request): bgneal@204: dates = Story.objects.dates('date_submitted', 'month', order='DESC') gremmie@1: return render_to_response('news/archive_index.html', { gremmie@1: 'title': 'News Archive', gremmie@1: 'dates': dates, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: ####################################################################### gremmie@1: bgneal@240: def archive(request, year, month): bgneal@204: stories = Story.objects.filter(date_submitted__year=year, date_submitted__month=month) gremmie@1: paginator = create_paginator(stories) bgneal@241: page = get_page(request.GET) gremmie@1: try: bgneal@240: the_page = paginator.page(page) gremmie@1: except InvalidPage: gremmie@1: raise Http404 gremmie@1: gremmie@1: month_name = datetime.date(int(year), int(month), 1).strftime('%B') gremmie@1: gremmie@1: return render_to_response('news/index.html', { gremmie@1: 'title': 'Archive for %s, %s' % (month_name, year), gremmie@1: 'page': the_page, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: def category_index(request): bgneal@197: categories = Category.objects.all().select_related() gremmie@1: cat_list = [] gremmie@1: for cat in categories: bgneal@197: cat_list.append((cat, cat.story_set.defer('tags')[:10])) gremmie@1: gremmie@1: return render_to_response('news/category_index.html', { gremmie@1: 'cat_list': cat_list, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: ####################################################################### gremmie@1: bgneal@240: def category(request, slug): bgneal@240: category = get_object_or_404(Category, slug=slug) gremmie@1: stories = Story.objects.filter(category=category) gremmie@1: paginator = create_paginator(stories) bgneal@241: page = get_page(request.GET) gremmie@1: try: bgneal@240: the_page = paginator.page(page) gremmie@1: except InvalidPage: gremmie@1: raise Http404 gremmie@1: gremmie@1: return render_to_response('news/index.html', { gremmie@1: 'title': 'Category: ' + category.title, gremmie@1: 'page': the_page, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: def story(request, story_id): gremmie@1: story = get_object_or_404(Story, pk=story_id) gremmie@1: return render_to_response('news/story.html', { gremmie@1: 'story': story, gremmie@1: }, gremmie@1: context_instance=RequestContext(request)) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: @login_required gremmie@1: def submit(request): gremmie@1: if request.method == "POST": gremmie@1: add_form = AddNewsForm(request.POST) gremmie@1: if add_form.is_valid(): gremmie@1: pending_story = add_form.save(commit=False) gremmie@1: pending_story.submitter = request.user gremmie@1: pending_story.short_text = clean_html(pending_story.short_text) gremmie@1: pending_story.long_text = clean_html(pending_story.long_text) gremmie@1: pending_story.save() gremmie@1: return HttpResponseRedirect(reverse('news.views.submit_thanks')) gremmie@1: else: gremmie@1: add_form = AddNewsForm() gremmie@1: gremmie@1: return render_to_response('news/submit_news.html', { gremmie@1: 'add_form': add_form, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: @login_required gremmie@1: def submit_thanks(request): gremmie@1: return render_to_response('news/submit_news.html', { gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: def tags(request): gremmie@1: tags = Tag.objects.cloud_for_model(Story) gremmie@1: return render_to_response('news/tag_index.html', { gremmie@1: 'tags': tags, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: ####################################################################### gremmie@1: bgneal@240: def tag(request, tag_name): gremmie@1: tag = get_object_or_404(Tag, name=tag_name) bgneal@197: stories = TaggedItem.objects.get_by_model(Story.objects.all().select_related(), tag) gremmie@1: paginator = create_paginator(stories) bgneal@241: page = get_page(request.GET) gremmie@1: try: bgneal@240: the_page = paginator.page(page) gremmie@1: except InvalidPage: gremmie@1: raise Http404 gremmie@1: gremmie@1: return render_to_response('news/index.html', { gremmie@1: 'title': 'Stories with tag: "%s"' % tag_name, gremmie@1: 'page': the_page, gremmie@1: }, bgneal@197: context_instance=RequestContext(request)) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: @login_required gremmie@1: def email_story(request, story_id): gremmie@1: story = get_object_or_404(Story, pk=story_id) gremmie@1: if request.method == 'POST': gremmie@1: send_form = SendStoryForm(request.POST) gremmie@1: if send_form.is_valid(): gremmie@1: to_name = send_form.name() gremmie@1: to_email = send_form.email() gremmie@1: from_name = get_full_name(request.user) gremmie@1: from_email = request.user.email gremmie@1: site = Site.objects.get_current() gremmie@1: gremmie@1: msg = render_to_string('news/send_story_email.txt', gremmie@1: { gremmie@1: 'to_name': to_name, gremmie@1: 'sender_name': from_name, gremmie@1: 'site_name' : site.name, gremmie@1: 'site_url' : site.domain, gremmie@1: 'story_title': story.title, gremmie@1: 'story_link': story.get_absolute_url(), gremmie@1: }) gremmie@1: gremmie@1: subject = 'Interesting Story at ' + site.name gremmie@1: send_mail(subject, msg, from_email, [to_email]) gremmie@1: return HttpResponseRedirect(reverse('news.views.email_thanks')) gremmie@1: else: gremmie@1: send_form = SendStoryForm() gremmie@1: gremmie@1: return render_to_response('news/send_story.html', { gremmie@1: 'send_form': send_form, gremmie@1: 'story': story, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: @login_required gremmie@1: def email_thanks(request): gremmie@1: return render_to_response('news/send_story.html', { gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: