def remove_image(self,del_img): soup = BeautifulSoup(self.contents.encode('utf-8')) imgs = soup.findAll('img') for img in imgs: if img["src"] == del_img: img.extract() self.contents = utf8_to_unicode(soup.prettify().replace("\n",""))
def preview_html(self): html = self.text_to_html(self.body.get()).encode('utf-8') name = "html_" + time.strftime("%Y%m%d_%H%M%S", time.localtime()) + ".html" name = os.path.join(DEFDIR, "cache", name) soup = BeautifulSoup(html) imgs = soup.findAll('img') for img in imgs: if os.path.isfile(img["src"]): img["src"] = "file://localhost/" + img["src"] html = soup.prettify().replace("\n","") try: fp = open(name,"wt") fp.write("<html>\n") fp.write('<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>\n') fp.write("<body>\n") fp.write(html) fp.write("</body></html>") fp.close() except: note(LABELS.loc.pt_err_cant_gen_prvw,"error") return viewer = Content_handler(self.refresh) try: viewer.open(name) except: note(LABELS.loc.pt_err_cant_prvw,"error")
def new_post(self, title, contents, categories, tags, publish, offline_idx=-1): """ Upload a new post, where: - title: post title, in unicode - contents: post contents, in unicode - categories: array of category names in unicode - tags: array of tag names in unicode - publish: draft post (False, not published) or final post (True, published) - offline_idx: post index, if it is offline, Return the new post ID (success) or -1 (error) """ app.title = LABELS.loc.wp_info_upld_post_cont soup = BeautifulSoup(unicode_to_utf8(contents)) for img in soup.findAll('img'): if os.path.isfile( img['src'] ): # just upload local files url = self.upload_images( img['src'] ) if url is not None: img['src'] = url contents = soup.prettify().replace("\n"," ") app.title = LABELS.loc.wp_info_upld_post_cont post = wp.WordPressPost() post.description = contents + unicode_to_utf8(LABELS.loc.promo_phrase) post.title = unicode_to_utf8( title ) post.categories = [ self.categoryName2Id(c)[0] for c in categories ] post.keywords = ",".join([ unicode_to_utf8(t) for t in tags ]) post.allowComments = True try: npost = self.blog.newPost(post, publish) except: note(LABELS.loc.wp_err_cant_pub_post,"error") npost = -1 if npost >= 0: app.title = LABELS.loc.wp_info_updt_post_list try: p = self.blog.getLastPostTitle( ) # indicate that the corresponding offline post now has a remote copy if offline_idx >= 0: self.posts[offline_idx] = p else: self.posts.insert( 0, p ) except: note(LABELS.loc.wp_err_cant_updt_post_list,"error") self.save() return npost
def edit_post(self, title, contents, categories, tags, post_idx, publish): """ Update a post. Return True or False, indicating if the updating operation was sucessfuly completed or not """ # when local post is edited it does not have a postid, in such case we need to # create a new post instead updating an existing one if self.post_is_only_local(post_idx): np_id = self.new_post(title, contents, categories, tags, publish, post_idx) return (np_id >= 0) app.title = LABELS.loc.wp_info_upld_post_cont soup = BeautifulSoup( unicode_to_utf8(contents) ) for img in soup.findAll('img'): if os.path.isfile( img['src'] ): # just upload local files url = self.upload_images( img['src'] ) if url is not None: img['src'] = url contents = soup.prettify().replace("\n"," ") app.title = LABELS.loc.wp_info_upld_post_cont post = wp.WordPressPost() post.id = self.posts[post_idx]['postid'] post.title = unicode_to_utf8( title ) post.description = contents post.categories = [ self.categoryName2Id(c)[0] for c in categories ] post.keywords = ",".join([ unicode_to_utf8(t) for t in tags ]) post.allowComments = True post.permaLink = self.posts[post_idx]['permaLink'] post.textMore = self.posts[post_idx]['mt_text_more'] post.excerpt = self.posts[post_idx]['mt_excerpt'] try: npost = self.blog.editPost(post.id, post, publish) except: note(LABELS.loc.wp_err_cant_updt_the_post,"error") return False else: app.title = LABELS.loc.wp_info_updt_post_list try: upd_post = self.blog.getPost(post.id) except: note(LABELS.loc.wp_err_cant_updt_post_list,"error") else: self.posts[post_idx] = upd_post self.save() return True