/[theodore]/bunnyblog/index.cgi


UCC Code Repository

Contents of /bunnyblog/index.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 27 - (show annotations) (download)
Thu Feb 7 02:38:31 2008 UTC (12 years, 7 months ago) by theodore
File size: 6186 byte(s)
Mini Update


1 #!/usr/bin/python
2 import cgi
3 import cgitb; cgitb.enable()
4 import blog
5
6 """
7 Here we set up all the variables for generating the page
8 """
9 main = ""
10 title = "<@[email protected]> - "
11 header = ""
12
13 """
14 Here we retrieve the blog and any arguments
15 Defaults are assigned here
16 """
17 form = cgi.FieldStorage()
18 myblog = blog.Blog(True)
19 numarticles = len(myblog.articles)
20 perpage = 10
21 pagenum = 1
22 httpstatuscode = "200 OK"
23
24
25 def listarticles(pagenum = 0, perpage = 10):
26 main = ""
27 start = numarticles - pagenum * perpage - 1
28 finish = start - perpage
29 if finish < 1:
30 articles = myblog.articles[start::-1]
31 else:
32 articles = myblog.articles[start:finish:-1]
33 for article in articles:
34 main += article.htmlize()
35 return main
36
37 def linkto(page,text="",classname=""):
38 if text == "prev":
39 page = page-1
40 text = "&lt;"*3 + " Prev"
41 classname = "class = 'prev'"
42 elif text == "next":
43 page = page+1
44 text = "Next " + 3*"&gt;"
45 classname = "class = 'next'"
46 elif text != "":
47 text = text
48 else:
49 text = str(page)
50 link = " <a href='index.cgi?pagenum=%d&amp;perpage=%d' %s>%s</a> " % (page,perpage,classname,text)
51 return link
52
53 def generatetoc(currentpage = 1):
54 toc = "<div class='toc'>"
55 if currentpage > 1:
56 toc += linkto(currentpage,"prev")
57 toc += "Page:"
58 for i in range(1,pages + 1):
59 if i == currentpage:
60 toc += "<a> %d </a>" % i
61 else:
62 toc += linkto(i)
63
64 if currentpage <= numarticles // perpage:
65 toc += linkto(currentpage,"next")
66 toc += "</div>"
67 return toc
68
69 if form.has_key('pagenum'):
70 if form.has_key('perpage'):
71 try:
72 perpage = int(form['perpage'].value)
73 except:
74 perpage = 10
75 if perpage < 1 or perpage > 100:
76 perpage = 10
77 pages = (numarticles + 1) // perpage
78 try:
79 pagenum = int(form['pagenum'].value)
80 except:
81 pagenum = 1
82 if pagenum < 1 or pagenum > pages:
83 pagenum = 1
84 toc = numarticles > perpage and generatetoc(pagenum) or ""
85 main += toc + "<hr />" + listarticles(pagenum-1, perpage) + "<hr />" + toc
86 header += "Archives - Page %d" % pagenum
87 title += header
88 elif form.has_key('action') and form.has_key('index'):
89 action = form['action'].value
90 articleindex = int(form['index'].value)
91 article = myblog.findbyindex(articleindex)
92 if article:
93 comment = form['comment'].value
94 author = form['author'].value
95 if action == 'addcomment' and comment and author:
96 if form.has_key('comment') and form.has_key('author'):
97 if action == 'addcomment' and comment and author:
98 article.addcomment(author,comment)
99 if myblog.saveblog():
100 main += "Successfully saved"
101 main += article.htmlize()
102 main += article.showcomments()
103 else:
104 main += blog.error('Add comment failed: blog unable to be saved.<br />Please contact the blog administrator.')
105 else:
106 main += blog.error('invalid comment or author')
107 else:
108 main += blog.error('nosuchindex: ' + str(articleindex))
109 elif form.has_key('article'):
110 try:
111 index = int(form['article'].value)
112 article = myblog.findbyindex(index)
113 if article:
114 main += article.htmlize()
115 main += article.showcomments()
116 title = article.title
117 else:
118 main += blog.error('indexoutrange')
119 except:
120 main += blog.error('indexoutrange')
121 elif form.has_key('title'):
122 articletitle = form['title'].value
123 article = myblog.findbytitle(articletitle)
124 if article:
125 main += article.htmlize()
126 main += article.showcomments()
127 header = article.title
128 title += header
129 else:
130 main += blog.error('nosuchtitle: ' + articletitle)
131 elif form.has_key('page'):
132 pagetodisplay = form['page'].value
133 if pagetodisplay == 'home':
134 pages = (numarticles +1) // 5
135 (cmspage,header) = blog.getcmspage("home")
136 main += cmspage + "<br /><hr />"
137 title = "Welcome to <@[email protected]>"
138 if len(myblog.articles):
139 main += "<h2>Latest Rant:</h2>" + myblog.articles[-1].htmlize() + "For more reading material, please continue to the " + linkto(1,"Archives")
140 elif pagetodisplay == 'archive':
141 perpage = 10
142 pages = (numarticles + 1) // perpage
143 pagenum = 1
144 if numarticles > perpage:
145 toc = generatetoc(pagenum)
146 main += toc + "<hr />" + listarticles(pagenum-1, perpage) + "<hr />" + toc
147 header += "Archives - Page %d" % pagenum
148 else:
149 main += listarticles(pagenum-1, perpage)
150 header += "Archives - Page %d" % pagenum
151 title += header
152 else:
153 (cmspage,header) = blog.getcmspage(pagetodisplay)
154 if cmspage.find("404") != -1:
155 httpstatuscode = "404 Not Found"
156 if blog.isadmin() :
157 main += "<br />This page doesn't appear to exist. <a href='admin.cgi?page=newcontent'>Click here to create a new page</a>"
158 main += cmspage
159 title = "<@[email protected]> - %s" % header
160 else:
161 pages = (numarticles + 1) // 5
162 (cmspage,header) = blog.getcmspage("home")
163 main += cmspage
164 if len(myblog.articles):
165 main += "<br /><h2>Latest Rant:</h2>" + myblog.articles[-1].htmlize() + "For more reading material, please continue to the " + linkto(1,"Archives")
166 else:
167 main += "Login to the <a href='admin.cgi'>admin page</a> to begin your new blog"
168 title = "Welcome to <@[email protected]>"
169
170
171 sidebar = blog.gettemplate("shoutbox",'data')
172 if blog.isadmin():
173 main += "<div id='logout_box'><a href='<@[email protected]>admin.cgi?login=logout'>Logout</a></div>"
174 else:
175 sidebar += "<hr /><a href='<@[email protected]>admin.cgi'>Click here to login</a>"
176 print "Status: %s\nContent-Type: text/html\r\n\n" % httpstatuscode # HTML is following$
177 print myblog.display(title,header,main, sidebar)

Properties

Name Value
svn:executable

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26