/[theodore]/bunnyblog/gallery.cgi


UCC Code Repository

Contents of /bunnyblog/gallery.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 26 - (show annotations) (download)
Wed Feb 6 15:44:48 2008 UTC (12 years, 8 months ago) by theodore
File size: 6642 byte(s)
Assorted updates from work


1 #!/usr/bin/python
2 import cgi
3 import cgitb; cgitb.enable()
4 import blog
5 import os
6 os.sys.path.insert(0,'./modules')
7 try:
8 from PIL import Image
9 except:
10 pass
11 del os.sys.path[0]
12
13 """
14 BunnyBlog - Image Gallery
15 _________________________
16
17 This file generates all the views for the image gallery and the image manager.
18 _________________________
19 """
20
21 """
22 Here we set up all the variables for generating the page
23 """
24 main = ""
25 title = "<@[email protected]> - "
26 header = "Hoverbox Image Gallery"
27 meta = """
28 <link rel="stylesheet" href='css/hoverbox.css' type="text/css" media="screen, projection" />
29 <!--[if IE]>
30 <link rel="stylesheet" href='css/ie_fixes.css' type="text/css" media="screen, projection" />
31 <![endif]-->
32 """
33
34 uploaddir = "data/images/"
35 thumbsdir = "data/images/thumbs/"
36
37 """
38 Here we retrieve the blog and any arguments
39 Defaults are assigned here
40 """
41 form = cgi.FieldStorage()
42 myblog = blog.Blog()
43 httpstatuscode = "200"
44
45 def massconvert():
46 for file in os.listdir("./data/images/"):
47 ext = file[-4:]
48 if ext != '.jpg' and ext != ".gif" and ext != ".png": #Skip nonimage files
49 continue
50
51 size = 256,256
52 im = Image.open(uploaddir+file).save(thumbsdir+file)
53
54 im = Image.open(thumbsdir+file)
55 im.thumbnail(size)
56 im.save(thumbsdir+file)
57
58 def imagelist():
59 list = ""
60 for file in os.listdir("./data/images/thumbs/"):
61 ext = file[-4:]
62 if ext != '.jpg' and ext != ".gif" and ext != ".png": #Skip nonimage files
63 continue
64 list += '<li><a href="gallery.cgi?image=%s"><img src="data/images/thumbs/%s" alt="description" /><div class="image_container"><img src="data/images/thumbs/%s" alt="description" class="preview" /></div></a></li>' % (file,file, file)
65 return list
66
67 def showgallery():
68 upload_img = "<a href='gallery.cgi?image=upload' style='float:right'>Upload a new picture</a>"
69 return """
70 %s<h2>Click any of these pictures to see the full image, along with its comments.</h2>
71 <div style='clear:right'>
72 <ul class="hoverbox">
73 %s
74 </ul>
75 <p id="hoverbox_footer"><a href="http://validator.w3.org/check/referer">XHTML</a> <a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a> <a href="http://www.contentquality.com/
76 mynewtester/cynthia.exe?Url1=http://host.sonspring.com/hoverbox/">508</a> | Hoverbox by <a href="http://sonspring.com/">Nathan Smith</a>. | Read the <a href="http://sonspring.com/
77 journal/hoverbox-image-gallery">Tutorial</a>.</p> </div>
78 """ % (blog.isadmin() and upload_img or "", imagelist())
79
80
81 if form.has_key('image'):
82 image = form['image'].value
83 if image == "gallery":
84 main = showgallery()
85 title = "<@[email protected]> - Image Gallery"
86 elif image == "upload":
87 if blog.isadmin():
88 main = blog.gettemplate("blog/img_upload")
89 else:
90 main = "Please login before trying to upload images"
91 elif image == "massconv":
92 if blog.isadmin():
93 massconvert()
94 else:
95 main = "Please login before trying to upload images"
96 else:
97 if os.path.exists("./data/images/"+image):
98 if blog.isadmin():
99 admin = '<span style="float:right"><a href="<@[email protected]>gallery.cgi?delete=%s">Delete this picture</a></span>' % image
100 else:
101 admin = ""
102 main += '%s<div style="text-align:center;overflow:auto;"><h2>%s</h2><br /><hr /><img src="data/images/%s" alt="description" /></div>' % (admin,image,image)
103 title = "<@[email protected]> - Viewing %s" % image
104 header = "<@[email protected]> - %s" % image
105 else:
106 main = blog.error(blog.cleanse("File - %s does not exist" % image))
107 header = "<@[email protected]> - ERROR finding image"
108 title = header
109 elif form.has_key('upload'):
110 if blog.isadmin():
111 file = form['upload'].file
112 filename = form['upload'].filename
113 ext = filename[-4:]
114 if ext != '.jpg' and ext != ".gif" and ext != ".png": #Skip nonimage files
115 main += blog.error('Upload failed - Disallowed filetype, %s . %s'%(filename,ext))
116 elif os.path.exists("./data/images/"+filename):
117 main = blog.error('Upload failed - File already uploaded')
118 else:
119 f = open(uploaddir+filename,"wb")
120 while 1:
121 chunk = file.read(100000)
122 if not chunk: break
123 f.write (chunk)
124 f.close()
125
126 size = 256,256
127 im = Image.open(uploaddir+filename).save(thumbsdir+filename)
128
129 im = Image.open(thumbsdir+filename)
130 pic_x, pic_y = im.size
131
132 if pic_x/pic_y > 1.5:
133 im = im.crop((.2*pic_x,0,.8*pic_x,pic_y))
134 elif pic_x/pic_y > 2.5:
135 im = im.crop((.3*pic_x,0,.7*pic_x,pic_y))
136 elif pic_x/pic_y > 1.5:
137 im = im.crop((0,.2*pic_y,pic_x,.8*pic_y))
138 elif pic_x/pic_y > 2.5:
139 im = im.crop((0,.3*pic_y,pic_x,.7*pic_y))
140
141 im.thumbnail(size)
142
143 im.save(thumbsdir+filename)
144
145 main = """
146 Upload of %s successful
147
148 Here is the thumbnail of the image:<br />
149
150 <div style="text-align:center;overflow:auto;">
151 <img src = "<@[email protected]>data/images/thumbs/%s" />
152 </div>
153 <br />
154 Here is the original image: <br />
155
156 <div style="text-align:center;overflow:auto;">
157 <img src = "<@[email protected]>data/images/%s" />
158 </div>
159 """ % (filename, filename, filename)
160 else:
161 main = "Please login before trying to upload images"
162 elif form.has_key('delete'):
163 image = form['delete'].value
164 try:
165 os.remove(uploaddir+image)
166 main += "File: " + uploaddir+image + " successfully deleted<br />"
167 except:
168 main += blog.error("Error: " + uploaddir+image + " was not able to be deleted. This file make have protected permissions, or no longer exists.<br />")
169 try:
170 os.remove(thumbsdir+image)
171 main += "File: " + thumbsdir+image + " successfully deleted<br />"
172 except:
173 main += blog.error("Error: " + thumbsdir+image + " was not able to be deleted. This file make have protected permissions, or no longer exists.<br />")
174 else:
175 main = showgallery()
176 title = "<@[email protected]> - Image Gallery"
177
178
179 sidebar = blog.gettemplate("shoutbox", 'data')
180 if blog.isadmin():
181 sidebar += "<hr />" + blog.gettemplate("admin/toolbar")
182 httpstatuscode = "200 OK"
183 print "Status: %s

Properties

Name Value
svn:executable

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26