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): quote_ids=qdb_cache.return_ID_list() if quote_ids is None:#database is empty quotes=[] else: quote_ids=quote_ids.IDs #get quote IDs quote_ids.sort() quote_ids.reverse() #most recent first #quotes_per_page=20 #set at start of file quote_list=quote_ids[:quotes_per_page] #gets latest quotes, or all if quotes per page>total quotes=[qdb_cache.return_quote(id) for id in quote_list] self.render('mainpage.html',quotes=quotes)
def get(self): #quotes_per_page=20 #set at start of file quote_id_list=qdb_cache.return_ID_list().IDs # list of quote ids rand_id_list=random_list(quote_id_list,quotes_per_page)#gives a list up to quotes_per_page long of quote_id_list numbers selected at random random_quotes=[qdb_cache.return_quote(quote_id) for quote_id in rand_id_list] #list of random quotes self.render('page.html',quotes=random_quotes,param='Random')