def wp_newCategory(self, blogid, user, password, struct): category = Category.from_wordpress(struct) _id = yield motor.Op(self.settings['db'].categories.insert, category.to_python()) cache.event('categories_changed') self.result(str(_id))
def mt_setPostCategories(self, postid, user, password, categories): # Sometimes we receive only id from categories, e.g. from Windows # Live Writer, so we must query for the names. if categories and 'categoryName' not in categories[0]: categories_ids = [ ObjectId(category.get('categoryId')) for category in categories] cursor = self.settings['db'].categories.find( {"_id": {'$in': categories_ids}}, fields=["name"]) categories = [] while (yield cursor.fetch_next): category = cursor.next_object() categories.append(dict( categoryId=category['_id'], categoryName=category['name'])) embedded_cats = [ EmbeddedCategory.from_metaweblog(cat).to_python() for cat in categories] result = yield self.settings['db'].posts.update( {'_id': ObjectId(postid)}, {'$set': { 'categories': embedded_cats, 'mod': datetime.datetime.utcnow(), }}) if result['n'] != 1: self.result(xmlrpclib.Fault(404, 'Not found')) else: self.result('') cache.event('post_changed')
def _new_post(self, user, password, struct, type): new_post = Post.from_metaweblog(struct, type) if new_post.status == 'publish': new_post.pub_date = datetime.datetime.utcnow() _id = yield self.settings['db'].posts.insert(new_post.to_python()) self.result(str(_id)) cache.event('post_created')
def _delete_post(self, postid): # TODO: a notion of 'trashed', not removed result = yield self.settings['db'].posts.remove(ObjectId(postid)) if result['n'] != 1: self.result(xmlrpclib.Fault(404, "Not found")) else: self.result(True) cache.event('post_deleted')
def post(self): category_slug = self.get_argument('category_slug') result = yield self.db.categories.remove({'slug': category_slug}) if not result.get('n'): raise tornado.web.HTTPError(404) yield self.db.posts.update( {}, { # Hack: Set *all* posts' mod dates to now. '$pull': { 'categories': { 'slug': category_slug } }, '$set': { 'mod': datetime.datetime.utcnow() } }, multi=True) # Yield and wait for listeners to run before redirecting, so there's # a good chance the categories page will reload the categories. yield cache.event('categories_changed') self.redirect(self.reverse_url('categories-page'))
def mt_setPostCategories(self, postid, user, password, categories): # Sometimes we receive only id from categories, e.g. from Windows # Live Writer, so we must query for the names. if categories and 'categoryName' not in categories[0]: categories_ids = [ ObjectId(category.get('categoryId')) for category in categories ] cursor = self.settings['db'].categories.find( {"_id": { '$in': categories_ids }}, fields=["name"]) categories = [] while (yield cursor.fetch_next): category = cursor.next_object() categories.append( dict(categoryId=category['_id'], categoryName=category['name'])) embedded_cats = [ EmbeddedCategory.from_metaweblog(cat).to_python() for cat in categories ] result = yield self.settings['db'].posts.update( {'_id': ObjectId(postid)}, { '$set': { 'categories': embedded_cats, 'mod': datetime.datetime.utcnow(), } }) if result['n'] != 1: self.result(xmlrpclib.Fault(404, 'Not found')) else: self.result('') cache.event('post_changed')
def _edit_post(self, postid, struct, post_type): new_post = Post.from_metaweblog(struct, post_type, is_edit=True) db = self.settings['db'] old_post_doc = yield db.posts.find_one(ObjectId(postid)) if not old_post_doc: self.result(xmlrpclib.Fault(404, "Not found")) else: old_post = Post(**old_post_doc) if not old_post.pub_date and new_post.status == 'publish': new_post.pub_date = datetime.datetime.utcnow() # TODO: more general solution for fields that must be preserved. new_post.guest_access_tokens = old_post.guest_access_tokens update_result = yield db.posts.update( {'_id': old_post_doc['_id']}, {'$set': new_post.to_python()}) # set fields to new values if update_result['n'] != 1: self.result(xmlrpclib.Fault(404, "Not found")) else: # If link changes, add redirect from old if (old_post.slug != new_post.slug and old_post['status'] == 'publish'): redirect_post = Post( redirect=new_post.slug, slug=old_post.slug, status='publish', type='redirect', mod=datetime.datetime.utcnow()) yield db.posts.insert(redirect_post.to_python()) # Done self.result(True) cache.event('post_changed')
def post(self): category_slug = self.get_argument('category_slug') result = yield self.db.categories.remove({'slug': category_slug}) if not result.get('n'): raise tornado.web.HTTPError(404) yield self.db.posts.update( {}, { # Hack: Set *all* posts' mod dates to now. '$pull': {'categories': {'slug': category_slug}}, '$set': {'mod': datetime.datetime.utcnow()} }, multi=True) # Yield and wait for listeners to run before redirecting, so there's # a good chance the categories page will reload the categories. yield cache.event('categories_changed') self.redirect(self.reverse_url('categories-page'))
def post(self): category_slug = self.get_argument("category_slug") result = yield self.settings["db"].categories.remove({"slug": category_slug}) if not result.get("n"): raise tornado.web.HTTPError(404) yield self.settings["db"].posts.update( {}, { # Hack: Set *all* posts' mod dates to now. "$pull": {"categories": {"slug": category_slug}}, "$set": {"mod": datetime.datetime.utcnow()}, }, multi=True, ) # Yield and wait for listeners to run before redirecting, so there's # a good chance the categories page will reload the categories. yield cache.event("categories_changed") self.redirect(self.reverse_url("categories-page"))
def inserted_category(_id, error): if error: self.result(xmlrpclib.Fault(500, str(error))) else: cache.event('categories_changed') self.result(str(_id))
def inserted_category(_id, error): if error: raise error cache.event('categories_changed') self.result(str(_id))
def wp_newCategory(self, blogid, user, password, struct): category = Category.from_wordpress(struct) _id = yield self.settings['db'].categories.insert(category.to_python()) yield cache.event('categories_changed') self.result(str(_id))