/[theodore]/bunnyblog/admin.cgi


UCC Code Repository

Contents of /bunnyblog/admin.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 17 - (show annotations) (download)
Fri Feb 1 03:48:08 2008 UTC (12 years, 10 months ago) by theodore
File size: 10423 byte(s)
 More menu stuff, update how Minimalistic theme places menu, is W3C for the time being.


1 #!/usr/bin/python
2 import cgi
3 import cgitb; cgitb.enable()
4 import blog
5 import os
6
7 """
8 Import modules from modules directory
9 """
10 os.sys.path.insert(0,'./modules')
11 from logintools import login
12 from logintools import logout
13 from logintools import isloggedin
14 from cgiutils import *
15 del os.sys.path[0]
16
17 os.environ['SCRIPT_NAME'] = "admin.cgi"
18
19 """
20 Admin Module Page
21 """
22
23 """
24 Here we set up all the variables for generating the page
25 """
26 main = ""
27 title = "Bunny Blog Admin Center"
28 header = title
29 meta = ""
30
31 """
32 Here we retrieve the blog and any arguments
33 Defaults are assigned here
34 """
35 form = cgi.FieldStorage()
36 myblog = blog.Blog()
37 perpage = 10
38 pagenum = 1
39
40 """
41 Stuff for login tools
42 """
43 userdir = 'data/users/'
44 script = 'admin.cgi' #os.environ['SCRIPT_NAME']
45 formdict = getform(['page'], form)
46 loginreturn = login(form, userdir, action=formdict['page'])
47 try:
48 action, userconfig = loginreturn
49 except:
50 pass
51
52 def listing(directory):
53 list = ""
54 for dir in os.listdir(directory):
55 if dir[-1] == '~' or dir[0] == ".": #Most likely Linux tmp Files, more than likely will not effect users, but makes dev work a little cleaner
56 continue
57 if dir == myblog.config['template']:
58 selected = "selected='selected'"
59 else:
60 selected = ""
61 dir = dir.replace(".pyhtml","")
62 list += "<option value='%s' %s>%s</option>" % (dir,selected,dir)
63 return list
64
65 def content_replace(main, filename="", content="", pagetitle="", bb_selected="checked='checked'",html_selected="", filetype=""):
66 main = main.replace("<@[email protected]>" , filename )
67 main = main.replace("<@[email protected]>" , content )
68 main = main.replace("<@[email protected]>" , pagetitle )
69 main = main.replace("<@[email protected]>" , bb_selected )
70 main = main.replace("<@[email protected]>" , html_selected )
71 main = main.replace("<@[email protected]>" , filetype )
72 return main
73
74 def makemeta(url="<@[email protected]>index.cgi", delay = 5):
75 return "<meta http-equiv='refresh' content='%s;url=%s' />" % (delay,url)
76
77
78 def checkcontent(form,action="new"):
79 checkedcontent = ""
80 error = ""
81 filename = ""
82 pageexists = False
83 bb_selected = "checked='checked'"
84 html_selected = ""
85 if form.has_key('filename'):
86 filename = form['filename'].value
87 if not filename.replace("_", "0").isalnum():
88 error += "<li>Filename contains disallowed letters, try again</li>"
89 try:
90 open('templates/cms/'+ filename + '.pyhtml', 'r')
91 pageexists = True
92 except:
93 pageexists = False
94
95 if action == "new" and pageexists:
96
97 error = "The filename you provided already exists.<br /><br />This error may occur if the request to add a new page submitted twice.<br />If you think this may have happened, open <a href='<@[email protected]>index.cgi?page=<@[email protected]>'>this link</a> in a new tab or window to see if the page exists.<br />If it does not, return here and try saving the page again, possibly with a new name."
98 options = """
99 <p>Alternatively, you can either:<br />
100 <a href="<@[email protected]>admin.cgi?page=editcontent&amp;page=<@[email protected]>">Edit the page with the file name you specifed, losing the current content you have provided.</a>
101 <h3>OR</h3>
102 Create a new page with the details you provided, and change the filename</a>
103 </p>
104 """
105 else:
106 error += "<li>No Filename</li>"
107 if form.has_key('pagetitle'):
108 pagetitle = form['pagetitle'].value
109 else:
110 pagetitle = filename
111 if form.has_key('html'):
112 html_or_bbcode = form['html'].value
113 if html_or_bbcode == "html":
114 html_selected = bb_selected
115 bb_selected = ""
116 if form.has_key('content'):
117 content = form['content'].value
118 else:
119 error = "<li>No page content</li>"
120
121 if error == "": #Everything is good
122 if bb_selected != "":
123 filetype = "bbcode"
124 else:
125 filetype = "html"
126 blog.savenewpage(filename,content,pagetitle, filetype)
127 meta = makemeta("%sindex.cgi?page=%s"%(myblog.config['site_url'],filename))
128 checkedcontent += "Successfully saved page '%s'<br />If this template and your browser support it you will be redirected to the page in 5 seconds. Otherwise, you can access this page at <a href='%sindex.cgi?page=%s'>%sindex.cgi?page=%s</a>.<br />" % (filename, myblog.config['site_url'],filename, myblog.config['site_url'] ,filename)
129 else:
130 error = "<h3>The following errors occured while trying to save the page:</h3><ul>"+error+"</ul><hr />"
131 checkedcontent += error + options + blog.gettemplate('admin/%scontent' % action)
132
133 checkedcontent = content_replace(checkedcontent, filename, content, pagetitle, bb_selected,html_selected)
134 return meta,checkedcontent
135
136 if form.has_key('page'):
137 page = form['page'].value
138 if not form.has_key('delete'):
139 main += blog.gettemplate("admin/" + page)
140 if page == "config":
141 main = main.replace("<@[email protected]>",listing("./templates/style/"))
142 if page == "content":
143 main = main.replace("<@[email protected]>",listing("./data/cms/"))
144 if page == "newcontent":
145 main = content_replace(main)
146 if page == "editcontent":
147 if form.has_key('delete'):
148 action = "delete"
149 else:
150 action = "edit"
151 if form.has_key('filename'):
152 filename = form['filename'].value
153 if action == "delete":
154 try:
155 os.remove('data/cms/'+filename+".pyhtml")
156 main += "File ' %s ' removed" % filename
157 except:
158 main += blog.error("Delete Failed")
159 else:
160 (content,title,filetype,lastedited) = blog.loadcmspage(filename)
161 main = content_replace(main, filename,content,title,"","",filetype)
162 main = main.replace('<@[email protected]>',lastedited)
163 else:
164 error = "Filename of page to edit not specified"
165 if page == "editmenu" or page == "editshout":
166 import re
167 if page == "editmenu":
168 menu = blog.gettemplate('mainmenu', 'data')
169 elif page == "editshout":
170 menu = blog.gettemplate('shoutbox', 'data')
171 menu = menu.replace("</li>","")
172 menu = menu.replace("<li>","")
173 htmlmenu = ""
174 blogtags = re.compile(r'<@[email protected]>index.cgi\?(page|title|index)=(.*?).><span>(.*?)</span>')
175 urltags = re.compile(r'<a href="(.*?)"><span>(.*?)</span>')
176 imgtags = re.compile(r'<@[email protected]>gallery.cgi\?image=(.*?)"><span>(.*?)</span>')
177 rsstags = re.compile(r'<@[email protected]>blog.rss"><span><img src="templates/blog/rss.png" style="height:15px;width:15px" alt="" />')
178 for line in menu.splitlines()[1:-1]:
179 line = line.strip()
180 blogs = blogtags.findall(line)
181 urls = urltags.findall(line)
182 imgs = imgtags.findall(line)
183 if blogs:
184 line = '[blog=%s:%s]%s[/blog]' % blogs[0]
185 elif urls:
186 line = '[url="%s"]%s[/url]' % urls[0]
187 elif imgs:
188 line = '[blog=image:%s]%s[/blog]' % imgs[0]
189 htmlmenu += line + "\n"
190 htmlmenu = htmlmenu.replace('<a href = "<@[email protected]>blog.rss"><span><img src="templates/blog/rss.png" style="height:1em;width:1em" alt="" /> Link to <@[email protected]> RSS Feed</span></a>', "[rss/]")
191 htmlmenu = htmlmenu.replace('<a href = "<@[email protected]>blog.rss"><span><img src="templates/blog/rss.png" style="height:1em;width:1em" alt="" /> ',"[rss]")
192 htmlmenu = htmlmenu.replace('</span></a>', "[/rss]")
193
194 htmlmenu = blog.cleanse(htmlmenu)
195 main = main.replace("<@[email protected]>", htmlmenu)
196
197 elif form.has_key('action'):
198 myblog = blog.Blog(True)
199 action = form['action'].value
200 if action == "addarticle":
201 if form.has_key('title') and form.has_key('content'):
202 title = blog.cleanse(form['title'].value)
203 content = blog.cleanse(form['content'].value)
204 if len(myblog.articles):
205 index = myblog.articles[-1].index + 1
206 else:
207 index = 0
208 if myblog.addarticle(title, content, index):
209 main += "<p>Successfully posted article: <span style='font-size:2em;'>&ldquo;</span> %s <span style='font-size:2em;'>&rdquo;</span><br /> A Link to the Article is at <a href='<@[email protected]>index.cgi?article=%d'><@[email protected]>index.cgi?article=%d</a></p>" % (title,index,index)
210 else:
211 main += "<p class='blog_engine_error'Save failed</br>Article name: %s was not saved. Click the back button in your browser and try again, if that fails there may be a problem with the server permissions" % title
212 else:
213 blog.error('addarticle failed')
214 elif action == "saveconfig":
215 config = dict()
216 if form.has_key('site_url'):
217 config['site_url'] = form['site_url'].value
218 if config['site_url'][-1] != "/":
219 config['site_url'] = config['site_url'] + '/'
220 if form.has_key('site_name'):
221 config['site_name'] = form['site_name'].value
222 else:
223 config['site_name'] = 'Bunny Blog'
224 if form.has_key('site_blurb'):
225 config['site_blurb'] = form['site_blurb'].value
226 else:
227 config['site_blurb'] = ''
228 if form.has_key('blog_author'):
229 config['blog_author'] = form['blog_author'].value
230 else:
231 config['blog_author'] = ''
232 if form.has_key('footer'):
233 config['footer'] = form['footer'].value
234 else:
235 config['footer'] = ''
236 config['template'] = form['template'].value
237 myblog.config.update(config)
238 myblog.saveconfig()
239 main += "Configuration file successfully updated"
240 elif action == "addcontent":
241 if form.has_key('filename') or form.has_key('html') or form.has_key('content'):
242 if not os.path.exists("./data/cms/"+form['filename'].value +".pyhtml"):
243 meta,main = checkcontent(form)
244 else:
245 main = "A page with this filename already exists, please try again."
246 else:
247 main += blog.gettemplate('admin/newcontent')
248 elif action == "savecontent":
249 if form.has_key('filename') or form.has_key('html') or form.has_key('content'):
250 meta,main = checkcontent(form,'edit')
251 else:
252 main += blog.gettemplate('admin/editcontent')
253 elif action == "logout":
254 cookieheader = logout(userdir)
255 print cookieheader
256 elif action == "deletearticle":
257 try:
258 title = myblog.delarticle(form['index'].value)
259 main += "Deleted Article: %s" % (article)
260 except:
261 main += "Delete failed"
262 elif action == "savemenu" or action == "saveshout":
263 if action == "savemenu":
264 filename = "mainmenu.pyhtml"
265 updated = "Main Menu"
266 elif action == "saveshout":
267 filename = "shoutbox.pyhtml"
268 updated = "Shoutbox"
269 if form.has_key('menu'):
270 menu = form['menu'].value
271 if blog.savemenu(menu, filename):
272 main = updated + " update successful"
273 else:
274 main = blog.error(updated + " update failed. Press back and try again.")
275 else:
276 menu = " "
277 if blog.savemenu(menu, filename):
278 main = updated + " update with blank content successful"
279 else:
280 main = blog.error(updated + " update failed. Press back and try again.")
281 else:
282 main += "This is the main admin page"
283
284
285
286
287 myblog.display(title,header,main,blog.gettemplate("admin/toolbar"), meta)

Properties

Name Value
svn:executable

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26