'Create a new or get an existing capped PyMongo collection.'

@classmethod
def _get_capped_collection(cls):

    db = cls._get_db()
    collection_name = cls._get_collection_name()
    max_size = (cls._meta.get('max_size') or (10 * (2 ** 20)))
    max_documents = cls._meta.get('max_documents')
    if (max_size % 256):
        max_size = (((max_size // 256) + 1) * 256)
    if (collection_name in db.collection_names()):
        collection = db[collection_name]
        options = collection.options()
        if ((options.get('max') != max_documents) or (options.get('size') != max_size)):
            raise InvalidCollectionError('Cannot    create    collection    "{}"    as    a    capped    collection    as    it    already    exists'.format(cls._collection))
        return collection
    opts = {'capped': True, 'size': max_size}
    if max_documents:
        opts['max'] = max_documents
    return db.create_collection(collection_name, **opts)
