paul@282 | 1 | # -*- coding: iso-8859-1 -*- |
paul@282 | 2 | """ |
paul@282 | 3 | MoinMoin - EventAggregatorUpdate Action |
paul@282 | 4 | |
paul@399 | 5 | @copyright: 2012, 2013, 2014 by Paul Boddie <paul@boddie.org.uk> |
paul@282 | 6 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@282 | 7 | """ |
paul@282 | 8 | |
paul@352 | 9 | from EventAggregatorSupport.Actions import get_date_functions |
paul@347 | 10 | from EventAggregatorSupport import * |
paul@352 | 11 | from MoinDateSupport import getParameterMonth |
paul@400 | 12 | from MoinSupport import getPageCacheKey, enforcePageCacheLimit |
paul@399 | 13 | from MoinMoin.Page import Page, is_cache_exception |
paul@399 | 14 | from MoinMoin import caching, config, log |
paul@282 | 15 | |
paul@399 | 16 | logging = log.getLogger(__name__) |
paul@282 | 17 | |
paul@282 | 18 | # Action function. |
paul@282 | 19 | |
paul@282 | 20 | def execute(pagename, request): |
paul@282 | 21 | |
paul@282 | 22 | """ |
paul@282 | 23 | On the given 'pagename', for the given 'request', write a page fragment |
paul@282 | 24 | producing the rendered calendar information for inclusion in an existing Web |
paul@282 | 25 | page. Since the page is not processed, all necessary parameters need to be |
paul@282 | 26 | supplied in the request. |
paul@282 | 27 | """ |
paul@282 | 28 | |
paul@282 | 29 | form = get_form(request) |
paul@282 | 30 | page = Page(request, pagename) |
paul@282 | 31 | |
paul@399 | 32 | # Attempt to get any previously cached view. |
paul@399 | 33 | |
paul@400 | 34 | key = getPageCacheKey(page, request) |
paul@399 | 35 | cache = caching.CacheEntry(request, page, key, scope='item') |
paul@399 | 36 | |
paul@399 | 37 | if cache.exists(): |
paul@399 | 38 | send(request, cache.content()) |
paul@399 | 39 | return |
paul@399 | 40 | |
paul@282 | 41 | # Find settings from the request parameters only. |
paul@282 | 42 | |
paul@282 | 43 | calendar_name = form.get("calendar", [None])[0] |
paul@282 | 44 | category_names = form.get("category", []) |
paul@330 | 45 | search_pattern = form.get("search", [None])[0] |
paul@282 | 46 | remote_sources = form.get("source", []) |
paul@282 | 47 | name_usage = getParameter(request, "names", "weekly") |
paul@282 | 48 | template_name = getParameter(request, "template") |
paul@282 | 49 | parent_name = getParameter(request, "parent") |
paul@282 | 50 | mode = getParameter(request, "mode", "calendar") |
paul@352 | 51 | raw_resolution = getParameter(request, "raw-resolution") |
paul@282 | 52 | resolution = getParameter(request, "resolution", mode == "day" and "date" or "month") |
paul@282 | 53 | map_name = getParameter(request, "map") |
paul@282 | 54 | |
paul@282 | 55 | # The underlying dimensions of the calendar are supplied in special |
paul@282 | 56 | # parameters. |
paul@282 | 57 | |
paul@282 | 58 | raw_calendar_start = getParameter(request, "calendarstart") |
paul@282 | 59 | raw_calendar_end = getParameter(request, "calendarend") |
paul@282 | 60 | |
paul@282 | 61 | # Different modes require different levels of precision by default. |
paul@282 | 62 | |
paul@282 | 63 | resolution = mode == "calendar" and "month" or resolution |
paul@282 | 64 | |
paul@352 | 65 | # Determine the limits of the calendar. |
paul@282 | 66 | |
paul@352 | 67 | get_date, _get_form_date = get_date_functions(raw_resolution) |
paul@282 | 68 | |
paul@282 | 69 | original_calendar_start = calendar_start = get_date(raw_calendar_start) |
paul@282 | 70 | original_calendar_end = calendar_end = get_date(raw_calendar_end) |
paul@282 | 71 | |
paul@352 | 72 | wider_calendar_start = getParameterMonth(getParameter(request, "wider-start")) |
paul@352 | 73 | wider_calendar_end = getParameterMonth(getParameter(request, "wider-end")) |
paul@352 | 74 | |
paul@352 | 75 | get_date, _get_form_date = get_date_functions(resolution) |
paul@352 | 76 | |
paul@282 | 77 | calendar_start = get_date(getParameter(request, "start")) or calendar_start |
paul@282 | 78 | calendar_end = get_date(getParameter(request, "end")) or calendar_end |
paul@282 | 79 | |
paul@282 | 80 | # Get the events according to the resolution of the calendar. |
paul@282 | 81 | |
paul@330 | 82 | all_shown_events, first, last = getEventsUsingParameters( |
paul@330 | 83 | category_names, search_pattern, remote_sources, calendar_start, calendar_end, |
paul@330 | 84 | resolution, request) |
paul@282 | 85 | |
paul@282 | 86 | # Define a view of the calendar, retaining useful navigational information. |
paul@282 | 87 | |
paul@339 | 88 | view = View(page, calendar_name, |
paul@339 | 89 | raw_calendar_start, raw_calendar_end, |
paul@339 | 90 | original_calendar_start, original_calendar_end, |
paul@339 | 91 | calendar_start, calendar_end, |
paul@339 | 92 | wider_calendar_start, wider_calendar_end, |
paul@330 | 93 | first, last, category_names, remote_sources, search_pattern, template_name, |
paul@352 | 94 | parent_name, mode, raw_resolution, resolution, name_usage, map_name) |
paul@282 | 95 | |
paul@399 | 96 | output = view.render(all_shown_events) |
paul@399 | 97 | |
paul@399 | 98 | # Attempt to cache the output and then send it. |
paul@399 | 99 | |
paul@399 | 100 | try: |
paul@400 | 101 | enforcePageCacheLimit(page, request) |
paul@399 | 102 | cache.update(output.encode(config.charset)) |
paul@399 | 103 | except caching.CacheError, exc: |
paul@399 | 104 | logging.warning("Could not cache output for EventAggregatorUpdate in page %s: %s" % (pagename, str(exc))) |
paul@399 | 105 | |
paul@399 | 106 | send(request, output) |
paul@399 | 107 | |
paul@399 | 108 | def send(request, output): |
paul@399 | 109 | |
paul@399 | 110 | "Send via the 'request' the given 'output' as a response." |
paul@399 | 111 | |
paul@282 | 112 | send_headers = get_send_headers(request) |
paul@282 | 113 | send_headers(["Content-Type: text/html; charset=%s" % config.charset]) |
paul@399 | 114 | request.write(output) |
paul@282 | 115 | |
paul@282 | 116 | # vim: tabstop=4 expandtab shiftwidth=4 |