예제 #1
0
파일: posts.py 프로젝트: bantana/motor-blog
    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()

            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)
예제 #2
0
파일: posts.py 프로젝트: bantana/motor-blog
    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))
예제 #3
0
파일: posts.py 프로젝트: mlhamel/motor-blog
    def _edit_post(self, postid, user, password, struct, type):
        try:
            self.new_post = Post.from_metaweblog(struct, type, is_edit=True)

            self.settings['db'].posts.find_one({'_id': ObjectId(postid)},
                callback=self._got_old_post)
        except Exception as error:
            logging.exception("Editing post")
            self.result(xmlrpclib.Fault(500, str(error)))
예제 #4
0
 def _new_post(self, user, password, struct, publish, type):
     try:
         new_post = Post.from_metaweblog(struct, type, publish=publish)
         self.settings['db'].posts.insert(
             new_post.to_python(),
             callback=self._new_post_inserted)
     except Exception as error:
         logging.exception("Creating post")
         self.result(xmlrpclib.Fault(500, str(error)))
예제 #5
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')
예제 #6
0
    def _new_post(self, user, password, struct, publish, type):
        def new_post_inserted(_id, error):
            if error:
                raise error

            self.result(str(_id))

        new_post = Post.from_metaweblog(struct, type, publish=publish)
        self.settings['db'].posts.insert(new_post.to_python(),
                                         callback=new_post_inserted)
예제 #7
0
    def _new_post(self, user, password, struct, publish, type):
        def new_post_inserted(_id, error):
            if error:
                raise error

            self.result(str(_id))

        new_post = Post.from_metaweblog(struct, type, publish=publish)
        self.settings['db'].posts.insert(
            new_post.to_python(),
            callback=new_post_inserted)
예제 #8
0
파일: posts.py 프로젝트: mlhamel/motor-blog
    def _new_post(self, user, password, struct, type):
        try:
            new_post = Post.from_metaweblog(struct, type)
            if new_post.status == 'publish':
                new_post.pub_date = datetime.datetime.utcnow()

            self.settings['db'].posts.insert(
                new_post.to_python(),
                callback=self._new_post_inserted)
        except Exception as error:
            logging.exception("Creating post")
            self.result(xmlrpclib.Fault(500, str(error)))
예제 #9
0
    def _edit_post(self, postid, user, password, struct, publish, type):
        # TODO: if link changes, add redirect from old
        def edited_post(result, error):
            if result['n'] != 1:
                self.result(xmlrpclib.Fault(404, "Not found"))
            else:
                self.result(True)

        new_post = Post.from_metaweblog(struct, type, publish=publish, is_edit=True)
        self.settings['db'].posts.update(
            {'_id': ObjectId(postid)},
            {'$set': new_post.to_python()}, # set fields to new values
            callback=edited_post)
예제 #10
0
    def _edit_post(self, postid, user, password, struct, publish, type):
        # TODO: if link changes, add redirect from old
        def edited_post(result, error):
            if result['n'] != 1:
                self.result(xmlrpclib.Fault(404, "Not found"))
            else:
                self.result(True)

        new_post = Post.from_metaweblog(struct,
                                        type,
                                        publish=publish,
                                        is_edit=True)
        self.settings['db'].posts.update(
            {'_id': ObjectId(postid)},
            {'$set': new_post.to_python()},  # set fields to new values
            callback=edited_post)
