candersonmiller / Anderson-Blogging-Engine

Blogging engine I'm writing for myself to fit my needs in Google App Engine

This URL has Read+Write access

Anderson-Blogging-Engine / image.py
100644 247 lines (204 sloc) 7.361 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
 
 
 
 
import wsgiref.handlers
 
import cgi
import datetime
 
import common
import textile
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.api import images
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
 
 
def findImageIdNumber(post_id):
imagePosts = db.GqlQuery("SELECT * FROM ImagePost WHERE post_id=:1 ORDER BY image_id DESC LIMIT 1",post_id)
image_id = 0
for ip in imagePosts:
image_id = ip.image_id + 1
return image_id
 
 
 
class Image (webapp.RequestHandler):
def get(self):
blogPost = db.get(self.request.get("img_id"))
if blogPost.image:
self.response.headers['Content-Type'] = "image/png"
self.response.out.write(blogPost.image)
else:
self.error(404)
 
class FullImageRender (webapp.RequestHandler):
def get(self):
imagePost = db.get(self.request.get("img_id"))
if imagePost.image:
self.response.headers['Content-Type'] = "image/png"
self.response.out.write(imagePost.image)
else:
self.error(404)
 
class ThumbnailRender (webapp.RequestHandler):
def get(self):
imagePost = db.get(self.request.get("img_id"))
if imagePost.thumbnail:
self.response.headers['Content-Type'] = "image/png"
self.response.out.write(imagePost.thumbnail)
else:
self.error(404)
 
class PostImage(webapp.RequestHandler):
def post(self):
 
post = common.ImagePost()
 
image = self.request.get("img")
thumbnail = images.resize(image,100)
middlesize = images.resize(image,200)
 
post_id = int(self.request.get("post_id"))
 
image_id = findImageIdNumber(post_id)
 
image = self.request.get("img")
 
 
if(image):
post.post_id = post_id
post.image_id = image_id
post.image = db.Blob(image)
post.thumbnail = db.Blob(thumbnail)
post.middlesize = db.Blob(middlesize)
post.put()
self.redirect('/edit?post=%d' % post_id)
 
 
class SortImage(webapp.RequestHandler):
def get(self,post,curr,prev,next):
post_id = int(post)
curr_id = int(curr)
prev_id = int(prev)
next_id = int(next)
 
 
 
 
if(next_id >= 0 and prev_id >= 0): #assuming that we're not pushing it at the last, and not putting it in front of all others
self.response.out.write("assuming that we're not pushing it at the last, and not putting it in front of all others")
if(curr_id < prev_id):
self.response.out.write("lower things between curr and prev (inclusive of prev), replace prev with curr")
imagePosts = db.GqlQuery("SELECT * FROM ImagePost WHERE post_id=:1 ORDER BY image_id DESC",post_id)
i = 0
numToReplace = 0
for ip in imagePosts:
if(ip.image_id < next_id and ip.image_id >= curr_id ):
if(i == 0):
numToReplace = ip.image_id
if(ip.image_id == curr_id):
ip.image_id = numToReplace
ip.put()
else:
ip.image_id = ip.image_id - 1
ip.put()
i += 1
if(curr_id > next_id):
self.response.out.write("raise things between next and curr (inclusive of next), replace next with curr")
imagePosts = db.GqlQuery("SELECT * FROM ImagePost WHERE post_id=:1 ORDER BY image_id ASC",post_id)
i = 0
numToReplace = 0
for ip in imagePosts:
if(ip.image_id >= next_id and ip.image_id <= curr_id ):
if(i == 0):
numToReplace = ip.image_id
if(ip.image_id == curr_id):
ip.image_id = numToReplace
ip.put()
else:
ip.image_id = ip.image_id + 1
ip.put()
i += 1
 
elif(next_id >= 0): # only a next, meaning that we're inserting at the beginning
self.response.out.write("only a next, meaning that we're inserting at the beginning")
imagePosts = db.GqlQuery("SELECT * FROM ImagePost WHERE post_id=:1 ORDER BY image_id ASC",post_id)
i = 0
for ip in imagePosts:
if(ip.image_id == curr_id):
ip.image_id = -1
ip.put()
if(ip.image_id >= next_id and ip.image_id < curr_id):
ip.image_id = ip.image_id + 1
ip.put()
i += 1
for ip in imagePosts:
if(ip.image_id == -1):
ip.image_id = 0
ip.put()
 
 
elif(prev_id >= 0): # only a previous, indicating that we're inserting at the end
self.response.out.write("only a previous, indicating that we're inserting at the end")
imagePosts = db.GqlQuery("SELECT * FROM ImagePost WHERE post_id=:1 ORDER BY image_id DESC",post_id)
i = 0
highest = -1
for ip in imagePosts:
if(ip.image_id > highest):
highest = ip.image_id
if(ip.image_id == curr_id):
ip.image_id = -1
ip.put()
if(ip.image_id <= prev_id and ip.image_id > curr_id):
ip.image_id = ip.image_id - 1
ip.put()
i += 1
for ip in imagePosts:
if(ip.image_id == -1):
ip.image_id = highest
ip.put()
 
class UpdatedImages(webapp.RequestHandler):
def get(self,post):
post_id = int(post)
imagePosts = db.GqlQuery("SELECT * FROM ImagePost WHERE post_id=:1 ORDER BY image_id ASC",post_id)
for imagePost in imagePosts:
self.response.out.write("<img id=\"%d\" src=\"/thumbnail?img_id=%s\"/>" % (imagePost.image_id,imagePost.key()))
 
class ImageURLs(webapp.RequestHandler):
def get(self,post):
post_id = int(post)
imageURLCode = common.getImageURLs(post_id)
self.response.out.write(imageURLCode)
 
class PhonePost(webapp.RequestHandler):
def post(self):
post = common.ImagePost()
image = self.request.get("img")
thumbnail = images.resize(image,100)
middlesize = images.resize(image,200)
post_id = -1
posts = db.GqlQuery("SELECT * FROM BlogPost ORDER BY post_id DESC LIMIT 1")
for po in posts:
post_id = po.post_id + 1
image_id = findImageIdNumber(post_id)
image = self.request.get("img")
if(image):
post.post_id = post_id
post.image_id = image_id
post.image = db.Blob(image)
post.thumbnail = db.Blob(thumbnail)
post.middlesize = db.Blob(middlesize)
post.put()
 
blogpost = common.BlogPost()
blogpost.post_id = post_id
 
titleFromPost = self.request.get("title")
if(titleFromPost):
blogpost.title = titleFromPost
else:
blogpost.title = "mobile picture"
 
blogpost.content = "<br/><img src=\"/fullimage?img_id=%s\">" % post.key()
if(str(self.request.get("published")) == "Yes" ):
blogpost.published = True
else:
blogpost.published = False
blogpost.put()
 
 
 
 
def main():
application = webapp.WSGIApplication([('/img', Image),
('/fullimage',FullImageRender),
('/thumbnail',ThumbnailRender),
(r'/updatedimages/(.*)',UpdatedImages),
(r'/imageurls/(.*)',ImageURLs),
('/phonepost',PhonePost),
('/postimage',PostImage),
(r'/sortimage/(.*)/(.*)/(.*)/(.*)',SortImage)
],debug=True)
run_wsgi_app(application)
 
 
if __name__ == '__main__':
  main()