def post(self, post_id_str, ip_num_str): post_id = int(post_id_str) main_post_key = models.Post.get_key(post_id) main_post = main_post_key.get() if main_post and main_post.parent_thread is None: errors = self._validate_form() if len(errors) > 0: delete_rpc = self._delete_files_async() child_posts_rpc = models.Post.get_child_posts_async(main_post_key) self.render_response("post_view.html", post_errors=errors, main_post=main_post, posts=child_posts_rpc.get_result()) delete_rpc.wait() else: ip_num = int(ip_num_str) ip = ipaddr.IPAddress(ip_num) ban_rpc = self.user_is_banned_async(ip) if not ban_rpc.get_result()[0]: uploads = self.get_uploads() if len(uploads) > 0: image_info = self.get_uploads()[0] image_key = image_info.key() delete_rpc = self._delete_files_async(image_info) if image_info.size > config.MAX_FILE_SIZE_BYTES: child_posts_rpc = models.Post.get_child_posts_async(main_post_key) blobstore.delete(image_key) self.render_response("post_view.html", post_errors=['The supplied file was too large.'], main_post=main_post, posts=child_posts_rpc.get_result()) logging.info("File was too large") delete_rpc.wait() return data = blobstore.fetch_data(image_key, 0, 50000) try: image_data = images.Image(image_data=data) _ = image_data.width _ = image_data.height except images.NotImageError: child_posts_rpc = models.Post.get_child_posts_async(main_post_key) blobstore.delete(image_key) self.render_response("post_view.html", post_errors=['The supplied file was not an image.'], main_post=main_post, posts=child_posts_rpc.get_result()) logging.info("Not an image", exc_info=True) delete_rpc.wait() return except images.Error: child_posts_rpc = models.Post.get_child_posts_async(main_post_key) blobstore.delete(image_key) self.render_response("post_view.html", post_errors=['An unknown error occurred when we attempted to process the file you supplied.'], main_post=main_post, posts=child_posts_rpc.get_result()) logging.warn("Unknown error when processing image", exc_info=True) delete_rpc.wait() return else: image_data = None image_info = None image_key = None delete_rpc = None main_post_changed = False post_id_rpc = models.Post.get_next_id_async() current_user = users.get_current_user() current_user_admin = users.is_current_user_admin() post_id = post_id_rpc.get_result() post_key = models.Post.get_key(post_id) post = models.Post(key=post_key, ip=ip_num) post.parent_thread = main_post_key post.thread_bumped = None if image_key and image_data and image_info: post.image = image_key post.image_width = image_data.width post.image_height = image_data.height post.image_url = utils.get_image_url(image_info) post_as = self.request.POST.get('post-as', None) if current_user: post.user = current_user if post_as == 'anonymous': post.username = '******' else: if current_user_admin and post_as == 'admin': post.capcode = "Admin" else: post.capcode = "Logged in" post.username = current_user.nickname() if 'post-include-email' in self.request.POST: post.email = current_user.email() else: poster_name = self.request.POST.get('poster-name', '').strip() if len(poster_name) > 0: post.username = poster_name else: post.username = '******' if 'poster-email' in self.request.POST: post.email = self.request.POST['poster-email'] if 'dont-bump' not in self.request.POST: main_post.thread_bumped = datetime.now() main_post_changed = True if 'post-subject' in self.request.POST: post.subject = cgi.escape(self.request.POST['post-subject']) if 'post-comment' in self.request.POST: post.comment = utils.format_comment(self.request.POST['post-comment']) if main_post_changed: ndb.put_multi([main_post, post]) else: post.put() track_rpc = self.mixpanel_track_async("New thread", {"Post number": post_id, "mp_note": "User made post #%s." % post_id}) self.redirect('/post/%s#%s' % (main_post_key.id(), post_id)) if delete_rpc: ndb.Future.wait_all([delete_rpc, track_rpc]) else: track_rpc.wait() else: child_posts_rpc = models.Post.get_child_posts_async(main_post_key) delete_rpc = self._delete_files_async() self.render_response("post_view.html", main_post=main_post, posts=child_posts_rpc.get_result()) delete_rpc.wait() else: delete_rpc = self._delete_files_async() self.abort(404) delete_rpc.wait()
def post(self, ip_num_str): errors = self._validate_form() if len(errors) > 0: index_threads_rpc = models.Post.get_index_page_async() delete_rpc = self._delete_files_async() self.render_response("index.html", post_errors=errors, threads=index_threads_rpc.get_result()) delete_rpc.wait() else: ip_num = int(ip_num_str) ip = ipaddr.IPAddress(ip_num) ban_rpc = self.user_is_banned_async(ip) if not ban_rpc.get_result()[0]: image_info = self.get_uploads()[0] image_key = image_info.key() delete_rpc = self._delete_files_async(image_info) if image_info.size > config.MAX_FILE_SIZE_BYTES: index_threads_rpc = models.Post.get_index_page_async() blobstore.delete(image_key) delete_rpc.wait() self.render_response("index.html", post_errors=['The supplied file was too large.'], threads=index_threads_rpc.get_result()) logging.info("File was too large") return data = blobstore.fetch_data(image_key, 0, 50000) try: image_data = images.Image(image_data=data) _ = image_data.width _ = image_data.height except images.NotImageError: index_threads_rpc = models.Post.get_index_page_async() blobstore.delete(image_key) delete_rpc.wait() self.render_response("index.html", post_errors=['The supplied file was not an image.'], threads=index_threads_rpc.get_result()) logging.info("Not an image", exc_info=True) return except images.Error: index_threads_rpc = models.Post.get_index_page_async() blobstore.delete(image_key) delete_rpc.wait() self.render_response("index.html", post_errors=['An unknown error occurred when we attempted to process the file you supplied.'], threads=index_threads_rpc.get_result()) logging.warn("Unknown error when processing image", exc_info=True) return post_id_rpc = models.Post.get_next_id_async() current_user = users.get_current_user() current_user_admin = users.is_current_user_admin() post_id = post_id_rpc.get_result() post_key = models.Post.get_key(post_id) post = models.Post(key=post_key, ip=ip_num) post.image = image_key post.image_width = image_data.width post.image_height = image_data.height post.image_url = utils.get_image_url(image_info) post_as = self.request.POST.get('post-as', None) if current_user: post.user = current_user if post_as == 'anonymous': post.username = '******' else: if current_user_admin and post_as == 'admin': post.capcode = "Admin" else: post.capcode = "Logged in" post.username = current_user.nickname() if 'post-include-email' in self.request.POST: post.email = current_user.email() else: poster_name = self.request.POST.get('poster-name', '').strip() if len(poster_name) > 0: post.username = self.request.POST['poster-name'] else: post.username = '******' if 'poster-email' in self.request.POST: post.email = self.request.POST['poster-email'] if 'post-subject' in self.request.POST: post.subject = self.request.POST['post-subject'] if 'post-comment' in self.request.POST: post.comment = utils.format_comment(self.request.POST['post-comment']) post.put() track_rpc = self.mixpanel_track_async("New thread", {"Post number": post_id, "mp_note": "User made thread #%s." % post_id}) self.redirect('/post/%s#%s' % (post_id, post_id)) ndb.Future.wait_all([delete_rpc, track_rpc]) else: index_threads_rpc = models.Post.get_index_page_async() delete_rpc = self._delete_files_async() logging.info("Banned user attempted to post") self.render_response("index.html", threads=index_threads_rpc.get_result()) delete_rpc.wait()