HTML Calendar code

K

Kajsa Anka

Before I re-invent something I would like to ask if there exists some code
that can be used for create the HTML code for a calendar which I then can
include on a web page.

The module in the standard library can create a calendar but I would like
display my schedule.

My idea is that it would read a simple text file and then create a weekly
calendar view (and/or a monthly) with my schedule.

Does some the code for this exist? (the ones I found all created calendars
but doesn't seem to include any schedule)

jem
 
D

Dan Bishop

Before I re-invent something I would like to ask if there exists some code
that can be used for create the HTML code for a calendar which I then can
include on a web page.

The module in the standard library can create a calendar but I would like
display my schedule.

My idea is that it would read a simple text file and then create a weekly
calendar view (and/or a monthly) with my schedule.

Does some the code for this exist? (the ones I found all created calendars
but doesn't seem to include any schedule)

Here's most of a monthly calendar CGI script I wrote a couple of years
ago. Modify it to your needs.

# ------------ CONSTANTS ------------

MONTH_NAMES = (None, # Use 1-based indexing
'January', 'February', 'March', 'April', 'May', 'June',
'July',
'August', 'September', 'October', 'November',
'December')

DAY_NAMES = ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday',
'Friday',
'Saturday')

# ------------ DATABASE FUNCTIONS ------------

official_events = shelve.open('../data/calendar/official.shelf')
unofficial_events = shelve.open('../data/calendar/unofficial.shelf')

def get_events(year, month, day):
key = '%04d-%02d-%02d' % (year, month, day)
events = official_events.get(key, [])
if common.user and common.user.permissions.calendar_u:
events += unofficial_events.get(key, [])
return events

# ------------ CALCULATION FUNCTIONS ------------

def is_leap_year(year):
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)

def days_in_month(month, year):
if month == 2:
if is_leap_year(year):
return 29
else:
return 28
elif month in (4, 6, 9, 11):
return 30
else:
return 31

# ------------ DISPLAY FUNCTIONS ------------

def get_links(year, month):
"Return URLs for previous month and next month."
old_year = year
old_month = month - 1
if old_month == 0:
old_month = 12
old_year -= 1
left_link = 'calendar.py?year=%d&month=%d' % (old_year,
old_month)
new_year = year
new_month = month + 1
if new_month == 13:
new_month = 1
new_year += 1
right_link = '?year=%d&month=%d' % (new_year, new_month)
return left_link, right_link

def print_navigation(year, month):
prev_link, next_link = get_links(year, month)
document.write('<table class="navigation"><tr>')
document.write('<td class="prev_link">')
document.write('<a href="%s">&larr; previous month</a>' % prev_link)
document.write('</td><td class="next_link">')
document.write('<a href="%s">next month &rarr;</a>' % next_link)
document.write('</td></tr></table>')

def print_day_names():
document.write('<tr>')
for day in DAY_NAMES:
document.write('<th class="dayname">%s</th>' % day)
document.write('</tr>')

def print_calendar(year, month):
document.write('<h2>%s %d</h2>' % (MONTH_NAMES[month], year))
print_navigation(year, month)
document.write('<table class="calendar">')
print_day_names()
days = days_in_month(month, year)
start_offset = (datetime.date(year, month, 1).weekday() + 1) % 7
document.write('<tr class="week">')
for i in xrange(start_offset):
document.write('<td class="invalid" />')
column = start_offset
for day in xrange(1, days+1):
events = get_events(year, month, day)
if events:
document.write('<td class="event">')
document.write(str(day))
document.write('<ul>')
for event in events:
document.write('<li>%s</li>' % cgi.escape(event))
document.write('</ul></td>')
else:
document.write('<td class="noevent">%d</td>' % day)
column += 1
if column == 7 and day != days:
document.write('</tr><tr class="week">')
column = 0
for i in xrange((7 - column) % 7):
document.write('<td class="invalid" />')
document.write('</tr></table>')

# ------------ MAIN PROGRAM ------------

current_date = datetime.date.today()
form = cgi.FieldStorage()

try:
year = int(form['year'].value)
except:
year = current_date.year

try:
month = int(form['month'].value)
except:
month = current_date.month
if month < 1 or month > 12:
month = current_date.month

common.print_header('Calendar')
document.write_txt2tags_file('../data/text/intro_calendar')
print_calendar(year, month)
common.print_footer()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top