/[theodore]/bunnyblog/admin.cgi


UCC Code Repository

Contents of /bunnyblog/admin.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6 - (hide annotations) (download)
Tue Jan 29 15:34:05 2008 UTC (12 years, 10 months ago) by theodore
File size: 9430 byte(s)
Bring things back to scratch.
Everything works again.
Lets try and deploy...


1 svn-admin 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 theodore 6 menu = blog.gettemplate('mainmenu', 'data')
169 svn-admin 1 elif page == "editshout":
170 theodore 6 menu = blog.gettemplate('shoutbox', 'data')
171 svn-admin 1 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     if form.has_key('site_blurb'):
220     config['site_blurb'] = form['site_blurb'].value
221     if form.has_key('blog_author'):
222     config['blog_author'] = form['blog_author'].value
223     if form.has_key('site_name'):
224     config['footer'] = form['footer'].value
225     if form.has_key('template'):
226     config['template'] = form['template'].value
227     myblog.config.update(config)
228     myblog.saveconfig()
229     main += "Configuration file successfully updated"
230     elif action == "addcontent":
231     if form.has_key('filename') or form.has_key('html') or form.has_key('content'):
232     meta,main = checkcontent(form)
233     else:
234     main += blog.gettemplate('admin/newcontent')
235     elif action == "savecontent":
236     if form.has_key('filename') or form.has_key('html') or form.has_key('content'):
237     meta,main = checkcontent(form,'edit')
238     else:
239     main += blog.gettemplate('admin/editcontent')
240     elif action == "logout":
241     cookieheader = logout(userdir)
242     print cookieheader
243     elif action == "deletearticle":
244     if form.has_key('index') and myblog.delarticle(form['index'].value):
245     main += "Deleted Article: %s" % (article.title)
246     else:
247     main += "Delete failed"
248     elif action == "savemenu" or action == "saveshout":
249     if action == "savemenu":
250 theodore 6 filename = "mainmenu.pyhtml"
251 svn-admin 1 updated = "Main Menu"
252 theodore 6 elif action == "saveshout":
253     filename = "shoutbox.pyhtml"
254 svn-admin 1 updated = "Shoutbox"
255     if form.has_key('menu'):
256     menu = form['menu'].value
257 theodore 6 if blog.savemenu(menu, filename):
258 svn-admin 1 main = updated + " update successful"
259     else:
260     main = blog.error(updated + " update failed. Press back and try again.")
261     else:
262     main += "This is the main admin page"
263    
264    
265    
266    
267 theodore 6 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