示例#1
0
def test_import_and_export_document():
    """
    Generates an archive file and then turns it back into the original parts.
    """
    doc1 = Document(data)
    user_slug = random_slug('test-user-')
    doc_slug = random_slug('test-doc-')
    doc1.set_parts(user_slug, doc_slug, minimal_document)
    file_name, file_text = doc1.export_txt_file()
    assert user_slug in file_name
    assert doc_slug in file_name
    doc2 = Document(data)
    doc2.import_txt_file(user_slug, doc_slug, file_text)
    assert doc1 == doc2
示例#2
0
def post_upload_txt(user_slug, doc_slug):
    """
    Create a document from an download file.
    @todo: Show a diff?
    """
    require_authority_for_user(user_slug)  # else 401s
    upload = bottle.request.files.get('upload')

    # Validation
    limit = int(config['UPLOAD_LIMIT_KB'])
    if upload.content_length > (limit * 1024):
        msg = "The uploaded file is too large (limit: {:d}K)."
        bottle.abort(msg.format(limit))
    name = upload.filename
    prefix = 'article-wiki_{:s}_{:s}_'.format(user_slug, doc_slug)
    if not name.startswith(prefix) or not name.endswith('.txt'):
        msg = "A '{:s}*.txt' filename is expected."
        bottle.abort(400, msg.format(prefix))

    # Load contents
    filepath = '/tmp/' + upload.filename
    if os.path.exists(filepath):
        os.unlink(filepath)
    upload.save('/tmp')
    try:
        contents = codecs.open(filepath, 'r', 'utf-8').read()
    except Exception:
        msg = "Failed to read path '{:s}'."
        bottle.abort(HTTP_NOT_FOUND, msg.format(user_slug))
    os.unlink(filepath)

    document = Document(data)
    host = domain_name(bottle.request)
    document.set_host(host)
    document.import_txt_file(user_slug, doc_slug, contents)
    document.save()

    uri = '/read/{:s}/{:s}'.format(user_slug, doc_slug)
    bottle.redirect(uri)