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