def post(self): content = self.request.get('quote') submitter=self.request.get('submitter_id') imgurl=self.request.get('imgurl') ip=str(self.request.remote_addr) #convenient variable type for database if imgurl and not re.match(r'http://.*(\.jpg|\.png|\.gif|\.bmp|\.tif)',imgurl): #ensures image URL is valid and of an image self.render('submit.html',error="Invalid image url.",submitter_id=submitter,imgurl=imgurl) elif content: if not submitter: submitter='Anonymous' quote_number=qdb_cache.highest_quote_ID() #highest current quote number if quote_number==0: #empty, initialize empty id list id_list=ID_list(IDs=[1]) #can't have an empty list because db query will return none instead of empty list id_list.put() #stick in database sleep(0.1)#to give db time to store the list quote_id=quote_number+1 #get quote id for this quote that's being submitted id_list_obj=qdb_cache.return_ID_list() #get current id list time=datetime.now() #TimeSubmitted is in DateTime type, Time is a string ready to be displayed in HTML #create quote object quote=DBQuote(quote=content,submitter_id=submitter,quote_id=quote_id,TimeSubmitted=time,Time=date_convert(time),submitter_ip=ip,imgurl=imgurl) qdb_cache.update_quote(quote_id,quote) if quote_id!=1: #1 already in the id list since there had to be at least one number in there upon init. qdb_cache.update_ID_list(quote_id,id_list_obj) sleep(0.1) #required because the homepage gets loaded before the database is updated, just looks a bit messy to refresh page self.redirect('/') else: self.render('submit.html',error="Content required.",submitter_id=submitter,imgurl=imgurl)
def get(self,u_or_d,i_d): #u_or_d decides whether the vote is up or down if re.match(r'[0-9]*',i_d): #make sure it's only a number just in case a non-integer id does slip into the database ip = '"'+str(self.request.remote_addr)+':'+str(i_d)+'"' #unique ID for quote ID/IP address combination query=qdb_cache.return_quote(i_d) if not_spammer(ip): delta={'u':1,'d':-1} query.score+=delta[u_or_d] #upvotes for u, downvotes for d qdb_cache.update_quote(i_d,query) #update cache and db visitor=Visitor(ip=str(ip),last_voted=time.time(),Banned=False) #updates visitor with the new last voted time qdb_cache.update_ip(ip,visitor) #update db and cache with new visitor instance self.write('Your vote was recorded. <a href=\'/\'>Return to the main page.</a>') else: self.write('You\'re doing that too much. Try again in 24 hours.')
def get(self,i_d): if re.match(r'[1-9][0-9]*',i_d): #make sure i_d is a number; this code is repeated elsewhere so there's probably a better way of doing it query=qdb_cache.return_quote(i_d) else: query=None if not query: self.write(query) return self.redirect('/error') else: if query.Flagged==True: query.Flagged=False #mark quote as flagged qdb_cache.update_quote(i_d,query) #update quote with flagged status self.write('Quote flag removed. <a href=\'/\'>Return to the main page.</a>') #placeholder message until page is written else: self.write('Quote flag already removed. <a href=\'/\'>Return to the main page.</a>')