views.py 5.3 KB

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