views.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 (
  12. Story,
  13. WebEdition,
  14. Paper,
  15. Author,
  16. Document,
  17. AttachedDocument,
  18. ArchiveItem,
  19. ArchiveSection,
  20. Editorial,
  21. )
  22. from darkroom.models import Photo, Gallery, Webcam
  23. from directory.models import Town
  24. from taggit.models import Tag, TaggedItem
  25. def index(request):
  26. stories = Story.objects.filter(published=True, published_on__lte=datetime.now)[:9]
  27. return render_to_response(
  28. "newsroom/index.html", locals(), context_instance=RequestContext(request)
  29. )
  30. def archive_index(request):
  31. archives = ArchiveItem.objects.all().order_by("-modified")
  32. return render_to_response(
  33. "newsroom/archive_index.html",
  34. locals(),
  35. context_instance=RequestContext(request),
  36. )
  37. def archive_year(request, year):
  38. archives = ArchiveItem.objects.filter(published_on__year=year)
  39. return render_to_response(
  40. "newsroom/archive_year.html", locals(), context_instance=RequestContext(request)
  41. )
  42. def archive_detail(request, year, slug):
  43. archive = get_object_or_404(ArchiveSection, slug=slug)
  44. return render_to_response(
  45. "newsroom/archive_detail.html",
  46. locals(),
  47. context_instance=RequestContext(request),
  48. )
  49. def author_list(request):
  50. authors = Author.objects.all()
  51. return render_to_response(
  52. "newsroom/author_list.html", locals(), context_instance=RequestContext(request)
  53. )
  54. def author_detail(request, type_slug, slug):
  55. author = Author.objects.get(slug=slug)
  56. stories = Story.objects.filter(published=True, authors=author)
  57. return render_to_response(
  58. "newsroom/author_detail.html",
  59. locals(),
  60. context_instance=RequestContext(request),
  61. )
  62. def update_view_count(request, story):
  63. """Take a story and a request and check the user agent against an exclusion file.
  64. By default, if no exclusion file has been set we just increment the view."""
  65. if not request.excluded_ip:
  66. story.view_count += 1
  67. story.save()
  68. def story_detail(request, year, month, day, slug):
  69. try:
  70. object = Story.objects.get(slug=slug)
  71. update_view_count(request, object)
  72. except Story.DoesNotExist:
  73. raise Http404
  74. return render_to_response(
  75. "newsroom/story_detail.html", locals(), context_instance=RequestContext(request)
  76. )
  77. def story_old_redirect(request, year, monthdayyear, mangled_slug):
  78. paper = mangled_slug[0:2]
  79. mangled_slug.remove(mangled_slug[0])
  80. mangled_slug.remove(mangled_slug[-1])
  81. slug = "-".join(mangled_slug)
  82. try:
  83. object = Story.objects.get(
  84. slug=slug, published_on__year=year, published_on__month=monthdayyear[0:2]
  85. )
  86. update_view_count(request, object)
  87. except Story.DoesNotExist:
  88. raise Http404
  89. return render_to_response(
  90. "newsroom/story_detail.html", locals(), context_instance=RequestContext(request)
  91. )
  92. def editorial_detail(request, year, month, day, slug):
  93. try:
  94. object = Editorial.objects.get(slug=slug)
  95. update_view_count(request, object)
  96. except Editorial.DoesNotExist:
  97. raise Http404
  98. return render_to_response(
  99. "newsroom/editorial_detail.html",
  100. locals(),
  101. context_instance=RequestContext(request),
  102. )
  103. def tag_index(request):
  104. tags = Tag.objects.all()
  105. if request.GET:
  106. for name in request.GET["filter"].split("+"):
  107. qs.append(Q(tags__name=name))
  108. import operator
  109. stories = Story.objects.filter(reduce(operator.or_, qs))
  110. return render_to_response(
  111. "newsroom/tag_index.html", locals(), context_instance=RequestContext(request)
  112. )
  113. def tag_detail(request, name):
  114. tag = get_object_or_404(Tag, name=name)
  115. stories = Story.objects.filter(tags__name=name)
  116. qs = []
  117. if request.GET:
  118. filter_list = request.GET["filter"].split("+")
  119. for name in filter_list:
  120. qs.append(Q(tags__name=name))
  121. import operator
  122. stories = stories.filter(reduce(operator.or_, qs))
  123. return render_to_response(
  124. "newsroom/tag_detail.html", locals(), context_instance=RequestContext(request)
  125. )
  126. def paper_index(request):
  127. """
  128. This view is awful. Hardcoded site ids for the various products.
  129. Really, its just part of a scaffold built around our previous
  130. site structure for transitional reasons.
  131. """
  132. webcams = Webcam.objects.all()
  133. edition = WebEdition.objects.published().latest()
  134. prev_edition = (
  135. WebEdition.objects.published()
  136. .filter(published_on__lt=edition.published_on)
  137. .latest()
  138. )
  139. other_editions = WebEdition.objects.filter(
  140. published_on=edition.published_on
  141. ).exclude(paper=edition.paper)
  142. prev_other_editions = WebEdition.objects.filter(
  143. published_on=prev_edition.published_on
  144. ).exclude(paper=edition.paper)
  145. latest_editions = WebEdition.objects.published().filter(
  146. published_on__gte=datetime.now() - timedelta(weeks=2)
  147. )
  148. latest_other_editions = WebEdition.objects.filter(
  149. published_on__gte=datetime.now() - timedelta(weeks=2)
  150. ).exclude(paper=edition.paper)
  151. return render_to_response(
  152. "newsroom/index.html", locals(), context_instance=RequestContext(request)
  153. )