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 buttonText = None title = 'Create A New Story' url = None url_linktext = None if users.get_current_user(): url = users.create_logout_url(self.request.uri) url_linktext = 'Logout' buttonText = 'Create Story' myAdventureKey = self.request.get('myAdventureKey') if myAdventureKey: title = 'Update Story Details' buttonText = 'Update Story Details' adventure = main.getAdventure(myAdventureKey) else: url = users.create_login_url(self.request.uri) url_linktext = 'Login To Create A Story' buttonText = '' defaultTemplateValues = main.getDefaultTemplateValues(self) templateValues = { 'title': title, 'url': url, 'url_linktext': url_linktext, 'buttonText': buttonText, 'adventure': adventure, } templateValues = dict(defaultTemplateValues, **templateValues) path = os.path.join(os.path.dirname(__file__), 'addAdventure.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): myAdventureKey = self.request.get('myAdventureKey') myVote = int(self.request.get('vote')) myiphone = self.request.get('iphone') myComment = self.request.get('comment') output = None adventureStatus = None #they either have to be logged in, or be on the iPhone if not users.get_current_user(): error = 'Error: You must be logged in to vote.' output = "You must be logged in to vote." if not myiphone: logging.info("Vote: trying to vote but user is not a reader and no iphone was passed in") self.response.out.write(output) return adventure = main.getAdventure(myAdventureKey) if not adventure: logging.warn("Vote: adventure key did not exist in db: " + myAdventureKey) output = "Error: Adventure key did not exist in database." self.response.out.write(output) return #we should be good, lets get the adventureStatus object now adventureStatus = admin.getAdventureStatus(adventure.adventureStatus) if not adventureStatus: logging.warn("Vote: could not get the adventureStatus record: " + myAdventureKey) return if myiphone: output = addAdventureVoteIphone(adventureStatus, myVote, myiphone, myComment) else: output = addAdventureVote(adventureStatus, myVote, users.get_current_user(), myComment) self.response.out.write(output) return
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): myAdventureKey = self.request.get('myAdventureKey') myiphone = self.request.get('iphone') myIP = os.environ['REMOTE_ADDR'] if not myiphone: logging.warn("Play: iphone is required") self.response.out("Error: iphone ID is required") return if not myIP: logging.warn("Play: IP is required") self.response.out("Error: IP is required") return if not myAdventureKey: logging.warn("Play: myAdventureKey is required") self.response.out("Error: adventure key is required for Play") return logging.info("Play: myAdventureKey(%s) myIphone(%s) myIP(%s)" % (myAdventureKey, myiphone, myIP)) # make sure this is a unique play (once every hour) # unique on IP address + adventure key alreadyVoted = False ipKey = "%s-%s" % (myIP, myAdventureKey) cacheGet = memcache.get(ipKey) if (cacheGet): logging.info("Play: already played in the last hour IP (%s)" % ipKey) alreadyVoted = True else: # unique on phone + adventure key phoneKey = "%s-%s" % (myiphone, myAdventureKey) cacheGet = memcache.get(phoneKey) if (cacheGet): logging.info("Play: already played in the last hour PHONE (%s)" % phoneKey) alreadyVoted = True # if it wasn't unique, return success still if alreadyVoted: self.response.out.write("SUCCESS") return memcache.add(ipKey, 1, 3600) memcache.add(phoneKey, 1, 3600) adventure = main.getAdventure(myAdventureKey) if not adventure: logging.warn("Vote: adventure key did not exist in db: " + myAdventureKey) output = "Error: Adventure key did not exist in database." self.response.out.write(output) return #we should be good, lets get the adventureStatus object now adventureStatus = admin.getAdventureStatus(adventure.adventureStatus) if not adventureStatus: logging.warn("Vote: could not get the adventureStatus record: " + myAdventureKey) return addAdventurePlay(adventureStatus) self.response.out.write("SUCCESS") return
def get(self): adventure = None shares = None shareInvites = None yourRole = None page = None error = None title = 'Share and Collaborate' myAdventureKey = self.request.get('myAdventureKey') if not users.get_current_user(): logging.info("ViewSharing get: you are not logged in") error = 'Error: you are not logged in' else: if myAdventureKey: #get the adventure adventure = main.getAdventure(myAdventureKey) if not adventure: logging.info("ViewSharing get: adventure key was not found") error = 'Error: adventure was not found' elif not main.isUserAdmin(users.get_current_user(), adventure): logging.warning('ViewSharing get: you are not an admin of this adventure') error = 'Error: You are not an admin of this adventure' else: #get all the shares for this adventure q1 = adventureModel.Share.all().filter('adventure =', adventure).filter('status >=', 1).order('status').order('-role').order('created') shares = q1.fetch(9999) logging.info("got %d shares for adventure %s" % (len(shares), adventure.title)) #find your role in this adventure for share in shares: if share.child == users.get_current_user(): yourRole = share break if yourRole == None and users.is_current_user_admin(): logging.info("Share get: used mega admin powers to grant admin to %s" % users.get_current_user().email()) yourRole = adventureModel.Share() yourRole.role = 3 defaultTemplateValues = main.getDefaultTemplateValues(self) templateValues = { 'adventure': adventure, 'adventureKey': myAdventureKey, 'error': error, 'title': title, 'shares': shares, 'yourRole': yourRole, } templateValues = dict(defaultTemplateValues, **templateValues) path = os.path.join(os.path.dirname(__file__), 'share.html') self.response.out.write(template.render(path, templateValues))
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): myAdventureKey = self.request.get('myAdventureKey') myVote = int(self.request.get('vote')) myiphone = self.request.get('iphone') myComment = self.request.get('comment') output = None adventureStatus = None #they either have to be logged in, or be on the iPhone if not users.get_current_user(): error = 'Error: You must be logged in to vote.' output = "You must be logged in to vote." if not myiphone: logging.info( "Vote: trying to vote but user is not a reader and no iphone was passed in" ) self.response.out.write(output) return adventure = main.getAdventure(myAdventureKey) if not adventure: logging.warn("Vote: adventure key did not exist in db: " + myAdventureKey) output = "Error: Adventure key did not exist in database." self.response.out.write(output) return #we should be good, lets get the adventureStatus object now adventureStatus = admin.getAdventureStatus(adventure.adventureStatus) if not adventureStatus: logging.warn("Vote: could not get the adventureStatus record: " + myAdventureKey) return if myiphone: output = addAdventureVoteIphone(adventureStatus, myVote, myiphone, myComment) else: output = addAdventureVote(adventureStatus, myVote, users.get_current_user(), myComment) self.response.out.write(output) return
def post(self): error = None myAdventureKey = self.request.get('myAdventureKey') adventure = main.getAdventure(myAdventureKey) if adventure == None: return elif not main.isUserReader(users.get_current_user(), adventure): logging.warning( 'GetPages post: you are not a reader of this adventure') error = 'Error: You are not a reader of this adventure' return pagesJSON = memcache.get('pages' + str(adventure.key())) if pagesJSON: logging.info( "GetPages: post: got %d pages from cache for story %s" % (len(pagesJSON), myAdventureKey)) else: pagesQuery = adventureModel.Page.all() pagesQuery.filter('adventure = ', adventure.key()) pagesQuery.order('created') pages = pagesQuery.fetch(9999) elQuery = adventureModel.PageElement.all() elQuery.filter('adventure = ', adventure.key()) elQuery.order('pageOrder') elements = elQuery.fetch(9999) pagesJSON = [] for page in pages: elementsArray = [] for element in elements: if element.page.key() == page.key(): elementsArray.append(element.toDict()) pageDict = page.toDict() pageDict['elements'] = elementsArray pagesJSON.append(pageDict) logging.info("GetPages: post: got %d pages from db for story %s" % (len(pagesJSON), myAdventureKey)) memcache.add('pages' + str(adventure.key()), pagesJSON, 3600) self.response.out.write(simplejson.dumps(pagesJSON))
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 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): error = None myAdventureKey = self.request.get('myAdventureKey') adventure = main.getAdventure(myAdventureKey) if adventure == None: return elif not main.isUserReader(users.get_current_user(), adventure): logging.warning('GetPages post: you are not a reader of this adventure') error = 'Error: You are not a reader of this adventure' return pagesJSON = memcache.get('pages' + str(adventure.key())) if pagesJSON: logging.info("GetPages: post: got %d pages from cache for story %s" % (len(pagesJSON), myAdventureKey)) else: pagesQuery = adventureModel.Page.all() pagesQuery.filter('adventure = ', adventure.key()) pagesQuery.order('created') pages = pagesQuery.fetch(9999) elQuery = adventureModel.PageElement.all() elQuery.filter('adventure = ', adventure.key()) elQuery.order('pageOrder') elements = elQuery.fetch(9999) pagesJSON = [] for page in pages: elementsArray = [] for element in elements: if element.page.key() == page.key(): elementsArray.append(element.toDict()) pageDict = page.toDict() pageDict['elements'] = elementsArray pagesJSON.append(pageDict) logging.info("GetPages: post: got %d pages from db for story %s" % (len(pagesJSON), myAdventureKey)) memcache.add('pages' + str(adventure.key()), pagesJSON, 3600) self.response.out.write(simplejson.dumps(pagesJSON))
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): myAdventureKey = self.request.get('myAdventureKey') myiphone = self.request.get('iphone') myIP = os.environ['REMOTE_ADDR'] if not myiphone: logging.warn("Play: iphone is required") self.response.out("Error: iphone ID is required") return if not myIP: logging.warn("Play: IP is required") self.response.out("Error: IP is required") return if not myAdventureKey: logging.warn("Play: myAdventureKey is required") self.response.out("Error: adventure key is required for Play") return logging.info("Play: myAdventureKey(%s) myIphone(%s) myIP(%s)" % (myAdventureKey, myiphone, myIP)) # make sure this is a unique play (once every hour) # unique on IP address + adventure key alreadyVoted = False ipKey = "%s-%s" % (myIP, myAdventureKey) cacheGet = memcache.get(ipKey) if (cacheGet): logging.info("Play: already played in the last hour IP (%s)" % ipKey) alreadyVoted = True else: # unique on phone + adventure key phoneKey = "%s-%s" % (myiphone, myAdventureKey) cacheGet = memcache.get(phoneKey) if (cacheGet): logging.info( "Play: already played in the last hour PHONE (%s)" % phoneKey) alreadyVoted = True # if it wasn't unique, return success still if alreadyVoted: self.response.out.write("SUCCESS") return memcache.add(ipKey, 1, 3600) memcache.add(phoneKey, 1, 3600) adventure = main.getAdventure(myAdventureKey) if not adventure: logging.warn("Vote: adventure key did not exist in db: " + myAdventureKey) output = "Error: Adventure key did not exist in database." self.response.out.write(output) return #we should be good, lets get the adventureStatus object now adventureStatus = admin.getAdventureStatus(adventure.adventureStatus) if not adventureStatus: logging.warn("Vote: could not get the adventureStatus record: " + myAdventureKey) return addAdventurePlay(adventureStatus) self.response.out.write("SUCCESS") return
def get(self, comment_in=None, myAdventureKey_in=None, submitFlag=False): adventure = None adventureStatus = None error = None myAdventureKey = None submitComment = comment_in comment = None if myAdventureKey_in: myAdventureKey = myAdventureKey_in else: myAdventureKey = self.request.get('myAdventureKey') adventure = main.getAdventure(myAdventureKey) if not adventure: logging.warn('Submit: adventure was not found in the DB: ' + myAdventureKey) error = "Adventure key was not found in the DB" else: #make sure the logged in person is an admin if not main.isUserAdmin(users.get_current_user(), adventure): logging.warning( 'RemoveShare post: you are not an admin of this adventure') error = 'Error: You are not an admin of this adventure' else: #try to get the AdventureStatus record, if it doesn't exist, create it adventureStatus = getAdventureStatus(adventure.adventureStatus) if not adventureStatus: logging.warn( "Submit: could not found adventureStatus record: " + myAdventureKey) else: logging.info('Submit: got adventureStatus record') #if we have a submitFlag, then we're trying to submit the form if submitFlag and adventure and adventureStatus: logging.info("Submit: submit flag was set. comment(%s)" % comment) #delete the previous submitted adventure submittedAdventure = adventureStatus.submittedAdventure memcache.delete(str(adventureStatus.key())) if submittedAdventure: deleteAdventure(submittedAdventure) #now duplicate the story adventureStatus.submittedAdventure = duplicateAdventure(adventure) if adventureStatus.submittedAdventure: comment = 'Your story has been submitted.' adventureStatus.status = 2 adventureStatus.comment = submitComment adventureStatus.put() #reset the cache memcache.delete('advStat' + str(adventure.key())) #send an email to the admins body = '''%s (%s) has just submitted a story for approval. Title: %s Author: %s RealAuthor: %s Description: %s Check it out at: http://istoryweb.appspot.com/admin ''' % (users.get_current_user().email(), users.get_current_user().nickname(), adventure.title, adventure.author, adventure.realAuthor, adventure.desc) mail.send_mail( users.get_current_user().email(), '*****@*****.**', users.get_current_user().nickname() + ' has just submitted a story for approval', body) else: error = 'Error, something went wrong with the submission.' defaultTemplateValues = main.getDefaultTemplateValues(self) templateValues = { 'comment': comment, 'error': error, 'adventureStatus': adventureStatus, 'title': 'Submit Your Story For Approval', } templateValues = dict(defaultTemplateValues, **templateValues) path = os.path.join(os.path.dirname(__file__), 'submit.html') self.response.out.write(template.render(path, templateValues))
def get(self, myAdventureKey=None, myPageKey=None): logging.info("XmlWriter: myAdventureKey(%s) myPageKey(%s)" % (myAdventureKey, myPageKey)) output = None cacheString = None if not myAdventureKey and not myPageKey: #main entry point, just get the list of advenures cacheString = 'xmlMainRatings' output = memcache.get(cacheString) if output: logging.info("XmlWriter: got main ratings from cache") else: #get approved adventures output = '' ratings = index.getRatings() for rating in ratings: output += rating.toXML() output = "<xml>\n%s</xml>" % output memcache.add(cacheString, output, 86400) logging.info("XmlWriter: got main ratings from db") elif myAdventureKey and myPageKey == 'files.xml': #get the list of pages for this adventure adventure = main.getAdventure(myAdventureKey) firstPage = None if not adventure: logging.warn("XmlWriter: could not fetch adventure with key: " + myAdventureKey) return cacheString = 'XmlPages' + myAdventureKey output = memcache.get(cacheString) if output: logging.info("XmlWriter: got pages for adventure %s from cache" % myAdventureKey) else: output = "<version>%.4f</version>\n" % adventure.version adventureStatus = main.getAdventure(adventure.adventureStatus) if not adventureStatus: logging.warn("XmlWriter: could not fetch adventureStatus with key: " + adventure.adventureStatus) return for image in adventureStatus.images: output += image.toXML() for page in adventure.pages.order('created'): if not firstPage: firstPage = str(page.key()) output += page.pageHeaderToXML() output += "<firstPage>%s.xml</firstPage>\n" % firstPage output = "<xml>\n%s</xml>" % output #if adventure is the editable one, set the duration to 300 cacheDuration = 86400 if adventure.key() == adventureStatus.editableAdventure.key(): cacheDuration = 300 memcache.add(cacheString, output, 86400) logging.info("XmlWriter: got pages for adventure %s from db, set cache duration to %d" % (myAdventureKey, cacheDuration)) elif myPageKey: #get the page data for this page cacheString = 'XmlPage' + myPageKey output = memcache.get(cacheString) if output: logging.info("XmlWriter: got page %s from cache" % myPageKey) else: output = '' page = main.getPage(myPageKey) if not page: logging.warn("XmlWriter: could not fetch page with key: " + myPageKey) return for pageElement in page.pageElements.order('pageOrder'): output += " " + pageElement.toXML() + "\n" output = "<xml>\n%s</xml>" % output adventureStatus = main.getAdventure(page.adventure.adventureStatus) #if adventure is the editable one, set the duration to 300 cacheDuration = 86400 if page.adventure.key() == adventureStatus.editableAdventure.key(): cacheDuration = 300 memcache.add(cacheString, output, 300) logging.info("XmlWriter: got page %s from db, set cache duration to %d" % (myPageKey, cacheDuration)) self.response.headers['Content-Type'] = 'text/plain' self.response.out.write(output)
def post(self): error = None newAdventure = False myAdventureKey = self.request.get('myAdventureKey') logging.info("AddAdventure for key: " + myAdventureKey) if not users.get_current_user(): logging.warning('AddAdventure post: you need to login') error = "Error: You need to login." if myAdventureKey: logging.info("existing adventure.") adventure = main.getAdventure(myAdventureKey) if not main.isUserAdmin(users.get_current_user(), adventure): logging.warning('AddAdventure post: you are not an admin of this adventure') error = "Error: You are not an admin of this adventure." else: logging.info("new adventure.") adventure = adventureModel.Adventure() newAdventure = True if error: defaultTemplateValues = main.getDefaultTemplateValues(self) templateValues = { 'error': error } templateValues = dict(defaultTemplateValues, **templateValues) path = os.path.join(os.path.dirname(__file__), 'addAdventure.html') self.response.out.write(template.render(path, templateValues)) return if not adventure.realAuthor: adventure.realAuthor = users.get_current_user() adventure.title = self.request.get('title') or "[no title]" adventure.author = self.request.get('author') or "[no author]" adventure.desc = self.request.get('desc') or "[no description]" adventure.approved = 0 adventure.version = 0.0 adventure.put() #we dont want to delete the cache of ratings because this adventure won't be approved or rated yet #memcache.delete("ratings") memcache.delete("adventures_" + users.get_current_user().email()) logging.info("AddAdventure data: " + simplejson.dumps(adventure.toDict())) if newAdventure: #now create an admin role for this user + adventure, only if its a new adventure share = adventureModel.Share() share.adventure = adventure share.owner = users.get_current_user() share.child = users.get_current_user() share.role = 3 share.status = 2 share.put() #now create the adventureStatus record adventureStatus = adventureModel.AdventureStatus() adventureStatus.editableAdventure = adventure adventureStatus.status = 1 adventureStatus.version = 0.0 adventureStatus.put() #save the adventureStatus key back to the adventure record adventure.adventureStatus = str(adventureStatus.key()) adventure.put() #now create the rating record for this adventure rating = adventureModel.AdventureRating() rating.adventureStatus = adventureStatus rating.voteCount = 0 rating.voteSum = 0 rating.plays = 0 rating.approved = 0 rating.rating = 0.0 rating.put() #handle the coverImage newCoverImage = self.request.get('coverImage') oldCoverImage = adventure.coverImage if newCoverImage: myImageSizeBytes = len(newCoverImage) 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 len(newCoverImage) > 100: logging.info("AddAdventure: got new cover image") imageOBJ = images.Image(newCoverImage) #resize to 320x150 imageOBJ.resize(320, 150) newImage = adventureModel.Image() newImage.imageData = imageOBJ.execute_transforms() #setup the rest of the image metadata newImage.adventure = adventure if adventure.adventureStatus: adventureStatus = main.getAdventure(adventure.adventureStatus) if adventureStatus: newImage.adventureStatus = adventureStatus if not newImage.adventureStatus: logging.warn("AddAdventure: trying to use new coverImage and adventureStatus could not be found") newImage.pageElement = "coverImage" newImage.imageName = "Cover Image" newImage.realAuthor = users.get_current_user() newImage.put() adventure.coverImage = str(newImage.key()) adventure.put() #delete the old cover image if it exists and no page elements are using it if oldCoverImage: oldCoverImage = db.Model.get(oldCoverImage) if oldCoverImage: deleteFlag = True for pageElement in oldCoverImage.pageElements: deleteFlag = False logging.info("AddAdventure: not deleting old cover image because it is in use by a page element") break #also check the editable, submitted, and published adventure on this adventureStatus if adventureStatus.editableAdventure: if adventureStatus.editableAdventure.coverImage == str(oldCoverImage.key()): deleteFlag = False if adventureStatus.submittedAdventure: if adventureStatus.submittedAdventure.coverImage == str(oldCoverImage.key()): deleteFlag = False if adventureStatus.publishedAdventure: if adventureStatus.publishedAdventure.coverImage == str(oldCoverImage.key()): deleteFlag = False if deleteFlag: logging.info("AddAdventure: deleting old cover image") oldCoverImage.delete() #remove the cache object for all users who have a role for this adventure q = adventureModel.Share.all().filter('adventure =', adventure) shares = q.fetch(9999) logging.info("AddAdventure: removed %d cache objects" % len(shares)) for share in shares: if share.child: memcache.delete("adventures_" + share.child.email()) memcache.delete("myStoriesXML" + share.child.email()) memcache.delete("images" + share.child.email()) logging.info("addAdventure: deleting memcache object: " + str(adventure.key())) memcache.delete(str(adventure.key())) memcache.delete('XmlPages' + str(adventure.key())) logging.info("AddAdventure done for key: " + myAdventureKey) #self.redirect('/myStories') #if not myAdventureKey: # #redirect you right to the story editor if its a new story # self.redirect('/storyEditor?myAdventureKey=' + str(adventure.key())) #else: #redirect you back to myStories self.redirect('/myStories')
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()))
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()))
def post(self): error = None adventure = None myAdventureKey = self.request.get('myAdventureKey') myName = self.request.get('name') myEmail = self.request.get('email') myRole = int(self.request.get('role')) logging.info("ViewSharing: post adventureKey(%s) name(%s) email(%s) role(%d)" % (myAdventureKey, myName, myEmail, myRole)) #make sure we're logged in if not users.get_current_user(): logging.warning('ViewSharing post: you are not logged in') error = 'Error: You are not logged in' self.response.out.write(error) return #make sure the email address is valid if not mail.is_email_valid(myEmail): logging.warning('ViewSharing post: invalid email address') error = 'Error: Invalid email address' self.response.out.write(error) return #get the adventure adventure = main.getAdventure(myAdventureKey) if not adventure: logging.warning('ViewSharing post: adventure key %s does not exist' % (myAdventureKey)) error = 'Error: adventure key %s does not exist' % (myAdventureKey) self.response.out.write(error) return #make sure we're the owner #if not(users.is_current_user_admin()) and adventure.realAuthor and adventure.realAuthor != users.get_current_user(): if not main.isUserAdmin(users.get_current_user(), adventure): logging.warning('ViewSharing post: you are not an admin of this adventure') error = 'Error: You are not an admin of this adventure' self.response.out.write(error) return #see if this person has already been invited q = adventureModel.Share.all().filter('status >=', 1).filter('adventure =', adventure).filter('childEmail =', myEmail).filter('role =', myRole) myShares = q.fetch(1) for myShare in myShares: logging.info('ViewSharing post: this email address has already been invited to this adventure for this role.') error = 'Error: This email address has already been invited to this adventure for this role.' self.response.out.write(error) return #create the invite and store it in the DB logging.info("ViewSharing post: creating new invite") share = adventureModel.Share() share.adventure = adventure share.owner = users.get_current_user() share.childEmail = myEmail share.childName = myName share.status = 1 share.inviteKey = '' for i in random.sample('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', 6): share.inviteKey += i share.role = myRole share.put() #remove the share invite cache object memcache.delete("invites_" + myEmail) #send the invite email subject = 'You have an iStory invite from %s' % (users.get_current_user().nickname()) body = """ Hello %s, You have been invited to collaborate with %s (%s) on the story, '%s'. Just click the link below and you'll be taken to the iStory website, where you can participate in this next generation story telling experience. http://istoryweb.appspot.com/shareInvite?key=%s We hope you enjoy iStory! Thanks, The iStory team """ % (myName, users.get_current_user().nickname(), users.get_current_user().email(), adventure.title, share.inviteKey) mail.send_mail('*****@*****.**', myEmail, subject, body) #send the json response back to the web logging.info('ViewSharing post: here is the invite: ' + simplejson.dumps(share.toDict()) ) self.response.out.write(simplejson.dumps(share.toDict()))
def get(self, comment_in=None, myAdventureKey_in=None, submitFlag=False): adventure = None adventureStatus = None error = None myAdventureKey = None submitComment = comment_in comment = None if myAdventureKey_in: myAdventureKey = myAdventureKey_in else: myAdventureKey = self.request.get('myAdventureKey') adventure = main.getAdventure(myAdventureKey) if not adventure: logging.warn('Submit: adventure was not found in the DB: ' + myAdventureKey) error = "Adventure key was not found in the DB" else: #make sure the logged in person is an admin if not main.isUserAdmin(users.get_current_user(), adventure): logging.warning('RemoveShare post: you are not an admin of this adventure') error = 'Error: You are not an admin of this adventure' else: #try to get the AdventureStatus record, if it doesn't exist, create it adventureStatus = getAdventureStatus(adventure.adventureStatus) if not adventureStatus: logging.warn("Submit: could not found adventureStatus record: " + myAdventureKey) else: logging.info('Submit: got adventureStatus record') #if we have a submitFlag, then we're trying to submit the form if submitFlag and adventure and adventureStatus: logging.info("Submit: submit flag was set. comment(%s)" % comment) #delete the previous submitted adventure submittedAdventure = adventureStatus.submittedAdventure memcache.delete(str(adventureStatus.key())) if submittedAdventure: deleteAdventure(submittedAdventure) #now duplicate the story adventureStatus.submittedAdventure = duplicateAdventure(adventure) if adventureStatus.submittedAdventure: comment = 'Your story has been submitted.' adventureStatus.status = 2 adventureStatus.comment = submitComment adventureStatus.put() #reset the cache memcache.delete('advStat' + str(adventure.key())) #send an email to the admins body = '''%s (%s) has just submitted a story for approval. Title: %s Author: %s RealAuthor: %s Description: %s Check it out at: http://istoryweb.appspot.com/admin ''' % (users.get_current_user().email(), users.get_current_user().nickname(), adventure.title, adventure.author, adventure.realAuthor, adventure.desc) mail.send_mail(users.get_current_user().email(), '*****@*****.**', users.get_current_user().nickname() + ' has just submitted a story for approval', body) else: error = 'Error, something went wrong with the submission.' defaultTemplateValues = main.getDefaultTemplateValues(self) templateValues = { 'comment': comment, 'error': error, 'adventureStatus': adventureStatus, 'title': 'Submit Your Story For Approval', } templateValues = dict(defaultTemplateValues, **templateValues) path = os.path.join(os.path.dirname(__file__), 'submit.html') self.response.out.write(template.render(path, templateValues))