views.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. from datetime import datetime, timedelta
  2. from django.conf import settings
  3. from xml.dom import minidom
  4. from django.template.context import RequestContext
  5. from django.views.generic import list_detail
  6. from django.shortcuts import render_to_response, get_object_or_404
  7. from django.http import Http404
  8. from django.contrib.sites.models import Site
  9. from django.db.models import Q
  10. from almanac.models import WeatherConditions
  11. from newsroom.models import Story, WebEdition, Paper, Author, Document, AttachedDocument, ArchiveItem, ArchiveSection, Editorial
  12. from darkroom.models import Photo, Gallery, Webcam
  13. from directory.models import Town
  14. from taggit.models import Tag, TaggedItem
  15. def index(request):
  16. stories=Story.objects.filter(published=True, published_on__lte=datetime.now)[:9]
  17. return render_to_response('newsroom/index.html', locals(),
  18. context_instance=RequestContext(request))
  19. def archive_index(request):
  20. archives=ArchiveItem.objects.all().order_by('-modified')
  21. return render_to_response('newsroom/archive_index.html', locals(),
  22. context_instance=RequestContext(request))
  23. def archive_year(request, year):
  24. archives=ArchiveItem.objects.filter(published_on__year=year)
  25. return render_to_response('newsroom/archive_year.html', locals(),
  26. context_instance=RequestContext(request))
  27. def archive_detail(request, year, slug):
  28. archive=get_object_or_404(ArchiveSection, slug=slug)
  29. return render_to_response('newsroom/archive_detail.html', locals(),
  30. context_instance=RequestContext(request))
  31. def author_list(request):
  32. authors=Author.objects.all()
  33. return render_to_response('newsroom/author_list.html', locals(),
  34. context_instance=RequestContext(request))
  35. def author_detail(request, type_slug, slug):
  36. author=Author.objects.get(slug=slug)
  37. stories=Story.objects.filter(published=True, authors=author)
  38. return render_to_response('newsroom/author_detail.html', locals(),
  39. context_instance=RequestContext(request))
  40. def update_view_count(request, story):
  41. '''Take a story and a request and check the user agent against an exclusion file.
  42. By default, if no exclusion file has been set we just increment the view.'''
  43. if not request.excluded_ip:
  44. story.view_count += 1
  45. story.save()
  46. def story_detail(request, year, month, day, slug):
  47. try:
  48. object=Story.objects.get(slug=slug)
  49. update_view_count(request, object)
  50. except Story.DoesNotExist:
  51. raise Http404
  52. return render_to_response('newsroom/story_detail.html', locals(),
  53. context_instance=RequestContext(request))
  54. def story_old_redirect(request, year, monthdayyear, mangled_slug):
  55. paper = mangled_slug[0:2]
  56. mangled_slug.remove(mangled_slug[0])
  57. mangled_slug.remove(mangled_slug[-1])
  58. slug = '-'.join(mangled_slug)
  59. try:
  60. object=Story.objects.get(slug=slug, published_on__year=year, published_on__month=monthdayyear[0:2])
  61. update_view_count(request, object)
  62. except Story.DoesNotExist:
  63. raise Http404
  64. return render_to_response('newsroom/story_detail.html', locals(),
  65. context_instance=RequestContext(request))
  66. def editorial_detail(request, year, month, day, slug):
  67. try:
  68. object=Editorial.objects.get(slug=slug)
  69. update_view_count(request, object)
  70. except Editorial.DoesNotExist:
  71. raise Http404
  72. return render_to_response('newsroom/editorial_detail.html', locals(),
  73. context_instance=RequestContext(request))
  74. def tag_index(request):
  75. tags = Tag.objects.all()
  76. if request.GET:
  77. for name in request.GET['filter'].split('+'):
  78. qs.append(Q(tags__name=name))
  79. import operator
  80. stories=Story.objects.filter(reduce(operator.or_, qs))
  81. return render_to_response('newsroom/tag_index.html', locals(),
  82. context_instance=RequestContext(request))
  83. def tag_detail(request, name):
  84. tag=get_object_or_404(Tag, name=name)
  85. stories=Story.objects.filter(tags__name=name)
  86. qs = []
  87. if request.GET:
  88. filter_list=request.GET['filter'].split('+')
  89. for name in filter_list:
  90. qs.append(Q(tags__name=name))
  91. import operator
  92. stories=stories.filter(reduce(operator.or_, qs))
  93. return render_to_response('newsroom/tag_detail.html', locals(),
  94. context_instance=RequestContext(request))
  95. def paper_index(request):
  96. '''
  97. This view is awful. Hardcoded site ids for the various products.
  98. Really, its just part of a scaffold built around our previous
  99. site structure for transitional reasons.
  100. '''
  101. webcams=Webcam.objects.all()
  102. edition=WebEdition.objects.published().latest()
  103. prev_edition = WebEdition.objects.published().filter(published_on__lt=edition.published_on).latest()
  104. other_editions=WebEdition.objects.filter(published_on=edition.published_on).exclude(paper=edition.paper)
  105. prev_other_editions=WebEdition.objects.filter(published_on=prev_edition.published_on).exclude(paper=edition.paper)
  106. latest_editions=WebEdition.objects.published().filter(published_on__gte=datetime.now()-timedelta(weeks=2))
  107. latest_other_editions=WebEdition.objects.filter(published_on__gte=datetime.now()-timedelta(weeks=2)).exclude(paper=edition.paper)
  108. return render_to_response('newsroom/index.html', locals(),
  109. context_instance=RequestContext(request))