def post(self): user = users.get_current_user() if not user: logging.warn('upload with no logged in user') print 'Not logged in' return upload_files = self.get_uploads('file') source = self.request.get("source") format = self.request.get("format") if len(upload_files) < 1: logging.warn('no file uploaded') self.redirect("/") return blob = upload_files[0] logging.info('received file %s as %s' % (source, format)) blobReader = blob.open() for wisdomText in blobReader: wisdomText = wisdomText.strip().decode('utf-8') logging.debug('processing [%s]' % wisdomText) wisdom = Wisdom(source = source, format = format, owner = user, uploadedOn = datetime.date.today(), text = wisdomText) wisdom.put() self.redirect('/')
def get(self): random.seed(time.time()) format = self.request.get("format") if not format: format = 'html' countQuery = Wisdom.all() owner = self.request.get("owner") if owner: countQuery.filter('owner =', owner) count = countQuery.count() if not count: self.response.out.write('It appreas that there is no wisdom with specified parameters.') return index = random.randint(1, count - 1) wisdom = Wisdom.all().fetch(offset=index, limit=1)[0] if format == 'json': self.response.headers['Content-Type'] = "application/json"; self.response.out.write(json.dumps({ 'wisdom': wisdom.text, 'owner': wisdom.owner.nickname(), 'uploadedOn': wisdom.uploadedOn.strftime('%d-%m-%Y') })) elif format == 'jsonp': callback = self.request.get("callback") if not callback: callback = self.request.get("jsonp") if not callback: self.response.out.write('Please specify callback or jsonp argument to get padded json.') return self.response.headers['Content-Type'] = "text/javascript"; prefix = "%s(" % callback if callback != '?' else '(' self.response.out.write(prefix + json.dumps({ 'wisdom': wisdom.text, 'owner': wisdom.owner.nickname(), 'uploadedOn': wisdom.uploadedOn.strftime('%d-%m-%Y') }) + ");") elif format == 'html': self.response.out.write(template.render("templates/wisdom.html", { "wisdomText" : wisdom.text })) else: #plain self.response.headers['Content-Type'] = "text/plain"; self.response.out.write(wisdom.text)
def get(self): page_num = self.request.get('page_num') if not page_num: page_num = 1 else: page_num = int(page_num) page_size = self.request.get('page_size') if not page_size: page_size = 50 else: page_size = int(page_size) user = users.get_current_user() if user: username = user.nickname() wisdoms_count = Wisdom.all().filter("owner =", user).count() wisdoms_page = Wisdom.all().fetch(offset = (page_num - 1) * page_size, limit = page_size ) else: username = None wisdoms_page = None wisdoms_count = 0 total_wisdoms_count = Wisdom.all().count() self.response.out.write(template.render("templates/index.html", { "username" : username, "login_url" : users.create_login_url("/"), "logout_url" : users.create_logout_url("/"), "wisdoms_count" : wisdoms_count, "wisdoms_page" : wisdoms_page, "total_wisdom_count": total_wisdoms_count, "page_size": page_size, "page_num": page_num, "first_page": page_num == 1, "prev_page": page_num - 1, "next_page": page_num + 1 }))