bpo-35078: Allow customization of CSS class name of a month in calendar module (gh-10137)

Refactor formatweekday(), formatmonthname() methods in LocaleHTMLCalendar and LocaleTextCalendar classes in calendar module to call the base class methods. This enables customizable CSS classes for LocaleHTMLCalendar and LocaleTextCalendar.

Patch by Srinivas Reddy Thatiparthy
This commit is contained in:
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి) 2020-06-02 17:03:09 +05:30 committed by GitHub
parent 337d3103a2
commit 85339f5c22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 17 deletions

View File

@ -571,19 +571,11 @@ class LocaleTextCalendar(TextCalendar):
def formatweekday(self, day, width): def formatweekday(self, day, width):
with different_locale(self.locale): with different_locale(self.locale):
if width >= 9: return super().formatweekday(day, width)
names = day_name
else:
names = day_abbr
name = names[day]
return name[:width].center(width)
def formatmonthname(self, theyear, themonth, width, withyear=True): def formatmonthname(self, theyear, themonth, width, withyear=True):
with different_locale(self.locale): with different_locale(self.locale):
s = month_name[themonth] return super().formatmonthname(theyear, themonth, width, withyear)
if withyear:
s = "%s %r" % (s, theyear)
return s.center(width)
class LocaleHTMLCalendar(HTMLCalendar): class LocaleHTMLCalendar(HTMLCalendar):
@ -601,16 +593,11 @@ class LocaleHTMLCalendar(HTMLCalendar):
def formatweekday(self, day): def formatweekday(self, day):
with different_locale(self.locale): with different_locale(self.locale):
s = day_abbr[day] return super().formatweekday(day)
return '<th class="%s">%s</th>' % (self.cssclasses[day], s)
def formatmonthname(self, theyear, themonth, withyear=True): def formatmonthname(self, theyear, themonth, withyear=True):
with different_locale(self.locale): with different_locale(self.locale):
s = month_name[themonth] return super().formatmonthname(theyear, themonth, withyear)
if withyear:
s = '%s %s' % (s, theyear)
return '<tr><th colspan="7" class="month">%s</th></tr>' % s
# Support for old module level interface # Support for old module level interface
c = TextCalendar() c = TextCalendar()

View File

@ -564,6 +564,30 @@ class CalendarTestCase(unittest.TestCase):
new_october = calendar.TextCalendar().formatmonthname(2010, 10, 10) new_october = calendar.TextCalendar().formatmonthname(2010, 10, 10)
self.assertEqual(old_october, new_october) self.assertEqual(old_october, new_october)
def test_locale_html_calendar_custom_css_class_month_name(self):
try:
cal = calendar.LocaleHTMLCalendar(locale='')
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.assertIn('class="month"', local_month)
cal.cssclass_month_head = "text-center month"
local_month = cal.formatmonthname(2010, 10, 10)
self.assertIn('class="text-center month"', local_month)
def test_locale_html_calendar_custom_css_class_weekday(self):
try:
cal = calendar.LocaleHTMLCalendar(locale='')
local_weekday = cal.formatweekday(6)
except locale.Error:
# cannot set the system default locale -- skip rest of test
raise unittest.SkipTest('cannot set the system default locale')
self.assertIn('class="sun"', local_weekday)
cal.cssclasses_weekday_head = ["mon2", "tue2", "wed2", "thu2", "fri2", "sat2", "sun2"]
local_weekday = cal.formatweekday(6)
self.assertIn('class="sun2"', local_weekday)
def test_itermonthdays3(self): def test_itermonthdays3(self):
# ensure itermonthdays3 doesn't overflow after datetime.MAXYEAR # ensure itermonthdays3 doesn't overflow after datetime.MAXYEAR
list(calendar.Calendar().itermonthdays3(datetime.MAXYEAR, 12)) list(calendar.Calendar().itermonthdays3(datetime.MAXYEAR, 12))

View File

@ -0,0 +1,3 @@
Refactor formatweekday, formatmonthname methods in LocaleHTMLCalendar and LocaleTextCalendar classes in calendar module to call the base class methods.This enables customizable CSS classes for LocaleHTMLCalendar.
Patch by Srinivas Reddy Thatiparthy