Beispiel #1
0
def storymap_delete(user, id):
    """Delete storymap"""
    try:
        key_name = storage.key_name(user['uid'], id)
        key_list, marker = storage.list_keys(key_name, 50)
        for key in key_list:
            storage.delete(key.name);

        del user['storymaps'][id]
        _user.save(user)

        return jsonify({})
    except Exception, e:
        traceback.print_exc()
        return jsonify({'error': str(e)})
Beispiel #2
0
def storymap_copy(user, id):
    """
    Copy storymap
    @id = storymap to copy
    @title = name of new copy
    """
    try:
        title = _request_get_required('title')
        dst_id = _make_storymap_id(user, title)

        src_key_prefix = storage.key_prefix(user['uid'], id)
        dst_key_prefix = storage.key_prefix(user['uid'], dst_id)

        src_re = re.compile(r'%s' % src_key_prefix)

        src_key_list, more = storage.list_keys(src_key_prefix, 999, '')
        for src_key in src_key_list:
            file_name = src_key.name.split(src_key_prefix)[-1]
            dst_key_name = "%s%s" % (dst_key_prefix, file_name)

            if file_name.endswith('.json'):
                json_string = src_key.get_contents_as_string()
                storage.save_json(dst_key_name,
                    src_re.sub(dst_key_prefix, json_string))
            else:
                storage.copy_key(src_key.name, dst_key_name)

        # Update meta
        user['storymaps'][dst_id] = {
            'id': dst_id,
            'title': title,
            'draft_on': user['storymaps'][id]['draft_on'],
            'published_on': user['storymaps'][id]['published_on']
        }
        _user.save(user)

        # Write new embed pages
        _write_embed_draft(dst_key_prefix, user['storymaps'][dst_id])
        if user['storymaps'][dst_id].get('published_on'):
            _write_embed_published(dst_key_prefix, user['storymaps'][dst_id])

        return jsonify(user['storymaps'][dst_id])
    except Exception, e:
        traceback.print_exc()
        return jsonify({'error': str(e)})
Beispiel #3
0
def storymap_copy(user, id):
    """
    Copy storymap
    @id = storymap to copy
    @title = name of new copy
    """
    try:
        title = _request_get_required("title")
        dst_id = _make_storymap_id(user, title)

        src_key_prefix = storage.key_prefix(user["uid"], id)
        dst_key_prefix = storage.key_prefix(user["uid"], dst_id)

        src_re = re.compile(r"%s" % src_key_prefix)

        src_key_list, more = storage.list_keys(src_key_prefix, 999, "")
        for src_key in src_key_list:
            file_name = src_key.name.split(src_key_prefix)[-1]
            dst_key_name = "%s%s" % (dst_key_prefix, file_name)

            if file_name.endswith(".json"):
                json_string = src_key.get_contents_as_string()
                storage.save_json(dst_key_name, src_re.sub(dst_key_prefix, json_string))
            else:
                storage.copy_key(src_key.name, dst_key_name)

        # Update meta
        user["storymaps"][dst_id] = {
            "id": dst_id,
            "title": title,
            "draft_on": user["storymaps"][id]["draft_on"],
            "published_on": user["storymaps"][id]["published_on"],
        }
        _user.save(user)

        # Write new embed pages
        _write_embed_draft(dst_key_prefix, user["storymaps"][dst_id])
        if user["storymaps"][dst_id].get("published_on"):
            _write_embed_published(dst_key_prefix, user["storymaps"][dst_id])

        return jsonify(user["storymaps"][dst_id])
    except Exception, e:
        traceback.print_exc()
        return jsonify({"error": str(e)})
Beispiel #4
0
def storymap_export(user, id):
    """
    Download a zip file of the StoryMap's data, for importing or self-hosting
    """
    key_prefix = storage.key_prefix(user['uid'], id)
    key_list, _ = storage.list_keys(key_prefix, 999, '')

    temp_file, temp_path = tempfile.mkstemp()

    @after_this_request
    def cleanup_temp_file(response):
        os.close(temp_file)
        os.remove(temp_path)
        return response

    with ZipFile(temp_path, mode='a') as zip_file:
        zip_file.writestr('metadata.json', json.dumps(user['storymaps'][id]))
        for key in key_list:
            file_name = key.name.split(key_prefix)[-1]
            zip_file.writestr(file_name, storage.get_contents_as_string(key))

    return send_file(temp_path, as_attachment=True, attachment_filename=('storymap-%s.zip' % id))