/[theodore]/bunnyblog/admin.cgi


UCC Code Repository

Contents of /bunnyblog/admin.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 7 - (show annotations) (download)
Tue Jan 29 15:46:39 2008 UTC (12 years, 10 months ago) by theodore
File size: 9549 byte(s)
Fixing bug with edit site - no footer text.
No handles blank details on site gracefully


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('templates/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 for line in menu.splitlines()[1:-1]:
178 line = line.strip()
179 blogs = blogtags.findall(line)
180 urls = urltags.findall(line)
181 imgs = imgtags.findall(line)
182 if blogs:
183 line = '[blog=%s:%s]%s[/blog]' % blogs[0]
184 elif urls:
185 line = '[url="%s"]%s[/url]' % urls[0]
186 elif imgs:
187 line = '[blog=image:%s]%s[/blog]' % imgs[0]
188 htmlmenu += line + "\n"
189
190
191 htmlmenu = blog.cleanse(htmlmenu)
192 main = main.replace("<@[email protected]>", htmlmenu)
193
194 elif form.has_key('action'):
195 myblog = blog.Blog(True)
196 action = form['action'].value
197 if action == "addarticle":
198 if form.has_key('title') and form.has_key('content'):
199 title = blog.cleanse(form['title'].value)
200 content = blog.cleanse(form['content'].value)
201 if len(myblog.articles):
202 index = myblog.articles[-1].index + 1
203 else:
204 index = 0
205 if myblog.addarticle(title, content, index):
206 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)
207 else:
208 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
209 else:
210 blog.error('addarticle failed')
211 elif action == "saveconfig":
212 config = dict()
213 if form.has_key('site_url'):
214 config['site_url'] = form['site_url'].value
215 if config['site_url'][-1] != "/":
216 config['site_url'] = config['site_url'] + '/'
217 if form.has_key('site_name'):
218 config['site_name'] = form['site_name'].value
219 else:
220 config['site_name'] = 'Bunny Blog'
221 if form.has_key('site_blurb'):
222 config['site_blurb'] = form['site_blurb'].value
223 else:
224 config['site_blurb'] = ''
225 if form.has_key('blog_author'):
226 config['blog_author'] = form['blog_author'].value
227 else:
228 config['blog_author'] = ''
229 if form.has_key('footer'):
230 config['footer'] = form['footer'].value
231 else:
232 config['footer'] = ''
233 config['template'] = form['template'].value
234 myblog.config.update(config)
235 myblog.saveconfig()
236 main += "Configuration file successfully updated"
237 elif action == "addcontent":
238 if form.has_key('filename') or form.has_key('html') or form.has_key('content'):
239 meta,main = checkcontent(form)
240 else:
241 main += blog.gettemplate('admin/newcontent')
242 elif action == "savecontent":
243 if form.has_key('filename') or form.has_key('html') or form.has_key('content'):
244 meta,main = checkcontent(form,'edit')
245 else:
246 main += blog.gettemplate('admin/editcontent')
247 elif action == "logout":
248 cookieheader = logout(userdir)
249 print cookieheader
250 elif action == "deletearticle":
251 if form.has_key('index') and myblog.delarticle(form['index'].value):
252 main += "Deleted Article: %s" % (article.title)
253 else:
254 main += "Delete failed"
255 elif action == "savemenu" or action == "saveshout":
256 if action == "savemenu":
257 filename = "mainmenu.pyhtml"
258 updated = "Main Menu"
259 elif action == "saveshout":
260 filename = "shoutbox.pyhtml"
261 updated = "Shoutbox"
262 if form.has_key('menu'):
263 menu = form['menu'].value
264 if blog.savemenu(menu, filename):
265 main = updated + " update successful"
266 else:
267 main = blog.error(updated + " update failed. Press back and try again.")
268 else:
269 main += "This is the main admin page"
270
271
272
273
274 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