Exemplo n.º 1
0
def create_from_file(session, pth, user):
    """Create a RO from a a file.

    Notes: if pth is a zipfile, it will be extracted and
           a ROContainer will be created with he content

    Args:
        session (DBSession):
        pth (str): path to file to read
        user (str): id of user

    Returns:
        (ResearchObject): or None if nothing has been recognized in
                          the file
    """
    # try to unpack zip files
    try:
        with ZipFile(pth, "r") as myzip:
            myzip.extractall(pj(dirname(pth), "archive"))

        remove(pth)
        # explore directory
        ros = []
        for fpth, fname in find_files(pj(dirname(pth), "archive"), ["*.wkf"]):
            ro_type = validate(fpth)
            if ro_type is not None:
                ros.append(create(session, fpth, ro_type))

        if len(ros) == 0:
            return None
        else:
            name = splitext(basename(pth))[0]
            cont = ROContainer()
            cont.init(session, dict(id=uuid1().hex, owner=user, name=name))
            for ro in ros:
                ROLink.connect(session, cont.id, ro.id, "contains")

            # search for project avatar
            avatar = fetch_avatar(pj(dirname(pth), "archive"))
            if avatar is not None:
                upload_ro_avatar(avatar, cont)

            # search project description in README
            descr = fetch_readme(pj(dirname(pth), "archive"))
            cont.store_description(descr)

            return cont
    except BadZipfile:
        # not a zip file, try to import single file
        ro_type = validate(pth)
        if ro_type is None:
            return None
        else:
            ro = create(session, pth, ro_type)
            return ro
Exemplo n.º 2
0
def view(request):
    session = DBSession()
    ro, view_params = edit_init(request, session, "home")

    if "update" in request.params:
        if "description" in request.params:
            ro.store_description(request.params["description"])
    elif "submit_avatar" in request.params:
        field_storage = request.params["avatar"]
        if field_storage == "":
            request.session.flash("Select an image first", "warning")
        else:
            try:
                img = load_image(field_storage)
                upload_ro_avatar(img, ro)
                request.session.flash("Avatar submitted", "success")
                loc = request.route_url("ro_view_home", uid=ro.id)
                return HTTPFound(location=loc)
            except IOError:
                request.session.flash("Unable to read image", "warning")
    else:
        pass

    return view_params