def post(self): logging.info("DeletePageElement: begin") adventure = None myElKey = self.request.get('myElKey') if myElKey: logging.info("DeletePageElement: myElKey found: " + myElKey) pageEl = db.Model.get(myElKey) adventure = pageEl.adventure else: logging.info("DeletePageElement: no myElKey passed in") return if users.get_current_user(): if not main.isUserAuthor(users.get_current_user(), adventure): logging.warning('DeletePageElement post: you are not an author of this adventure') error = 'Error: You are not an author of this adventure' return else: return if (pageEl == None or adventure == None): logging.info("DeletePageElement: pageEl or adventure were null") return if (pageEl.enabled != 0): logging.info("DisablePageElement: cannot delete a page element that is not disabled") return pageEl.delete() memcache.delete("pages" + str(adventure.key())) memcache.delete('XmlPages' + str(adventure.key())) logging.info("DeletePageElement: returning json: " + simplejson.dumps("success")) self.response.out.write(simplejson.dumps("success"))
def get(self): adventure = None page = None error = None myAdventureKey = self.request.get('myAdventureKey') if myAdventureKey: adventure = main.getAdventure(myAdventureKey) else: error = 'error: no adventure key passed in' if adventure == None: error = 'error: could not find Adventure ' + myAdventureKey + ' in the database' elif not main.isUserAuthor(users.get_current_user(), adventure): logging.warning('StoryEditor get: you are not an author of this adventure') error = 'Error: You are not an author of this adventure' elif not users.get_current_user(): error = 'error: you are not logged in' if error: logging.info("########## ERROR: " + error); defaultTemplateValues = main.getDefaultTemplateValues(self) templateValues = { 'adventure': adventure, 'error': error, 'title': "StoryForge - " + adventure.title, } templateValues = dict(defaultTemplateValues, **templateValues) path = os.path.join(os.path.dirname(__file__), 'storyEditor.html') self.response.out.write(template.render(path, templateValues))
def get(self): adventure = None page = None error = None myAdventureKey = self.request.get('myAdventureKey') if myAdventureKey: adventure = main.getAdventure(myAdventureKey) else: error = 'error: no adventure key passed in' if adventure == None: error = 'error: could not find Adventure ' + myAdventureKey + ' in the database' elif not main.isUserAuthor(users.get_current_user(), adventure): logging.warning( 'StoryEditor get: you are not an author of this adventure') error = 'Error: You are not an author of this adventure' elif not users.get_current_user(): error = 'error: you are not logged in' if error: logging.info("########## ERROR: " + error) defaultTemplateValues = main.getDefaultTemplateValues(self) templateValues = { 'adventure': adventure, 'error': error, 'title': "StoryForge - " + adventure.title, } templateValues = dict(defaultTemplateValues, **templateValues) path = os.path.join(os.path.dirname(__file__), 'storyEditor.html') self.response.out.write(template.render(path, templateValues))
def post(self): logging.info("DeletePage: begin") myPageKey = self.request.get('myPageKey') if myPageKey: logging.info("DeletePage: myPageKey found: " + myPageKey) page = db.Model.get(myPageKey) adventure = page.adventure if (page == None or adventure == None): logging.info("DeletePage: page or adventure were null") return else: logging.info("DeletePage: no pageKey passed in") return if users.get_current_user(): if not main.isUserAuthor(users.get_current_user(), adventure): logging.warning( 'DeletePage post: you are not an author of this adventure') error = 'Error: You are not an author of this adventure' return else: return memcache.delete("pages" + str(adventure.key())) query = adventureModel.PageElement.all().filter('page =', page.key()) pageElements = query.fetch(1000) for pageElement in pageElements: logging.info(pageElement.key()) db.delete(pageElements) page.delete() jsonResponse = "page and all page elements deleted" self.response.out.write(simplejson.dumps(jsonResponse)) logging.info("returning addPage json: " + simplejson.dumps(jsonResponse))
def post(self): logging.info("DeletePage: begin") myPageKey = self.request.get('myPageKey') if myPageKey: logging.info("DeletePage: myPageKey found: " + myPageKey) page = db.Model.get(myPageKey) adventure = page.adventure if (page == None or adventure == None): logging.info("DeletePage: page or adventure were null") return else: logging.info("DeletePage: no pageKey passed in") return if users.get_current_user(): if not main.isUserAuthor(users.get_current_user(), adventure): logging.warning('DeletePage post: you are not an author of this adventure') error = 'Error: You are not an author of this adventure' return else: return memcache.delete("pages" + str(adventure.key())) query = adventureModel.PageElement.all().filter('page =', page.key()) pageElements = query.fetch(1000) for pageElement in pageElements: logging.info(pageElement.key()) db.delete(pageElements) page.delete() jsonResponse = "page and all page elements deleted" self.response.out.write(simplejson.dumps(jsonResponse)) logging.info("returning addPage json: " + simplejson.dumps(jsonResponse))
def get(self, myAdventureKey=None): adventure = None page = None error = None title = None userVote = None if not myAdventureKey: myAdventureKey = self.request.get('myAdventureKey') if myAdventureKey: adventure = memcache.get(myAdventureKey) if adventure: logging.info("Play: got adventure from cache") else: logging.info("Play: got adventure from db") adventure = main.getAdventure(myAdventureKey) if adventure: memcache.add(myAdventureKey, adventure, 3600) else: error = 'Error: no adventure key passed in' if adventure == None: error = 'Error: could not find Adventure [' + myAdventureKey + '] in the database' elif not main.isUserReader(users.get_current_user(), adventure): error = 'Error: You are not a reader of this adventure' else: title = adventure.title if error: logging.info('Play get: ' + error) #add to the play stat counter and get the userVote if its not a playLite request if adventure: if not self.request.get('playLite'): adventureStatus = admin.getAdventureStatus(adventure.adventureStatus) if not adventureStatus: logging.warn("Play: could not get adventureStatus with adventure key: " + str(adventure.key())) else: userVote = ratings.getUserVote(adventureStatus, users.get_current_user(), None) ratings.addAdventurePlay(adventureStatus) defaultTemplateValues = main.getDefaultTemplateValues(self) templateValues = { 'adventure': adventure, 'error': error, 'title': title, 'userVote': userVote, 'isUserAuthor': main.isUserAuthor(users.get_current_user(), adventure), 'isUserAdmin': main.isUserAdmin(users.get_current_user(), adventure), } templateValues = dict(defaultTemplateValues, **templateValues) if self.request.get('playLite'): path = os.path.join(os.path.dirname(__file__), 'playStoryLite.html') else: path = os.path.join(os.path.dirname(__file__), 'playStory.html') self.response.out.write(template.render(path, templateValues))
def post(self): logging.info("AddPageElement: begin") adventure = None myPageKey = self.request.get('myPageKey') myElementType = self.request.get('elementType') myPageOrder = self.request.get('pageOrder') elementTypes = { "addPageElementText": 1, "addPageElementImage": 2, "addPageElementChoice": 3, } page = None if myPageKey: logging.info("AddPageElement: myPageKey found: " + myPageKey) page = db.Model.get(myPageKey) adventure = page.adventure else: logging.info("AddPageElement: no pageKey passed in") return if users.get_current_user(): if not main.isUserAuthor(users.get_current_user(), adventure): logging.warning('AddPageElement post: you are not an author of this adventure') error = 'Error: You are not an author of this adventure' return else: return if (page == None or adventure == None): logging.info("AddPageElement: page or adventure were null") return pageElement = adventureModel.PageElement() pageElement.page = page.key() pageElement.adventure = adventure.key() pageElement.dataType = elementTypes[myElementType] pageElement.pageOrder = int(myPageOrder or 1) pageElement.dataA = self.request.get('dataA') pageElement.dataB = self.request.get('dataB') pageElement.enabled = 1; pageElement.put() adventure.version = adventure.version + .0001 adventure.put() memcache.delete("pages" + str(adventure.key())) memcache.delete('XmlPages' + str(adventure.key())) memcache.delete('myStoriesXML' + users.get_current_user().email()) memcache.delete('adventures_' + users.get_current_user().email()) memcache.delete(str(adventure.key())) logging.info("AddPageElement: returning json: " + simplejson.dumps(pageElement.toDict())) self.response.out.write(simplejson.dumps(pageElement.toDict()))
def get(self): #THIS CLASS IS CURRENTLY NOT BEING USED adventure = None error = None if users.get_current_user(): pass else: url = users.create_login_url(self.request.uri) url_linktext = 'Please login to use the StoryForge' template_values = { 'url': url, 'url_linktext': url_linktext, } main.printHeader(self) path = os.path.join(os.path.dirname(__file__), 'pleaseLogin.html') self.response.out.write(template.render(path, template_values)) main.printFooter(self, None) return myAdventureKey = self.request.get('myAdventureKey') if myAdventureKey: adventure = main.getAdventure(myAdventureKey) else: error = 'error: no adventure key passed in' if adventure == None: error = 'error: could not find Adventure ' + myAdventureKey + ' in the database' elif not main.isUserAuthor(users.get_current_user(), adventure): logging.warning('ImageManager get: you are not an author of this adventure') error = 'Error: You are not an author of this adventure' adventure = None template_values = { 'adventure': adventure, 'error': error } main.printHeader(self, 'Image Manager') path = os.path.join(os.path.dirname(__file__), 'images.html') self.response.out.write(template.render(path, template_values)) main.printFooter(self, template_values)
def get(self): #THIS CLASS IS CURRENTLY NOT BEING USED adventure = None error = None if users.get_current_user(): pass else: url = users.create_login_url(self.request.uri) url_linktext = 'Please login to use the StoryForge' template_values = { 'url': url, 'url_linktext': url_linktext, } main.printHeader(self) path = os.path.join(os.path.dirname(__file__), 'pleaseLogin.html') self.response.out.write(template.render(path, template_values)) main.printFooter(self, None) return myAdventureKey = self.request.get('myAdventureKey') if myAdventureKey: adventure = main.getAdventure(myAdventureKey) else: error = 'error: no adventure key passed in' if adventure == None: error = 'error: could not find Adventure ' + myAdventureKey + ' in the database' elif not main.isUserAuthor(users.get_current_user(), adventure): logging.warning( 'ImageManager get: you are not an author of this adventure') error = 'Error: You are not an author of this adventure' adventure = None template_values = {'adventure': adventure, 'error': error} main.printHeader(self, 'Image Manager') path = os.path.join(os.path.dirname(__file__), 'images.html') self.response.out.write(template.render(path, template_values)) main.printFooter(self, template_values)
def post(self): logging.info("AddPage: begin") myPageKey = self.request.get('myPageKey') myAdventureKey = self.request.get('myAdventureKey') adventure = None page = None if myPageKey: logging.info("AddPage: myPageKey found: " + myPageKey) page = db.Model.get(myPageKey) adventure = page.adventure elif myAdventureKey: logging.info("AddPage: myAdventureKey found: " + myAdventureKey) page = adventureModel.Page() adventure = main.getAdventure(myAdventureKey) page.adventure = adventure.key() else: logging.info("AddPage: no pageKey or adventureKey passed in") return if users.get_current_user(): if not main.isUserAuthor(users.get_current_user(), adventure): logging.warning( 'AddPage post: you are not an author of this adventure') error = 'Error: You are not an author of this adventure' return else: return if (page == None or adventure == None): logging.info("DeletePage: page or adventure were null") return page.name = self.request.get('pageName') page.put() memcache.delete("pages" + str(adventure.key())) self.response.out.write(simplejson.dumps(page.toDict())) logging.info("returning addPage json: " + simplejson.dumps(page.toDict()))
def post(self): logging.info("AddPage: begin") myPageKey = self.request.get('myPageKey') myAdventureKey = self.request.get('myAdventureKey') adventure = None page = None if myPageKey: logging.info("AddPage: myPageKey found: " + myPageKey) page = db.Model.get(myPageKey) adventure = page.adventure elif myAdventureKey: logging.info("AddPage: myAdventureKey found: " + myAdventureKey) page = adventureModel.Page() adventure = main.getAdventure(myAdventureKey) page.adventure = adventure.key() else: logging.info("AddPage: no pageKey or adventureKey passed in") return if users.get_current_user(): if not main.isUserAuthor(users.get_current_user(), adventure): logging.warning('AddPage post: you are not an author of this adventure') error = 'Error: You are not an author of this adventure' return else: return if (page == None or adventure == None): logging.info("DeletePage: page or adventure were null") return page.name = self.request.get('pageName') page.put() memcache.delete("pages" + str(adventure.key())) self.response.out.write(simplejson.dumps(page.toDict())) logging.info("returning addPage json: " + simplejson.dumps(page.toDict()))
def post(self): logging.info("MovePageElement: begin") myElKey = self.request.get('myElKey') myNewOrderString = self.request.get('myNewOrder') adventure = None; changingPageEl = None; if myElKey and myNewOrderString: logging.info("MovePageElement: myElKey found: " + myElKey) changingPageEl = db.Model.get(myElKey) adventure = changingPageEl.adventure else: logging.info("MovePageElement: myElKey or myNewOrder not passed in") logging.info(myElKey + " " + myNewOrder) return if users.get_current_user(): if not main.isUserAuthor(users.get_current_user(), adventure): logging.warning('MovePageElement post: you are not an author of this adventure') error = 'Error: You are not an author of this adventure' return myNewOrder = int(myNewOrderString) changingPageEl.pageOrder = myNewOrder changingPageEl.put() # elQuery = adventureModel.PageElement.all() # elQuery.filter('adventure = ', changingPageEl.adventure.key()) # elQuery.filter('page = ', changingPageEl.page.key()) # elQuery.order('pageOrder') # elements = elQuery.fetch(9999) # # now re-arrange the elements to be in the right order # elementsArray = [] # counter = 0 # stagingElement = None # for element in elements: # element.pageOrder = counter # if (changingPageEl.key() == element.key()): # if (counter > 0 and myNewOrder < counter): # oldElement = elementsArray.pop() # element.pageOrder = counter - 1 # elementsArray.append(element) # if (oldElement): # oldElement.pageOrder = counter # elementsArray.append(oldElement) # elif (myNewOrder > counter): # element.pageOrder = element.pageOrder + 1 # stagingElement = element # elif stagingElement: # stagingElement.pageOrder = counter # elementsArray.append(stagingElement) # stagingElement = None # element.pageOrder = element.pageOrder - 1 # elementsArray.append(element) # else: # elementsArray.append(element) # counter = counter + 1 # # # print the new element order out # counter = 0 # jsonArray = [] # for element in elementsArray: # logging.info("MovePageElement: elementsArray[" + str(counter) + "] order(" + str(element.pageOrder) + ") : " + element.dataA) # counter = counter + 1 # element.put() # jsonArray.append(element.toDict()) memcache.delete("pages" + str(adventure.key())) memcache.delete('XmlPages' + str(adventure.key())) self.response.out.write("SUCCESS")
def post(self): #this method supports adding or updating page elements elementTypes = { "addPageElementText": 1, "addPageElementImage": 2, "addPageElementChoice": 3, } logging.info("SavePageElement begin") myPageElKey = self.request.get('myPageElKey') pageElement = None page = None adventure = None if myPageElKey: #existing page element logging.info("SavePageElement: key(" + myPageElKey + ") passed in did exist in DB, must be existing") pageElement = db.Model.get(myPageElKey) if pageElement: page = pageElement.page adventure = pageElement.adventure else: logging.warn("SavePageElement: key(%s) passed in did not exist in DB" % myPageElKey) return else: #new page element logging.info("SavePageElement: no page element key given, must be new") myPageKey = self.request.get('myPageKey') if (not myPageKey): logging.info("SavePageELement: expected myPageKey but it is null") return page = db.Model.get(myPageKey) if page: adventure = page.adventure pageElement = adventureModel.PageElement() #we dont want to change these things so we only set them when the page element is new pageElement.adventure = adventure.key() dataType = self.request.get('elementType') try: pageElement.dataType = int(dataType) except: logging.info("SavePageElement: expected elementType because new pageElement, but did not get it") return else: logging.warn("SavePageElement: new page element bu pageKey did not exist in DB: " + myPageKey) return if users.get_current_user(): if not main.isUserAuthor(users.get_current_user(), adventure): logging.warning('SavePageElement post: you are not an author of this adventure') error = 'Error: You are not an author of this adventure' return else: return if not adventure or not page: logging.info("SavePageElement: could not find page or adventure. myPageKey(" + myPageKey + ")") logging.info("SavePageElement: getting page from key") pageElement.page = page.key() myPageOrder = self.request.get('pageOrder') pageElement.pageOrder = int(myPageOrder or 0) logging.info("SavePageElement: getting dataA") pageElement.dataA = self.request.get('dataA') if pageElement.dataA: logging.info("SavePageElement: replacing funky chars") #this replaces the funky quotes and stuff from MS Word / Google Docs with normal characters pageElement.dataA = pageElement.dataA.replace('%u2019', "'") pageElement.dataA = pageElement.dataA.replace('%u201C', '"') pageElement.dataA = pageElement.dataA.replace('%u201D', '"') pageElement.dataA = pageElement.dataA.replace('%u2026', '...') pageElement.dataA = pageElement.dataA.replace('%u2013', '-') logging.info("SavePageElement: getting dataB") pageElement.dataB = self.request.get('dataB') pageElement.enabled = 1; logging.info("SavePageElement: saving page element to DB") pageElement.put() adventure.version = adventure.version + .0001 adventure.put() logging.info("SavePageElement: getting imageRef") myImgRef = self.request.get('imageRef') if myImgRef: logging.info("imageRef passed in: " + myImgRef) img = db.Model.get(myImgRef) img.imageName = self.request.get('imageName') img.pageElement = str(pageElement.key()) img.put() #memcache.delete("img" + str(img.key())) #memcache.delete("images" + users.get_current_user().email()) pageElement.dataA = img.imageName pageElement.imageRef = img.key() pageElement.put() #logging.info("dataA: " + pageElement.dataA) #logging.info("dataB: " + pageElement.dataB) logging.info("SavePageElement: clearning memcache") memcache.delete("pages" + str(adventure.key())) memcache.delete('XmlPages' + str(adventure.key())) memcache.delete('myStoriesXML' + users.get_current_user().email()) memcache.delete('adventures_' + users.get_current_user().email()) memcache.delete(str(adventure.key())) #logging.info("SavePageElement: returning json: " + simplejson.dumps(pageElement.toDict())) logging.info("SavePageElement: returning json") self.response.out.write(simplejson.dumps(pageElement.toDict()))
def post(self): logging.info("Uploader post start") pageElement = None adventure = None page = None newImage = None myImageKey = self.request.get('imageRef') myImageData = self.request.get('imageData') myImageName = self.request.get('imageName') or 'No Name' myPageElKey = self.request.get('myPageElKey') myPageKey = self.request.get('myPageKey') myPageOrder = int(self.request.get('myPageOrder') or -1) logging.info("Uploader: myImageKey(" + myImageKey + ") myPageElKey(" + myPageElKey + ") myPageKey(" + myPageKey + ") order(" + str(myPageOrder) + ")") if myImageData: myImageSizeBytes = len(myImageData) logging.info("GOT IMAGE DATA!! " + str(myImageSizeBytes) + ' bytes.') if myImageSizeBytes > 1048576: logging.info( "ERROR: Image was too large(%d bytes). 1 megabyte is the max size." % (myImageSizeBytes)) self.response.out.write( "ERROR: Image was too large. 1 megabyte is the max size.") return if not myImageData and not myPageElKey and not myImageKey: self.error(404) return #try to get the existing page element, it might not exist if myPageElKey: pageElement = db.Model.get(myPageElKey) if pageElement: adventure = pageElement.adventure page = pageElement.page else: self.error(404) return else: #we create the page element that will reference the new image logging.info('Uploader: creating new pageElement') pageElement = adventureModel.PageElement() pageElement.dataType = 2 pageElement.enabled = 1 pageElement.pageOrder = myPageOrder if (not myPageKey): logging.info("Uploader: expected myPageKey but it is null") if myImageData: self.response.out.write(simplejson.dumps(len(myImageData))) #self.error(404) return page = db.Model.get(myPageKey) adventure = page.adventure if not page or not adventure: self.error(404) return pageElement.adventure = adventure.key() pageElement.page = page.key() pageElement.put() #figure out which image to use #we might have a new page element and be trying to create it with an existing image (case 1) #we might have an existing page element with an image already there (case 2) #we need a new image (case 3) #in cases 1 and 2, we're also renaming the image (if a new name was given) if myImageKey: newImage = db.Model.get(myImageKey) elif pageElement.imageRef: newImage = pageElement.imageRef else: newImage = adventureModel.Image() if newImage.imageData and len(myImageData) > 100: #if the existing image data is different from the new image data, we need to create a new image #then set the image data, we dont need to set the image data if the old and new images are the same if newImage.imageData != myImageData: logging.info("the existing image data is different(" + str(len(myImageData)) + ").. lets create a new image") newImage = adventureModel.Image() newImage.imageData = db.Blob(myImageData) elif len(myImageData) > 100: #else its a new image, so save the image data from the form newImage.imageData = db.Blob(myImageData) if not page or not adventure: self.error(404) return #make sure the user is logged in and owns this adventure and page if users.get_current_user(): if not main.isUserAuthor(users.get_current_user(), adventure): logging.warning( 'Uploader post: you are not an author of this adventure') self.error(404) return else: self.error(404) return #assign the pageElement reference newImage.pageElement = str(pageElement.key()) #now we read the image data from the form and save the image into the DB adventureStatus = main.getAdventure(adventure.adventureStatus) if adventureStatus: newImage.adventureStatus = adventureStatus newImage.adventure = adventure.key() newImage.realAuthor = users.get_current_user() newImage.imageName = myImageName logging.info("imageName(" + newImage.imageName + ")") #last step- if the image is greater than 900 pixels in either dimension, resize it if newImage.imageData: imageOBJ = images.Image(newImage.imageData) contentType, width, height = getImageInfo(newImage.imageData) #keep resizing the image until it is below a megabyte startResize = 900 counter = 1 while ((counter == 1 and (width > 900 or height > 900)) or len(newImage.imageData) > 999999) and counter < 10: resizeW, resizeH, resizeBool = resizeImage( imageOBJ, width, height, startResize, startResize) logging.info("resize try #%d: resizing image to %dx%d" % (counter, resizeW, resizeH)) imageOBJ.resize(resizeW, resizeH) #now execute the transforms newImage.imageData = imageOBJ.execute_transforms() startResize = startResize - 50 counter = counter + 1 #finally save this image to the db logging.info("saving image to db, size is %d bytes" % (len(newImage.imageData))) newImage.put() memcache.delete("img" + str(newImage.key())) memcache.delete("images" + users.get_current_user().email()) #now link the image to the new page element pageElement.imageRef = newImage.key() pageElement.dataA = newImage.imageName pageElement.put() #send the json response, it includes the page element key self.response.out.write(simplejson.dumps(newImage.toDict()))
logging.info("attempting to resize image with key " + imageKey) try: image = db.get(imageKey) except Exception, e: logging.info('%s: %s' % (e.__class__.__name__, e)) if image.imageData: logging.info("got image data.") adventure = image.adventure imageOBJ = images.Image(image.imageData) else: self.error(404) return #make sure the user is logged in and owns this adventure and page if users.get_current_user(): if not main.isUserAuthor(users.get_current_user(), adventure): self.error(404) return else: self.error(404) return #get the image dimensions contentType, width, height = getImageInfo(image.imageData) logging.info("left(" + str(left) + ") right(" + str(right) + ") top(" + str(top) + ") bottom(" + str(bottom) + ") width(" + str(width) + ") height(" + str(height) + ")") #get the borders of the bounding box, as a proportion left_x = left / width top_y = top / height
def get(self, myAdventureKey=None): adventure = None page = None error = None title = None userVote = None if not myAdventureKey: myAdventureKey = self.request.get('myAdventureKey') if myAdventureKey: adventure = memcache.get(myAdventureKey) if adventure: logging.info("Play: got adventure from cache") else: logging.info("Play: got adventure from db") adventure = main.getAdventure(myAdventureKey) if adventure: memcache.add(myAdventureKey, adventure, 3600) else: error = 'Error: no adventure key passed in' if adventure == None: error = 'Error: could not find Adventure [' + myAdventureKey + '] in the database' elif not main.isUserReader(users.get_current_user(), adventure): error = 'Error: You are not a reader of this adventure' else: title = adventure.title if error: logging.info('Play get: ' + error) #add to the play stat counter and get the userVote if its not a playLite request if adventure: if not self.request.get('playLite'): adventureStatus = admin.getAdventureStatus( adventure.adventureStatus) if not adventureStatus: logging.warn( "Play: could not get adventureStatus with adventure key: " + str(adventure.key())) else: userVote = ratings.getUserVote(adventureStatus, users.get_current_user(), None) ratings.addAdventurePlay(adventureStatus) defaultTemplateValues = main.getDefaultTemplateValues(self) templateValues = { 'adventure': adventure, 'error': error, 'title': title, 'userVote': userVote, 'isUserAuthor': main.isUserAuthor(users.get_current_user(), adventure), 'isUserAdmin': main.isUserAdmin(users.get_current_user(), adventure), } templateValues = dict(defaultTemplateValues, **templateValues) if self.request.get('playLite'): path = os.path.join(os.path.dirname(__file__), 'playStoryLite.html') else: path = os.path.join(os.path.dirname(__file__), 'playStory.html') self.response.out.write(template.render(path, templateValues))
def post(self): logging.info("Uploader post start") pageElement = None adventure = None page = None newImage = None myImageKey = self.request.get('imageRef') myImageData = self.request.get('imageData') myImageName = self.request.get('imageName') or 'No Name' myPageElKey = self.request.get('myPageElKey') myPageKey = self.request.get('myPageKey') myPageOrder = int(self.request.get('myPageOrder') or -1) logging.info("Uploader: myImageKey(" + myImageKey + ") myPageElKey(" + myPageElKey + ") myPageKey(" + myPageKey + ") order(" + str(myPageOrder) + ")") if myImageData: myImageSizeBytes = len(myImageData) logging.info("GOT IMAGE DATA!! " + str(myImageSizeBytes) + ' bytes.') if myImageSizeBytes > 1048576: logging.info("ERROR: Image was too large(%d bytes). 1 megabyte is the max size." % (myImageSizeBytes)) self.response.out.write("ERROR: Image was too large. 1 megabyte is the max size.") return if not myImageData and not myPageElKey and not myImageKey: self.error(404) return #try to get the existing page element, it might not exist if myPageElKey: pageElement = db.Model.get(myPageElKey) if pageElement: adventure = pageElement.adventure page = pageElement.page else: self.error(404) return else: #we create the page element that will reference the new image logging.info('Uploader: creating new pageElement') pageElement = adventureModel.PageElement() pageElement.dataType = 2 pageElement.enabled = 1 pageElement.pageOrder = myPageOrder if (not myPageKey): logging.info("Uploader: expected myPageKey but it is null") if myImageData: self.response.out.write(simplejson.dumps(len(myImageData))) #self.error(404) return page = db.Model.get(myPageKey) adventure = page.adventure if not page or not adventure: self.error(404) return pageElement.adventure = adventure.key() pageElement.page = page.key() pageElement.put() #figure out which image to use #we might have a new page element and be trying to create it with an existing image (case 1) #we might have an existing page element with an image already there (case 2) #we need a new image (case 3) #in cases 1 and 2, we're also renaming the image (if a new name was given) if myImageKey: newImage = db.Model.get(myImageKey) elif pageElement.imageRef: newImage = pageElement.imageRef else: newImage = adventureModel.Image() if newImage.imageData and len(myImageData) > 100: #if the existing image data is different from the new image data, we need to create a new image #then set the image data, we dont need to set the image data if the old and new images are the same if newImage.imageData != myImageData: logging.info("the existing image data is different(" + str(len(myImageData)) + ").. lets create a new image") newImage = adventureModel.Image() newImage.imageData = db.Blob(myImageData) elif len(myImageData) > 100: #else its a new image, so save the image data from the form newImage.imageData = db.Blob(myImageData) if not page or not adventure: self.error(404) return #make sure the user is logged in and owns this adventure and page if users.get_current_user(): if not main.isUserAuthor(users.get_current_user(), adventure): logging.warning('Uploader post: you are not an author of this adventure') self.error(404) return else: self.error(404) return #assign the pageElement reference newImage.pageElement = str(pageElement.key()) #now we read the image data from the form and save the image into the DB adventureStatus = main.getAdventure(adventure.adventureStatus) if adventureStatus: newImage.adventureStatus = adventureStatus newImage.adventure = adventure.key() newImage.realAuthor = users.get_current_user() newImage.imageName = myImageName logging.info("imageName(" + newImage.imageName + ")") #last step- if the image is greater than 900 pixels in either dimension, resize it if newImage.imageData: imageOBJ = images.Image(newImage.imageData) contentType, width, height = getImageInfo(newImage.imageData) #keep resizing the image until it is below a megabyte startResize = 900 counter = 1 while ((counter == 1 and (width > 900 or height > 900)) or len(newImage.imageData) > 999999) and counter < 10: resizeW, resizeH, resizeBool = resizeImage(imageOBJ, width, height, startResize, startResize) logging.info("resize try #%d: resizing image to %dx%d" % (counter, resizeW, resizeH)) imageOBJ.resize(resizeW, resizeH) #now execute the transforms newImage.imageData = imageOBJ.execute_transforms() startResize = startResize - 50 counter = counter + 1 #finally save this image to the db logging.info("saving image to db, size is %d bytes" % (len(newImage.imageData))) newImage.put() memcache.delete("img" + str(newImage.key())) memcache.delete("images" + users.get_current_user().email()) #now link the image to the new page element pageElement.imageRef = newImage.key() pageElement.dataA = newImage.imageName pageElement.put() #send the json response, it includes the page element key self.response.out.write(simplejson.dumps(newImage.toDict()))
logging.info("attempting to resize image with key " + imageKey) try: image = db.get(imageKey) except Exception, e: logging.info('%s: %s' % (e.__class__.__name__, e)) if image.imageData: logging.info("got image data.") adventure = image.adventure imageOBJ = images.Image(image.imageData) else: self.error(404) return #make sure the user is logged in and owns this adventure and page if users.get_current_user(): if not main.isUserAuthor(users.get_current_user(), adventure): self.error(404) return else: self.error(404) return #get the image dimensions contentType, width, height = getImageInfo(image.imageData) logging.info("left(" + str(left) + ") right(" + str(right) + ") top(" + str(top) + ") bottom(" + str(bottom) + ") width(" + str(width) + ") height(" + str(height) + ")") #get the borders of the bounding box, as a proportion left_x = left / width; top_y = top / height; right_x = right / width; bottom_y = bottom / height;