def pageinplace(): """Check if a url is in place """ url = request.args["url"] post_id = request.args.get("post_id", "") if post_id.isdigit(): post_id = int(post_id) post = Post.get_by_url(url=url) # same post or the post doesn't exist if (post and post.post_id == post_id) or (not post): return jsonify(success=True, in_place=False) else: return jsonify(success=True, in_place=True)
def import_blog(): f = request.files["file"] try: data = f.stream.read().decode("utf-8") data = json.loads(data) links = data.pop("links", []) medias = data.pop("medias", []) posts = data.pop("posts", []) for link in links: new_link = Link.get_by_href(link["href"]) if new_link: continue else: new_link = Link() for item in link: new_link.__dict__[item] = link[item] new_link.link_id = None new_link.create_time = \ datetime.fromtimestamp(new_link.create_time) new_link.save() for media in medias: new_media = Media.get_by_fileid(media["fileid"]) if new_media: continue else: new_media = Media() for item in media: new_media.__dict__[item] = media[item] # Notice, media id should not be set to None new_media.media_id = None new_media.create_time = \ datetime.fromtimestamp(new_media.create_time) new_media.save() for post in posts: # If posts exist, continue new_post = Post.get_by_url(post["url"], public_only=False) if new_post: continue else: new_post = Post() for item in post: new_post.__dict__[item] = post[item] new_post.post_id = None new_post.create_time = \ datetime.fromtimestamp(new_post.create_time) new_post.update_time = \ datetime.fromtimestamp(new_post.update_time) new_post.raw_content = re.sub('<[^<]+?>', "", new_post.content) newtags = new_post.tags new_post.tags = "" new_post.update_tags(newtags) new_post.save() # Restore all posts comments = post["commentlist"] for comment in comments: new_comment = Comment() for item in comment: new_comment.__dict__[item] = comment[item] new_comment.post_id = new_post.post_id new_comment.comment_id = None new_comment.create_time = \ datetime.fromtimestamp(new_comment.create_time) new_comment.save() except Exception as e: return str(e) return "Done"