paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - MoinDateSupport library (derived from EventAggregatorSupport) |
paul@0 | 4 | |
paul@0 | 5 | @copyright: 2008, 2009, 2010, 2011, 2012 by Paul Boddie <paul@boddie.org.uk> |
paul@0 | 6 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@0 | 7 | """ |
paul@0 | 8 | |
paul@0 | 9 | from MoinSupport import * |
paul@0 | 10 | |
paul@0 | 11 | __version__ = "0.1" |
paul@0 | 12 | |
paul@0 | 13 | # User interface functions. |
paul@0 | 14 | |
paul@0 | 15 | def getParameterDate(arg): |
paul@0 | 16 | |
paul@0 | 17 | "Interpret 'arg', recognising keywords and simple arithmetic operations." |
paul@0 | 18 | |
paul@0 | 19 | n = None |
paul@0 | 20 | |
paul@0 | 21 | if arg is None: |
paul@0 | 22 | return None |
paul@0 | 23 | |
paul@0 | 24 | elif arg.startswith("current"): |
paul@0 | 25 | date = getCurrentDate() |
paul@0 | 26 | if len(arg) > 8: |
paul@0 | 27 | n = int(arg[7:]) |
paul@0 | 28 | |
paul@0 | 29 | elif arg.startswith("yearstart"): |
paul@0 | 30 | date = Date((getCurrentYear(), 1, 1)) |
paul@0 | 31 | if len(arg) > 10: |
paul@0 | 32 | n = int(arg[9:]) |
paul@0 | 33 | |
paul@0 | 34 | elif arg.startswith("yearend"): |
paul@0 | 35 | date = Date((getCurrentYear(), 12, 31)) |
paul@0 | 36 | if len(arg) > 8: |
paul@0 | 37 | n = int(arg[7:]) |
paul@0 | 38 | |
paul@0 | 39 | else: |
paul@0 | 40 | date = getDate(arg) |
paul@0 | 41 | |
paul@0 | 42 | if n is not None: |
paul@0 | 43 | date = date.day_update(n) |
paul@0 | 44 | |
paul@0 | 45 | return date |
paul@0 | 46 | |
paul@0 | 47 | def getParameterMonth(arg): |
paul@0 | 48 | |
paul@0 | 49 | "Interpret 'arg', recognising keywords and simple arithmetic operations." |
paul@0 | 50 | |
paul@0 | 51 | n = None |
paul@0 | 52 | |
paul@0 | 53 | if arg is None: |
paul@0 | 54 | return None |
paul@0 | 55 | |
paul@0 | 56 | elif arg.startswith("current"): |
paul@0 | 57 | date = getCurrentMonth() |
paul@0 | 58 | if len(arg) > 8: |
paul@0 | 59 | n = int(arg[7:]) |
paul@0 | 60 | |
paul@0 | 61 | elif arg.startswith("yearstart"): |
paul@0 | 62 | date = Month((getCurrentYear(), 1)) |
paul@0 | 63 | if len(arg) > 10: |
paul@0 | 64 | n = int(arg[9:]) |
paul@0 | 65 | |
paul@0 | 66 | elif arg.startswith("yearend"): |
paul@0 | 67 | date = Month((getCurrentYear(), 12)) |
paul@0 | 68 | if len(arg) > 8: |
paul@0 | 69 | n = int(arg[7:]) |
paul@0 | 70 | |
paul@0 | 71 | else: |
paul@0 | 72 | date = getMonth(arg) |
paul@0 | 73 | |
paul@0 | 74 | if n is not None: |
paul@0 | 75 | date = date.month_update(n) |
paul@0 | 76 | |
paul@0 | 77 | return date |
paul@0 | 78 | |
paul@0 | 79 | def getFormDate(request, calendar_name, argname): |
paul@0 | 80 | |
paul@0 | 81 | """ |
paul@0 | 82 | Return the date from the 'request' for the calendar with the given |
paul@0 | 83 | 'calendar_name' using the parameter having the given 'argname'. |
paul@0 | 84 | """ |
paul@0 | 85 | |
paul@0 | 86 | arg = getQualifiedParameter(request, calendar_name, argname) |
paul@0 | 87 | return getParameterDate(arg) |
paul@0 | 88 | |
paul@0 | 89 | def getFormMonth(request, calendar_name, argname): |
paul@0 | 90 | |
paul@0 | 91 | """ |
paul@0 | 92 | Return the month from the 'request' for the calendar with the given |
paul@0 | 93 | 'calendar_name' using the parameter having the given 'argname'. |
paul@0 | 94 | """ |
paul@0 | 95 | |
paul@0 | 96 | arg = getQualifiedParameter(request, calendar_name, argname) |
paul@0 | 97 | return getParameterMonth(arg) |
paul@0 | 98 | |
paul@0 | 99 | def getFormDateTriple(request, yeararg, montharg, dayarg): |
paul@0 | 100 | |
paul@0 | 101 | """ |
paul@0 | 102 | Return the date from the 'request' for the calendar with the given |
paul@0 | 103 | 'calendar_name' using the parameters having the given 'yeararg', 'montharg' |
paul@0 | 104 | and 'dayarg' names. |
paul@0 | 105 | """ |
paul@0 | 106 | |
paul@0 | 107 | year = getParameter(request, yeararg) |
paul@0 | 108 | month = getParameter(request, montharg) |
paul@0 | 109 | day = getParameter(request, dayarg) |
paul@0 | 110 | if year and month and day: |
paul@0 | 111 | return Date((int(year), int(month), int(day))) |
paul@0 | 112 | else: |
paul@0 | 113 | return None |
paul@0 | 114 | |
paul@0 | 115 | def getFormMonthPair(request, yeararg, montharg): |
paul@0 | 116 | |
paul@0 | 117 | """ |
paul@0 | 118 | Return the month from the 'request' for the calendar with the given |
paul@0 | 119 | 'calendar_name' using the parameters having the given 'yeararg' and |
paul@0 | 120 | 'montharg' names. |
paul@0 | 121 | """ |
paul@0 | 122 | |
paul@0 | 123 | year = getParameter(request, yeararg) |
paul@0 | 124 | month = getParameter(request, montharg) |
paul@0 | 125 | if year and month: |
paul@0 | 126 | return Month((int(year), int(month))) |
paul@0 | 127 | else: |
paul@0 | 128 | return None |
paul@0 | 129 | |
paul@0 | 130 | def getFullDateLabel(request, date): |
paul@0 | 131 | |
paul@0 | 132 | """ |
paul@0 | 133 | Return the full month plus year label using the given 'request' and |
paul@0 | 134 | 'year_month'. |
paul@0 | 135 | """ |
paul@0 | 136 | |
paul@0 | 137 | if not date: |
paul@0 | 138 | return "" |
paul@0 | 139 | |
paul@0 | 140 | _ = request.getText |
paul@0 | 141 | year, month, day = date.as_tuple()[:3] |
paul@0 | 142 | start_weekday, number_of_days = date.month_properties() |
paul@0 | 143 | weekday = (start_weekday + day - 1) % 7 |
paul@0 | 144 | day_label = _(getDayLabel(weekday)) |
paul@0 | 145 | month_label = _(getMonthLabel(month)) |
paul@0 | 146 | return "%s %s %s %s" % (day_label, day, month_label, year) |
paul@0 | 147 | |
paul@0 | 148 | def getFullMonthLabel(request, year_month): |
paul@0 | 149 | |
paul@0 | 150 | """ |
paul@0 | 151 | Return the full month plus year label using the given 'request' and |
paul@0 | 152 | 'year_month'. |
paul@0 | 153 | """ |
paul@0 | 154 | |
paul@0 | 155 | if not year_month: |
paul@0 | 156 | return "" |
paul@0 | 157 | |
paul@0 | 158 | _ = request.getText |
paul@0 | 159 | year, month = year_month.as_tuple()[:2] |
paul@0 | 160 | month_label = _(getMonthLabel(month)) |
paul@0 | 161 | return "%s %s" % (month_label, year) |
paul@0 | 162 | |
paul@0 | 163 | # vim: tabstop=4 expandtab shiftwidth=4 |