Пример #1
0
    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')
Пример #2
0
    def _delete_post(self, postid):
        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')
Пример #3
0
    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')
Пример #4
0
    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()

            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')
Пример #5
0
    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(
            {},
            {
                '$pull': {'categories': {'slug': category_slug}},
                '$set': {'mod': datetime.datetime.utcnow()}
            },
            multi=True)

        yield cache.event('categories_changed')
        self.redirect(self.reverse_url('categories-page'))
Пример #6
0
    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))