def tandem_choose_date(request, year=datetime.now().year, month=datetime.now().month, template_name='signup/tandem_choose_date.html'):
month, year = int(month), int(year)
c = calendar.Calendar()
c.setfirstweekday(calendar.SUNDAY)
cal = c.monthdatescalendar(year, month)
_events = TandemDay.objects.filter(date__range=(cal[0][0], cal[len(cal) - 1][6]))
events = {}
for e in _events:
if e.date.month not in events:
events[e.date.month] = {}
if e.date.day not in events[e.date.month]:
events[e.date.month][e.date.day] = []
events[e.date.month][e.date.day].append(e)
print(events)
data = {}
data['title'] = "Tandem Sign Up"
data['month_name'] = calendar.month_name[month]
data['year'] = year
data['month'] = month
data['previous'] = {'month': (month - 1 + 11) % 12 + 1, 'year': year - (1 if month == 1 else 0)}
data['next'] = {'month': (month - 1 + 1) % 12 + 1, 'year': year + (1 if month == 12 else 0)}
data['calendar'] = cal
data['events'] = events
data['nofollow'] = (True if year < 2017 or year > datetime.now().year + 2 else False)
return render(request, template_name, data)
python类Calendar()的实例源码
def test_itermonthdates(self):
# ensure itermonthdates doesn't overflow after datetime.MAXYEAR
# see #15421
list(calendar.Calendar().itermonthdates(datetime.MAXYEAR, 12))
def test_yeardatescalendar(self):
def shrink(cal):
return [[[' '.join('{:02d}/{:02d}/{}'.format(
d.month, d.day, str(d.year)[-2:]) for d in z)
for z in y] for y in x] for x in cal]
self.assertEqual(
shrink(calendar.Calendar().yeardatescalendar(2004)),
result_2004_dates
)
def test_yeardayscalendar(self):
self.assertEqual(
calendar.Calendar().yeardayscalendar(2004),
result_2004_days
)
def test_itermonthdates(self):
# ensure itermonthdates doesn't overflow after datetime.MAXYEAR
# see #15421
list(calendar.Calendar().itermonthdates(datetime.MAXYEAR, 12))
def test_option_type(self):
self.assertFailure('-t')
self.assertFailure('--type')
self.assertFailure('-t', 'spam')
stdout = self.run_ok('--type', 'text', '2004')
self.assertEqual(stdout, conv(result_2004_text))
stdout = self.run_ok('--type', 'html', '2004')
self.assertEqual(stdout[:6], b'<?xml ')
self.assertIn(b'<title>Calendar for 2004</title>', stdout)
def test_itermonthdates(self):
# ensure itermonthdates doesn't overflow after datetime.MAXYEAR
# see #15421
list(calendar.Calendar().itermonthdates(datetime.MAXYEAR, 12))
def make_calendar(date):
date_object = datetime.strptime(date, FORMAT_DATE)
calendar_obj = calendar.Calendar().monthdayscalendar(date_object.year, date_object.month)
return [[x if x != 0 else '' for x in week] for week in calendar_obj], date_object.month, date_object.year
def test_yeardatescalendar(self):
def shrink(cal):
return [[[' '.join('{:02d}/{:02d}/{}'.format(
d.month, d.day, str(d.year)[-2:]) for d in z)
for z in y] for y in x] for x in cal]
self.assertEqual(
shrink(calendar.Calendar().yeardatescalendar(2004)),
result_2004_dates
)
def test_yeardayscalendar(self):
self.assertEqual(
calendar.Calendar().yeardayscalendar(2004),
result_2004_days
)
def test_itermonthdates(self):
# ensure itermonthdates doesn't overflow after datetime.MAXYEAR
# see #15421
list(calendar.Calendar().itermonthdates(datetime.MAXYEAR, 12))
def get_text_calendar_dates(date1, date2, cols=3):
""" Get array of datetimes between two dates suitable for formatting """
"""
The return value is a list of years.
Each year contains a list of month rows.
Each month row contains cols months (default 3).
Each month contains list of 6 weeks (the max possible).
Each week contains 1 to 7 days.
Days are datetime.date objects.
"""
year1 = date1.year
year2 = date2.year
# start and end rows
row1 = int((date1.month - 1) / cols)
row2 = int((date2.month - 1) / cols) + 1
# generate base calendar array
Calendar = calendar.Calendar()
cal = []
for yr in range(year1, year2+1):
ycal = Calendar.yeardatescalendar(yr, width=cols)
if yr == year1 and yr == year2:
ycal = ycal[row1:row2]
elif yr == year1:
ycal = ycal[row1:]
elif yr == year2:
ycal = ycal[:row2]
cal.append(ycal)
return cal
def test_localecalendars(self):
# ensure that Locale{Text,HTML}Calendar resets the locale properly
# (it is still not thread-safe though)
old_october = calendar.TextCalendar().formatmonthname(2010, 10, 10)
try:
cal = calendar.LocaleTextCalendar(locale='')
local_weekday = cal.formatweekday(1, 10)
local_month = cal.formatmonthname(2010, 10, 10)
except locale.Error:
# cannot set the system default locale -- skip rest of test
raise unittest.SkipTest('cannot set the system default locale')
# should be encodable
local_weekday.encode('utf-8')
local_month.encode('utf-8')
self.assertEqual(len(local_weekday), 10)
self.assertGreaterEqual(len(local_month), 10)
cal = calendar.LocaleHTMLCalendar(locale='')
local_weekday = cal.formatweekday(1)
local_month = cal.formatmonthname(2010, 10)
# should be encodable
local_weekday.encode('utf-8')
local_month.encode('utf-8')
new_october = calendar.TextCalendar().formatmonthname(2010, 10, 10)
self.assertEqual(old_october, new_october)
def test_localecalendars(self):
# ensure that Locale{Text,HTML}Calendar resets the locale properly
# (it is still not thread-safe though)
old_october = calendar.TextCalendar().formatmonthname(2010, 10, 10)
try:
cal = calendar.LocaleTextCalendar(locale='')
local_weekday = cal.formatweekday(1, 10)
local_month = cal.formatmonthname(2010, 10, 10)
except locale.Error:
# cannot set the system default locale -- skip rest of test
raise unittest.SkipTest('cannot set the system default locale')
# should be encodable
local_weekday.encode('utf-8')
local_month.encode('utf-8')
self.assertEqual(len(local_weekday), 10)
self.assertGreaterEqual(len(local_month), 10)
cal = calendar.LocaleHTMLCalendar(locale='')
local_weekday = cal.formatweekday(1)
local_month = cal.formatmonthname(2010, 10)
# should be encodable
local_weekday.encode('utf-8')
local_month.encode('utf-8')
new_october = calendar.TextCalendar().formatmonthname(2010, 10, 10)
self.assertEqual(old_october, new_october)
def test_locale_calendars(self):
# ensure that Locale{Text,HTML}Calendar resets the locale properly
# (it is still not thread-safe though)
old_october = calendar.TextCalendar().formatmonthname(2010, 10, 10)
try:
cal = calendar.LocaleTextCalendar(locale='')
local_weekday = cal.formatweekday(1, 10)
local_month = cal.formatmonthname(2010, 10, 10)
except locale.Error:
# cannot set the system default locale -- skip rest of test
raise unittest.SkipTest('cannot set the system default locale')
self.assertIsInstance(local_weekday, str)
self.assertIsInstance(local_month, str)
self.assertEqual(len(local_weekday), 10)
self.assertGreaterEqual(len(local_month), 10)
cal = calendar.LocaleHTMLCalendar(locale='')
local_weekday = cal.formatweekday(1)
local_month = cal.formatmonthname(2010, 10)
self.assertIsInstance(local_weekday, str)
self.assertIsInstance(local_month, str)
new_october = calendar.TextCalendar().formatmonthname(2010, 10, 10)
self.assertEqual(old_october, new_october)
def test_localecalendars(self):
# ensure that Locale{Text,HTML}Calendar resets the locale properly
# (it is still not thread-safe though)
old_october = calendar.TextCalendar().formatmonthname(2010, 10, 10)
try:
cal = calendar.LocaleTextCalendar(locale='')
local_weekday = cal.formatweekday(1, 10)
local_month = cal.formatmonthname(2010, 10, 10)
except locale.Error:
# cannot set the system default locale -- skip rest of test
raise unittest.SkipTest('cannot set the system default locale')
# should be encodable
local_weekday.encode('utf-8')
local_month.encode('utf-8')
self.assertEqual(len(local_weekday), 10)
self.assertGreaterEqual(len(local_month), 10)
cal = calendar.LocaleHTMLCalendar(locale='')
local_weekday = cal.formatweekday(1)
local_month = cal.formatmonthname(2010, 10)
# should be encodable
local_weekday.encode('utf-8')
local_month.encode('utf-8')
new_october = calendar.TextCalendar().formatmonthname(2010, 10, 10)
self.assertEqual(old_october, new_october)
def test_locale_calendars(self):
# ensure that Locale{Text,HTML}Calendar resets the locale properly
# (it is still not thread-safe though)
old_october = calendar.TextCalendar().formatmonthname(2010, 10, 10)
try:
cal = calendar.LocaleTextCalendar(locale='')
local_weekday = cal.formatweekday(1, 10)
local_month = cal.formatmonthname(2010, 10, 10)
except locale.Error:
# cannot set the system default locale -- skip rest of test
raise unittest.SkipTest('cannot set the system default locale')
self.assertIsInstance(local_weekday, str)
self.assertIsInstance(local_month, str)
self.assertEqual(len(local_weekday), 10)
self.assertGreaterEqual(len(local_month), 10)
cal = calendar.LocaleHTMLCalendar(locale='')
local_weekday = cal.formatweekday(1)
local_month = cal.formatmonthname(2010, 10)
self.assertIsInstance(local_weekday, str)
self.assertIsInstance(local_month, str)
new_october = calendar.TextCalendar().formatmonthname(2010, 10, 10)
self.assertEqual(old_october, new_october)
def test_localecalendars(self):
# ensure that Locale{Text,HTML}Calendar resets the locale properly
# (it is still not thread-safe though)
old_october = calendar.TextCalendar().formatmonthname(2010, 10, 10)
try:
cal = calendar.LocaleTextCalendar(locale='')
local_weekday = cal.formatweekday(1, 10)
local_month = cal.formatmonthname(2010, 10, 10)
except locale.Error:
# cannot set the system default locale -- skip rest of test
raise unittest.SkipTest('cannot set the system default locale')
# should be encodable
local_weekday.encode('utf-8')
local_month.encode('utf-8')
self.assertEqual(len(local_weekday), 10)
self.assertGreaterEqual(len(local_month), 10)
cal = calendar.LocaleHTMLCalendar(locale='')
local_weekday = cal.formatweekday(1)
local_month = cal.formatmonthname(2010, 10)
# should be encodable
local_weekday.encode('utf-8')
local_month.encode('utf-8')
new_october = calendar.TextCalendar().formatmonthname(2010, 10, 10)
self.assertEqual(old_october, new_october)
def test_locale_calendars(self):
# ensure that Locale{Text,HTML}Calendar resets the locale properly
# (it is still not thread-safe though)
old_october = calendar.TextCalendar().formatmonthname(2010, 10, 10)
try:
cal = calendar.LocaleTextCalendar(locale='')
local_weekday = cal.formatweekday(1, 10)
local_month = cal.formatmonthname(2010, 10, 10)
except locale.Error:
# cannot set the system default locale -- skip rest of test
raise unittest.SkipTest('cannot set the system default locale')
self.assertIsInstance(local_weekday, str)
self.assertIsInstance(local_month, str)
self.assertEqual(len(local_weekday), 10)
self.assertGreaterEqual(len(local_month), 10)
cal = calendar.LocaleHTMLCalendar(locale='')
local_weekday = cal.formatweekday(1)
local_month = cal.formatmonthname(2010, 10)
self.assertIsInstance(local_weekday, str)
self.assertIsInstance(local_month, str)
new_october = calendar.TextCalendar().formatmonthname(2010, 10, 10)
self.assertEqual(old_october, new_october)
def test_output(self):
self.assertEqual(
self.normalize_calendar(calendar.calendar(2004)),
self.normalize_calendar(result_2004_text)
)
def test_output_textcalendar(self):
self.assertEqual(
calendar.TextCalendar().formatyear(2004).strip(),
result_2004_text.strip()
)
def test_output_htmlcalendar(self):
encoding = 'ascii'
cal = calendar.HTMLCalendar()
self.assertEqual(
cal.formatyearpage(2004, encoding=encoding).strip(b' \t\n'),
result_2004_html.strip(' \t\n').encode(encoding)
)
def test_isleap(self):
# Make sure that the return is right for a few years, and
# ensure that the return values are 1 or 0, not just true or
# false (see SF bug #485794). Specific additional tests may
# be appropriate; this tests a single "cycle".
self.assertEqual(calendar.isleap(2000), 1)
self.assertEqual(calendar.isleap(2001), 0)
self.assertEqual(calendar.isleap(2002), 0)
self.assertEqual(calendar.isleap(2003), 0)
def test_setfirstweekday(self):
self.assertRaises(TypeError, calendar.setfirstweekday, 'flabber')
self.assertRaises(ValueError, calendar.setfirstweekday, -1)
self.assertRaises(ValueError, calendar.setfirstweekday, 200)
orig = calendar.firstweekday()
calendar.setfirstweekday(calendar.SUNDAY)
self.assertEqual(calendar.firstweekday(), calendar.SUNDAY)
calendar.setfirstweekday(calendar.MONDAY)
self.assertEqual(calendar.firstweekday(), calendar.MONDAY)
calendar.setfirstweekday(orig)
def test_days(self):
for attr in "day_name", "day_abbr":
value = getattr(calendar, attr)
self.assertEqual(len(value), 7)
self.assertEqual(len(value[:]), 7)
# ensure they're all unique
self.assertEqual(len(set(value)), 7)
# verify it "acts like a sequence" in two forms of iteration
self.assertEqual(value[::-1], list(reversed(value)))
def test_months(self):
for attr in "month_name", "month_abbr":
value = getattr(calendar, attr)
self.assertEqual(len(value), 13)
self.assertEqual(len(value[:]), 13)
self.assertEqual(value[0], "")
# ensure they're all unique
self.assertEqual(len(set(value)), 13)
# verify it "acts like a sequence" in two forms of iteration
self.assertEqual(value[::-1], list(reversed(value)))
def test_localecalendars(self):
# ensure that Locale{Text,HTML}Calendar resets the locale properly
# (it is still not thread-safe though)
old_october = calendar.TextCalendar().formatmonthname(2010, 10, 10)
try:
calendar.LocaleTextCalendar(locale='').formatmonthname(2010, 10, 10)
except locale.Error:
# cannot set the system default locale -- skip rest of test
return
calendar.LocaleHTMLCalendar(locale='').formatmonthname(2010, 10)
new_october = calendar.TextCalendar().formatmonthname(2010, 10, 10)
self.assertEqual(old_october, new_october)
def setUp(self):
self.oldfirstweekday = calendar.firstweekday()
calendar.setfirstweekday(self.firstweekday)
def tearDown(self):
calendar.setfirstweekday(self.oldfirstweekday)
def test_timegm(self):
for secs in self.TIMESTAMPS:
tuple = time.gmtime(secs)
self.assertEqual(secs, calendar.timegm(tuple))