views.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import csv
  2. import pywapi
  3. from datetime import datetime, date, timedelta
  4. from dateutil.relativedelta import relativedelta
  5. from dateutil.parser import parse
  6. from django import http
  7. from django.template.loader import get_template
  8. from django.template import Context
  9. import ho.pisa as pisa
  10. import cStringIO as StringIO
  11. import cgi
  12. from django.http import HttpResponse
  13. from django.template.context import RequestContext
  14. from django.views.generic import list_detail
  15. from django.shortcuts import render_to_response
  16. from almanac.models import (
  17. Tide,
  18. TideDay,
  19. TideChart,
  20. Day,
  21. WeatherConditions,
  22. WeatherAlert,
  23. WeatherForecast,
  24. )
  25. from darkroom.models import Webcam
  26. from directory.models import Place, Point, Town
  27. def index(request):
  28. webcams = Webcam.objects.all()
  29. towns = Town.objects.filter(in_coverage=True)
  30. town_weather = []
  31. for t in towns:
  32. if t.slug == "castine" or t.slug == "stonington" or t.slug == "blue-hill":
  33. town_weather.append(pywapi.get_weather_from_yahoo(t.zipcode, "imperial"))
  34. if datetime.now().hour > 20 or datetime.now().hour < 4:
  35. moon = "moon"
  36. else:
  37. moon = ""
  38. return render_to_response(
  39. "almanac/index.html", locals(), context_instance=RequestContext(request)
  40. )
  41. def tide_index(request):
  42. charts = TideChart.objects.filter(public=True).order_by("-year")
  43. months = [
  44. "jan",
  45. "feb",
  46. "mar",
  47. "apr",
  48. "may",
  49. "jun",
  50. "jul",
  51. "sep",
  52. "oct",
  53. "nov",
  54. "dec",
  55. ]
  56. return render_to_response(
  57. "almanac/tide_index.html", locals(), context_instance=RequestContext(request)
  58. )
  59. def tide_week_detail(request, slug, year, month, day):
  60. chart = TideChart.objects.get(place__slug=slug, year__exact=year)
  61. start_date = parse(str(year) + "-" + month + "-" + str(day))
  62. end_date = start_date + timedelta(days=3)
  63. first_day_set = (
  64. TideDay.objects.filter(tide_chart__place__slug__exact=slug)
  65. .exclude(day__date__gt=end_date)
  66. .filter(day__date__gte=start_date)
  67. )
  68. second_start_date = start_date + timedelta(days=4)
  69. second_end_date = second_start_date + timedelta(days=3)
  70. second_day_set = (
  71. TideDay.objects.filter(tide_chart__place__slug__exact=slug)
  72. .exclude(day__date__gt=second_end_date)
  73. .filter(day__date__gte=second_start_date)
  74. )
  75. return render_to_response(
  76. "almanac/tide_week_detail.html",
  77. locals(),
  78. context_instance=RequestContext(request),
  79. )
  80. def tide_month_detail(request, slug, year, month):
  81. chart = TideChart.objects.get(place__slug=slug, year__exact=year)
  82. days = TideDay.objects.filter(
  83. tide_chart__place__slug__exact=slug,
  84. day__date__year=year,
  85. day__date__month=parse(month).month,
  86. )
  87. full_date = datetime(int(year), parse(month).month, 1)
  88. month = month.capitalize()
  89. today = date.today()
  90. return render_to_response(
  91. "almanac/tide_month_detail.html",
  92. locals(),
  93. context_instance=RequestContext(request),
  94. )
  95. def tide_year_detail(request, slug, year):
  96. astro_days = Day.objects.filter(chart__year__exact=year)
  97. chart = TideChart.objects.get(place__slug=slug, year__exact=year)
  98. return render_to_response(
  99. "almanac/tide_year_detail.html",
  100. locals(),
  101. context_instance=RequestContext(request),
  102. )
  103. def seasonal_guide(request, slug, year, month, day):
  104. today = datetime.today()
  105. chart = TideChart.objects.get(place__slug=slug, year__exact=year)
  106. start_date = parse(str(year) + "-" + month + "-" + str(day))
  107. end_date = start_date + relativedelta(months=+1, days=-1)
  108. days = (
  109. TideDay.objects.filter(tide_chart__place__slug=slug)
  110. .exclude(day__date__gt=end_date)
  111. .filter(day__date__gte=start_date)
  112. )
  113. n = len(days) / 2
  114. first_day_set = days[0:n]
  115. second_day_set = days[n:]
  116. # end_date=start_date+timedelta(days=13)
  117. # first_day_set=TideDay.objects.filter(tide_chart__place__slug__exact=slug).exclude(
  118. # day__date__gt=end_date).filter(
  119. # day__date__gte=start_date)
  120. # second_start_date=start_date+timedelta(days=14)
  121. # second_end_date=second_start_date+timedelta(days=13)
  122. # second_day_set=TideDay.objects.filter(tide_chart__place__slug__exact=slug).exclude(
  123. # day__date__gt=second_end_date).filter(
  124. # day__date__gte=second_start_date)
  125. noon = datetime(today.year, today.month, today.day, 12, 0)
  126. return render_to_response(
  127. "almanac/tide_seasonal_guide.html",
  128. locals(),
  129. context_instance=RequestContext(request),
  130. )
  131. def weekly_dump(request, slug, year, month, day):
  132. response = HttpResponse(mimetype="text/csv")
  133. response["Content-Disposition"] = "attachment; filename=tides.csv"
  134. chart = TideChart.objects.get(place__slug=slug, year__exact=year)
  135. start_date = parse(str(year) + "-" + month + "-" + str(day))
  136. end_date = start_date + timedelta(days=7)
  137. days = (
  138. TideDay.objects.filter(tide_chart__place__slug=slug)
  139. .exclude(day__date__gt=end_date)
  140. .filter(day__date__gte=start_date)
  141. )
  142. writer = csv.writer(response)
  143. for tday in days.all():
  144. day = tday.day
  145. tide1 = tday.tide_1
  146. tide2 = tday.tide_2
  147. tide3 = tday.tide_3
  148. tide4 = tday.tide_4
  149. if tide4:
  150. writer.writerow(
  151. [
  152. tday.day.date,
  153. tide1,
  154. tide1.state,
  155. tide1.level,
  156. tide2,
  157. tide2.state,
  158. tide2.level,
  159. tide3,
  160. tide3.state,
  161. tide3.level,
  162. tide4,
  163. tide4.state,
  164. tide4.level,
  165. day.moonphase,
  166. day.moonphase_time,
  167. day.moonrise,
  168. day.moonset,
  169. day.sunrise,
  170. day.sunset,
  171. ]
  172. )
  173. else:
  174. writer.writerow(
  175. [
  176. tday.day.date,
  177. tide1,
  178. tide1.state,
  179. tide1.level,
  180. tide2,
  181. tide2.state,
  182. tide2.level,
  183. tide3,
  184. tide3.state,
  185. tide3.level,
  186. "",
  187. "",
  188. "",
  189. day.moonphase,
  190. day.moonphase_time,
  191. day.moonrise,
  192. day.moonset,
  193. day.sunrise,
  194. day.sunset,
  195. ]
  196. )
  197. return response
  198. def write_pdf(template_src, context_dict):
  199. template = get_template(template_src)
  200. context = Context(context_dict)
  201. html = template.render(context)
  202. result = StringIO.StringIO()
  203. pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")), result)
  204. if not pdf.err:
  205. return http.HttpResponse(result.getvalue(), mimetype="application/pdf")
  206. return http.HttpResponse("Gremlin's ate your pdf! %s" % cgi.escape(html))
  207. def seasonal_guide_pdf(request, slug, year, month, day):
  208. today = datetime.today()
  209. chart = TideChart.objects.get(place__slug=slug, year__exact=year)
  210. start_date = parse(str(year) + "-" + month + "-" + str(day))
  211. end_date = start_date + relativedelta(months=+1, days=-1)
  212. days = (
  213. TideDay.objects.filter(tide_chart__place__slug=slug)
  214. .exclude(day__date__gt=end_date)
  215. .filter(day__date__gte=start_date)
  216. )
  217. n = len(days) / 2
  218. first_day_set = days[0:n]
  219. second_day_set = days[n:]
  220. noon = datetime(today.year, today.month, today.day, 12, 0)
  221. return write_pdf(
  222. "almanac/pdf/seasonal_guide.html",
  223. {
  224. "pagesize": "letter",
  225. "today": today,
  226. "noon": noon,
  227. "chart": chart,
  228. "days": days,
  229. "first_day_set": first_day_set,
  230. "second_day_set": second_day_set,
  231. },
  232. )