コード例 #1
0
ファイル: file.py プロジェクト: k1000/django-stratus
def upload(request, repo_name, branch=REPO_BRANCH):
    file_source = path = ""
    msgs = []
    json_convert = None
    success = False

    if request.method == "POST":
        # from _os_helpers import ProgressBarUploadHandler
        # request.upload_handlers.insert(0, ProgressBarUploadHandler())

        repo = get_repo(repo_name)
        dir_path = request.GET.get("upload_dir")  #!!! FIX security

        orig_file_name = request.GET.get("qqfile")
        file_name = slugify(orig_file_name)
        if orig_file_name is not file_name:
            msgs.append(MSG_FILE_NAME_CHANGED % (orig_file_name, file_name))

        file_path = os.path.join(dir_path, file_name)  #!!!FIX convert correctly unicode names

        file_abs_path = os.path.join(repo.working_dir, file_path)

        if request.META["CONTENT_TYPE"] == "application/octet-stream":
            try:
                destination = open(file_abs_path, "wb+")
                for chunk in request.raw_post_data:
                    destination.write(chunk)
                destination.close()
            except:
                file_writen = False
            else:
                file_writen = True
        else:
            file_writen = handle_uploaded_file(file_abs_path, request.FILES["file_source"])

        if file_writen:
            signals.file_created.send(sender=repo, file_path=file_path, url="")
            msgs.append(MSG_UPLOAD_SUCCESS % file_path)
            message = MSG_COMMIT_SUCCESS % file_path
            msg = mk_commit(repo, message, file_path)
            msgs.append(msg)
            success = True
            # return HttpResponse('{ "success": true }', mimetype='application/javascript')
        else:
            msgs.append(MSG_CANT_SAVE_FILE)
            success = False
            # return HttpResponse('{ "success": false }', mimetype='application/javascript')

        if request.is_ajax():
            json_convert = message_convert

    else:
        form = FileUploadForm(initial={"message": "%s added" % path})

    context = dict(
        STRATUS_MEDIA_URL=STRATUS_MEDIA_URL,
        breadcrumbs=make_crumbs(path),
        msg=msgs,
        repo_name=repo_name,
        branch_name=branch,
        path=path,
        repo=repo,
        success=success,
    )
    return mix_response(request, "stratus/upload_file.html", context, json_convert)
コード例 #2
0
ファイル: file.py プロジェクト: k1000/django-stratus
def edit(request, repo_name, branch=REPO_BRANCH, path=None):

    file_source = ""
    msgs = []
    json_convert = None

    if path in FILE_BLACK_LIST:
        msg = MSG_NOT_ALLOWED
        return error_view(request, result_msg)

    if path[-1:] == "/":
        path = path[:-1]
    file_path = path  #!!! FIX security

    repo = get_repo(repo_name)
    tree = repo.tree()

    # edit only if exist in tree
    try:
        tree = tree[path]
    except KeyError:
        msg.append(MSG_NO_FILE_IN_TREE)
        return error_view(request, msg)

    # edit only if it is file
    if not tree.type is "blob":
        msgs.append(MSG_CANT_VIEW)
        return error_view(request, msg)

    mime = tree.mime_type.split("/")
    file_meta = dict(
        STRATUS_MEDIA_URL=STRATUS_MEDIA_URL,
        abspath=tree.abspath,
        repo=repo,
        mime=tree.mime_type,
        size=tree.size,
        tree=tree,
        mime_type=mime[0],
        type=file_type_from_mime(tree.mime_type),
    )

    if file_meta["mime_type"] in EDITABLE_MIME_TYPES:
        form_class = TextFileEditForm
    else:
        form_class = FileEditForm

    signals.file_edit_start.send(sender=repo, file_path=file_path, url="")

    if request.method == "POST":
        form = form_class(request.POST, request.FILES)
        if form.is_valid():
            file_abs_path = os.path.join(repo.working_dir, file_path)
            if file_meta["mime_type"] == "text" or mime[1] in ["xml"]:
                file_source = form.cleaned_data["file_source"]
                file_writen = write_file(file_abs_path, file_source)
            else:
                file_writen = handle_uploaded_file(file_abs_path, request.FILES["file_source"])

            if file_writen:
                msgs.append("File has been saved")
                message = form.cleaned_data["message"]
                ammend = form.cleaned_data.get("ammend", None)
                msg = mk_commit(repo, message, file_path, ammend)
                msgs.append(msg)
            else:
                msgs.append(MSG_CANT_SAVE_FILE)
        else:
            msgs.append(form.errors)

        if request.is_ajax():
            json_convert = message_convert
    else:
        if file_meta["mime_type"] in EDITABLE_MIME_TYPES:
            file_source = tree.data_stream[3].read
        else:
            file_source = file_meta["abspath"]

        form = form_class(initial={"file_source": file_source, "message": "modified %s" % path})

    context = dict(
        STRATUS_MEDIA_URL=STRATUS_MEDIA_URL,
        form=form,
        file_source=file_source,
        breadcrumbs=make_crumbs(path),
        file_meta=file_meta,
        msg=msgs,
        repo_name=repo_name,
        branch_name=branch,
        repo=repo,
        delete_form=FileDeleteForm(initial={"message": MSG_DELETE_SUCCESS % path}),
        path=path,
        name=path.split("/")[-1:][0],
    )

    if mime[0] == "image":
        import base64

        context["img_base"] = base64.b64encode(file_source)
        from getimageinfo import getImageInfo

        context["img_meta"] = getImageInfo(file_source)

    return mix_response(request, "stratus/edit.html", context, json_convert)