예제 #11
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()

            # 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 main(args):
    start = time.time()

    opts = options.options()
    destination_url = '/' + opts.base_url.lstrip('/')
    parts = urlparse(args.source_url)
    source_base_url = urljoin(
        '%s://%s' % (parts[0], parts[1]), parts[2].split('/xmlrpc.php')[0])

    print 'Base URL', source_base_url

    db = pymongo.Connection(safe=True).motorblog
    motordb = motor.MotorConnection().open_sync().motorblog
    if args.wipe:
        print 'Wiping motorblog database'
        db.connection.drop_database('motorblog')
        print 'Creating capped collection "events"'
        create_events_collection(motordb)
        print 'Recreating indexes'
        ensure_indexes(db)

    source = Blog(
        args.source_url, args.source_username, args.source_password,
        use_cache=not args.refresh, verbose=args.verbose)
    print 'Getting media library'

    media_library = set([
        m['link'] for m in source.get_media_library()])

    print '    %s assets\n' % len(media_library)

    print 'Getting posts and pages'
    post_structs = source.get_recent_posts(args.nposts)
    print '    %s posts' % len(post_structs)
    page_structs = source.get_pages()
    print '    %s pages' % len(page_structs)
    print

    for structs, type in [
        (post_structs, 'post'),
        (page_structs, 'page'),
    ]:
        print '%sS' % type.upper()
        for struct in structs:
            categories = struct.pop('categories', [])
            struct['description'] = wordpress_to_markdown(
                struct, media_library, db, destination_url, source_base_url)

            post = Post.from_metaweblog(struct, type)

            print '%-34s %s' % (post.title, post.status.upper())
            for category_name in categories:
                doc = db.categories.find_one({'name': category_name})
                if doc:
                    category = Category(**doc)
                else:
                    category = Category(
                        name=category_name, slug=slugify(category_name))
                    category.id = db.categories.insert(category.to_python())
                print '    %-30s %s' % (
                    category_name, ' NEW' if not doc else ''
                )

                post.categories.append(category)

            db.posts.insert(post.to_python())

        print '\nFinished %s %ss' % (len(structs), type)


    print 'Posting "categories_changed" event'
    db.events.insert(
        {'ts': datetime.datetime.utcnow(), 'name': 'categories_changed'},
        manipulate=False) # No need to add _id

    print '\nFinished in %.2f seconds' % (time.time() - start)
예제 #13
0
def main(args):
    start = time.time()

    opts = options.options()
    destination_url = '/' + opts.base_url.lstrip('/')
    parts = urlparse(args.source_url)
    source_base_url = urljoin('%s://%s' % (parts[0], parts[1]),
                              parts[2].split('/xmlrpc.php')[0])

    print 'Base URL', source_base_url

    db = pymongo.Connection(safe=True).motorblog
    motordb = motor.MotorClient().open_sync().motorblog
    if args.wipe:
        print 'Wiping motorblog database'
        db.connection.drop_database('motorblog')
        print 'Creating capped collection "events"'
        create_events_collection(motordb)
        print 'Recreating indexes'
        ensure_indexes(db)

    source = Blog(args.source_url,
                  args.source_username,
                  args.source_password,
                  use_cache=not args.refresh,
                  verbose=args.verbose)
    print 'Getting media library'

    media_library = set([m['link'] for m in source.get_media_library()])

    print '    %s assets\n' % len(media_library)

    print 'Getting posts and pages'
    post_structs = source.get_recent_posts(args.nposts)
    print '    %s posts' % len(post_structs)
    page_structs = source.get_pages()
    print '    %s pages' % len(page_structs)
    print

    for structs, post_type in [
        (post_structs, 'post'),
        (page_structs, 'page'),
    ]:
        print '%sS' % post_type.upper()
        for struct in structs:
            categories = struct.pop('categories', [])
            struct['description'] = wordpress_to_markdown(
                struct, media_library, db, destination_url, source_base_url)

            post = Post.from_metaweblog(struct, post_type)

            print '%-34s %s' % (post.title, post.status.upper())
            for category_name in categories:
                doc = db.categories.find_one({'name': category_name})
                if doc:
                    category = Category(**doc)
                else:
                    category = Category(name=category_name,
                                        slug=slugify(category_name))
                    category.id = db.categories.insert(category.to_python())
                print '    %-30s %s' % (category_name,
                                        ' NEW' if not doc else '')

                post.categories.append(category)

            db.posts.insert(post.to_python())

        print '\nFinished %s %ss' % (len(structs), post_type)

    print 'Posting "categories_changed" event'

    db.events.insert(
        {
            'ts': datetime.datetime.utcnow(),
            'name': 'categories_changed'
        },
        manipulate=False)  # No need to add _id

    print '\nFinished in %.2f seconds' % (time.time() - start)