Ejemplo n.º 1
0
    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))
Ejemplo n.º 2
0
    def get(self, slug):
        slug = slug.rstrip('/')
        guest_access_token = self.get_argument('guest-access-token', None)
        postdoc = yield self.settings['db'].posts.find_one({'slug': slug}, {
            'summary': False,
            'original': False
        })

        if not postdoc:
            raise tornado.web.HTTPError(404)

        post = Post(**postdoc)

        # If an access token is provided, it must be valid. Otherwise,
        # administrator must be logged in.
        if guest_access_token:
            if not post.has_guest_access_token(ObjectId(guest_access_token)):
                raise tornado.web.HTTPError(401)
        elif not self.current_user:
            raise tornado.web.HTTPError(401)

        if post.status == 'publish':
            # Not a draft any more
            self.redirect(self.reverse_url('post', slug))
            return

        category_docs = yield self.get_categories()
        categories = [Category(**doc) for doc in category_docs]
        self.render('draft.jade',
                    post=post,
                    prev=None,
                    next=None,
                    categories=categories)
Ejemplo n.º 3
0
    def _get(self, *args, **kwargs):
        categorydocs = yield motor.Op(self.get_categories)
        self.categories = categories = [Category(**doc) for doc in categorydocs]

        postdocs = yield motor.Op(self.get_posts, *args, **kwargs)
        self.posts = posts = [
            Post(**doc) if doc else None
            for doc in postdocs]

        mod = max(
            thing.last_modified
            for things in (posts, categories)
            for thing in things if thing)

        if mod:
            # If-Modified-Since header is only good to the second. Truncate
            # our own mod-date to match its precision.
            mod = mod.replace(microsecond=0)
            self.set_header('Last-Modified', mod)

            # Adapted from StaticFileHandler
            ims_value = self.request.headers.get("If-Modified-Since")
            if ims_value is not None:
                date_tuple = email.utils.parsedate(ims_value)
                if_since = models.utc_tz.localize(
                    datetime.datetime.fromtimestamp(time.mktime(date_tuple)))
                if if_since >= mod:
                    # No change since client's last request. Tornado will take
                    # care of the rest.
                    self.set_status(304)
                    self.finish()
                    return

        gen.engine(get)(self, *args, **kwargs)
Ejemplo n.º 4
0
    def get(self):
        # TODO: refactor with check_last_modified(), this is gross
        #   we need an async version of RequestHandler.prepare()
        category_docs = yield self.get_categories()
        self.categories = [Category(**doc) for doc in category_docs]

        q = self.get_argument('q', None)
        if q:
            response = yield self.db.command('text',
                                             'posts',
                                             search=q,
                                             filter={
                                                 'status': 'publish',
                                                 'type': 'post'
                                             },
                                             projection={
                                                 'original': False,
                                                 'plain': False
                                             },
                                             limit=50)

            posts = [Post(**result['obj']) for result in response['results']]
        else:
            posts = []
        self.render('search.jade', q=q, posts=posts)
Ejemplo n.º 5
0
    def wp_getCategories(self, blogid, user, password):
        # Could cache this as we do on the web side, but not worth the risk
        db = self.settings['db']
        categories = yield db.categories.find().sort([('name', 1)
                                                      ]).to_list(100)

        self.result(
            [Category(**c).to_wordpress(self.application) for c in categories])
Ejemplo n.º 6
0
 def got_category(category, error):
     if error:
         raise error
     elif category:
         wp_categories.append(
             Category(**category).to_wordpress(self.application))
     else:
         # Done
         self.result(wp_categories)
Ejemplo n.º 7
0
    def wp_newCategory(self, blogid, user, password, struct):
        def inserted_category(_id, error):
            if error:
                self.result(xmlrpclib.Fault(500, str(error)))
            else:
                cache.event('categories_changed')
                self.result(str(_id))

        category = Category.from_wordpress(struct)
        self.settings['db'].categories.insert(
            category.to_python(), callback=inserted_category)
Ejemplo n.º 8
0
    def wp_newCategory(self, blogid, user, password, struct):
        def inserted_category(_id, error):
            if error:
                raise error

            cache.event('categories_changed')
            self.result(str(_id))

        category = Category.from_wordpress(struct)
        self.settings['db'].categories.insert(
            category.to_python(), callback=inserted_category)
Ejemplo n.º 9
0
    def _get(self, *args, **kwargs):
        category_docs = yield self.get_categories()
        self.categories = categories = [
            Category(**doc) for doc in category_docs]

        post_docs = yield self.get_posts(*args, **kwargs)
        if post_docs:
            self.posts = posts = [
                Post(**doc) if doc else None
                for doc in post_docs]
        else:
            self.posts = posts = []

        if posts or categories:
            mod = max(
                thing.last_modified
                for things in (posts, categories)
                for thing in things if thing)

            # If-Modified-Since header is only good to the second. Truncate
            # our own mod-date to match its precision.
            mod = mod.replace(microsecond=0)
            self.set_header('Last-Modified', mod)

            # Adapted from StaticFileHandler
            ims_value = self.request.headers.get("If-Modified-Since")
            if ims_value is not None:
                date_tuple = email.utils.parsedate(ims_value)
                if_since = models.utc_tz.localize(
                    datetime.datetime.fromtimestamp(time.mktime(date_tuple)))
                if if_since >= mod:
                    # No change since client's last request. Tornado will take
                    # care of the rest.
                    self.set_status(304)
                    self.finish()
                    return

        # Yielding, and returning result, are unneeded. We're not waiting for
        # a return value, we're waiting for get() to call finish(). But let's
        # yield and return anyway for sanity's sake.
        result = yield gen.coroutine(get)(self, *args, **kwargs)
        raise gen.Return(result)
Ejemplo n.º 10
0
 def get(self):
     category_docs = yield self.get_categories()
     categories = [Category(**doc) for doc in category_docs]
     self.render('admin-templates/categories.html', categories=categories)
Ejemplo n.º 11
0
 def get_categories(self):
     cursor = self.settings['db'].categories.find().sort('name')
     category_docs = yield cursor.to_list(100)
     categories = [Category(**doc) for doc in category_docs]
     raise gen.Return(categories)
Ejemplo n.º 12
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.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)
Ejemplo n.º 13
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))
Ejemplo n.º 14
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)